Skip to content

Commit

Permalink
Test type equality in more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Jan 15, 2025
1 parent 6809909 commit 98c697c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
69 changes: 31 additions & 38 deletions MaxMind.Db.Test/DecoderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class DecoderTest
[MemberData(nameof(TestUInt16))]
[MemberData(nameof(TestUInt32))]
[MemberData(nameof(TestInt32s))]
[MemberData(nameof(TestInt64s))]
[MemberData(nameof(TestUInt64s))]
[MemberData(nameof(TestBigIntegers))]
[MemberData(nameof(TestDoubles))]
[MemberData(nameof(TestFloats))]
Expand All @@ -26,7 +26,7 @@ public static class DecoderTest
[MemberData(nameof(TestBytes))]
[MemberData(nameof(TestMaps))]
[MemberData(nameof(TestArrays))]
public static void TestTypeDecoding<T>(Dictionary<T, byte[]> tests, bool useShouldBe = false) where T : class
public static void TestTypeDecoding<T>(Dictionary<T, byte[]> tests) where T : class
{
foreach (var entry in tests)
{
Expand All @@ -36,14 +36,7 @@ public static void TestTypeDecoding<T>(Dictionary<T, byte[]> tests, bool useShou
using var database = new ArrayBuffer(input);
var decoder = new Decoder(database, 0, false);
var val = decoder.Decode<T>(0, out _);
if (useShouldBe)
{
Assert.Equal(expect, val);
}
else
{
Assert.Equivalent(expect, val);
}
}
}

Expand All @@ -55,7 +48,7 @@ public static IEnumerable<object[]> TestUInt16()
{(1 << 8) - 1, [0xa1, 0xff] },
{500, [0xa2, 0x1, 0xf4] },
{10872, [0xa2, 0x2a, 0x78] },
{ushort.MaxValue, [0xa2, 0xff, 0xff] }
{(int) ushort.MaxValue, [0xa2, 0xff, 0xff] }
};

yield return [uint16s];
Expand All @@ -65,13 +58,13 @@ public static IEnumerable<object[]> TestUInt32()
{
var uint32s = new Dictionary<object, byte[]>
{
{0, [0xc0] },
{(1 << 8) - 1, [0xc1, 0xff] },
{500, [0xc2, 0x1, 0xf4] },
{10872, [0xc2, 0x2a, 0x78] },
{(1 << 16) - 1, [0xc2, 0xff, 0xff] },
{(1 << 24) - 1, [0xc3, 0xff, 0xff, 0xff] },
{uint.MaxValue, [0xc4, 0xff, 0xff, 0xff, 0xff] }
{0L, [0xc0] },
{(1L << 8) - 1, [0xc1, 0xff] },
{500L, [0xc2, 0x1, 0xf4] },
{10872L, [0xc2, 0x2a, 0x78] },
{(1L << 16) - 1, [0xc2, 0xff, 0xff] },
{(1L << 24) - 1, [0xc3, 0xff, 0xff, 0xff] },
{(long) uint.MaxValue, [0xc4, 0xff, 0xff, 0xff, 0xff] }
};

yield return [uint32s];
Expand All @@ -98,18 +91,18 @@ public static IEnumerable<object[]> TestInt32s()
yield return [int32s];
}

public static IEnumerable<object[]> TestInt64s()
public static IEnumerable<object[]> TestUInt64s()
{
var int64s = new Dictionary<object, byte[]>
var uint64s = new Dictionary<object, byte[]>
{
{0L, [0x0, 0x2] },
{500L, [0x2, 0x2, 0x1, 0xf4] },
{10872, [0x2, 0x2, 0x2a, 0x78] }
{0UL, [0x0, 0x2] },
{500UL, [0x2, 0x2, 0x1, 0xf4] },
{10872UL, [0x2, 0x2, 0x2a, 0x78] }
};

for (var power = 1; power < 8; power++)
{
var key = Int64Pow(2, 8 * power) - 1;
var key = UInt64Pow(2, 8 * power) - 1;
var value = new byte[2 + power];

value[0] = (byte)power;
Expand All @@ -119,15 +112,15 @@ public static IEnumerable<object[]> TestInt64s()
value[i] = 0xff;
}

int64s.Add(key, value);
uint64s.Add(key, value);
}

yield return [int64s];
yield return [uint64s];
}

public static long Int64Pow(long x, int pow)
public static ulong UInt64Pow(ulong x, int pow)
{
long ret = 1;
ulong ret = 1;
while (pow != 0)
{
if ((pow & 1) == 1)
Expand Down Expand Up @@ -162,7 +155,7 @@ public static IEnumerable<object[]> TestBigIntegers()
bigInts.Add(key, value);
}

yield return [bigInts, /*useShouldBe*/ true];
yield return [bigInts];
}

