Skip to content

Commit e7f7768

Browse files
authored
Work and improvements in unit tests (#232)
***NO_CI***
1 parent c99d8e3 commit e7f7768

13 files changed

+1247
-1068
lines changed

Tests/NFUnitTestConversions/UnitTestConvertTests.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ public void Convert_Negative()
9696
string number = "-12";
9797
int actualNumber = -12;
9898
sbyte value_sb = Convert.ToSByte(number);
99-
Assert.AreEqual(value_sb, (sbyte)actualNumber, "Test1");
100-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { byte value_b = Convert.ToByte(number); }, "Test2");
99+
Assert.AreEqual(value_sb, (sbyte)actualNumber);
100+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToByte(number); });
101101
short value_s16 = Convert.ToInt16(number);
102-
Assert.AreEqual(value_s16, (short)actualNumber, "Test3");
103-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { ushort value_u16 = Convert.ToUInt16(number); }, "Test4");
102+
Assert.AreEqual(value_s16, (short)actualNumber);
103+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToUInt16(number); });
104104
int value_s32 = Convert.ToInt32(number);
105-
Assert.AreEqual(value_s32, actualNumber, "Test5");
106-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { uint value_u32 = Convert.ToUInt32(number); }, "Test6");
105+
Assert.AreEqual(value_s32, actualNumber);
106+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToUInt32(number); });
107107
long value_s64 = Convert.ToInt32(number);
108-
Assert.AreEqual(value_s64, actualNumber, "Test7");
109-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { ulong value_u64 = Convert.ToUInt64(number); }, "Test8");
108+
Assert.AreEqual(value_s64, actualNumber);
109+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToUInt64(number); });
110110
}
111111

112112
[TestMethod]
@@ -118,8 +118,8 @@ public void Convert_Double()
118118
double value_dd = Convert.ToDouble(number);
119119
Assert.AreEqual(value_dd, actualNumber);
120120

121-
Assert.AreEqual(-129, Convert.ToDouble("-129"), "The value '-129' did not parse to -129"); // can't handle not having a decimal point!
122-
Assert.AreEqual(-123.456, Convert.ToDouble("-123.456"), "The value -123.456 did not parse to -123.456");
121+
Assert.AreEqual(-129, Convert.ToDouble("-129")); // can't handle not having a decimal point!
122+
Assert.AreEqual(-123.456, Convert.ToDouble("-123.456"));
123123
}
124124

125125
[TestMethod]

Tests/NFUnitTestSystemLib/UnitTestDateTime.cs

+12-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void DateTime_ConstructorTest1()
2323
OutputHelper.WriteLine(dt.ToString());
2424
Type type = dt.GetType();
2525
// Verifying its type
26-
Assert.IsType(type, Type.GetType("System.DateTime"));
26+
Assert.IsInstanceOfType(type, Type.GetType("System.DateTime"));
2727
}
2828

2929
[TestMethod]
@@ -49,7 +49,7 @@ public void DateTime_ConstructorTest2()
4949
dt.Hour + ":" + dt.Minute + ":" + dt.Second + ":" + dt.Millisecond + "'");
5050
}
5151
Type t = dt.GetType();
52-
Assert.IsType(t, Type.GetType("System.DateTime"));
52+
Assert.IsInstanceOfType(t, Type.GetType("System.DateTime"));
5353
}
5454
}
5555

@@ -301,7 +301,6 @@ public void DateTime_ToStringTest10()
301301
// expected format is dddd, dd MMMM yyyy HH:mm
302302

303303
int minLength = 25;
304-
int actualLength = dtOutput1.Length;
305304

306305
// check length
307306
Assert.IsTrue(dtOutput1.Length >= minLength, $"Wrong output1 length: {dtOutput1.Length}, should have been at least {minLength}");
@@ -386,7 +385,6 @@ public void DateTime_ToStringTest11()
386385
// expected format is dddd, dd MMMM yyyy HH:mm:ss
387386

388387
int minLength = 26;
389-
int actualLength = dtOutput1.Length;
390388

