Skip to content

Commit a698a8d

Browse files
committed
Add new tests
1 parent 7320199 commit a698a8d

12 files changed

+1089
-152
lines changed

Testing/EncodingTests.cs renamed to Testing/CharTests.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66

77
namespace Kermalis.EndianBinaryIOTests;
88

9-
public sealed class EncodingTests
9+
public sealed class CharTests
1010
{
11-
private interface ICharObj
12-
{
13-
string Str { get; set; }
14-
}
15-
private sealed class CharObj : ICharObj
11+
private sealed class CharObj
1612
{
1713
public byte Len { get; set; }
1814
[BinaryStringVariableLength(nameof(Len))]
@@ -29,16 +25,7 @@ private sealed class CharObj : ICharObj
2925
0x0D, 0x0A, // "\r\n"
3026
0x46, 0x75, 0x6E, 0x6E, 0x69, 0x65, 0x73 // "Funnies"
3127
};
32-
private const string TEST_STR_UTF = "Jummy😀\r\n😳Funnies";
33-
private static readonly byte[] _testBytes_UTF8 = new byte[]
34-
{
35-
0x12, // Len
36-
0x4A, 0x75, 0x6D, 0x6D, 0x79, // "Jummy"
37-
0xF0, 0x9F, 0x98, 0x80, // "😀"
38-
0x0D, 0x0A, // "\r\n"
39-
0xF0, 0x9F, 0x98, 0xB3, // "😳"
40-
0x46, 0x75, 0x6E, 0x6E, 0x69, 0x65, 0x73 // "Funnies"
41-
};
28+
private const string TEST_STR_UTF16LE = "Jummy😀\r\n😳Funnies";
4229
private static readonly byte[] _testBytes_UTF16LE = new byte[]
4330
{
4431
0x12, // Len
@@ -55,18 +42,20 @@ private static void Get(bool ascii, out byte[] input, out string str)
5542
{
5643
if (ascii)
5744
{
58-
input = _testBytes_ASCII; str = TEST_STR_ASCII;
45+
input = _testBytes_ASCII;
46+
str = TEST_STR_ASCII;
5947
}
6048
else
6149
{
62-
input = _testBytes_UTF16LE; str = TEST_STR_UTF;
50+
input = _testBytes_UTF16LE;
51+
str = TEST_STR_UTF16LE;
6352
}
6453
}
65-
private static void TestRead<T>(bool ascii, byte[] input, string str) where T : ICharObj, new()
54+
private static void TestRead(bool ascii, byte[] input, string str)
6655
{
6756
using (var stream = new MemoryStream(input))
6857
{
69-
T obj = new EndianBinaryReader(stream, endianness: Endianness.LittleEndian, ascii: ascii).ReadObject<T>();
58+
CharObj obj = new EndianBinaryReader(stream, ascii: ascii).ReadObject<CharObj>();
7059
Assert.Equal(str, obj.Str);
7160
}
7261
}
@@ -80,7 +69,7 @@ private static void TestWrite(bool ascii, byte[] input, string str)
8069
Len = (byte)str.Length,
8170
Str = str,
8271
};
83-
new EndianBinaryWriter(stream, endianness: Endianness.LittleEndian, ascii: ascii).WriteObject(obj);
72+
new EndianBinaryWriter(stream, ascii: ascii).WriteObject(obj);
8473
}
8574
Assert.True(bytes.SequenceEqual(input));
8675
}
@@ -91,7 +80,7 @@ private static void TestWrite(bool ascii, byte[] input, string str)
9180
public void ReadDefaults(bool ascii)
9281
{
9382
Get(ascii, out byte[] input, out string str);
94-
TestRead<CharObj>(ascii, input, str);
83+
TestRead(ascii, input, str);
9584
}
9685

9786
[Theory]

