|
| 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 | +} |
0 commit comments