Skip to content

Work and improvements in unit tests #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Tests/NFUnitTestConversions/UnitTestConvertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ public void Convert_Negative()
string number = "-12";
int actualNumber = -12;
sbyte value_sb = Convert.ToSByte(number);
Assert.AreEqual(value_sb, (sbyte)actualNumber, "Test1");
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { byte value_b = Convert.ToByte(number); }, "Test2");
Assert.AreEqual(value_sb, (sbyte)actualNumber);
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToByte(number); });
short value_s16 = Convert.ToInt16(number);
Assert.AreEqual(value_s16, (short)actualNumber, "Test3");
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { ushort value_u16 = Convert.ToUInt16(number); }, "Test4");
Assert.AreEqual(value_s16, (short)actualNumber);
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToUInt16(number); });
int value_s32 = Convert.ToInt32(number);
Assert.AreEqual(value_s32, actualNumber, "Test5");
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { uint value_u32 = Convert.ToUInt32(number); }, "Test6");
Assert.AreEqual(value_s32, actualNumber);
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToUInt32(number); });
long value_s64 = Convert.ToInt32(number);
Assert.AreEqual(value_s64, actualNumber, "Test7");
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { ulong value_u64 = Convert.ToUInt64(number); }, "Test8");
Assert.AreEqual(value_s64, actualNumber);
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Convert.ToUInt64(number); });
}

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

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

[TestMethod]
Expand Down
26 changes: 12 additions & 14 deletions Tests/NFUnitTestSystemLib/UnitTestDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void DateTime_ConstructorTest1()
OutputHelper.WriteLine(dt.ToString());
Type type = dt.GetType();
// Verifying its type
Assert.IsType(type, Type.GetType("System.DateTime"));
Assert.IsInstanceOfType(type, Type.GetType("System.DateTime"));
}

[TestMethod]
Expand All @@ -49,7 +49,7 @@ public void DateTime_ConstructorTest2()
dt.Hour + ":" + dt.Minute + ":" + dt.Second + ":" + dt.Millisecond + "'");
}
Type t = dt.GetType();
Assert.IsType(t, Type.GetType("System.DateTime"));
Assert.IsInstanceOfType(t, Type.GetType("System.DateTime"));
}
}

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

int minLength = 25;
int actualLength = dtOutput1.Length;

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

int minLength = 26;
int actualLength = dtOutput1.Length;

// check length
Assert.IsTrue(dtOutput1.Length >= minLength, $"Wrong output1 length: {dtOutput1.Length}, should have been at least {minLength}");
Expand Down Expand Up @@ -1774,10 +1772,10 @@ public void DateTime_AddTicks_PositiveTest20()
for (int i = 0; i < dt1Arr.Length; i++)
{
DateTime dt1 = dt1Arr[i];
long ticks = (long)random.Next(1000);
long ticksVAlue = random.Next(1000);
// Adding '" + ticks + "' ticks to '" + dt1.ToString() + "'
DateTime dt2 = dt1.AddTicks(ticks);
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticks));
DateTime dt2 = dt1.AddTicks(ticksVAlue);
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticksVAlue));
}
}

Expand All @@ -1795,10 +1793,10 @@ public void DateTime_AddTicks_NegativeTest21()
for (int i = 0; i < 10; i++)
{
DateTime dt1 = dt1Arr[i];
long ticks = -(long)random.Next(1000);
long ticksValue = -(long)random.Next(1000);
// Adding '" + ticks + "' ticks to '" + dt1.ToString() + "'
DateTime dt2 = dt1.AddTicks(ticks);
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticks));
DateTime dt2 = dt1.AddTicks(ticksValue);
Assert.AreEqual(dt2.Ticks, (dt1.Ticks + ticksValue));
}
}

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

[TestMethod]
public void DateTime_AboveMaxDatTime_ArgumentOutOfRangeExceptionTest59()
{
// Creating a DateTime later than DateTime.MaxValue and,
// verifying ArgumentOutOfRangeException is thrown
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { DateTime dt1 = new DateTime(DateTime.MaxValue.Ticks + 1); });
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { DateTime dt2 = new DateTime(10000, 1, 1, 0, 0, 0, 0); });
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { _ = new DateTime(DateTime.MaxValue.Ticks + 1); });
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { _ = new DateTime(10000, 1, 1, 0, 0, 0, 0); });
}

