Files
TuneBossPC/TuneBoss.Core.Tests/MapTableFormatTests.cs
T
2026-09-28 16:55:31 +08:00

102 lines
3.6 KiB
C#

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]));
}
}