Support reading 16-byte (128-bit) NSNumber values, as well as non-power-of-2 sized values

This commit is contained in:
Frederik Carlier
2019-06-01 23:41:53 +02:00
parent bdbce8d547
commit b9c41214c4
3 changed files with 116 additions and 7 deletions

View File

@@ -16,9 +16,16 @@ namespace plistcil.test
[Theory]
[InlineData(new byte[] {0x57}, 0x57)]
[InlineData(new byte[] {0x12, 0x34}, 0x1234)]
[InlineData(new byte[] {0x12, 0x34, 0x56}, 0x123456)]
[InlineData(new byte[] {0x40, 0x2d, 0xf8, 0x4d}, 0x402df84d)]
[InlineData(new byte[] {0x12, 0x34, 0x56, 0x78, 0x9a }, 0x123456789a)]
[InlineData(new byte[] {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc }, 0x123456789abc)]
[InlineData(new byte[] {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde }, 0x123456789abcde)]
[InlineData(new byte[] {0x41, 0xb4, 0x83, 0x98, 0x2a, 0x00, 0x00, 0x00}, 0x41b483982a000000)]
[InlineData(new byte[] {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x19}, unchecked((long)0xfffffffffffffc19))]
[InlineData(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x19 },
unchecked((long)0xfffffffffffffc19))]
public void ParseLongTest(byte[] binaryValue, long expectedValue)
{
Assert.Equal(expectedValue, BinaryPropertyListParser.ParseLong(binaryValue));

View File

@@ -1,10 +1,77 @@
using Claunia.PropertyList;
using System;
using System.Collections.Generic;
using Xunit;
namespace plistcil.test
{
public class NSNumberTests
{
public static IEnumerable<object[]> SpanConstructorTestData()
{
return new List<object[]>
{
// INTEGER values
// 0
new object[] { new byte[] { 0x00 }, NSNumber.INTEGER, false, 0, 0.0 },
// 1-byte value < sbyte.maxValue
new object[] { new byte[] { 0x10 }, NSNumber.INTEGER, true, 16, 16.0 },
// 1-byte value > sbyte.MaxValue
new object[] { new byte[] { 0xFF }, NSNumber.INTEGER, true, byte.MaxValue, (double)byte.MaxValue},
// 2-byte value < short.maxValue
new object[] { new byte[] { 0x10, 0x00 }, NSNumber.INTEGER, true, 4096, 4096.0 },
// 2-byte value > short.maxValue
new object[] { new byte[] { 0xFF, 0xFF }, NSNumber.INTEGER, true, ushort.MaxValue, (double)ushort.MaxValue},
// 4-byte value < int.maxValue
new object[] { new byte[] { 0x10, 0x00, 0x00, 0x00 }, NSNumber.INTEGER, true, 0x10000000, 1.0 * 0x10000000 },
// 4-bit value > int.MaxValue
new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, NSNumber.INTEGER, true, uint.MaxValue, (double)uint.MaxValue },
// 64-bit value < long.MaxValue
new object[] { new byte[] { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NSNumber.INTEGER, true, 0x1000000000000000, 1.0 * 0x1000000000000000 },
// 64-bit value > long.MaxValue
new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, NSNumber.INTEGER, true, -1, -1.0 },
// 128-bit positive value
new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xa0, 0x00 }, NSNumber.INTEGER, true, unchecked((long)0xffffffffffffa000), 1.0 * unchecked((long)0xffffffffffffa000) },
// 128-bit negative value
new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, NSNumber.INTEGER, true, -1, -1.0 },
// REAL values
// 4-byte value (float)
new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00 }, NSNumber.REAL, false, 0, 0.0 },
new object[] { new byte[] { 0x41, 0x20, 0x00, 0x00 }, NSNumber.REAL, true, 10, 10.0 },
new object[] { new byte[] { 0x3d, 0xcc, 0xcc, 0xcd }, NSNumber.REAL, false, 0, 0.1 },
// 8-byte value (double)
new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NSNumber.REAL, false, 0, 0.0 },
new object[] { new byte[] { 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NSNumber.REAL, true, 10, 10.0 },
new object[] { new byte[] { 0x3f, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a }, NSNumber.REAL, false, 0, 0.1 }
};
}
[Theory]
[MemberData(nameof(SpanConstructorTestData))]
public void SpanConstructorTest(byte[] data, int type, bool boolValue, long longValue, double doubleValue)
{
NSNumber number = new NSNumber((Span<byte>)data, type);
Assert.Equal(boolValue, number.ToBool());
Assert.Equal(longValue, number.ToLong());
Assert.Equal(doubleValue, number.ToDouble(), 5);
}
[Fact]
public static void NSNumberConstructorTest()
{
@@ -72,5 +139,16 @@ namespace plistcil.test
Assert.Equal(7200d, number.ToDouble());
}
#endif
[Fact]
public void EqualTest()
{
NSNumber a = new NSNumber(2);
NSNumber b = new NSNumber(2);
Assert.Equal(a.GetHashCode(), b.GetHashCode());
Assert.True(a.Equals(b));
Assert.True(b.Equals(a));
}
}
}