[TestMethod]
Expand Down
95 changes: 81 additions & 14 deletions Tests/NFUnitTestSystemLib/UnitTestDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,53 +46,111 @@ public void Epsilon()
[TestMethod]
public void NaN()
{
// Identical expressions should not be used on both sides of operators
// on purpose to test the NaN value
#pragma warning disable S1764
Assert.IsTrue(double.NaN.Equals(0.0d / 0.0d));
Assert.IsTrue(double.IsNaN(0.0d / 0.0d));
#pragma warning restore S1764 // Identical expressions should not be used on both sides of operators
}

[TestMethod]
public void NegativeInfinity()
{
Assert.AreEqual(-1.0 / 0.0, double.NegativeInfinity);
Assert.IsTrue(double.IsNegativeInfinity(-1.0 / 0.0));
}

[TestMethod]
public void PositiveInfinity()
{
Assert.AreEqual(1.0 / 0.0, double.PositiveInfinity);
Assert.IsTrue(double.IsPositiveInfinity(1.0 / 0.0));
}

[TestMethod]
public void Equals()
{
DoubleTestData[] testData = new DoubleTestData[]
{
new DoubleTestData((double)789, (double)789, true),
new DoubleTestData((double)789, (double)-789, false),
new DoubleTestData((double)789, (double)0, false),
new DoubleTestData(double.NaN, double.NaN, true),
new DoubleTestData(double.NaN, -double.NaN, true),
new DoubleTestData((double)789, (float)789, false),
new DoubleTestData((double)789, "789", false)
new DoubleTestData(
(double)789,
(double)789,
true,
"789 should be equal to 789"),
new DoubleTestData(
(double)789,
(double)-789,
false,
"789 should not be equal to -789"),
new DoubleTestData(
(double)789,
(double)0,
false,
"789 should not be equal to 0"),
new DoubleTestData(
double.NaN,
double.NaN,
true,
"NaN should be equal to NaN"),
new DoubleTestData(
double.NaN,
-double.NaN,
true,
"NaN should be equal to -NaN"),
new DoubleTestData(
(double)789,
(float)789,
false,
"789 should not be equal to 789f"),
new DoubleTestData(
(double)789,
"789",
false,
"789(double) should not be equal to '789' (string)"),
new DoubleTestData(
(0.0d),
(-0.0d),
true,
"0.0d should be equal to -0.0d")

};

// Floating point numbers should not be tested for equality
// intended as this is a unit test
#pragma warning disable S1244

foreach (var test in testData)
{
if (test.Value is double d2)
{
Assert.AreEqual(test.Expected, test.D1.Equals(d2));
Assert.AreEqual(
test.Expected,
test.D1.Equals(d2),
test.AssertMessage);

if (double.IsNaN((double)test.D1) && double.IsNaN(d2))
{
Assert.AreEqual(!test.Expected, (double)test.D1 == d2);
Assert.AreEqual(test.Expected, (double)test.D1 != d2);
Assert.AreEqual(
!test.Expected,
(double)test.D1 == d2);
Assert.AreEqual(
test.Expected,
(double)test.D1 != d2);
}
else
{
Assert.AreEqual(test.Expected, (double)test.D1 == d2);
Assert.AreEqual(!test.Expected, (double)test.D1 != d2);
Assert.AreEqual(
test.Expected,
(double)test.D1 == d2);
Assert.AreEqual(
!test.Expected,
(double)test.D1 != d2);
}

Assert.AreEqual(test.Expected, test.D1.GetHashCode().Equals(d2.GetHashCode()));
Assert.AreEqual(
test.Expected,
test.D1.GetHashCode().Equals(d2.GetHashCode()));
}

if (test.Expected)
Expand All @@ -104,19 +162,28 @@ public void Equals()
Assert.IsFalse(test.D1.Equals(test.Value));
}
}

#pragma warning restore S1244

}

private sealed class DoubleTestData
{
public object D1 { get; }
public object Value { get; }
public bool Expected { get; }
public string AssertMessage { get; }

public DoubleTestData(object d1, object value, bool expected)
public DoubleTestData(
object d1,
object value,
bool expected,
string assertMessage = "")
{
D1 = d1;
Value = value;
Expected = expected;
AssertMessage = assertMessage;
}
}
}
Expand Down
Loading
Loading