Skip to content

Commit bc3b302

Browse files
committed
Add unit test for hash code of class objects (#212)
***NO_CI***
1 parent e862eb6 commit bc3b302

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Tests/NFUnitTestClasses/NFUnitTestClasses.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<Compile Include="UnitTestMethodsTests.cs" />
3737
<Compile Include="UnitTestOperatorTests.cs" />
3838
<Compile Include="UnitTestPropertiesTests.cs" />
39+
<Compile Include="UnitTestHashCodeTests.cs" />
3940
<Compile Include="UnitTestStaticTests.cs" />
4041
</ItemGroup>
4142
<ItemGroup>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
using System.Diagnostics;
9+
10+
namespace NFUnitTestClasses
11+
{
12+
[TestClass]
13+
class UnitTestHashCodeTests
14+
{
15+
[TestMethod]
16+
public void HashCode_Test_01()
17+
{
18+
// generate a hash code for a class with fields
19+
SomeKey key = new(42);
20+
21+
int hashCode = key.GetHashCode();
22+
23+
OutputHelper.WriteLine($"Hash code for SomeKey(42) is {hashCode}");
24+
OutputHelper.WriteLine($"Hash code for SomeKey(42) is {key.GetHashCode()}");
25+
26+
Assert.AreNotEqual(0, key.GetHashCode(), "Hash code for a class should not be 0");
27+
Assert.AreEqual($"{key.GetHashCode()}".GetHashCode(), $"{hashCode}".GetHashCode());
28+
}
29+
30+
[TestMethod]
31+
public void HashCode_Test_02()
32+
{
33+
// generate a hash code for a class without fields
34+
SomeOtherKey key = new();
35+
36+
int hashCode = key.GetHashCode();
37+
38+
OutputHelper.WriteLine($"Hash code for SomeOtherKey is {hashCode}");
39+
OutputHelper.WriteLine($"Hash code for SomeOtherKey is {key.GetHashCode()}");
40+
41+
Assert.AreNotEqual(0, key.GetHashCode(), "Hash code for a class should not be 0");
42+
Assert.AreEqual($"{key.GetHashCode()}".GetHashCode(), $"{hashCode}".GetHashCode());
43+
}
44+
45+
[TestMethod]
46+
public void HashCode_Test_03()
47+
{
48+
// generate a hash code for a class with fields with different types
49+
SomeOtherFunnyKey key = new();
50+
51+
int hashCode = key.GetHashCode();
52+
53+
OutputHelper.WriteLine($"Hash code for SomeOtherFunnyKey is {hashCode}");
54+
OutputHelper.WriteLine($"Hash code for SomeOtherFunnyKey is {key.GetHashCode()}");
55+
56+
Assert.AreNotEqual(0, key.GetHashCode(), "Hash code for a class should not be 0");
57+
Assert.AreEqual($"{key.GetHashCode()}".GetHashCode(), $"{hashCode}".GetHashCode());
58+
}
59+
60+
public class SomeKey
61+
{
62+
public SomeKey(int value)
63+
{
64+
Value = value;
65+
}
66+
public int Value { get; }
67+
}
68+
69+
public class SomeOtherKey
70+
{
71+
public SomeOtherKey()
72+
{
73+
}
74+
}
75+
76+
public class SomeOtherFunnyKey
77+
{
78+
public string Name { get; set; }
79+
80+
public int GetValue { get; set; }
81+
82+
public SomeOtherFunnyKey()
83+
{
84+
}
85+
}
86+
87+
}
88+
}

0 commit comments

Comments
 (0)