Skip to content
68 changes: 41 additions & 27 deletions Tests/NFUnitTestBitConverter/BitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,26 @@ public void BitConverterTest_DoubleToInt64Bits()
}

[TestMethod]
public void BitConverterTest_GetBytesBool()
[DataRow(true, new byte[] { 1 })]
[DataRow(false, new byte[] { 0 })]
public void BitConverterTest_GetBytesBool(bool value, byte[] expected)
{
Helper.GetBytesBool(true, new byte[] { 1 });
Helper.GetBytesBool(false, new byte[] { 0 });
Helper.GetBytesBool(value, expected);
}

[TestMethod]
public void BitConverterTest_GetBytesChar()
[DataRow('\0', new byte[] { 0x00, 0x00 })]
[DataRow(' ', new byte[] { 0x20, 0x00 })]
[DataRow('*', new byte[] { 0x2A, 0x00 })]
[DataRow('3', new byte[] { 0x33, 0x00 })]
[DataRow('A', new byte[] { 0x41, 0x00 })]
[DataRow('[', new byte[] { 0x5B, 0x00 })]
[DataRow('a', new byte[] { 0x61, 0x00 })]
[DataRow('{', new byte[] { 0x7B, 0x00 })]
[DataRow('测', new byte[] { 0x4B, 0x6D })]
public void BitConverterTest_GetBytesChar(char value, byte[] expected)
{
Helper.GetBytesChar('\0', new byte[] { 0x00, 0x00 });
Helper.GetBytesChar(' ', new byte[] { 0x20, 0x00 });
Helper.GetBytesChar('*', new byte[] { 0x2A, 0x00 });
Helper.GetBytesChar('3', new byte[] { 0x33, 0x00 });
Helper.GetBytesChar('A', new byte[] { 0x41, 0x00 });
Helper.GetBytesChar('[', new byte[] { 0x5B, 0x00 });
Helper.GetBytesChar('a', new byte[] { 0x61, 0x00 });
Helper.GetBytesChar('{', new byte[] { 0x7B, 0x00 });
Helper.GetBytesChar('测', new byte[] { 0x4B, 0x6D });
Helper.GetBytesChar(value, expected);
}

[TestMethod]
Expand All @@ -90,27 +92,39 @@ public void BitConverterTest_GetBytesDouble()
}

[TestMethod]
public void BitConverterTest_GetBytesInt16()
[DataRow((short)0, new byte[] { 0x00, 0x00 })]
[DataRow((short)15, new byte[] { 0x0F, 0x00 })]
[DataRow((short)-15, new byte[] { 0xF1, 0xFF })]
[DataRow((short)10000, new byte[] { 0x10, 0x27 })]
[DataRow((short)-10000, new byte[] { 0xF0, 0xD8 })]
public void BitConverterTest_GetBytesInt16(short value, byte[] expected)
{
Helper.GetBytesInt16(value, expected);
}

[TestMethod]
public void BitConverterTest_GetBytesInt16_MinMax()
{
Helper.GetBytesInt16(0, new byte[] { 0x00, 0x00 });
Helper.GetBytesInt16(15, new byte[] { 0x0F, 0x00 });
Helper.GetBytesInt16(-15, new byte[] { 0xF1, 0xFF });
Helper.GetBytesInt16(10000, new byte[] { 0x10, 0x27 });
Helper.GetBytesInt16(-10000, new byte[] { 0xF0, 0xD8 });
Helper.GetBytesInt16(short.MinValue, new byte[] { 0x00, 0x80 });
Helper.GetBytesInt16(short.MaxValue, new byte[] { 0xFF, 0x7F });
}

[TestMethod]
public void BitConverterTest_GetBytesInt32()
[DataRow(0, new byte[] { 0x00, 0x00, 0x00, 0x00 })]
[DataRow(15, new byte[] { 0x0F, 0x00, 0x00, 0x00 })]
[DataRow(-15, new byte[] { 0xF1, 0xFF, 0xFF, 0xFF })]
[DataRow(1048576, new byte[] { 0x00, 0x00, 0x10, 0x00 })]
[DataRow(-1048576, new byte[] { 0x00, 0x00, 0xF0, 0xFF })]
[DataRow(1000000000, new byte[] { 0x00, 0xCA, 0x9A, 0x3B })]
[DataRow(-1000000000, new byte[] { 0x00, 0x36, 0x65, 0xC4 })]
public void BitConverterTest_GetBytesInt32(int value, byte[] expected)
{
Helper.GetBytesInt32(value, expected);
}

