HashtableTest.cs 2.9 KB

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