Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit ba4a4ec

Browse files
committed
Add more tests for BitConverter
Brings coverage of BitConvert to ~95%.
1 parent 6e5d09b commit ba4a4ec

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<RootNamespace>System.Runtime.Extensions.Tests</RootNamespace>
1010
<AssemblyName>System.Runtime.Extensions.Tests</AssemblyName>
1111
<RestorePackages>true</RestorePackages>
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1213
</PropertyGroup>
1314
<!-- Default configurations to help VS understand the configurations -->
1415
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

src/System.Runtime.Extensions/tests/System/BitConverter.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,101 @@
66

77
public static class BitConverterTests
88
{
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+
9104
[Fact]
10105
public static void DoubleToInt64Bits()
11106
{
@@ -125,5 +220,11 @@ private static void VerifyRoundtrip<TInput>(Func<TInput, Byte[]> getBytes, Func<
125220

126221
Assert.Equal(expectedBytes, bytes);
127222
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));
128229
}
129230
}

0 commit comments

Comments
 (0)