Initial Commit

This commit is contained in:
2026-09-28 16:55:31 +08:00
commit 14f8706198
128 changed files with 19860 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
using TuneBoss.Core.IO;
using Xunit;
namespace TuneBoss.Core.Tests;
public class MapFileTests
{
[Fact]
public void Md5Hex_is_uppercase_no_separators()
=> Assert.Equal("D41D8CD98F00B204E9800998ECF8427E", MapFile.Md5Hex(""));
[Fact]
public void Envelope_round_trips()
{
var env = MapFile.BuildEnvelope("the-tag", "the data section");
var r = MapFile.TryParse(env);
Assert.True(r.Ok);
Assert.Equal("the-tag", r.Tag);
Assert.Equal("the data section", r.Data);
}
[Fact]
public void Envelope_tolerates_crlf_newlines()
{
var env = MapFile.BuildEnvelope("t", "d").Replace("\n", "\r\n");
Assert.True(MapFile.TryParse(env).Ok);
}
[Fact]
public void Tampered_tag_is_rejected()
{
var env = MapFile.BuildEnvelope("original", "data");
var bad = env.Replace("_____original", "_____TAMPERED");
var r = MapFile.TryParse(bad);
Assert.False(r.Ok);
Assert.Contains("Tag checksum", r.Error);
}
[Fact]
public void Tampered_data_is_rejected()
{
var env = MapFile.BuildEnvelope("tag", "good");
var bad = env.Replace("_____good", "_____bad!");
Assert.False(MapFile.TryParse(bad).Ok);
}
[Fact]
public void Missing_data_record_is_rejected()
=> Assert.False(MapFile.TryParse(MapFile.Md5Hex("x") + "_____x").Ok);
[Fact]
public void Tag_round_trips_all_fields()
{
var t = new MapTag
{
CreatorId = "TUNER01", FwVersion = 193, HwVersion = 10,
Make = 1, Model = 6, CylinderCount = 1, SaveType = MapSaveType.FullConfig,
EngineBlock = "stock", Camshaft = "stage2", Exhaust = "akra",
AirFilter = "k&n", CylinderHead = "ported", Injector = "440cc",
ThrottleBody = "34mm", JackRod = "none", MiscNote = "dyno 18.2hp",
};
var back = MapTag.Parse(t.Build());
Assert.Equal(t.CreatorId, back.CreatorId);
Assert.Equal(t.FwVersion, back.FwVersion);
Assert.Equal(t.Make, back.Make);
Assert.Equal(t.Model, back.Model);
Assert.Equal(t.CylinderCount, back.CylinderCount);
Assert.Equal(t.SaveType, back.SaveType);
Assert.Equal(t.Exhaust, back.Exhaust);
Assert.Equal(t.MiscNote, back.MiscNote);
}
[Fact]
public void Tag_has_16_fields()
=> Assert.Equal(MapTag.FieldCount, new MapTag().Build().Split(MapTag.FieldSep).Length);
[Fact]
public void Tag_parse_tolerates_short_input()
{
var t = MapTag.Parse("CREATOR___193___10___1___6___1___4");
Assert.Equal("CREATOR", t.CreatorId);
Assert.Equal(MapSaveType.FullConfig, t.SaveType);
Assert.Equal("", t.MiscNote); // absent field → empty, no throw
}
}
+101
View File
@@ -0,0 +1,101 @@
using TuneBoss.Core.IO;
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class MapTableFormatTests
{
private static byte[] Ramp(int n, int start = 0, int step = 1)
{
var a = new byte[n];
for (int i = 0; i < n; i++) a[i] = (byte)((start + i * step) & 0xFF);
return a;
}
[Fact]
public void Section2D_round_trips()
{
var t = new TuningTable
{
Data = Ramp(TuningTable.DataBytes, 1),
LoadAxis = Ramp(TuningTable.Rows, 10, 5),
RpmAxis = Ramp(TuningTable.Cols, 8, 9),
MaxRaw = 250, MinRaw = 3, Mult = 1, Sign = 0,
};
var section = MapTableFormat.Section2D("Fuel_PulseWidth_Adjust1", t);
Assert.Equal("Fuel_PulseWidth_Adjust1", MapTableFormat.LabelOf(section));
// Single section + trailing empty after the ____ terminator.
var parts = MapTableFormat.SplitSections(section);
var back = MapTableFormat.Parse2D(parts[0]);
Assert.Equal(t.Data, back.Data);
Assert.Equal(t.LoadAxis, back.LoadAxis);
Assert.Equal(t.RpmAxis, back.RpmAxis);
Assert.Equal(t.MaxRaw, back.MaxRaw);
Assert.Equal(t.MinRaw, back.MinRaw);
Assert.Equal(t.Mult, back.Mult);
Assert.Equal(t.Sign, back.Sign);
}
[Fact]
public void Afr_round_trips_without_modifiers()
{
var t = new AfrTable
{
Data = Ramp(AfrTable.DataBytes, 130),
LoadAxis = Ramp(AfrTable.Rows, 20),
RpmAxis = Ramp(AfrTable.Cols, 5, 7),
};
var back = MapTableFormat.ParseAfr(MapTableFormat.SplitSections(MapTableFormat.SectionAfr("AFR_Target1", t))[0]);
Assert.Equal(t.Data, back.Data);
Assert.Equal(t.LoadAxis, back.LoadAxis);
Assert.Equal(t.RpmAxis, back.RpmAxis);
}
[Fact]
public void Sub_derives_rpm_from_parent()
{
var parentRpm = Ramp(TuningTable.Cols, 0, 1); // 0,1,2,...,23
var sub = new SubTable { Data = Ramp(SubTable.DataBytes, 100) };
var section = MapTableFormat.SplitSections(MapTableFormat.SectionSub("Spark_Angle_Adjust1_SubTable", sub))[0];
var back = MapTableFormat.ParseSub(section, parentRpm);
Assert.Equal(sub.Data, back.Data);
for (int x = 0; x < SubTable.Cols; x++)
{
Assert.Equal(parentRpm[x * 4], back.RpmAxis[x * 2]);
Assert.Equal(parentRpm[x * 4 + 3], back.RpmAxis[x * 2 + 1]);
}
}
[Fact]
public void OneD_round_trips_24_byte_wire()
{
var t = new OneDimTable
{
MaxRaw = 200, MinRaw = 0, RawSignedFlag = 0, ConvSignedFlag = 1,
RawMultiplier = 2, ConvMultiplier = 3, RawRounding = 0, ConvOffset = -5,
};
for (int i = 0; i < OneDimTable.PointCount; i++) { t.RawAxis[i] = i * 8; t.ConvValues[i] = 40 - i; }
var section = MapTableFormat.SplitSections(MapTableFormat.Section1D("O2_Conversion_Table", t))[0];
var back = MapTableFormat.Parse1D(section);
Assert.Equal(t.ToWirePayload(), back.ToWirePayload()); // metadata + axes + values round-trip verbatim
}
[Fact]
public void ParseBytes_handles_trailing_space()
=> Assert.Equal(new byte[] { 10, 20, 255 }, MapTableFormat.ParseBytes("10 20 255 "));
[Fact]
public void Two_sections_split_positionally()
{
var t = new TuningTable();
var doc = MapTableFormat.Section2D("A", t) + MapTableFormat.Section2D("B", t);
var parts = MapTableFormat.SplitSections(doc);
Assert.Equal("A", MapTableFormat.LabelOf(parts[0]));
Assert.Equal("B", MapTableFormat.LabelOf(parts[1]));
}
}
+71
View File
@@ -0,0 +1,71 @@
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class MemoryLayoutTests
{
[Theory]
[InlineData(70)]
[InlineData(80)]
[InlineData(83)]
[InlineData(90)]
[InlineData(130)]
[InlineData(193)]
public void Gen3_1D_availability_matches_registry_firmware_gating(byte fw)
{
var layout = MemoryLayout.For(TestData.Ecu(TestData.Gen3, fw), EcuVariant.TwoCylinderR15);
Assert.Equal(37, layout.OneDimTables.Length);
foreach (var def in OneDimTableRegistry.All)
{
var region = layout.OneDimTables[def.Slot - 1];
Assert.Equal(fw >= def.MinFirmware, region.IsAvailable);
}
}
[Theory]
[InlineData(149, false)]
[InlineData(150, true)]
[InlineData(193, true)]
public void Gen3_misc_padding_is_gated_at_fw150(byte fw, bool available)
{
var l = MemoryLayout.For(TestData.Ecu(TestData.Gen3, fw), EcuVariant.TwoCylinder);
Assert.Equal(available, l.MiscLaunchFuel.IsAvailable);
Assert.Equal(available, l.MiscLaunchFuel2.IsAvailable);
Assert.Equal(available, l.MiscAuxOut.IsAvailable);
Assert.Equal(available, l.MiscExtended.IsAvailable);
}
[Theory]
[InlineData(70)]
[InlineData(150)]
[InlineData(193)]
public void MiscCore_is_always_present_and_136_bytes(byte fw)
{
var l = MemoryLayout.For(TestData.Ecu(TestData.Gen3, fw), EcuVariant.TwoCylinder);
Assert.True(l.MiscCore.IsAvailable);
Assert.Equal(0x88, l.MiscCore.Length);
}
[Theory]
[InlineData(EcuVariant.SingleCylinder)]
[InlineData(EcuVariant.TwoCylinder)]
[InlineData(EcuVariant.TwoCylinderR15)]
public void MiscExtended_length_follows_variant_when_available(EcuVariant variant)
{
var l = MemoryLayout.For(TestData.Ecu(TestData.Gen3, 193), variant);
Assert.True(l.MiscExtended.IsAvailable);
Assert.Equal((byte)MiscValues.ExtendedLengthFor(variant), l.MiscExtended.Length);
}
[Fact]
public void Gen1_exposes_only_the_base_18_oneDim_tables()
{
var l = MemoryLayout.For(TestData.Ecu(TestData.Gen1, 193), EcuVariant.SingleCylinder);
for (int i = 0; i < 37; i++)
Assert.Equal(i < 18, l.OneDimTables[i].IsAvailable);
}
}
+104
View File
@@ -0,0 +1,104 @@
using System.Numerics;
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
/// <summary>
/// These are the high-value regression tests: they tie the bit-editor specs to the
/// field registry and validate every mask. The "spec key exists" test is exactly what
/// would have caught the ShiftBoss byte-84 mislabel and the Core_25/31/33/120 key drift.
/// </summary>
public class MiscBitSpecTests
{
[Fact]
public void Every_spec_key_resolves_to_a_field()
{
foreach (var spec in MiscBitSpecRegistry.All)
{
bool found = MiscFieldRegistry.All.Any(
f => f.Block == spec.Block && f.Label == spec.FieldLabel);
Assert.True(found,
$"Spec '{spec.FieldLabel}' ({spec.Block}) has no matching MiscField — " +
"the bit editor will silently fail to attach.");
}
}
[Fact]
public void Find_returns_spec_by_key()
{
var s = MiscBitSpecRegistry.Find(MiscBlock.Core, "ShiftBoss_Enable");
Assert.NotNull(s);
Assert.Equal("ShiftBoss_Enable", s!.FieldLabel);
Assert.Null(MiscBitSpecRegistry.Find(MiscBlock.Core, "does_not_exist"));
}
[Fact]
public void All_masks_are_within_a_byte_and_nonzero()
{
foreach (var spec in MiscBitSpecRegistry.All)
foreach (var f in spec.Fields)
Assert.InRange(f.Mask, 1, 255);
}
[Fact]
public void Flag_fields_are_a_single_bit()
{
foreach (var spec in MiscBitSpecRegistry.All)
foreach (var f in spec.Fields)
if (f.Kind == MiscEditorKind.Flag)
Assert.Equal(1, BitOperations.PopCount((uint)f.Mask));
}
[Fact]
public void Enum_options_fit_the_mask_range()
{
foreach (var spec in MiscBitSpecRegistry.All)
foreach (var f in spec.Fields)
{
if (f.Kind != MiscEditorKind.Enum) continue;
Assert.NotNull(f.Options);
int shift = BitOperations.TrailingZeroCount((uint)f.Mask);
int maxVal = f.Mask >> shift; // e.g. 0x06 -> 3
Assert.True(f.Options!.Length >= 2,
$"{spec.FieldLabel}/{f.Label}: enum needs >= 2 options");
Assert.True(f.Options.Length <= maxVal + 1,
$"{spec.FieldLabel}/{f.Label}: {f.Options.Length} options exceed mask range {maxVal + 1}");
}
}
[Fact]
public void Fields_within_a_spec_do_not_overlap_bits()
{
foreach (var spec in MiscBitSpecRegistry.All)
{
int used = 0;
foreach (var f in spec.Fields)
{
Assert.True((used & f.Mask) == 0,
$"{spec.FieldLabel}: field '{f.Label}' mask 0x{f.Mask:X2} overlaps another field");
used |= f.Mask;
}
}
}
[Fact]
public void Constraint_indices_are_valid_bit_positions()
{
foreach (var spec in MiscBitSpecRegistry.All)
{
foreach (var (a, b) in spec.MutualExclusive ?? Enumerable.Empty<(int, int)>())
{
Assert.InRange(a, 0, 7);
Assert.InRange(b, 0, 7);
}
foreach (var (a, b) in spec.Linked ?? Enumerable.Empty<(int, int)>())
{
Assert.InRange(a, 0, 7);
Assert.InRange(b, 0, 7);
}
}
}
}
@@ -0,0 +1,43 @@
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class MiscFieldRegistryTests
{
private static int BlockSize(MiscBlock b) => b switch
{
MiscBlock.Core => 136,
MiscBlock.LaunchFuel => 12,
MiscBlock.LaunchFuel2 => 12,
MiscBlock.AuxOut => 12,
MiscBlock.Extended => 152,
_ => 0,
};
[Fact]
public void All_field_indices_are_within_their_block()
{
foreach (var f in MiscFieldRegistry.All)
Assert.InRange(f.Index, 0, BlockSize(f.Block) - 1);
}
[Fact]
public void No_two_fields_share_a_block_and_index()
{
var dupes = MiscFieldRegistry.All
.GroupBy(f => (f.Block, f.Index))
.Where(g => g.Count() > 1)
.Select(g => $"{g.Key.Block}[{g.Key.Index}]: {string.Join(", ", g.Select(x => x.Label))}")
.ToList();
Assert.True(dupes.Count == 0, "Duplicate (block,index) mappings:\n" + string.Join("\n", dupes));
}
[Fact]
public void All_labels_are_nonblank()
{
foreach (var f in MiscFieldRegistry.All)
Assert.False(string.IsNullOrWhiteSpace(f.Label));
}
}
+58
View File
@@ -0,0 +1,58 @@
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class MiscValuesTests
{
[Fact]
public void Core_is_sliced_from_offset_4()
{
var data = new byte[136];
for (int i = 0; i < data.Length; i++) data[i] = (byte)i;
var mv = MiscValues.FromPayloads(TestData.WithHeader(data, 0x46));
Assert.Equal(136, mv.Core.Length);
Assert.Equal(0, mv.Core[0]); // header skipped
Assert.Equal(135, mv.Core[135]);
}
[Fact]
public void Short_core_payload_yields_empty()
{
// 4 header + 100 data = 104 < 4 + 136, so Slice returns empty.
var mv = MiscValues.FromPayloads(TestData.WithHeader(new byte[100]));
Assert.Empty(mv.Core);
}
[Fact]
public void Optional_blocks_default_to_empty()
{
var mv = MiscValues.FromPayloads(TestData.WithHeader(new byte[136]));
Assert.Empty(mv.LaunchFuel);
Assert.Empty(mv.LaunchFuel2);
Assert.Empty(mv.AuxOut);
Assert.Empty(mv.Extended);
}
[Theory]
[InlineData(EcuVariant.SingleCylinder, 0x29)]
[InlineData(EcuVariant.TwoCylinder, 0x73)]
[InlineData(EcuVariant.TwoCylinderR15, 0x97)]
public void ExtendedLengthFor_matches_variant(EcuVariant v, int expected)
{
Assert.Equal(expected, MiscValues.ExtendedLengthFor(v));
}
[Fact]
public void Extended_slice_size_follows_payload_length()
{
int n = MiscValues.ExtendedLengthFor(EcuVariant.TwoCylinderR15); // 0x97
var mv = MiscValues.FromPayloads(
corePayload: new byte[140], // 4 + 136
extendedPayload: TestData.WithHeader(new byte[n]));
Assert.Equal(n, mv.Extended.Length);
}
}
+54
View File
@@ -0,0 +1,54 @@
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class OneDimDisplayTests
{
[Theory]
[InlineData(0, 1)]
[InlineData(1, 1)]
[InlineData(100, 100)]
[InlineData(50, 50)]
public void Multiplier_0_or_1_is_unscaled(int mult, int expected)
=> Assert.Equal(expected, OneDimDisplay.EffectiveMultiplier(mult));
[Theory]
[InlineData(130, 100, 13000)] // RPM
[InlineData(45, 1, 45)] // unscaled
[InlineData(7, 0, 7)] // mult 0 -> ×1
[InlineData(-5, 2, -10)] // signed
public void ToDisplay_scales(int raw, int mult, long expected)
=> Assert.Equal(expected, OneDimDisplay.ToDisplay(raw, mult));
[Theory]
[InlineData(13000, 100, 130)]
[InlineData(13100, 100, 131)]
[InlineData(42, 1, 42)]
[InlineData(-10, 2, -5)]
public void TryFromDisplay_accepts_multiples(double display, int mult, int expectedRaw)
{
Assert.True(OneDimDisplay.TryFromDisplay(display, mult, out int raw));
Assert.Equal(expectedRaw, raw);
}
[Theory]
[InlineData(13050, 100)] // not a multiple of 100
[InlineData(42.5, 1)] // not a whole number
[InlineData(13, 5)] // not a multiple of 5
public void TryFromDisplay_rejects_non_multiples(double display, int mult)
{
Assert.False(OneDimDisplay.TryFromDisplay(display, mult, out _));
}
[Fact]
public void Round_trips_display_then_raw()
{
for (int raw = -120; raw <= 120; raw += 7)
{
long disp = OneDimDisplay.ToDisplay(raw, 100);
Assert.True(OneDimDisplay.TryFromDisplay(disp, 100, out int back));
Assert.Equal(raw, back);
}
}
}
@@ -0,0 +1,42 @@
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class OneDimRegistryTests
{
[Fact]
public void Has_37_tables()
{
Assert.Equal(37, OneDimTableRegistry.All.Count);
}
[Fact]
public void Slots_are_unique_and_contiguous_1_to_37()
{
var slots = OneDimTableRegistry.All.Select(d => d.Slot).OrderBy(s => s).ToList();
Assert.Equal(Enumerable.Range(1, 37), slots);
}
[Fact]
public void Min_firmware_values_are_known_tiers()
{
var allowed = new[] { 0, 80, 83, 90, 130 };
foreach (var d in OneDimTableRegistry.All)
Assert.Contains(d.MinFirmware, allowed);
}
[Fact]
public void Every_group_used_appears_in_group_order()
{
foreach (var d in OneDimTableRegistry.All)
Assert.Contains(d.Group, OneDimTableRegistry.GroupOrder);
}
[Fact]
public void Labels_are_nonblank()
{
foreach (var d in OneDimTableRegistry.All)
Assert.False(string.IsNullOrWhiteSpace(d.Label));
}
}
+99
View File
@@ -0,0 +1,99 @@
using TuneBoss.Core.Models;
using Xunit;
namespace TuneBoss.Core.Tests;
public class OneDimTableTests
{
[Fact]
public void FromWire_then_ToWire_is_byte_identical_unsigned()
{
var wire = TestData.OneDimWire(
axis: new byte[] { 0, 10, 20, 40, 80, 120, 200, 255 },
conv: new byte[] { 5, 6, 7, 8, 9, 10, 11, 12 },
rawMult: 100, convMult: 1, maxRaw: 255, minRaw: 0, convOffset: -3);
var t = OneDimTable.FromWire(wire);
var rt = t.ToWirePayload();
Assert.Equal(wire, rt); // full verbatim round-trip
}
[Fact]
public void Metadata_bytes_round_trip_verbatim()
{
var wire = TestData.OneDimWire(
axis: new byte[8], conv: new byte[8],
rawSigned: 1, convSigned: 1, rawMult: 100, convMult: 50,
maxRaw: 200, minRaw: 10, rawRound: 7, convOffset: -12);
var t = OneDimTable.FromWire(wire);
Assert.Equal(1, t.RawSignedFlag);
Assert.Equal(1, t.ConvSignedFlag);
Assert.Equal(100, t.RawMultiplier);
Assert.Equal(50, t.ConvMultiplier);
Assert.Equal(7, t.RawRounding);
Assert.Equal(-12, t.ConvOffset);
Assert.Equal(wire, t.ToWirePayload());
}
[Fact]
public void Signed_axis_and_values_decode_negative()
{
var wire = TestData.OneDimWire(
axis: new byte[] { 0xFF, 0xFE, 0, 1, 2, 3, 4, 5 }, // -1, -2, 0, 1...
conv: new byte[] { 0x80, 0xFF, 0, 1, 2, 3, 4, 5 }, // -128, -1, 0...
rawSigned: 1, convSigned: 1);
var t = OneDimTable.FromWire(wire);
Assert.Equal(-1, t.RawAxis[0]);
Assert.Equal(-2, t.RawAxis[1]);
Assert.Equal(-128, t.ConvValues[0]);
Assert.Equal(-1, t.ConvValues[1]);
Assert.Equal(wire, t.ToWirePayload()); // signed values re-encode exactly
}
[Fact]
public void Unsigned_high_byte_stays_positive()
{
var wire = TestData.OneDimWire(
axis: new byte[] { 200, 255, 0, 0, 0, 0, 0, 0 },
conv: new byte[] { 255, 200, 0, 0, 0, 0, 0, 0 },
rawSigned: 0, convSigned: 0);
var t = OneDimTable.FromWire(wire);
Assert.Equal(200, t.RawAxis[0]);
Assert.Equal(255, t.RawAxis[1]);
Assert.Equal(255, t.ConvValues[0]);
}
[Fact]
public void FromWire_honours_offset()
{
var inner = TestData.OneDimWire(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[8]);
var framed = TestData.WithHeader(inner); // 4-byte header + 24-byte block
var t = OneDimTable.FromWire(framed, 4);
Assert.Equal(1, t.RawAxis[0]);
Assert.Equal(8, t.RawAxis[7]);
}
[Fact]
public void FromWire_short_payload_throws()
{
Assert.Throws<ArgumentException>(() => OneDimTable.FromWire(new byte[10]));
}
[Fact]
public void Clone_is_independent()
{
var t = OneDimTable.FromWire(TestData.OneDimWire(new byte[8], new byte[8]));
var c = t.Clone();
c.ConvValues[0] = 99;
Assert.NotEqual(99, t.ConvValues[0]);
}
}
+46
View File
@@ -0,0 +1,46 @@
using TuneBoss.Core.Models;
namespace TuneBoss.Core.Tests;
/// <summary>Shared builders for synthetic device data.</summary>
internal static class TestData
{
public const byte Gen1 = 245, Gen2 = 1, Gen3 = 2;
public static EcuInfo Ecu(byte appInfoVersion, byte fw, byte rpmMax = 130) => new()
{
Username = "SIM-RIDER",
UniqueId = "AABBCCDD-EEFF0011-22334455",
AppInfoVersion = appInfoVersion,
FwVersion = fw,
HwVersion = 10,
UpgradeRpmMax = rpmMax,
BikeManufacturer = 1,
};
/// <summary>Wraps a data block in the 4-byte response header the parsers skip (51 xx xx 00).</summary>
public static byte[] WithHeader(byte[] data, byte cmd = 0x43)
{
var p = new byte[4 + data.Length];
p[0] = 0x51; p[1] = cmd; p[2] = (byte)data.Length; p[3] = 0x00;
Buffer.BlockCopy(data, 0, p, 4, data.Length);
return p;
}
/// <summary>A valid 24-byte 1D block with chosen sign flags / multipliers.</summary>
public static byte[] OneDimWire(
byte[] axis, byte[] conv,
byte rawSigned = 0, byte convSigned = 0,
byte rawMult = 1, byte convMult = 1,
byte maxRaw = 255, byte minRaw = 0, byte rawRound = 0, sbyte convOffset = 0)
{
var d = new byte[OneDimTable.WireLength];
Array.Copy(axis, 0, d, 0, 8);
Array.Copy(conv, 0, d, 8, 8);
d[16] = maxRaw; d[17] = minRaw;
d[18] = rawSigned; d[19] = convSigned;
d[20] = rawMult; d[21] = convMult;
d[22] = rawRound; d[23] = unchecked((byte)convOffset);
return d;
}
}
@@ -0,0 +1,82 @@
using TuneBoss.Core.Models;
using TuneBoss.Core.Protocol;
using Xunit;
namespace TuneBoss.Core.Tests;
public class TpsCalibrationTests
{
// ── Write payload ───────────────────────────────────────────────────────────
[Fact]
public void CalWritePayload_matches_legacy_ramp()
{
var p = TpsCalibration.BuildCalWritePayload(10, 180);
Assert.Equal(new byte[] { 10, 180, 182, 184, 186, 188, 190, 192 }, p);
}
[Fact]
public void CalWritePayload_wraps_high_max_into_byte()
{
// max+12 above 255 wraps (unchecked byte), matching the legacy (byte) cast.
var p = TpsCalibration.BuildCalWritePayload(8, 250);
Assert.Equal(8, p[0]);
Assert.Equal(250, p[1]);
Assert.Equal((byte)(250 + 12), p[7]); // 262 & 0xFF = 6
}
// ── Validation ──────────────────────────────────────────────────────────────
[Fact]
public void Validate_in_range_is_ok()
=> Assert.Equal(TpsCalibration.Validation.Ok,
TpsCalibration.Validate(minRaw: 10, maxRaw: 188, absMin: 5, absMax: 195));
[Fact]
public void Validate_min_below_floor()
=> Assert.Equal(TpsCalibration.Validation.MinBelowFloor,
TpsCalibration.Validate(minRaw: 3, maxRaw: 188, absMin: 5, absMax: 195));
[Fact]
public void Validate_max_above_ceiling()
=> Assert.Equal(TpsCalibration.Validation.MaxAboveCeiling,
TpsCalibration.Validate(minRaw: 10, maxRaw: 200, absMin: 5, absMax: 195));
[Fact]
public void Validate_extreme_crossed_values()
=> Assert.Equal(TpsCalibration.Validation.Extreme,
TpsCalibration.Validate(minRaw: 196, maxRaw: 196, absMin: 5, absMax: 195));
// ── Address resolution ──────────────────────────────────────────────────────
[Fact]
public void Gen3_uses_small_address_layout()
{
var a = TpsCalibration.For(GenerationType.Gen3, fwVersion: 193);
Assert.Equal(0x0078, a.CalWrite);
Assert.Equal(0x0088, a.AbsBounds);
Assert.Equal(0x0094, a.AdvSettings);
Assert.True(a.AdvSupported); // 193 >= 152
}
[Fact]
public void Gen3_below_152_has_no_advanced_settings()
=> Assert.False(TpsCalibration.For(GenerationType.Gen3, 150).AdvSupported);
[Fact]
public void Gen2_shares_gen3_addresses()
{
var g2 = TpsCalibration.For(GenerationType.Gen2, 100);
var g3 = TpsCalibration.For(GenerationType.Gen3, 100);
Assert.Equal(g3.CalWrite, g2.CalWrite);
Assert.Equal(g3.AbsBounds, g2.AbsBounds);
}
[Fact]
public void Gen1_uses_legacy_high_addresses()
{
var a = TpsCalibration.For(GenerationType.Gen1, 70);
Assert.Equal(0x06FC, a.CalWrite);
Assert.False(a.AdvSupported);
}
[Fact]
public void CalDoneFlag_is_one() => Assert.Equal(1, TpsCalibration.CalDoneFlag);
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!-- Adjust the path if your Core project lives elsewhere. From a solution
folder this is typically ..\TuneBoss.Core\TuneBoss.Core.csproj -->
<ProjectReference Include="..\TuneBoss.Core\TuneBoss.Core.csproj" />
</ItemGroup>
</Project>