[TestMethod]
public void BitConverterTest_GetBytesInt32_MinMax()
{
Helper.GetBytesInt32(0, new byte[] { 0x00, 0x00, 0x00, 0x00 });
Helper.GetBytesInt32(15, new byte[] { 0x0F, 0x00, 0x00, 0x00 });
Helper.GetBytesInt32(-15, new byte[] { 0xF1, 0xFF, 0xFF, 0xFF });
Helper.GetBytesInt32(1048576, new byte[] { 0x00, 0x00, 0x10, 0x00 });
Helper.GetBytesInt32(-1048576, new byte[] { 0x00, 0x00, 0xF0, 0xFF });
Helper.GetBytesInt32(1000000000, new byte[] { 0x00, 0xCA, 0x9A, 0x3B });
Helper.GetBytesInt32(-1000000000, new byte[] { 0x00, 0x36, 0x65, 0xC4 });
Helper.GetBytesInt32(int.MinValue, new byte[] { 0x00, 0x00, 0x00, 0x80 });
Helper.GetBytesInt32(int.MaxValue, new byte[] { 0xFF, 0xFF, 0xFF, 0x7F });
}
Expand Down
10 changes: 4 additions & 6 deletions Tests/NFUnitTestSystemLib/UnitTestBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ public void FalseString_Get_ReturnsFalse()
}

[TestMethod]
public void GetHashCode_Invoke_ReturnsExpected()
[DataRow(true, 1)]
[DataRow(false, 0)]
public void GetHashCode_Invoke_ReturnsExpected(bool value, int expected)
{
bool _true = true;
bool _false = false;

Assert.AreEqual(_true.GetHashCode(), 1);
Assert.AreEqual(_false.GetHashCode(), 0);
Assert.AreEqual(value.GetHashCode(), expected);
}
}
}
72 changes: 42 additions & 30 deletions Tests/NFUnitTestSystemLib/UnitTestByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,57 @@ public void MinValue()
}

[TestMethod]
public void Equals()
[DataRow((byte)78, (byte)78, true)]
[DataRow((byte)78, (byte)0, false)]
[DataRow((byte)0, (byte)0, true)]
public void Equals_ByteToByte(byte b, byte obj, bool expected)
{
ByteTestData[] testData = new ByteTestData[]
OutputHelper.WriteLine($"Testing combination {b} and {obj}");

if (expected)
{
new ByteTestData((byte)78, (byte)78, true),
new ByteTestData((byte)78, (byte)0, false),
new ByteTestData((byte)0, (byte)0, true),
new ByteTestData((byte)78, null, false),
new ByteTestData((byte)78, "78", false),
new ByteTestData((byte)78, 78, false)
};

foreach (var test in testData)
Assert.AreEqual(b, obj);
Assert.IsTrue(b.GetHashCode().Equals(obj.GetHashCode()));
}
else
{
OutputHelper.WriteLine($"Testing combination {test.B} and {test.Obj}");
Assert.AreNotEqual(b, obj);
Assert.IsFalse(b.GetHashCode().Equals(obj.GetHashCode()));
}
Assert.AreEqual(b, b.GetHashCode());
}

if (test.Obj is byte b2)
{
Assert.AreEqual(test.Expected, test.B.Equals(b2), $"Casting Obj wasn't successful for {test.Obj}");
Assert.AreEqual(test.Expected, test.B.GetHashCode().Equals(b2.GetHashCode()), $"HashCode of {test.B}({test.B.GetHashCode()}) differs from the one of {b2}(b2.GetHashCode())");
Assert.AreEqual((byte)test.B, test.B.GetHashCode(), $"HashCode of {(byte)test.B} different from expected, is {test.B.GetHashCode()}");
}
[TestMethod]
public void Equals_ByteToNull()
{
byte b = 78;
object obj = null;

Assert.AreEqual(test.Expected, test.B.Equals(test.Obj), $"Equality test between {test.B} and {test.Obj} failed");
}
OutputHelper.WriteLine($"Testing combination {b} and {obj}");

Assert.AreNotEqual(b, obj);
}

[TestMethod]
public void Equals_ByteToString()
{
byte b = 78;
object obj = "78";

OutputHelper.WriteLine($"Testing combination {b} and {obj}");

Assert.AreNotEqual(b, obj);
}

private sealed class ByteTestData
[TestMethod]
public void Equals_ByteToInt()
{
public object B { get; }
public object Obj { get; }
public bool Expected { get; }
byte b = 78;
object obj = 78;

public ByteTestData(object b, object obj, bool expected)
{
B = b;
Obj = obj;
Expected = expected;
}
OutputHelper.WriteLine($"Testing combination {b} and {obj}");

Assert.AreNotEqual(b, obj);
}
}
}
69 changes: 37 additions & 32 deletions Tests/NFUnitTestSystemLib/UnitTestInt16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,51 @@ public void MinValue()
}

