Skip to content

Commit dc3f0c6

Browse files
committed
Add unit tests for hashtable with classes as keys
1 parent 0c98d54 commit dc3f0c6

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Tests/HashtableTests/HashtableTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using nanoFramework.TestFramework;
88
using System;
99
using System.Collections;
10+
using System.Diagnostics;
1011

1112
namespace NFUnitTests
1213
{
@@ -21,6 +22,15 @@ public void SetupTests()
2122
{
2223
// need this empty method to get everything setup in the class
2324
// otherwise the 1st test will incur in that time penalty thus not proving an accurate execution time
25+
26+
Hashtable ht = new Hashtable();
27+
28+
for (ulong i = 1; i <= 100; i++)
29+
{
30+
SomeKey se = new SomeKey();
31+
Debug.WriteLine($"Adding element no {i}");
32+
ht.Add(se, i);
33+
}
2434
}
2535

2636
[TestMethod]
@@ -677,6 +687,20 @@ public void Hashtable_InsertNullKey()
677687
}, "No exception was thrown when adding a NULL key.");
678688
}
679689

690+
[TestMethod]
691+
public void Hashtable_ClassAsKey()
692+
{
693+
Hashtable ht = CreateClassHashtable(10);
694+
695+
foreach (var key in ht.Keys)
696+
{
697+
Assert.AreEqual($"Value_{key.GetHashCode()}", ht[key] as string);
698+
}
699+
700+
701+
702+
}
703+
680704
#region helper classes and methods
681705

682706
/// <summary>
@@ -783,6 +807,26 @@ public static Hashtable CreateStringHashtable(int count, int start = 0)
783807
return hashtable;
784808
}
785809

810+
public static Hashtable CreateClassHashtable(int count, int start = 0)
811+
{
812+
var hashtable = new Hashtable();
813+
814+
for (int i = start; i < start + count; i++)
815+
{
816+
var key = new SomeKey();
817+
string value = "Value_" + key.GetHashCode();
818+
819+
hashtable.Add(key, value);
820+
}
821+
822+
return hashtable;
823+
}
824+
825+
public class SomeKey
826+
{
827+
828+
}
829+
786830
private class Foo
787831
{
788832
public string StringValue { get; set; } = "Hello World";

0 commit comments

Comments
 (0)