HashtableTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // TODO: add tests for Comparer and HashCodeProvider
  2. // NOTE: add MCS prefix to Hashtable.cs to use this test.
  3. using System;
  4. using MCS.System.Collections;
  5. using System.Collections;
  6. using NUnit.Framework;
  7. namespace Testsuite.System.Collections {
  8. /// <summary>Hashtable test.</summary>
  9. public class HashtableTest {
  10. public static ITest Suite {
  11. get {
  12. TestSuite suite= new TestSuite("All Hashtable Tests");
  13. suite.AddTest(BasicOperationsTest.Suite);
  14. return suite;
  15. }
  16. }
  17. }
  18. public class BasicOperationsTest : TestCase {
  19. protected MCS.System.Collections.Hashtable ht;
  20. private static Random rnd;
  21. public BasicOperationsTest(String name) : base(name) {}
  22. protected override void SetUp() {
  23. ht=new MCS.System.Collections.Hashtable();
  24. rnd=new Random();
  25. }
  26. public static ITest Suite {
  27. get {
  28. return new TestSuite(typeof(BasicOperationsTest));
  29. }
  30. }
  31. private void SetDefaultData() {
  32. ht.Clear();
  33. ht.Add("k1","another");
  34. ht.Add("k2","yet");
  35. ht.Add("k3","hashtable");
  36. }
  37. public void TestAddRemoveClear() {
  38. ht.Clear();
  39. Assert(ht.Count==0);
  40. SetDefaultData();
  41. Assert(ht.Count==3);
  42. bool thrown=false;
  43. try {
  44. ht.Add("k2","cool");
  45. } catch (ArgumentException) {thrown=true;}
  46. Assert("Must throw ArgumentException!",thrown);
  47. ht["k2"]="cool";
  48. Assert(ht.Count==3);
  49. Assert(ht["k2"].Equals("cool"));
  50. }
  51. public void TestCopyTo() {
  52. SetDefaultData();
  53. Object[] entries=new Object[ht.Count];
  54. ht.CopyTo(entries,0);
  55. Assert("Not an entry.",entries[0] is DictionaryEntry);
  56. }
  57. public void TestUnderHeavyLoad() {
  58. Console.WriteLine("Testing "+ht);
  59. ht.Clear();
  60. int max=100000;
  61. String[] cache=new String[max*2];
  62. int n=0;
  63. for (int i=0;i<max;i++) {
  64. int id=rnd.Next()&0xFFFF;
  65. String key=""+id+"-key-"+id;
  66. String val="value-"+id;
  67. if (ht[key]==null) {
  68. ht[key]=val;
  69. cache[n]=key;
  70. cache[n+max]=val;
  71. n++;
  72. }
  73. }
  74. Assert(ht.Count==n);
  75. for (int i=0;i<n;i++) {
  76. String key=cache[i];
  77. String val=ht[key] as String;
  78. String err="ht[\""+key+"\"]=\""+val+
  79. "\", expected \""+cache[i+max]+"\"";
  80. Assert(err,val!=null && val.Equals(cache[i+max]));
  81. }
  82. int r1=(n/3);
  83. int r2=r1+(n/5);
  84. for (int i=r1;i<r2;i++) {
  85. ht.Remove(cache[i]);
  86. }
  87. for (int i=0;i<n;i++) {
  88. if (i>=r1 && i<r2) {
  89. Assert(ht[cache[i]]==null);
  90. } else {
  91. String key=cache[i];
  92. String val=ht[key] as String;
  93. String err="ht[\""+key+"\"]=\""+val+
  94. "\", expected \""+cache[i+max]+"\"";
  95. Assert(err,val!=null && val.Equals(cache[i+max]));
  96. }
  97. }
  98. ICollection keys=ht.Keys;
  99. int nKeys=0;
  100. foreach (Object key in keys) {
  101. Assert((key as String) != null);
  102. nKeys++;
  103. }
  104. Assert(nKeys==ht.Count);
  105. ICollection vals=ht.Values;
  106. int nVals=0;
  107. foreach (Object val in vals) {
  108. Assert((val as String) != null);
  109. nVals++;
  110. }
  111. Assert(nVals==ht.Count);
  112. }
  113. }
  114. }