1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
using System;
|
23
|
using C5;
|
24
|
using NUnit.Framework;
|
25
|
using SCG = System.Collections.Generic;
|
26
|
|
27
|
|
28
|
namespace C5UnitTests.interfaces
|
29
|
{
|
30
|
[TestFixture]
|
31
|
public class ICollectionsTests
|
32
|
{
|
33
|
public void TryC5Coll(ICollection<double> coll)
|
34
|
{
|
35
|
Assert.AreEqual(0, coll.Count);
|
36
|
double[] arr = { };
|
37
|
coll.CopyTo(arr, 0);
|
38
|
Assert.IsFalse(coll.IsReadOnly);
|
39
|
coll.Add(2.3);
|
40
|
coll.Add(3.2);
|
41
|
Assert.AreEqual(2, coll.Count);
|
42
|
Assert.IsTrue(coll.Contains(2.3));
|
43
|
Assert.IsFalse(coll.Contains(3.1));
|
44
|
Assert.IsFalse(coll.Remove(3.1));
|
45
|
Assert.IsTrue(coll.Remove(3.2));
|
46
|
Assert.IsFalse(coll.Contains(3.1));
|
47
|
Assert.AreEqual(1, coll.Count);
|
48
|
coll.Clear();
|
49
|
Assert.AreEqual(0, coll.Count);
|
50
|
Assert.IsFalse(coll.Remove(3.1));
|
51
|
}
|
52
|
|
53
|
public void TrySCGColl(SCG.ICollection<double> coll)
|
54
|
{
|
55
|
// All members of SCG.ICollection<T>
|
56
|
Assert.AreEqual(0, coll.Count);
|
57
|
double[] arr = { };
|
58
|
coll.CopyTo(arr, 0);
|
59
|
Assert.IsFalse(coll.IsReadOnly);
|
60
|
coll.Add(2.3);
|
61
|
coll.Add(3.2);
|
62
|
Assert.AreEqual(2, coll.Count);
|
63
|
Assert.IsTrue(coll.Contains(2.3));
|
64
|
Assert.IsFalse(coll.Contains(3.1));
|
65
|
Assert.IsFalse(coll.Remove(3.1));
|
66
|
Assert.IsTrue(coll.Remove(3.2));
|
67
|
Assert.IsFalse(coll.Contains(3.1));
|
68
|
Assert.AreEqual(1, coll.Count);
|
69
|
coll.Clear();
|
70
|
Assert.AreEqual(0, coll.Count);
|
71
|
Assert.IsFalse(coll.Remove(3.1));
|
72
|
}
|
73
|
|
74
|
public void TryBothColl(ICollection<double> coll)
|
75
|
{
|
76
|
TryC5Coll(coll);
|
77
|
TrySCGColl(coll);
|
78
|
}
|
79
|
|
80
|
|
81
|
[Test]
|
82
|
public void Test1()
|
83
|
{
|
84
|
TryBothColl(new HashSet<double>());
|
85
|
TryBothColl(new HashBag<double>());
|
86
|
TryBothColl(new TreeSet<double>());
|
87
|
TryBothColl(new TreeBag<double>());
|
88
|
TryBothColl(new ArrayList<double>());
|
89
|
TryBothColl(new LinkedList<double>());
|
90
|
TryBothColl(new HashedArrayList<double>());
|
91
|
TryBothColl(new HashedLinkedList<double>());
|
92
|
TryBothColl(new SortedArray<double>());
|
93
|
}
|
94
|
}
|
95
|
|
96
|
[TestFixture]
|
97
|
public class SCIListTests
|
98
|
{
|
99
|
class A { }
|
100
|
class B : A { }
|
101
|
class C : B { }
|
102
|
|
103
|
public void TrySCIList(System.Collections.IList list)
|
104
|
{
|
105
|
// Should be called with a C5.IList<B> which is not a WrappedArray
|
106
|
Assert.AreEqual(0, list.Count);
|
107
|
list.CopyTo(new A[0], 0);
|
108
|
list.CopyTo(new B[0], 0);
|
109
|
list.CopyTo(new C[0], 0);
|
110
|
Assert.IsTrue(!list.IsFixedSize);
|
111
|
Assert.IsFalse(list.IsReadOnly);
|
112
|
Assert.IsFalse(list.IsSynchronized);
|
113
|
Assert.AreNotEqual(null, list.SyncRoot);
|
114
|
Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
|
115
|
Assert.AreEqual(0, list.Add(b1));
|
116
|
Assert.AreEqual(1, list.Add(c1));
|
117
|
Assert.AreEqual(2, list.Count);
|
118
|
Assert.IsTrue(list.Contains(c1));
|
119
|
Assert.IsFalse(list.Contains(b2));
|
120
|
list[0] = b2;
|
121
|
Assert.AreEqual(b2, list[0]);
|
122
|
list[1] = c2;
|
123
|
Assert.AreEqual(c2, list[1]);
|
124
|
Assert.IsTrue(list.Contains(b2));
|
125
|
Assert.IsTrue(list.Contains(c2));
|
126
|
Array arrA = new A[2], arrB = new B[2];
|
127
|
list.CopyTo(arrA, 0);
|
128
|
list.CopyTo(arrB, 0);
|
129
|
Assert.AreEqual(b2, arrA.GetValue(0));
|
130
|
Assert.AreEqual(b2, arrB.GetValue(0));
|
131
|
Assert.AreEqual(c2, arrA.GetValue(1));
|
132
|
Assert.AreEqual(c2, arrB.GetValue(1));
|
133
|
Assert.AreEqual(0, list.IndexOf(b2));
|
134
|
Assert.AreEqual(-1, list.IndexOf(b1));
|
135
|
list.Remove(b1);
|
136
|
list.Remove(b2);
|
137
|
Assert.IsFalse(list.Contains(b2));
|
138
|
Assert.AreEqual(1, list.Count); // Contains c2 only
|
139
|
list.Insert(0, b2);
|
140
|
list.Insert(2, b1);
|
141
|
Assert.AreEqual(b2, list[0]);
|
142
|
Assert.AreEqual(c2, list[1]);
|
143
|
Assert.AreEqual(b1, list[2]);
|
144
|
list.Remove(c2);
|
145
|
Assert.AreEqual(b2, list[0]);
|
146
|
Assert.AreEqual(b1, list[1]);
|
147
|
list.RemoveAt(1);
|
148
|
Assert.AreEqual(b2, list[0]);
|
149
|
list.Clear();
|
150
|
Assert.AreEqual(0, list.Count);
|
151
|
list.Remove(b1);
|
152
|
}
|
153
|
|
154
|
[Test]
|
155
|
public void Test1()
|
156
|
{
|
157
|
TrySCIList(new ArrayList<B>());
|
158
|
TrySCIList(new HashedArrayList<B>());
|
159
|
TrySCIList(new LinkedList<B>());
|
160
|
TrySCIList(new HashedLinkedList<B>());
|
161
|
}
|
162
|
|
163
|
[Test]
|
164
|
public void TryWrappedArrayAsSCIList1()
|
165
|
{
|
166
|
B[] myarray = new B[] { new B(), new B(), new C() };
|
167
|
System.Collections.IList list = new WrappedArray<B>(myarray);
|
168
|
// Should be called with a three-element WrappedArray<B>
|
169
|
Assert.AreEqual(3, list.Count);
|
170
|
Assert.IsTrue(list.IsFixedSize);
|
171
|
Assert.IsFalse(list.IsSynchronized);
|
172
|
Assert.AreNotEqual(null, list.SyncRoot);
|
173
|
Assert.AreEqual(myarray.SyncRoot, list.SyncRoot);
|
174
|
Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
|
175
|
list[0] = b2;
|
176
|
Assert.AreEqual(b2, list[0]);
|
177
|
list[1] = c2;
|
178
|
Assert.AreEqual(c2, list[1]);
|
179
|
Assert.IsTrue(list.Contains(b2));
|
180
|
Assert.IsTrue(list.Contains(c2));
|
181
|
Array arrA = new A[3], arrB = new B[3];
|
182
|
list.CopyTo(arrA, 0);
|
183
|
list.CopyTo(arrB, 0);
|
184
|
Assert.AreEqual(b2, arrA.GetValue(0));
|
185
|
Assert.AreEqual(b2, arrB.GetValue(0));
|
186
|
Assert.AreEqual(c2, arrA.GetValue(1));
|
187
|
Assert.AreEqual(c2, arrB.GetValue(1));
|
188
|
Assert.AreEqual(0, list.IndexOf(b2));
|
189
|
Assert.AreEqual(-1, list.IndexOf(b1));
|
190
|
Assert.AreEqual(-1, list.IndexOf(c1));
|
191
|
Assert.IsFalse(list.Contains(b1));
|
192
|
Assert.IsFalse(list.Contains(c1));
|
193
|
}
|
194
|
|
195
|
[Test]
|
196
|
public void TryWrappedArrayAsSCIList2()
|
197
|
{
|
198
|
B[] myarray = new B[] { };
|
199
|
System.Collections.IList list = new WrappedArray<B>(myarray);
|
200
|
// Should be called with an empty WrappedArray<B>
|
201
|
Assert.AreEqual(0, list.Count);
|
202
|
list.CopyTo(new A[0], 0);
|
203
|
list.CopyTo(new B[0], 0);
|
204
|
list.CopyTo(new C[0], 0);
|
205
|
Assert.IsFalse(list.IsSynchronized);
|
206
|
Assert.AreNotEqual(null, list.SyncRoot);
|
207
|
Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
|
208
|
Assert.IsFalse(list.Contains(b2));
|
209
|
Assert.IsFalse(list.Contains(c2));
|
210
|
Assert.AreEqual(-1, list.IndexOf(b1));
|
211
|
Assert.AreEqual(-1, list.IndexOf(c1));
|
212
|
}
|
213
|
|
214
|
[Test]
|
215
|
public void TryGuardedListAsSCIList1()
|
216
|
{
|
217
|
B b1_ = new B(), b2_ = new B();
|
218
|
C c1_ = new C(), c2_ = new C();
|
219
|
ArrayList<B> mylist = new ArrayList<B>();
|
220
|
mylist.AddAll(new B[] { b1_, b2_, c1_ });
|
221
|
System.Collections.IList list = new GuardedList<B>(mylist);
|
222
|
Object b1 = b1_, b2 = b2_, c1 = c1_, c2 = c2_;
|
223
|
// Should be called with a three-element GuardedList<B>
|
224
|
Assert.AreEqual(3, list.Count);
|
225
|
Assert.IsTrue(list.IsFixedSize);
|
226
|
Assert.IsTrue(list.IsReadOnly);
|
227
|
Assert.IsFalse(list.IsSynchronized);
|
228
|
Assert.AreNotEqual(null, list.SyncRoot);
|
229
|
Assert.AreEqual(list.SyncRoot, ((System.Collections.IList)mylist).SyncRoot);
|
230
|
Assert.IsTrue(list.Contains(b1));
|
231
|
Assert.IsTrue(list.Contains(b2));
|
232
|
Assert.IsTrue(list.Contains(c1));
|
233
|
Assert.IsFalse(list.Contains(c2));
|
234
|
Array arrA = new A[3], arrB = new B[3];
|
235
|
list.CopyTo(arrA, 0);
|
236
|
list.CopyTo(arrB, 0);
|
237
|
Assert.AreEqual(b1, arrA.GetValue(0));
|
238
|
Assert.AreEqual(b1, arrB.GetValue(0));
|
239
|
Assert.AreEqual(b2, arrA.GetValue(1));
|
240
|
Assert.AreEqual(b2, arrB.GetValue(1));
|
241
|
Assert.AreEqual(0, list.IndexOf(b1));
|
242
|
Assert.AreEqual(-1, list.IndexOf(c2));
|
243
|
}
|
244
|
|
245
|
[Test]
|
246
|
public void TryGuardedListAsSCIList2()
|
247
|
{
|
248
|
System.Collections.IList list = new GuardedList<B>(new ArrayList<B>());
|
249
|
// Should be called with an empty GuardedList<B>
|
250
|
Assert.AreEqual(0, list.Count);
|
251
|
list.CopyTo(new A[0], 0);
|
252
|
list.CopyTo(new B[0], 0);
|
253
|
list.CopyTo(new C[0], 0);
|
254
|
Assert.IsFalse(list.IsSynchronized);
|
255
|
Assert.AreNotEqual(null, list.SyncRoot);
|
256
|
Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
|
257
|
Assert.IsFalse(list.Contains(b2));
|
258
|
Assert.IsFalse(list.Contains(c2));
|
259
|
Assert.AreEqual(-1, list.IndexOf(b1));
|
260
|
Assert.AreEqual(-1, list.IndexOf(c1));
|
261
|
}
|
262
|
|
263
|
[Test]
|
264
|
public void TryViewOfGuardedListAsSCIList1()
|
265
|
{
|
266
|
B b1_ = new B(), b2_ = new B();
|
267
|
C c1_ = new C(), c2_ = new C();
|
268
|
ArrayList<B> mylist = new ArrayList<B>();
|
269
|
mylist.AddAll(new B[] { new B(), b1_, b2_, c1_, new B()});
|
270
|
System.Collections.IList list = new GuardedList<B>(mylist).View(1, 3);
|
271
|
Object b1 = b1_, b2 = b2_, c1 = c1_, c2 = c2_;
|
272
|
// Should be called with a three-element view of a GuardedList<B>
|
273
|
Assert.AreEqual(3, list.Count);
|
274
|
Assert.IsTrue(list.IsFixedSize);
|
275
|
Assert.IsTrue(list.IsReadOnly);
|
276
|
Assert.IsFalse(list.IsSynchronized);
|
277
|
Assert.AreNotEqual(null, list.SyncRoot);
|
278
|
Assert.AreEqual(list.SyncRoot, ((System.Collections.IList)mylist).SyncRoot);
|
279
|
Assert.IsTrue(list.Contains(b1));
|
280
|
Assert.IsTrue(list.Contains(b2));
|
281
|
Assert.IsTrue(list.Contains(c1));
|
282
|
Assert.IsFalse(list.Contains(c2));
|
283
|
Array arrA = new A[3], arrB = new B[3];
|
284
|
list.CopyTo(arrA, 0);
|
285
|
list.CopyTo(arrB, 0);
|
286
|
Assert.AreEqual(b1, arrA.GetValue(0));
|
287
|
Assert.AreEqual(b1, arrB.GetValue(0));
|
288
|
Assert.AreEqual(b2, arrA.GetValue(1));
|
289
|
Assert.AreEqual(b2, arrB.GetValue(1));
|
290
|
Assert.AreEqual(0, list.IndexOf(b1));
|
291
|
Assert.AreEqual(-1, list.IndexOf(c2));
|
292
|
}
|
293
|
|
294
|
[Test]
|
295
|
public void TryViewOfGuardedListAsSCIList2()
|
296
|
{
|
297
|
System.Collections.IList list = new GuardedList<B>(new ArrayList<B>()).View(0, 0);
|
298
|
Assert.AreEqual(0, list.Count);
|
299
|
list.CopyTo(new A[0], 0);
|
300
|
list.CopyTo(new B[0], 0);
|
301
|
list.CopyTo(new C[0], 0);
|
302
|
Assert.IsFalse(list.IsSynchronized);
|
303
|
Assert.AreNotEqual(null, list.SyncRoot);
|
304
|
Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
|
305
|
Assert.IsFalse(list.Contains(b2));
|
306
|
Assert.IsFalse(list.Contains(c2));
|
307
|
Assert.AreEqual(-1, list.IndexOf(b1));
|
308
|
Assert.AreEqual(-1, list.IndexOf(c1));
|
309
|
}
|
310
|
|
311
|
void TryListViewAsSCIList1(IList<B> mylist)
|
312
|
{
|
313
|
B b1_ = new B(), b2_ = new B();
|
314
|
C c1_ = new C(), c2_ = new C();
|
315
|
mylist.AddAll(new B[] { new B(), b1_, b2_, c1_, new B() });
|
316
|
System.Collections.IList list = mylist.View(1, 3);
|
317
|
Object b1 = b1_, b2 = b2_, c1 = c1_, c2 = c2_;
|
318
|
// Should be called with a three-element view on ArrayList<B>
|
319
|
Assert.AreEqual(3, list.Count);
|
320
|
Assert.IsFalse(list.IsSynchronized);
|
321
|
Assert.AreNotEqual(null, list.SyncRoot);
|
322
|
Assert.AreEqual(list.SyncRoot, mylist.SyncRoot);
|
323
|
Assert.IsTrue(list.Contains(b1));
|
324
|
Assert.IsTrue(list.Contains(b2));
|
325
|
Assert.IsTrue(list.Contains(c1));
|
326
|
Assert.IsFalse(list.Contains(c2));
|
327
|
Array arrA = new A[3], arrB = new B[3];
|
328
|
list.CopyTo(arrA, 0);
|
329
|
list.CopyTo(arrB, 0);
|
330
|
Assert.AreEqual(b1, arrA.GetValue(0));
|
331
|
Assert.AreEqual(b1, arrB.GetValue(0));
|
332
|
Assert.AreEqual(b2, arrA.GetValue(1));
|
333
|
Assert.AreEqual(b2, arrB.GetValue(1));
|
334
|
Assert.AreEqual(0, list.IndexOf(b1));
|
335
|
Assert.AreEqual(-1, list.IndexOf(c2));
|
336
|
}
|
337
|
|
338
|
void TryListViewAsSCIList2(IList<B> mylist)
|
339
|
{
|
340
|
System.Collections.IList list = mylist.View(0, 0);
|
341
|
Assert.AreEqual(0, list.Count);
|
342
|
list.CopyTo(new A[0], 0);
|
343
|
list.CopyTo(new B[0], 0);
|
344
|
list.CopyTo(new C[0], 0);
|
345
|
Assert.IsFalse(list.IsSynchronized);
|
346
|
Assert.AreNotEqual(null, list.SyncRoot);
|
347
|
Assert.AreEqual(list.SyncRoot, mylist.SyncRoot);
|
348
|
Object b1 = new B(), b2 = new B(), c1 = new C(), c2 = new C();
|
349
|
Assert.IsFalse(list.Contains(b2));
|
350
|
Assert.IsFalse(list.Contains(c2));
|
351
|
Assert.AreEqual(-1, list.IndexOf(b1));
|
352
|
Assert.AreEqual(-1, list.IndexOf(c1));
|
353
|
}
|
354
|
|
355
|
[Test]
|
356
|
public void TryArrayListViewAsSCIList()
|
357
|
{
|
358
|
TryListViewAsSCIList1(new ArrayList<B>());
|
359
|
TryListViewAsSCIList2(new ArrayList<B>());
|
360
|
}
|
361
|
|
362
|
[Test]
|
363
|
public void TryLinkedListViewAsSCIList()
|
364
|
{
|
365
|
TryListViewAsSCIList1(new LinkedList<B>());
|
366
|
TryListViewAsSCIList2(new LinkedList<B>());
|
367
|
}
|
368
|
|
369
|
[Test]
|
370
|
public void TryHashedArrayListViewAsSCIList()
|
371
|
{
|
372
|
TryListViewAsSCIList1(new HashedArrayList<B>());
|
373
|
TryListViewAsSCIList2(new HashedArrayList<B>());
|
374
|
}
|
375
|
|
376
|
[Test]
|
377
|
public void TryHashedLinkedListViewAsSCIList()
|
378
|
{
|
379
|
TryListViewAsSCIList1(new HashedLinkedList<B>());
|
380
|
TryListViewAsSCIList2(new HashedLinkedList<B>());
|
381
|
}
|
382
|
|
383
|
[Test]
|
384
|
public void TryGuardedViewAsSCIList()
|
385
|
{
|
386
|
ArrayList<B> mylist = new ArrayList<B>();
|
387
|
TryListViewAsSCIList2(new GuardedList<B>(mylist));
|
388
|
}
|
389
|
}
|
390
|
|
391
|
[TestFixture]
|
392
|
public class IDictionaryTests
|
393
|
{
|
394
|
public void TryDictionary(IDictionary<string,string> dict)
|
395
|
{
|
396
|
Assert.AreEqual(0, dict.Count);
|
397
|
Assert.IsTrue(dict.IsEmpty);
|
398
|
Assert.IsFalse(dict.IsReadOnly);
|
399
|
KeyValuePair<string,string>[] arr = { };
|
400
|
dict.CopyTo(arr, 0);
|
401
|
dict["R"] = "A";
|
402
|
dict["S"] = "B";
|
403
|
dict["T"] = "C";
|
404
|
String old;
|
405
|
Assert.IsTrue(dict.Update("R", "A1"));
|
406
|
Assert.AreEqual("A1", dict["R"]);
|
407
|
|
408
|
Assert.IsFalse(dict.Update("U", "D1"));
|
409
|
Assert.IsFalse(dict.Contains("U"));
|
410
|
|
411
|
Assert.IsTrue(dict.Update("R", "A2", out old));
|
412
|
Assert.AreEqual("A2", dict["R"]);
|
413
|
Assert.AreEqual("A1", old);
|
414
|
|
415
|
Assert.IsFalse(dict.Update("U", "D2", out old));
|
416
|
Assert.AreEqual(null, old);
|
417
|
Assert.IsFalse(dict.Contains("U"));
|
418
|
|
419
|
Assert.IsTrue(dict.UpdateOrAdd("R", "A3"));
|
420
|
Assert.AreEqual("A3", dict["R"]);
|
421
|
|
422
|
Assert.IsFalse(dict.UpdateOrAdd("U", "D3"));
|
423
|
Assert.IsTrue(dict.Contains("U"));
|
424
|
Assert.AreEqual("D3", dict["U"]);
|
425
|
|
426
|
Assert.IsTrue(dict.UpdateOrAdd("R", "A4", out old));
|
427
|
Assert.AreEqual("A4", dict["R"]);
|
428
|
Assert.AreEqual("A3", old);
|
429
|
|
430
|
Assert.IsTrue(dict.UpdateOrAdd("U", "D4", out old));
|
431
|
Assert.IsTrue(dict.Contains("U"));
|
432
|
Assert.AreEqual("D4", dict["U"]);
|
433
|
Assert.AreEqual("D3", old);
|
434
|
|
435
|
Assert.IsFalse(dict.UpdateOrAdd("V", "E1", out old));
|
436
|
Assert.IsTrue(dict.Contains("V"));
|
437
|
Assert.AreEqual("E1", dict["V"]);
|
438
|
Assert.AreEqual(null, old);
|
439
|
}
|
440
|
|
441
|
[Test]
|
442
|
public void TestHashDictionary()
|
443
|
{
|
444
|
TryDictionary(new HashDictionary<string,string>());
|
445
|
}
|
446
|
|
447
|
[Test]
|
448
|
public void TestTreeDictionary()
|
449
|
{
|
450
|
TryDictionary(new TreeDictionary<string, string>());
|
451
|
}
|
452
|
}
|
453
|
}
|