391389
// check length
392390
Assert.IsTrue(dtOutput1.Length >= minLength, $"Wrong output1 length: {dtOutput1.Length}, should have been at least {minLength}");
@@ -1774,10 +1772,10 @@ public void DateTime_AddTicks_PositiveTest20()
17741772
for (int i = 0; i < dt1Arr.Length; i++)
17751773
{
17761774
DateTime dt1 = dt1Arr[i];
1777-
long ticks = (long)random.Next(1000);
1775+
long ticksVAlue = random.Next(1000);
17781776
// Adding '" + ticks + "' ticks to '" + dt1.ToString() + "'
1779-
DateTime dt2 = dt1.AddTicks(ticks);
1780-
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticks));
1777+
DateTime dt2 = dt1.AddTicks(ticksVAlue);
1778+
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticksVAlue));
17811779
}
17821780
}
17831781

@@ -1795,10 +1793,10 @@ public void DateTime_AddTicks_NegativeTest21()
17951793
for (int i = 0; i < 10; i++)
17961794
{
17971795
DateTime dt1 = dt1Arr[i];
1798-
long ticks = -(long)random.Next(1000);
1796+
long ticksValue = -(long)random.Next(1000);
17991797
// Adding '" + ticks + "' ticks to '" + dt1.ToString() + "'
1800-
DateTime dt2 = dt1.AddTicks(ticks);
1801-
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticks));
1798+
DateTime dt2 = dt1.AddTicks(ticksValue);
1799+
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticksValue));
18021800
}
18031801
}
18041802

@@ -2113,7 +2111,7 @@ public void DateTime_DateTest41()
21132111
DateTime dt = GetRandomDateTime();
21142112
DateTime _date = dt.Date;
21152113
if ((_date.Year != dt.Year) || (_date.Month != dt.Month) || (_date.Day != dt.Day) ||
2116-
(_date.Hour != 0) || (_date.Minute != 0) | (_date.Second != 0) || (_date.Millisecond != 0))
2114+
(_date.Hour != 0) || (_date.Minute != 0) || (_date.Second != 0) || (_date.Millisecond != 0))
21172115
{
21182116
throw new Exception("Failure : expected Date(mm/dd/yr/hr/mn/sec/msec) = '" + dt.Month + "/" + dt.Day +
21192117
"/" + dt.Year + "/0:0:0:0' but got '" + _date.Month + "/" + _date.Day + "/" +
@@ -2272,16 +2270,16 @@ public void DateTime_BelowMinDateTime_ArgumentOutOfRangeExceptionTest58()
22722270
{
22732271
// Creating a DateTime with -ve Ticks and,
22742272
// verifying ArgumentOutOfRangeException is thrown
2275-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { DateTime dt = new DateTime(-(new Random().Next(10) + 1)); });
2273+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { _ = new DateTime(-(new Random().Next(10) + 1)); });
22762274
}
22772275

22782276
[TestMethod]
22792277
public void DateTime_AboveMaxDatTime_ArgumentOutOfRangeExceptionTest59()
22802278
{
22812279
// Creating a DateTime later than DateTime.MaxValue and,
22822280
// verifying ArgumentOutOfRangeException is thrown
2283-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { DateTime dt1 = new DateTime(DateTime.MaxValue.Ticks + 1); });
2284-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { DateTime dt2 = new DateTime(10000, 1, 1, 0, 0, 0, 0); });
2281+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { _ = new DateTime(DateTime.MaxValue.Ticks + 1); });
2282+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { _ = new DateTime(10000, 1, 1, 0, 0, 0, 0); });
22852283
}
22862284

22872285
[TestMethod]

Tests/NFUnitTestSystemLib/UnitTestDouble.cs

+81-14
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,111 @@ public void Epsilon()
4646
[TestMethod]
4747
public void NaN()
4848
{
49+
// Identical expressions should not be used on both sides of operators
50+
// on purpose to test the NaN value
51+
#pragma warning disable S1764
4952
Assert.IsTrue(double.NaN.Equals(0.0d / 0.0d));
53+
Assert.IsTrue(double.IsNaN(0.0d / 0.0d));
54+
#pragma warning restore S1764 // Identical expressions should not be used on both sides of operators
5055
}
5156

5257
[TestMethod]
5358
public void NegativeInfinity()
5459
{
5560
Assert.AreEqual(-1.0 / 0.0, double.NegativeInfinity);
61+
Assert.IsTrue(double.IsNegativeInfinity(-1.0 / 0.0));
5662
}
5763

