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

100 lines
3.0 KiB
C#

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