Skip to content

Commit 94f8bd6

Browse files
authored
Fix Unit Tests for Parse (#171)
***NO_CI***
1 parent edb8714 commit 94f8bd6

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

Tests/NFUnitTestSystemLib/UnitTestParseTests.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace NFUnitTestSystemLib
1313
[TestClass]
1414
class UnitTestParseTests
1515
{
16-
public int[] intArr = null;
16+
private static int[] _intArr = new int[0];
1717

18-
public string[] GetRandomStringArray(int max, bool signed)
18+
public static string[] GetRandomStringArray(int max, bool signed)
1919
{
2020
Random random = new Random();
2121

@@ -34,13 +34,13 @@ public string[] GetRandomStringArray(int max, bool signed)
3434
"+123 ",
3535
"56",
3636
"62",
37-
"100"
37+
"100",
38+
"22"
3839
};
3940

40-
4141
string[] arr2 = new string[arr1.Length];
4242

43-
intArr = new int[] {
43+
_intArr = new int[] {
4444
0,
4545
0,
4646
0,
@@ -56,6 +56,8 @@ public string[] GetRandomStringArray(int max, bool signed)
5656
56,
5757
62,
5858
100,
59+
22,
60+
0,
5961
0,
6062
0,
6163
0,
@@ -74,23 +76,23 @@ public string[] GetRandomStringArray(int max, bool signed)
7476
};
7577

7678
// sanity check for when the test arrays above change
77-
Debug.Assert(intArr.Length == 2 * arr1.Length);
79+
Assert.Equal(_intArr.Length, 2 * arr1.Length);
7880

7981
for (int i = 0; i < arr2.Length; i++)
8082
{
8183
if (signed && ((i % 2) == 0))
8284
{
83-
intArr[i + arr1.Length] = -(random.Next(max));
84-
arr2[i] = (intArr[i + arr1.Length].ToString());
85+
_intArr[i + arr1.Length] = -(random.Next(max));
86+
arr2[i] = (_intArr[i + arr1.Length].ToString());
8587
}
8688
else
8789
{
88-
intArr[i + arr1.Length] = random.Next(max);
89-
arr2[i] = intArr[i + arr1.Length].ToString();
90+
_intArr[i + arr1.Length] = random.Next(max);
91+
arr2[i] = _intArr[i + arr1.Length].ToString();
9092
}
9193
}
9294

93-
string[] arr = new string[intArr.Length];
95+
string[] arr = new string[_intArr.Length];
9496

9597
Array.Copy(arr1, arr, arr1.Length);
9698
Array.Copy(arr2, 0, arr, arr1.Length, arr2.Length);
@@ -105,10 +107,10 @@ public void ParseSByte_Test_1()
105107
OutputHelper.WriteLine("SByte MaxValue = " + sbyte.MaxValue.ToString());
106108

107109
string[] strArr = GetRandomStringArray(sbyte.MaxValue, true);
108-
sbyte[] _sByte = new sbyte[intArr.Length];
110+
sbyte[] _sByte = new sbyte[_intArr.Length];
109111
for (int i = 0; i < _sByte.Length; i++)
110112
{
111-
_sByte[i] = (sbyte)intArr[i];
113+
_sByte[i] = (sbyte)_intArr[i];
112114
}
113115

114116
for (int i = 0; i < strArr.Length; i++)
@@ -127,15 +129,16 @@ public void ParseByte_Test_2()
127129
OutputHelper.WriteLine("Byte MaxValue = " + byte.MaxValue.ToString());
128130

129131
string[] strArr = GetRandomStringArray(byte.MaxValue, false);
130-
byte[] _byte = new byte[intArr.Length];
132+
byte[] _byte = new byte[_intArr.Length];
133+
131134
for (int i = 0; i < _byte.Length; i++)
132135
{
133-
_byte[i] = (byte)intArr[i];
136+
_byte[i] = (byte)_intArr[i];
134137
}
135138

136139
for (int i = 0; i < strArr.Length; i++)
137140
{
138-
Assert.Equal(byte.Parse(strArr[i]), _byte[i]);
141+
Assert.Equal(byte.Parse(strArr[i]), _byte[i], $"Parse failed for {strArr[i]}, expecting: {_byte[i]}");
139142

140143
Assert.True(byte.TryParse(strArr[i], out byte result), $"TryParse failed for {strArr[i]} expecting: {_byte[i]}");
141144
Assert.Equal(_byte[i], result);
@@ -149,10 +152,10 @@ public void ParseInt16_Test_3()
149152
OutputHelper.WriteLine("Int16 MaxValue = " + short.MaxValue.ToString());
150153

151154
string[] strArr = GetRandomStringArray(short.MaxValue, true);
152-
short[] _int16 = new short[intArr.Length];
155+
short[] _int16 = new short[_intArr.Length];
153156
for (int i = 0; i < _int16.Length; i++)
154157
{
155-
_int16[i] = (short)intArr[i];
158+
_int16[i] = (short)_intArr[i];
156159
}
157160

158161
for (int i = 0; i < strArr.Length; i++)
@@ -172,10 +175,10 @@ public void ParseUInt16_Test_4()
172175

173176

174177
string[] strArr = GetRandomStringArray(ushort.MaxValue, false);
175-
ushort[] _uInt16 = new ushort[intArr.Length];
178+
ushort[] _uInt16 = new ushort[_intArr.Length];
176179
for (int i = 0; i < _uInt16.Length; i++)
177180
{
178-
_uInt16[i] = (ushort)intArr[i];
181+
_uInt16[i] = (ushort)_intArr[i];
179182
}
180183

181184
for (int i = 0; i < strArr.Length; i++)
@@ -194,10 +197,10 @@ public void ParseInt32_Test_5()
194197
OutputHelper.WriteLine("Int32 MaxValue = " + int.MaxValue.ToString());
195198

196199
string[] strArr = GetRandomStringArray(int.MaxValue, true);
197-
int[] _int32 = new int[intArr.Length];
200+
int[] _int32 = new int[_intArr.Length];
198201
for (int i = 0; i < _int32.Length; i++)
199202
{
200-
_int32[i] = intArr[i];
203+
_int32[i] = _intArr[i];
201204
}
202205

203206
for (int i = 0; i < strArr.Length; i++)
@@ -515,7 +518,11 @@ public void ParseDouble_Test_Invalid_Values()
515518

516519
for (int i = 0; i < strArr.Length; i++)
517520
{
518-
Assert.Throws(typeof(FormatException), () => { _ = double.Parse(strArr[i]); }, $"Should throw exception of type FormatExeception while parsing string: '{strArr[i]}'");
521+
OutputHelper.WriteLine($"parse {strArr[i]}");
522+
523+
Assert.Throws(typeof(FormatException),
524+
() => { double.Parse(strArr[i]); },
525+
$"Should throw exception of type FormatExeception while parsing string: '{strArr[i]}'");
519526

520527
Assert.False(double.TryParse(strArr[i], out double _), $"TryParse should return false while parsing string: '{strArr[i]}'");
521528
}

0 commit comments

Comments
 (0)