Testing/DecimalTests.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Kermalis.EndianBinaryIO;
2+
using System.IO;
3+
using System.Linq;
4+
using Xunit;
5+
6+
namespace Kermalis.EndianBinaryIOTests;
7+
8+
public sealed class DecimalTests
9+
{
10+
#region Constants
11+
12+
private const decimal TEST_VAL = 12_345_678_909_876_543_210.123456789m;
13+
private static readonly byte[] _testValBytesLE = new byte[sizeof(decimal)]
14+
{
15+
0x15, 0x71, 0x84, 0xA0, /**/ 0x75, 0x40, 0xAD, 0xBE, /**/ 0x32, 0x1B, 0xE4, 0x27, /**/ 0x00, 0x00, 0x09, 0x00,
16+
};
17+
private static readonly byte[] _testValBytesBE = new byte[sizeof(decimal)]
18+
{
19+
0xA0, 0x84, 0x71, 0x15, /**/ 0xBE, 0xAD, 0x40, 0x75, /**/ 0x27, 0xE4, 0x1B, 0x32, /**/ 0x00, 0x09, 0x00, 0x00,
20+
};
21+
22+
private static readonly decimal[] _testArr = new decimal[4]
23+
{
24+
-18_185_544_635_427_120_524.93305179m,
25+
-22_010_447_631_927_599_247.18039726m,
26+
41_455_770_299_484_821_081.65900781m,
27+
22_442_965_292_979_427_993.29821457m,
28+
};
29+
private static readonly byte[] _testArrBytesLE = new byte[4 * sizeof(decimal)]
30+
{
31+
0x5B, 0xC5, 0x4B, 0x5E, /**/ 0x65, 0xE6, 0xFF, 0x01, /**/ 0xE3, 0x45, 0xE0, 0x05, /**/ 0x00, 0x00, 0x08, 0x80,
32+
0xAE, 0xF2, 0x4B, 0xBB, /**/ 0xA4, 0x20, 0x17, 0xB3, /**/ 0x5B, 0xA9, 0x1C, 0x07, /**/ 0x00, 0x00, 0x08, 0x80,
33+
0xED, 0xC9, 0xFE, 0x4A, /**/ 0x54, 0x20, 0x93, 0x1A, /**/ 0x15, 0x24, 0x65, 0x0D, /**/ 0x00, 0x00, 0x08, 0x00,
34+
0x11, 0x83, 0x14, 0x50, /**/ 0x63, 0x4A, 0x69, 0xA3, /**/ 0x46, 0x70, 0x40, 0x07, /**/ 0x00, 0x00, 0x08, 0x00,
35+
};
36+
private static readonly byte[] _testArrBytesBE = new byte[4 * sizeof(decimal)]
37+
{
38+
0x5E, 0x4B, 0xC5, 0x5B, /**/ 0x01, 0xFF, 0xE6, 0x65, /**/ 0x05, 0xE0, 0x45, 0xE3, /**/ 0x80, 0x08, 0x00, 0x00,
39+
0xBB, 0x4B, 0xF2, 0xAE, /**/ 0xB3, 0x17, 0x20, 0xA4, /**/ 0x07, 0x1C, 0xA9, 0x5B, /**/ 0x80, 0x08, 0x00, 0x00,
40+
0x4A, 0xFE, 0xC9, 0xED, /**/ 0x1A, 0x93, 0x20, 0x54, /**/ 0x0D, 0x65, 0x24, 0x15, /**/ 0x00, 0x08, 0x00, 0x00,
41+
0x50, 0x14, 0x83, 0x11, /**/ 0xA3, 0x69, 0x4A, 0x63, /**/ 0x07, 0x40, 0x70, 0x46, /**/ 0x00, 0x08, 0x00, 0x00,
42+
};
43+
44+
#endregion
45+
46+
[Theory]
47+
[InlineData(true)]
48+
[InlineData(false)]
49+
public void ReadDecimal(bool le)
50+
{
51+
byte[] input = le ? _testValBytesLE : _testValBytesBE;
52+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
53+
54+
decimal val;
55+
using (var stream = new MemoryStream(input))
56+
{
57+
val = new EndianBinaryReader(stream, endianness: e).ReadDecimal();
58+
}
59+
Assert.Equal(TEST_VAL, val);
60+
}
61+
[Theory]
62+
[InlineData(true)]
63+
[InlineData(false)]
64+
public void ReadDecimals(bool le)
65+
{
66+
byte[] input = le ? _testArrBytesLE : _testArrBytesBE;
67+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
68+
69+
decimal[] arr = new decimal[4];
70+
using (var stream = new MemoryStream(input))
71+
{
72+
new EndianBinaryReader(stream, endianness: e).ReadDecimals(arr);
73+
}
74+
Assert.True(arr.SequenceEqual(_testArr));
75+
}
76+
[Theory]
77+
[InlineData(true)]
78+
[InlineData(false)]
79+
public void WriteDecimal(bool le)
80+
{
81+
byte[] input = le ? _testValBytesLE : _testValBytesBE;
82+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
83+
84+
byte[] bytes = new byte[sizeof(decimal)];
85+
using (var stream = new MemoryStream(bytes))
86+
{
87+
new EndianBinaryWriter(stream, endianness: e).WriteDecimal(TEST_VAL);
88+
}
89+
Assert.True(bytes.SequenceEqual(input));
90+
}
91+
[Theory]
92+
[InlineData(true)]
93+
[InlineData(false)]
94+
public void WriteDecimals(bool le)
95+
{
96+
byte[] input = le ? _testArrBytesLE : _testArrBytesBE;
97+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
98+
99+
byte[] bytes = new byte[4 * sizeof(decimal)];
100+
using (var stream = new MemoryStream(bytes))
101+
{
102+
new EndianBinaryWriter(stream, endianness: e).WriteDecimals(_testArr);
103+
}
104+
Assert.True(bytes.SequenceEqual(input));
105+
}
106+
}

