NameValueCollectionTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // created on 7/21/2001 at 2:36 PM
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. using NUnit.Framework;
  7. namespace Testsuite.System.Collections.Specialized {
  8. /// <summary>Microsoft NameValueCollection test.</summary>
  9. public class NameValueCollectionTest {
  10. public static ITest Suite {
  11. get {
  12. TestSuite suite= new TestSuite("All NameValueCollection Tests");
  13. suite.AddTest(BasicOperationsTest.Suite);
  14. return suite;
  15. }
  16. }
  17. }
  18. public class BasicOperationsTest : TestCase {
  19. protected NameValueCollection nvc;
  20. private static Random rnd;
  21. public BasicOperationsTest(String name) : base(name) {}
  22. protected override void SetUp() {
  23. nvc = new NameValueCollection();
  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. nvc.Clear();
  33. nvc.Add("k1","this");
  34. nvc.Add("k2","test");
  35. nvc.Add("k3","is");
  36. nvc.Add("k4","silly");
  37. }
  38. private static string FormatForPrinting(NameValueCollection nv)
  39. {
  40. if (nv==null)
  41. return null;
  42. int max = nv.Count;
  43. StringBuilder sb = new StringBuilder("-\t-Key-\t-Value-\n");
  44. for (int i=0; i<max; i++){
  45. sb.Append("\t"+nv.GetKey(i)+"\t"+nv[i]+"\n");
  46. }
  47. return sb.ToString();
  48. }
  49. public void TestAddRemoveClearSetGet()
  50. {
  51. nvc.Clear();
  52. Assert(nvc.Count==0&& !nvc.HasKeys());
  53. SetDefaultData();
  54. Assert(nvc.Count==4);
  55. Assert("Get operation returns wrong result.\n"+FormatForPrinting(nvc),(nvc.Get(0).Equals("this"))&&(nvc.Get("k1").Equals("this")));
  56. nvc.Add("k2","programmer");
  57. Assert(nvc["k2"].Equals("test,programmer"));
  58. nvc["k2"]="project";
  59. nvc.Add("k2","project");
  60. Assert(nvc.Count==4);
  61. Assert("Wrong effect of add(samekey,samevalue)\n"+FormatForPrinting(nvc),nvc["k2"].Equals("project"));
  62. // TODO: add Remove test
  63. nvc.Remove("k4");
  64. Assert("wrong nvc.Count="+nvc.Count,nvc.Count==3);
  65. Assert(nvc["k4"]==null);
  66. NameValueCollection nvc1 = new NameValueCollection();
  67. nvc1["k1"]="these";
  68. nvc1["k5"]="!";
  69. nvc.Add(nvc1);
  70. Assert(FormatForPrinting(nvc)+"Count is wrong after Add(nvc1) Count="+nvc.Count,nvc.Count==4);
  71. Assert("Values are wrong after Add(nvc1)",(nvc["k1"].Equals("this,these"))&&(nvc["k5"].Equals("!")));
  72. nvc.Set("k3","accomplished");
  73. Assert("Wrong result of Set operation",nvc["k3"].Equals("accomplished"));
  74. }
  75. public void TestGetKeyGetValues()
  76. {
  77. SetDefaultData();
  78. Assert(nvc.GetKey(0).Equals("k1"));
  79. string[] values = nvc.GetValues(0);
  80. Assert(values[0].Equals("this"));
  81. }
  82. public void TestCopyTo() {
  83. SetDefaultData();
  84. string[] entries=new string[nvc.Count];
  85. nvc.CopyTo(entries,0);
  86. //Message(FormatForPrinting(nvc));
  87. //Assert("Not an entry.",entries[0] is DictionaryEntry);
  88. }
  89. public void TestUnderHeavyLoad() {
  90. //TODO: add memory and time measurement
  91. nvc.Clear();
  92. int max=10000;
  93. String[] cache=new String[max*2];
  94. int n=0;
  95. for (int i=0;i<max;i++) {
  96. int id=rnd.Next()&0xFFFF;
  97. String key=""+id+"-key-"+id;
  98. String val="value-"+id;
  99. if (nvc[key]==null) {
  100. nvc[key]=val;
  101. cache[n]=key;
  102. cache[n+max]=val;
  103. n++;
  104. }
  105. }
  106. Assert(nvc.Count==n);
  107. for (int i=0;i<n;i++) {
  108. String key=cache[i];
  109. String val=nvc[key] as String;
  110. String err="nvc[\""+key+"\"]=\""+val+
  111. "\", expected \""+cache[i+max]+"\"";
  112. Assert(err,val!=null && val.Equals(cache[i+max]));
  113. }
  114. int r1=(n/3);
  115. int r2=r1+(n/5);
  116. for (int i=r1;i<r2;i++) {
  117. nvc.Remove(cache[i]);
  118. }
  119. for (int i=0;i<n;i++) {
  120. if (i>=r1 && i<r2) {
  121. Assert(nvc[cache[i]]==null);
  122. } else {
  123. String key=cache[i];
  124. String val=nvc[key] as String;
  125. String err="ht[\""+key+"\"]=\""+val+
  126. "\", expected \""+cache[i+max]+"\"";
  127. Assert(err,val!=null && val.Equals(cache[i+max]));
  128. }
  129. }
  130. }
  131. }
  132. }