Skip to content

Commit 975b6fa

Browse files
authored
Add unit tests for Equal and GetHashCode (#176)
***NO_CI***
1 parent ad3b7cf commit 975b6fa

14 files changed

+1045
-0
lines changed

Tests/NFUnitTestSystemLib/NFUnitTestSystemLib.nfproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
2828
</PropertyGroup>
2929
<ItemGroup>
30+
<Compile Include="UnitTestUInt64.cs" />
31+
<Compile Include="UnitTestUInt32.cs" />
32+
<Compile Include="UnitTestSingle.cs" />
33+
<Compile Include="UnitTestSByte.cs" />
34+
<Compile Include="UnitTestInt64.cs" />
35+
<Compile Include="UnitTestInt32.cs" />
36+
<Compile Include="UnitTestUInt16.cs" />
37+
<Compile Include="UnitTestInt16.cs" />
38+
<Compile Include="UnitTestDouble.cs" />
39+
<Compile Include="UnitTestChar.cs" />
40+
<Compile Include="UnitTestByte.cs" />
3041
<Compile Include="UnitTestDateTime.cs" />
3142
<Compile Include="UnitTestGCTest.cs" />
3243
<Compile Include="UnitTestGuid.cs" />
@@ -37,6 +48,7 @@
3748
<Compile Include="UnitTestReflectionMemberTest.cs" />
3849
<Compile Include="UnitTestReflectionTypeTest.cs" />
3950
<Compile Include="UnitTestStringTests.cs" />
51+
<Compile Include="UnitTestBoolean.cs" />
4052
<Compile Include="UnitTestTimeSpan.cs" />
4153
<Compile Include="UnitTestTypeTests.cs" />
4254
<Compile Include="UnitTestWeakReferenceTests.cs" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.TestFramework;
8+
9+
namespace NFUnitTestSystemLib
10+
{
11+
[TestClass]
12+
class BooleanTests
13+
{
14+
[TestMethod]
15+
public void TrueString_Get_ReturnsTrue()
16+
{
17+
Assert.Equal("True", bool.TrueString);
18+
}
19+
20+
[TestMethod]
21+
public void FalseString_Get_ReturnsFalse()
22+
{
23+
Assert.Equal("False", bool.FalseString);
24+
}
25+
26+
[TestMethod]
27+
public void GetHashCode_Invoke_ReturnsExpected()
28+
{
29+
bool _true = true;
30+
bool _false = false;
31+
32+
Assert.Equal(_true.GetHashCode(), 1);
33+
Assert.Equal(_false.GetHashCode(), 0);
34+
}
35+
}
36+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.TestFramework;
8+
9+
namespace NFUnitTestSystemLib
10+
{
11+
[TestClass]
12+
public class ByteTests
13+
{
14+
[TestMethod]
15+
public void Ctor_Empty()
16+
{
17+
var b = new byte();
18+
Assert.Equal((byte)0, b);
19+
}
20+
21+
[TestMethod]
22+
public void Ctor_Value()
23+
{
24+
byte b = 41;
25+
Assert.Equal((byte)41, b);
26+
}
27+
28+
[TestMethod]
29+
public void MaxValue()
30+
{
31+
Assert.Equal((byte)0xFF, byte.MaxValue);
32+
}
33+
34+
[TestMethod]
35+
public void MinValue()
36+
{
37+
Assert.Equal((byte)0, byte.MinValue);
38+
}
39+
40+
[TestMethod]
41+
public void Equals()
42+
{
43+
ByteTestData[] testData = new ByteTestData[]
44+
{
45+
new ByteTestData((byte)78, (byte)78, true),
46+
new ByteTestData((byte)78, (byte)0, false),
47+
new ByteTestData((byte)0, (byte)0, true),
48+
new ByteTestData((byte)78, null, false),
49+
new ByteTestData((byte)78, "78", false),
50+
new ByteTestData((byte)78, 78, false)
51+
};
52+
53+
foreach (var test in testData)
54+
{
55+
OutputHelper.WriteLine($"Testing combination {test.B} and {test.Obj}");
56+
57+
if (test.Obj is byte b2)
58+
{
59+
Assert.Equal(test.Expected, test.B.Equals(b2), $"Casting Obj wasn't successful for {test.Obj}");
60+
Assert.Equal(test.Expected, test.B.GetHashCode().Equals(b2.GetHashCode()), $"HashCode of {test.B}({test.B.GetHashCode()}) differs from the one of {b2}(b2.GetHashCode())");
61+
Assert.Equal((byte)test.B, test.B.GetHashCode(), $"HashCode of {(byte)test.B} different from expected, is {test.B.GetHashCode()}");
62+
}
63+
64+
Assert.Equal(test.Expected, test.B.Equals(test.Obj), $"Equality test between {test.B} and {test.Obj} failed");
65+
}
66+
}
67+
68+
private sealed class ByteTestData
69+
{
70+
public object B { get; }
71+
public object Obj { get; }
72+
public bool Expected { get; }
73+
74+
public ByteTestData(object b, object obj, bool expected)
75+
{
76+
B = b;
77+
Obj = obj;
78+
Expected = expected;
79+
}
80+
}
81+
}
82+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.TestFramework;
8+
9+
namespace NFUnitTestSystemLib
10+
{
11+
[TestClass]
12+
class CharTests
13+
{
14+
[TestMethod]
15+
public void Equals()
16+
{
17+
CharTestData[] testData = new CharTestData[]
18+
{
19+
new CharTestData((char)'a', (char)'a', true),
20+
new CharTestData((char)'a', (char)'A', false),
21+
new CharTestData((char)'a', (char)'b', false),
22+
new CharTestData((char)'a', (int)'a', false),
23+
new CharTestData((char)'a', "a", false),
24+
new CharTestData((char)'a', null, false)
25+
};
26+
27+
foreach (var test in testData)
28+
{
29+
if (test.Obj is char)
30+
{
31+
Assert.Equal(test.Expected, test.C.Equals((char)test.Obj));
32+
Assert.Equal(test.Expected, test.C.GetHashCode().Equals(test.Obj.GetHashCode()));
33+
}
34+
35+
Assert.Equal(test.Expected, test.C.Equals(test.Obj));
36+
}
37+
}
38+
39+
private sealed class CharTestData
40+
{
41+
public object C { get; }
42+
public object Obj { get; }
43+
public bool Expected { get; }
44+
45+
public CharTestData(object c, object obj, bool expected)
46+
{
47+
C = c;
48+
Obj = obj;
49+
Expected = expected;
50+
}
51+
}
52+
}
53+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.TestFramework;
8+
9+
namespace NFUnitTestSystemLib
10+
{
11+
[TestClass]
12+
public class DoubleTests
13+
{
14+
[TestMethod]
15+
public void Ctor_Empty()
16+
{
17+
var i = new double();
18+
Assert.Equal(0, i);
19+
}
20+
21+
[TestMethod]
22+
public void Ctor_Value()
23+
{
24+
double d = 41;
25+
Assert.Equal(41, d);
26+
27+
d = 41.3;
28+
Assert.Equal(41.3, d);
29+
}
30+
31+
[TestMethod]
32+
public void MaxValue()
33+
{
34+
Assert.Equal(1.7976931348623157E+308, double.MaxValue);
35+
}
36+
37+
[TestMethod]
38+
public void MinValue()
39+
{
40+
Assert.Equal(-1.7976931348623157E+308, double.MinValue);
41+
}
42+
43+
[TestMethod]
44+
public void Epsilon()
45+
{
46+
Assert.Equal(4.9406564584124654E-324, double.Epsilon);
47+
}
48+
49+
[TestMethod]
50+
public void NaN()
51+
{
52+
Assert.True(double.NaN.Equals(0.0d / 0.0d));
53+
}
54+
55+
[TestMethod]
56+
public void NegativeInfinity()
57+
{
58+
Assert.Equal(-1.0 / 0.0, double.NegativeInfinity);
59+
}
60+
61+
[TestMethod]
62+
public void PositiveInfinity()
63+
{
64+
Assert.Equal(1.0 / 0.0, double.PositiveInfinity);
65+
}
66+
67+
[TestMethod]
68+
public void Equals()
69+
{
70+
DoubleTestData[] testData = new DoubleTestData[]
71+
{
72+
new DoubleTestData((double)789, (double)789, true),
73+
new DoubleTestData((double)789, (double)-789, false),
74+
new DoubleTestData((double)789, (double)0, false),
75+
new DoubleTestData(double.NaN, double.NaN, true),
76+
new DoubleTestData(double.NaN, -double.NaN, true),
77+
new DoubleTestData((double)789, (float)789, false),
78+
new DoubleTestData((double)789, "789", false)
79+
};
80+
81+
foreach (var test in testData)
82+
{
83+
if (test.Value is double d2)
84+
{
85+
Assert.Equal(test.Expected, test.D1.Equals(d2));
86+
87+
if (double.IsNaN((double)test.D1) && double.IsNaN(d2))
88+
{
89+
Assert.Equal(!test.Expected, (double)test.D1 == d2);
90+
Assert.Equal(test.Expected, (double)test.D1 != d2);
91+
}
92+
else
93+
{
94+
Assert.Equal(test.Expected, (double)test.D1 == d2);
95+
Assert.Equal(!test.Expected, (double)test.D1 != d2);
96+
}
97+
98+
Assert.Equal(test.Expected, test.D1.GetHashCode().Equals(d2.GetHashCode()));
99+
}
100+
101+
Assert.Equal(test.Expected, test.D1.Equals(test.Value));
102+
}
103+
}
104+
105+
private sealed class DoubleTestData
106+
{
107+
public object D1 { get; }
108+
public object Value { get; }
109+
public bool Expected { get; }
110+
111+
public DoubleTestData(object d1, object value, bool expected)
112+
{
113+
D1 = d1;
114+
Value = value;
115+
Expected = expected;
116+
}
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)