|
6 | 6 |
|
7 | 7 | public static class BitConverterTests
|
8 | 8 | {
|
| 9 | + [Fact] |
| 10 | + public static unsafe void IsLittleEndian() |
| 11 | + { |
| 12 | + short s = 1; |
| 13 | + Assert.Equal(BitConverter.IsLittleEndian, *((byte*)&s) == 1); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public static void ValueArgumentNull() |
| 18 | + { |
| 19 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToBoolean(null, 0)); |
| 20 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToChar(null, 0)); |
| 21 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToDouble(null, 0)); |
| 22 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToInt16(null, 0)); |
| 23 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToInt32(null, 0)); |
| 24 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToInt64(null, 0)); |
| 25 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToSingle(null, 0)); |
| 26 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToUInt16(null, 0)); |
| 27 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToUInt32(null, 0)); |
| 28 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToUInt64(null, 0)); |
| 29 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToString(null)); |
| 30 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToString(null, 0)); |
| 31 | + Assert.Throws<ArgumentNullException>("value", () => BitConverter.ToString(null, 0, 0)); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public static void StartIndexBeyondLength() |
| 36 | + { |
| 37 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToBoolean(new byte[1], -1)); |
| 38 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToBoolean(new byte[1], 1)); |
| 39 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToBoolean(new byte[1], 2)); |
| 40 | + |
| 41 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToChar(new byte[2], -1)); |
| 42 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToChar(new byte[2], 2)); |
| 43 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToChar(new byte[2], 3)); |
| 44 | + |
| 45 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToDouble(new byte[8], -1)); |
| 46 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToDouble(new byte[8], 8)); |
| 47 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToDouble(new byte[8], 9)); |
| 48 | + |
| 49 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt16(new byte[2], -1)); |
| 50 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt16(new byte[2], 2)); |
| 51 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt16(new byte[2], 3)); |
| 52 | + |
| 53 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt32(new byte[4], -1)); |
| 54 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt32(new byte[4], 4)); |
| 55 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt32(new byte[4], 5)); |
| 56 | + |
| 57 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt64(new byte[8], -1)); |
| 58 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt64(new byte[8], 8)); |
| 59 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToInt64(new byte[8], 9)); |
| 60 | + |
| 61 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToSingle(new byte[4], -1)); |
| 62 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToSingle(new byte[4], 4)); |
| 63 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToSingle(new byte[4], 5)); |
| 64 | + |
| 65 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt16(new byte[2], -1)); |
| 66 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt16(new byte[2], 2)); |
| 67 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt16(new byte[2], 3)); |
| 68 | + |
| 69 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt32(new byte[4], -1)); |
| 70 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt32(new byte[4], 4)); |
| 71 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt32(new byte[4], 5)); |
| 72 | + |
| 73 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt64(new byte[8], -1)); |
| 74 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt64(new byte[8], 8)); |
| 75 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToUInt64(new byte[8], 9)); |
| 76 | + |
| 77 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToString(new byte[1], -1)); |
| 78 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToString(new byte[1], 1)); |
| 79 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToString(new byte[1], 2)); |
| 80 | + |
| 81 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToString(new byte[1], -1, 0)); |
| 82 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToString(new byte[1], 1, 0)); |
| 83 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToString(new byte[1], 2, 0)); |
| 84 | + |
| 85 | + Assert.Throws<ArgumentOutOfRangeException>("length", () => BitConverter.ToString(new byte[1], 0, -1)); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public static void StartIndexPlusNeededLengthTooLong() |
| 90 | + { |
| 91 | + Assert.Throws<ArgumentOutOfRangeException>("startIndex", () => BitConverter.ToBoolean(new byte[0], 0)); |
| 92 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToChar(new byte[2], 1)); |
| 93 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToDouble(new byte[8], 1)); |
| 94 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToInt16(new byte[2], 1)); |
| 95 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToInt32(new byte[4], 1)); |
| 96 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToInt64(new byte[8], 1)); |
| 97 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToSingle(new byte[4], 1)); |
| 98 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToUInt16(new byte[2], 1)); |
| 99 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToUInt32(new byte[4], 1)); |
| 100 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToUInt64(new byte[8], 1)); |
| 101 | + Assert.Throws<ArgumentException>("value", () => BitConverter.ToString(new byte[2], 1, 2)); |
| 102 | + } |
| 103 | + |
9 | 104 | [Fact]
|
10 | 105 | public static void DoubleToInt64Bits()
|
11 | 106 | {
|
@@ -125,5 +220,11 @@ private static void VerifyRoundtrip<TInput>(Func<TInput, Byte[]> getBytes, Func<
|
125 | 220 |
|
126 | 221 | Assert.Equal(expectedBytes, bytes);
|
127 | 222 | Assert.Equal(input, convertBack(bytes, 0));
|
| 223 | + |
| 224 | + // Also try unaligned startIndex |
| 225 | + byte[] longerBytes = new byte[bytes.Length + 1]; |
| 226 | + longerBytes[0] = 0; |
| 227 | + Array.Copy(bytes, 0, longerBytes, 1, bytes.Length); |
| 228 | + Assert.Equal(input, convertBack(longerBytes, 1)); |
128 | 229 | }
|
129 | 230 | }
|
0 commit comments