Testing/DoubleTests.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Kermalis.EndianBinaryIO;
2+
using System.IO;
3+
using System.Linq;
4+
using Xunit;
5+
6+
namespace Kermalis.EndianBinaryIOTests;
7+
8+
public sealed class DoubleTests
9+
{
10+
#region Constants
11+
12+
private const double TEST_VAL = 12_345_678.12345678d;
13+
private static readonly byte[] _testValBytesLE = new byte[sizeof(double)]
14+
{
15+
0xA2, 0x5B, 0xF3, 0xC3, 0x29, 0x8C, 0x67, 0x41,
16+
};
17+
private static readonly byte[] _testValBytesBE = new byte[sizeof(double)]
18+
{
19+
0x41, 0x67, 0x8C, 0x29, 0xC3, 0xF3, 0x5B, 0xA2,
20+
};
21+
22+
private static readonly double[] _testArr = new double[4]
23+
{
24+
-51_692_240.59455357d,
25+
-68_231_145.04473292d,
26+
98_110_687.70543043d,
27+
75_442_096.25828312d,
28+
};
29+
private static readonly byte[] _testArrBytesLE = new byte[4 * sizeof(double)]
30+
{
31+
0x4D, 0xA5, 0xC1, 0x84, 0x16, 0xA6, 0x88, 0xC1,
32+
0x77, 0xCE, 0x2D, 0xA4, 0x7F, 0x44, 0x90, 0xC1,
33+
0x5B, 0x5C, 0xD2, 0x7E, 0x33, 0x64, 0x97, 0x41,
34+
0x5F, 0x7B, 0x08, 0xC1, 0x9E, 0xFC, 0x91, 0x41,
35+
};
36+
private static readonly byte[] _testArrBytesBE = new byte[4 * sizeof(double)]
37+
{
38+
0xC1, 0x88, 0xA6, 0x16, 0x84, 0xC1, 0xA5, 0x4D,
39+
0xC1, 0x90, 0x44, 0x7F, 0xA4, 0x2D, 0xCE, 0x77,
40+
0x41, 0x97, 0x64, 0x33, 0x7E, 0xD2, 0x5C, 0x5B,
41+
0x41, 0x91, 0xFC, 0x9E, 0xC1, 0x08, 0x7B, 0x5F,
42+
};
43+
44+
#endregion
45+
46+
[Theory]
47+
[InlineData(true)]
48+
[InlineData(false)]
49+
public void ReadDouble(bool le)
50+
{
51+
byte[] input = le ? _testValBytesLE : _testValBytesBE;
52+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
53+
54+
double val;
55+
using (var stream = new MemoryStream(input))
56+
{
57+
val = new EndianBinaryReader(stream, endianness: e).ReadDouble();
58+
}
59+
Assert.Equal(TEST_VAL, val);
60+
}
61+
[Theory]
62+
[InlineData(true)]
63+
[InlineData(false)]
64+
public void ReadDoubles(bool le)
65+
{
66+
byte[] input = le ? _testArrBytesLE : _testArrBytesBE;
67+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
68+
69+
double[] arr = new double[4];
70+
using (var stream = new MemoryStream(input))
71+
{
72+
new EndianBinaryReader(stream, endianness: e).ReadDoubles(arr);
73+
}
74+
Assert.True(arr.SequenceEqual(_testArr));
75+
}
76+
[Theory]
77+
[InlineData(true)]
78+
[InlineData(false)]
79+
public void WriteDouble(bool le)
80+
{
81+
byte[] input = le ? _testValBytesLE : _testValBytesBE;
82+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
83+
84+
byte[] bytes = new byte[sizeof(double)];
85+
using (var stream = new MemoryStream(bytes))
86+
{
87+
new EndianBinaryWriter(stream, endianness: e).WriteDouble(TEST_VAL);
88+
}
89+
Assert.True(bytes.SequenceEqual(input));
90+
}
91+
[Theory]
92+
[InlineData(true)]
93+
[InlineData(false)]
94+
public void WriteDoubles(bool le)
95+
{
96+
byte[] input = le ? _testArrBytesLE : _testArrBytesBE;
97+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
98+
99+
byte[] bytes = new byte[4 * sizeof(double)];
100+
using (var stream = new MemoryStream(bytes))
101+
{
102+
new EndianBinaryWriter(stream, endianness: e).WriteDoubles(_testArr);
103+
}
104+
Assert.True(bytes.SequenceEqual(input));
105+
}
106+
}

0 commit comments

Comments
 (0)