[TestMethod]
public void Equals()
[DataRow((short)789, (short)789, true)]
[DataRow((short)789, (short)-789, false)]
[DataRow((short)789, (short)0, false)]
[DataRow((short)0, (short)0, true)]
[DataRow((short)-789, (short)-789, true)]
[DataRow((short)-789, (short)789, false)]
public void Equals_Int16ToInt16(short i1, short obj, bool expected)
{
Int16TestData[] testData = new Int16TestData[]
if (expected)
{
new Int16TestData((short)789, (short)789, true),
new Int16TestData((short)789, (short)-789, false),
new Int16TestData((short)789, (short)0, false),
new Int16TestData((short)0, (short)0, true),
new Int16TestData((short)-789, (short)-789, true),
new Int16TestData((short)-789, (short)789, false),
new Int16TestData((short)789, null, false),
new Int16TestData((short)789, "789", false),
new Int16TestData((short)789, 789, false)
};

foreach (var test in testData)
Assert.AreEqual(i1, obj);
Assert.IsTrue(i1.GetHashCode().Equals(obj.GetHashCode()));
}
else
{
if (test.Obj is short)
{
short i2 = (short)test.Obj;
Assert.AreEqual(test.Expected, test.I1.Equals(i2));
Assert.AreEqual(test.Expected, test.I1.GetHashCode().Equals(i2.GetHashCode()));
}

Assert.AreEqual(test.Expected, test.I1.Equals(test.Obj));
Assert.AreNotEqual(i1, obj);
Assert.IsFalse(i1.GetHashCode().Equals(obj.GetHashCode()));
}
}

private sealed class Int16TestData
[TestMethod]
public void Equals_Int16ToNull()
{
public object I1 { get; }
public object Obj { get; }
public bool Expected { get; }
short i1 = 789;
object obj = null;

public Int16TestData(object i1, object obj, bool expected)
{
I1 = i1;
Obj = obj;
Expected = expected;
}
Assert.AreNotEqual(i1, obj);
}

[TestMethod]
public void Equals_Int16ToString()
{
short i1 = 789;
object obj = "789";

Assert.AreNotEqual(i1, obj);
}

[TestMethod]
public void Equals_Int16ToInt()
{
short i1 = 789;
object obj = 789;

Assert.AreNotEqual(i1, obj);
}
}
}
60 changes: 29 additions & 31 deletions Tests/NFUnitTestSystemLib/UnitTestInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,43 @@ public void MinValue()
}

[TestMethod]
public void Equals()
[DataRow(789, 789, true)]
[DataRow(789, -789, false)]
[DataRow(789, 0, false)]
[DataRow(0, 0, true)]
[DataRow(-789, -789, true)]
[DataRow(-789, 789, false)]
public void Equals_Int32ToInt32(int i1, int obj, bool expected)
{
Int32TestData[] testData = new Int32TestData[]
if (expected)
{
new Int32TestData((int)789, (int)789, true),
new Int32TestData((int)789, (int)-789, false),
new Int32TestData((int)789, (int)0, false),
new Int32TestData((int)0, (int)0, true),
new Int32TestData((int)-789, (int)-789, true),
new Int32TestData((int)-789, (int)789, false),
new Int32TestData((int)789, null, false),
new Int32TestData((int)89, "789", false),
};

foreach (var test in testData)
Assert.AreEqual(i1, obj);
Assert.IsTrue(i1.GetHashCode().Equals(obj.GetHashCode()));
}
else
{
if (test.Obj is int)
{
int i2 = (int)test.Obj;
Assert.AreEqual(test.Expected, test.I1.Equals(i2));
Assert.AreEqual(test.Expected, test.I1.GetHashCode().Equals(i2.GetHashCode()));
}

Assert.AreEqual(test.Expected, test.I1.Equals(test.Obj));
Assert.AreNotEqual(i1, obj);
Assert.IsFalse(i1.GetHashCode().Equals(obj.GetHashCode()));
}
Assert.AreEqual(i1, i1.GetHashCode());
}

private sealed class Int32TestData
[TestMethod]
public void Equals_Int32ToNull()
{
public object I1 { get; }
public object Obj { get; }
public bool Expected { get; }
int i1 = 789;
object obj = null;

public Int32TestData(object i1, object obj, bool expected)
{
I1 = i1;
Obj = obj;
Expected = expected;
}
Assert.AreNotEqual(i1, obj);
}

[TestMethod]
public void Equals_Int32ToString()
{
int i1 = 89;
object obj = "789";

Assert.AreNotEqual(i1, obj);
}
}
}
Loading
Loading