1 // created on 7/21/2001 at 2:36 PM
\r
3 // Author: Martin Willemoes Hansen <mwh@sysrq.dk>
\r
5 // (C) 2003 Martin Willemoes Hansen
\r
9 using System.Collections;
\r
10 using System.Collections.Specialized;
\r
13 using NUnit.Framework;
\r
15 namespace MonoTests.System.Collections.Specialized {
\r
18 public class BasicOperationsTest : Assertion {
\r
20 protected NameValueCollection nvc;
\r
21 private static Random rnd;
\r
24 public void GetReady()
\r
26 nvc = new NameValueCollection();
\r
30 private void SetDefaultData()
\r
33 nvc.Add("k1","this");
\r
34 nvc.Add("k2","test");
\r
36 nvc.Add("k4","silly");
\r
39 private static string FormatForPrinting (NameValueCollection nv)
\r
44 StringBuilder sb = new StringBuilder("-\t-Key-\t-Value-\n");
\r
45 for (int i=0; i<max; i++){
\r
47 sb.Append("\t"+nv.GetKey(i)+"\t"+nv[i]+"\n");
\r
49 return sb.ToString();
\r
53 public void AddRemoveClearSetGet()
\r
56 Assert(nvc.Count==0&& !nvc.HasKeys());
\r
59 Assert(nvc.Count==4);
\r
60 Assert("Get operation returns wrong result.\n"+FormatForPrinting(nvc),(nvc.Get(0).Equals("this"))&&(nvc.Get("k1").Equals("this")));
\r
63 nvc.Add("k2","programmer");
\r
64 Assert(nvc["k2"].Equals("test,programmer"));
\r
66 nvc["k2"]="project";
\r
67 nvc.Add("k2","project");
\r
68 Assert(nvc.Count==4);
\r
69 Assert("Wrong effect of add(samekey,samevalue)\n"+FormatForPrinting(nvc), nvc["k2"].Equals("project,project"));
\r
70 // TODO: add Remove test
\r
72 Assert("wrong nvc.Count="+nvc.Count,nvc.Count==3);
\r
73 Assert(nvc["k4"]==null);
\r
75 NameValueCollection nvc1 = new NameValueCollection();
\r
79 Assert(FormatForPrinting(nvc)+"Count is wrong after Add(nvc1) Count="+nvc.Count,nvc.Count==4);
\r
80 Assert("Values are wrong after Add(nvc1)",(nvc["k1"].Equals("this,these"))&&(nvc["k5"].Equals("!")));
\r
82 nvc.Set("k3","accomplished");
\r
83 Assert("Wrong result of Set operation",nvc["k3"].Equals("accomplished"));
\r
88 public void GetKeyGetValues()
\r
91 Assert(nvc.GetKey(0).Equals("k1"));
\r
92 string[] values = nvc.GetValues(0);
\r
93 Assert(values[0].Equals("this"));
\r
98 public void CopyTo() {
\r
100 string[] entries=new string[nvc.Count];
\r
101 nvc.CopyTo(entries,0);
\r
102 //Message(FormatForPrinting(nvc));
\r
103 //Assert("Not an entry.",entries[0] is DictionaryEntry);
\r
107 public void UnderHeavyLoad() {
\r
109 //TODO: add memory and time measurement
\r
113 String[] cache=new String[max*2];
\r
116 for (int i=0;i<max;i++) {
\r
117 int id=rnd.Next()&0xFFFF;
\r
118 String key=""+id+"-key-"+id;
\r
119 String val="value-"+id;
\r
120 if (nvc[key]==null) {
\r
128 Assert(nvc.Count==n);
\r
130 for (int i=0;i<n;i++) {
\r
131 String key=cache[i];
\r
132 String val=nvc[key] as String;
\r
133 String err="nvc[\""+key+"\"]=\""+val+
\r
134 "\", expected \""+cache[i+max]+"\"";
\r
135 Assert(err,val!=null && val.Equals(cache[i+max]));
\r
141 for (int i=r1;i<r2;i++) {
\r
142 nvc.Remove(cache[i]);
\r
146 for (int i=0;i<n;i++) {
\r
147 if (i>=r1 && i<r2) {
\r
148 Assert(nvc[cache[i]]==null);
\r
150 String key=cache[i];
\r
151 String val=nvc[key] as String;
\r
152 String err="ht[\""+key+"\"]=\""+val+
\r
153 "\", expected \""+cache[i+max]+"\"";
\r
154 Assert(err,val!=null && val.Equals(cache[i+max]));
\r