Skip to content

Commit bce53de

Browse files
committed
Split integer tests
1 parent 073cd0e commit bce53de

File tree

4 files changed

+257
-124
lines changed

4 files changed

+257
-124
lines changed

Testing/ByteTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 ByteTests
9+
{
10+
#region Constants
11+
12+
private const byte TEST_VAL = 210;
13+
private static readonly byte[] _testValBytes = new byte[1]
14+
{
15+
0xD2,
16+
};
17+
18+
private static readonly byte[] _testArr = new byte[4]
19+
{
20+
99,
21+
209,
22+
3,
23+
64,
24+
};
25+
private static readonly byte[] _testArrBytes = new byte[4]
26+
{
27+
0x63,
28+
0xD1,
29+
0x03,
30+
0x40,
31+
};
32+
33+
#endregion
34+
35+
[Fact]
36+
public void ReadByte()
37+
{
38+
byte val;
39+
using (var stream = new MemoryStream(_testValBytes))
40+
{
41+
val = new EndianBinaryReader(stream).ReadByte();
42+
}
43+
Assert.Equal(TEST_VAL, val);
44+
}
45+
[Fact]
46+
public void ReadBytes()
47+
{
48+
byte[] arr = new byte[4];
49+
using (var stream = new MemoryStream(_testArrBytes))
50+
{
51+
new EndianBinaryReader(stream).ReadBytes(arr);
52+
}
53+
Assert.True(arr.SequenceEqual(_testArr));
54+
}
55+
[Fact]
56+
public void WriteByte()
57+
{
58+
byte[] bytes = new byte[1];
59+
using (var stream = new MemoryStream(bytes))
60+
{
61+
new EndianBinaryWriter(stream).WriteByte(TEST_VAL);
62+
}
63+
Assert.True(bytes.SequenceEqual(_testValBytes));
64+
}
65+
[Fact]
66+
public void WriteBytes()
67+
{
68+
byte[] bytes = new byte[4];
69+
using (var stream = new MemoryStream(bytes))
70+
{
71+
new EndianBinaryWriter(stream).WriteBytes(_testArr);
72+
}
73+
Assert.True(bytes.SequenceEqual(_testArrBytes));
74+
}
75+
}

Testing/Int16Tests.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 Int16Tests
9+
{
10+
#region Constants
11+
12+
private const short TEST_VAL = -6_969;
13+
private static readonly byte[] _testValBytesLE = new byte[sizeof(short)]
14+
{
15+
0xC7, 0xE4,
16+
};
17+
private static readonly byte[] _testValBytesBE = new byte[sizeof(short)]
18+
{
19+
0xE4, 0xC7,
20+
};
21+
22+
private static readonly short[] _testArr = new short[4]
23+
{
24+
-8_517,
25+
-22_343,
26+
26_381,
27+
2_131,
28+
};
29+
private static readonly byte[] _testArrBytesLE = new byte[4 * sizeof(short)]
30+
{
31+
0xBB, 0xDE,
32+
0xB9, 0xA8,
33+
0x0D, 0x67,
34+
0x53, 0x08,
35+
};
36+
private static readonly byte[] _testArrBytesBE = new byte[4 * sizeof(short)]
37+
{
38+
0xDE, 0xBB,
39+
0xA8, 0xB9,
40+
0x67, 0x0D,
41+
0x08, 0x53,
42+
};
43+
44+
#endregion
45+
46+
[Theory]
47+
[InlineData(true)]
48+
[InlineData(false)]
49+
public void ReadInt16(bool le)
50+
{
51+
byte[] input = le ? _testValBytesLE : _testValBytesBE;
52+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
53+
54+
short val;
55+
using (var stream = new MemoryStream(input))
56+
{
57+
val = new EndianBinaryReader(stream, endianness: e).ReadInt16();
58+
}
59+
Assert.Equal(TEST_VAL, val);
60+
}
61+
[Theory]
62+
[InlineData(true)]
63+
[InlineData(false)]
64+
public void ReadInt16s(bool le)
65+
{
66+
byte[] input = le ? _testArrBytesLE : _testArrBytesBE;
67+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
68+
69+
short[] arr = new short[4];
70+
using (var stream = new MemoryStream(input))
71+
{
72+
new EndianBinaryReader(stream, endianness: e).ReadInt16s(arr);
73+
}
74+
Assert.True(arr.SequenceEqual(_testArr));
75+
}
76+
[Theory]
77+
[InlineData(true)]
78+
[InlineData(false)]
79+
public void WriteInt16(bool le)
80+
{
81+
byte[] input = le ? _testValBytesLE : _testValBytesBE;
82+
Endianness e = le ? Endianness.LittleEndian : Endianness.BigEndian;
83+
84+
byte[] bytes = new byte[sizeof(short)];
85+
using (var stream = new MemoryStream(bytes))
86+
{
87+
new EndianBinaryWriter(stream, endianness: e).WriteInt16(TEST_VAL);
88+
}
89+
Assert.True(bytes.SequenceEqual(input));
90+
}
91+
[Theory]
92+
[InlineData(true)]
93+
[InlineData(false)]
94+
public void WriteInt16s(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(short)];
100+
using (var stream = new MemoryStream(bytes))
101+
{
102+
new EndianBinaryWriter(stream, endianness: e).WriteInt16s(_testArr);
103+
}
104+
Assert.True(bytes.SequenceEqual(input));
105+
}
106+
}