public static IEnumerable<object[]> TestDoubles()
Expand Down Expand Up @@ -204,16 +197,16 @@ public static IEnumerable<object[]> TestPointers()
{
var pointers = new Dictionary<object, byte[]>
{
{0, [0x20, 0x0] },
{5, [0x20, 0x5] },
{10, [0x20, 0xa] },
{(1 << 10) - 1, [0x23, 0xff] },
{3017, [0x28, 0x3, 0xc9] },
{(1 << 19) - 5, [0x2f, 0xf7, 0xfb] },
{(1 << 19) + (1 << 11) - 1, [0x2f, 0xff, 0xff] },
{(1 << 27) - 2, [0x37, 0xf7, 0xf7, 0xfe] },
{((long) 1 << 27) + (1 << 19) + (1 << 11) - 1, [0x37, 0xff, 0xff, 0xff] },
{((long) 1 << 31) - 1, [0x38, 0x7f, 0xff, 0xff, 0xff] }
{0L, [0x20, 0x0] },
{5L, [0x20, 0x5] },
{10L, [0x20, 0xa] },
{(1L << 10) - 1, [0x23, 0xff] },
{3017L, [0x28, 0x3, 0xc9] },
{(1L << 19) - 5, [0x2f, 0xf7, 0xfb] },
{(1L << 19) + (1 << 11) - 1, [0x2f, 0xff, 0xff] },
{(1L << 27) - 2, [0x37, 0xf7, 0xf7, 0xfe] },
{(1L << 27) + (1 << 19) + (1 << 11) - 1, [0x37, 0xff, 0xff, 0xff] },
{(1L << 31) - 1, [0x38, 0x7f, 0xff, 0xff, 0xff] }
};

yield return [pointers];
Expand Down
26 changes: 13 additions & 13 deletions MaxMind.Db.Test/ReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ private static void TestDecodingTypes(IDictionary<string, object>? record)
Assert.Equal("unicode! ☯ - ♫", record["utf8_string"]);

var array = (List<object>)record["array"];
Assert.Equivalent(3, array.Count);
Assert.Equivalent(1, array[0]);
Assert.Equivalent(2, array[1]);
Assert.Equivalent(3, array[2]);
Assert.Equal(3, array.Count);
Assert.Equal(1L, array[0]);
Assert.Equal(2L, array[1]);
Assert.Equal(3L, array[2]);

var map = (Dictionary<string, object>)record["map"];
Assert.Single(map);
Expand All @@ -364,17 +364,17 @@ private static void TestDecodingTypes(IDictionary<string, object>? record)
Assert.Equal("hello", mapX["utf8_stringX"]);

var arrayX = (List<object>)mapX["arrayX"];
Assert.Equivalent(3, arrayX.Count);
Assert.Equivalent(7, arrayX[0]);
Assert.Equivalent(8, arrayX[1]);
Assert.Equivalent(9, arrayX[2]);
Assert.Equal(3, arrayX.Count);
Assert.Equal(7L, arrayX[0]);
Assert.Equal(8L, arrayX[1]);
Assert.Equal(9L, arrayX[2]);

Assert.Equal(42.123456, (double)record["double"], 9);
Assert.Equal(1.1F, (float)record["float"], 5);
Assert.Equal(-268435456, record["int32"]);
Assert.Equal(100, record["uint16"]);
Assert.Equivalent(268435456, record["uint32"]);
Assert.Equivalent(1152921504606846976, record["uint64"]);
Assert.Equal(268435456L, record["uint32"]);
Assert.Equal(1152921504606846976UL, record["uint64"]);
Assert.Equal(
BigInteger.Parse("1329227995784915872903807060280344576"),
record["uint128"]);
Expand Down Expand Up @@ -407,7 +407,7 @@ public void TestDecodingTypesToObject()
Assert.Equal(-268435456, record.Int32);
Assert.Equal(100, record.Uint16);
Assert.Equal(268435456, record.Uint32);
Assert.Equivalent(1152921504606846976, record.Uint64);
Assert.Equal(1152921504606846976UL, record.Uint64);
Assert.Equal(BigInteger.Parse("1329227995784915872903807060280344576"), record.Uint128);

Assert.Equal("injected string", record.Nonexistant.Injected);
Expand Down Expand Up @@ -443,8 +443,8 @@ public void TestZeros()
Assert.Equal(0, (float)record["float"], 5);
Assert.Equal(0, record["int32"]);
Assert.Equal(0, record["uint16"]);
Assert.Equivalent(0, record["uint32"]);
Assert.Equivalent(0, record["uint64"]);
Assert.Equal(0L, record["uint32"]);
Assert.Equal(0UL, record["uint64"]);
Assert.Equal(new BigInteger(0), record["uint128"]);
}

Expand Down

0 comments on commit 98c697c

Please sign in to comment.