remove svn:executable from .cs files
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / BasicOperationsTest.cs
1 // created on 7/21/2001 at 2:36 PM\r
2 //\r
3 // Author: Martin Willemoes Hansen <mwh@sysrq.dk>\r
4 // \r
5 // (C) 2003 Martin Willemoes Hansen\r
6 //\r
7 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Collections.Specialized;\r
11 using System.Text;\r
12 \r
13 using NUnit.Framework;\r
14 \r
15 namespace MonoTests.System.Collections.Specialized {\r
16 \r
17         [TestFixture]\r
18         public class BasicOperationsTest : Assertion {\r
19 \r
20                 protected NameValueCollection nvc;\r
21                 private static Random rnd;\r
22 \r
23                 [SetUp]\r
24                 public void GetReady() \r
25                 {\r
26                         nvc = new NameValueCollection();\r
27                         rnd=new Random();\r
28                 }\r
29 \r
30                 private void SetDefaultData() \r
31                 {\r
32                         nvc.Clear();\r
33                         nvc.Add("k1","this");\r
34                         nvc.Add("k2","test");\r
35                         nvc.Add("k3","is");\r
36                         nvc.Add("k4","silly");\r
37                 }\r
38 \r
39                 private static string FormatForPrinting (NameValueCollection nv)\r
40                 {\r
41                         if (nv==null) \r
42                                 return null;\r
43                         int max = nv.Count;\r
44                         StringBuilder sb = new StringBuilder("-\t-Key-\t-Value-\n");\r
45                         for (int i=0; i<max; i++){\r
46                                 \r
47                                 sb.Append("\t"+nv.GetKey(i)+"\t"+nv[i]+"\n");\r
48                         }\r
49                         return sb.ToString();\r
50                 }\r
51 \r
52                 [Test]\r
53                 public void AddRemoveClearSetGet() \r
54                 {\r
55                         nvc.Clear();\r
56                         Assert(nvc.Count==0&& !nvc.HasKeys());\r
57 \r
58                         SetDefaultData();\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
61 \r
62 \r
63                         nvc.Add("k2","programmer");\r
64                         Assert(nvc["k2"].Equals("test,programmer"));\r
65 \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
71                         nvc.Remove("k4");\r
72                         Assert("wrong nvc.Count="+nvc.Count,nvc.Count==3);\r
73                         Assert(nvc["k4"]==null);\r
74                         \r
75                         NameValueCollection nvc1 = new NameValueCollection();\r
76                         nvc1["k1"]="these";\r
77                         nvc1["k5"]="!";\r
78                         nvc.Add(nvc1);\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
81                         \r
82                         nvc.Set("k3","accomplished");\r
83                         Assert("Wrong result of Set operation",nvc["k3"].Equals("accomplished"));\r
84                         \r
85                 }\r
86                 \r
87                 [Test]\r
88                 public void GetKeyGetValues()\r
89                 {\r
90                         SetDefaultData();\r
91                         Assert(nvc.GetKey(0).Equals("k1"));\r
92                         string[] values = nvc.GetValues(0);\r
93                         Assert(values[0].Equals("this"));\r
94                         \r
95                 }\r
96                 \r
97                 [Test]\r
98                 public void CopyTo() {\r
99                         SetDefaultData();\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
104                 }\r
105 \r
106                 [Test]\r
107                 public void UnderHeavyLoad() {\r
108                         \r
109                         //TODO: add memory and time measurement\r
110                         \r
111                         nvc.Clear();\r
112                         int max=10000;\r
113                         String[] cache=new String[max*2];\r
114                         int n=0;\r
115 \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
121                                         nvc[key]=val;\r
122                                         cache[n]=key;\r
123                                         cache[n+max]=val;\r
124                                         n++;\r
125                                 }\r
126                         }\r
127 \r
128                         Assert(nvc.Count==n);\r
129 \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
136                         }\r
137 \r
138                         int r1=(n/3);\r
139                         int r2=r1+(n/5);\r
140 \r
141                         for (int i=r1;i<r2;i++) {\r
142                                 nvc.Remove(cache[i]);\r
143                         }\r
144 \r
145 \r
146                         for (int i=0;i<n;i++) {\r
147                                 if (i>=r1 && i<r2) {\r
148                                         Assert(nvc[cache[i]]==null);\r
149                                 } else {\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
155                                 }\r
156                         }\r
157 \r
158                 }\r
159 \r
160         }\r
161 }\r
162 \r