Testing/IntegerTests.cs

Lines changed: 0 additions & 124 deletions
This file was deleted.

Testing/SByteTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Kermalis.EndianBinaryIO;
2+
using System;
3+
using System.IO;
4+
using System.Linq;
5+
using Xunit;
6+
7+
namespace Kermalis.EndianBinaryIOTests;
8+
9+
public sealed class SByteTests
10+
{
11+
#region Constants
12+
13+
private const sbyte TEST_VAL = -93;
14+
private static readonly byte[] _testValBytes = new byte[1]
15+
{
16+
0xA3,
17+
};
18+
19+
private static readonly sbyte[] _testArr = new sbyte[4]
20+
{
21+
-21,
22+
6,
23+
17,
24+
-100,
25+
};
26+
private static readonly byte[] _testArrBytes = new byte[4]
27+
{
28+
0xEB,
29+
0x06,
30+
0x11,
31+
0x9C,
32+
};
33+
34+
#endregion
35+
36+
[Fact]
37+
public void ReadSByte()
38+
{
39+
sbyte val;
40+
using (var stream = new MemoryStream(_testValBytes))
41+
{
42+
val = new EndianBinaryReader(stream).ReadSByte();
43+
}
44+
Assert.Equal(TEST_VAL, val);
45+
}
46+
[Fact]
47+
public void ReadSBytes()
48+
{
49+
sbyte[] arr = new sbyte[4];
50+
using (var stream = new MemoryStream(_testArrBytes))
51+
{
52+
new EndianBinaryReader(stream).ReadSBytes(arr);
53+
}
54+
Assert.True(arr.SequenceEqual(_testArr));
55+
}
56+
[Fact]
57+
public void WriteSByte()
58+
{
59+
byte[] bytes = new byte[1];
60+
using (var stream = new MemoryStream(bytes))
61+
{
62+
new EndianBinaryWriter(stream).WriteSByte(TEST_VAL);
63+
}
64+
Assert.True(bytes.SequenceEqual(_testValBytes));
65+
}
66+
[Fact]
67+
public void WriteSBytes()
68+
{
69+
byte[] bytes = new byte[4];
70+
using (var stream = new MemoryStream(bytes))
71+
{
72+
new EndianBinaryWriter(stream).WriteSBytes(_testArr);
73+
}
74+
Assert.True(bytes.SequenceEqual(_testArrBytes));
75+
}
76+
}

0 commit comments

Comments
 (0)