5864
[TestMethod]
5965
public void PositiveInfinity()
6066
{
6167
Assert.AreEqual(1.0 / 0.0, double.PositiveInfinity);
68+
Assert.IsTrue(double.IsPositiveInfinity(1.0 / 0.0));
6269
}
6370

6471
[TestMethod]
6572
public void Equals()
6673
{
6774
DoubleTestData[] testData = new DoubleTestData[]
6875
{
69-
new DoubleTestData((double)789, (double)789, true),
70-
new DoubleTestData((double)789, (double)-789, false),
71-
new DoubleTestData((double)789, (double)0, false),
72-
new DoubleTestData(double.NaN, double.NaN, true),
73-
new DoubleTestData(double.NaN, -double.NaN, true),
74-
new DoubleTestData((double)789, (float)789, false),
75-
new DoubleTestData((double)789, "789", false)
76+
new DoubleTestData(
77+
(double)789,
78+
(double)789,
79+
true,
80+
"789 should be equal to 789"),
81+
new DoubleTestData(
82+
(double)789,
83+
(double)-789,
84+
false,
85+
"789 should not be equal to -789"),
86+
new DoubleTestData(
87+
(double)789,
88+
(double)0,
89+
false,
90+
"789 should not be equal to 0"),
91+
new DoubleTestData(
92+
double.NaN,
93+
double.NaN,
94+
true,
95+
"NaN should be equal to NaN"),
96+
new DoubleTestData(
97+
double.NaN,
98+
-double.NaN,
99+
true,
100+
"NaN should be equal to -NaN"),
101+
new DoubleTestData(
102+
(double)789,
103+
(float)789,
104+
false,
105+
"789 should not be equal to 789f"),
106+
new DoubleTestData(
107+
(double)789,
108+
"789",
109+
false,
110+
"789(double) should not be equal to '789' (string)"),
111+
new DoubleTestData(
112+
(0.0d),
113+
(-0.0d),
114+
true,
115+
"0.0d should be equal to -0.0d")
116+
76117
};
77118

119+
// Floating point numbers should not be tested for equality
120+
// intended as this is a unit test
121+
#pragma warning disable S1244
122+
78123
foreach (var test in testData)
79124
{
80125
if (test.Value is double d2)
81126
{
82-
Assert.AreEqual(test.Expected, test.D1.Equals(d2));
127+
Assert.AreEqual(
128+
test.Expected,
129+
test.D1.Equals(d2),
130+
test.AssertMessage);
83131

84132
if (double.IsNaN((double)test.D1) && double.IsNaN(d2))
85133
{
86-
Assert.AreEqual(!test.Expected, (double)test.D1 == d2);
87-
Assert.AreEqual(test.Expected, (double)test.D1 != d2);
134+
Assert.AreEqual(
135+
!test.Expected,
136+
(double)test.D1 == d2);
137+
Assert.AreEqual(
138+
test.Expected,
139+
(double)test.D1 != d2);
88140
}
89141
else
90142
{
91-
Assert.AreEqual(test.Expected, (double)test.D1 == d2);
92-
Assert.AreEqual(!test.Expected, (double)test.D1 != d2);
143+
Assert.AreEqual(
144+
test.Expected,
145+
(double)test.D1 == d2);
146+
Assert.AreEqual(
147+
!test.Expected,
148+
(double)test.D1 != d2);
93149
}
94150

95-
Assert.AreEqual(test.Expected, test.D1.GetHashCode().Equals(d2.GetHashCode()));
151+
Assert.AreEqual(
152+
test.Expected,
153+
test.D1.GetHashCode().Equals(d2.GetHashCode()));
96154
}
97155

98156
if (test.Expected)
@@ -104,19 +162,28 @@ public void Equals()
104162
Assert.IsFalse(test.D1.Equals(test.Value));
105163
}
106164
}
165+
166+
#pragma warning restore S1244
167+
107168
}
108169

109170
private sealed class DoubleTestData
110171
{
111172
public object D1 { get; }
112173
public object Value { get; }
113174
public bool Expected { get; }
175+
public string AssertMessage { get; }
114176

115-
public DoubleTestData(object d1, object value, bool expected)
177+
public DoubleTestData(
178+
object d1,
179+
object value,
180+
bool expected,
181+
string assertMessage = "")
116182
{
117183
D1 = d1;
118184
Value = value;
119185
Expected = expected;
186+
AssertMessage = assertMessage;
120187
}
121188
}
122189
}

0 commit comments

Comments
 (0)