BasicOperationsTest.cs 3.9 KB

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