BasicOperationsTest.cs 3.7 KB

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