Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / trees / Bag.cs @ 3

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.trees.TreeBag
29
{
30
  using CollectionOfInt = TreeBag<int>;
31

    
32
  [TestFixture]
33
  public class GenericTesters
34
  {
35
    [Test]
36
    public void TestEvents()
37
    {
38
      Fun<CollectionOfInt> factory = delegate() { return new CollectionOfInt(TenEqualityComparer.Default); };
39
      new C5UnitTests.Templates.Events.SortedIndexedTester<CollectionOfInt>().Test(factory);
40
    }
41

    
42
    [Test]
43
    public void Extensible()
44
    {
45
      C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
46
      C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
47
    }
48
  }
49

    
50
  static class Factory
51
  {
52
    public static ICollection<T> New<T>() { return new TreeBag<T>(); }
53
  }
54

    
55

    
56
  [TestFixture]
57
  public class Formatting
58
  {
59
    ICollection<int> coll;
60
    IFormatProvider rad16;
61
    [SetUp]
62
    public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
63
    [TearDown]
64
    public void Dispose() { coll = null; rad16 = null; }
65
    [Test]
66
    public void Format()
67
    {
68
      Assert.AreEqual("{{  }}", coll.ToString());
69
      coll.AddAll<int>(new int[] { -4, 28, 129, 65530, -4, 28 });
70
      Assert.AreEqual("{{ -4(*2), 28(*2), 129(*1), 65530(*1) }}", coll.ToString());
71
      Assert.AreEqual("{{ -4(*2), 1C(*2), 81(*1), FFFA(*1) }}", coll.ToString(null, rad16));
72
      Assert.AreEqual("{{ -4(*2), 28(*2)... }}", coll.ToString("L18", null));
73
      Assert.AreEqual("{{ -4(*2), 1C(*2)... }}", coll.ToString("L18", rad16));
74
    }
75
  }
76

    
77
  [TestFixture]
78
  public class Combined
79
  {
80
    private IIndexedSorted<KeyValuePair<int, int>> lst;
81

    
82

    
83
    [SetUp]
84
    public void Init()
85
    {
86
      lst = new TreeBag<KeyValuePair<int, int>>(new KeyValuePairComparer<int, int>(new IC()));
87
      for (int i = 0; i < 10; i++)
88
        lst.Add(new KeyValuePair<int, int>(i, i + 30));
89
    }
90

    
91

    
92
    [TearDown]
93
    public void Dispose() { lst = null; }
94

    
95

    
96
    [Test]
97
    public void Find()
98
    {
99
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
100

    
101
      Assert.IsTrue(lst.Find(ref p));
102
      Assert.AreEqual(3, p.Key);
103
      Assert.AreEqual(33, p.Value);
104
      p = new KeyValuePair<int, int>(13, 78);
105
      Assert.IsFalse(lst.Find(ref p));
106
    }
107

    
108

    
109
    [Test]
110
    public void FindOrAdd()
111
    {
112
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
113

    
114
      Assert.IsTrue(lst.FindOrAdd(ref p));
115
      Assert.AreEqual(3, p.Key);
116
      Assert.AreEqual(33, p.Value);
117
      p = new KeyValuePair<int, int>(13, 79);
118
      Assert.IsFalse(lst.FindOrAdd(ref p));
119
      Assert.AreEqual(13, lst[11].Key);
120
      Assert.AreEqual(79, lst[11].Value);
121
    }
122

    
123

    
124
    [Test]
125
    public void Update()
126
    {
127
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
128

    
129
      Assert.IsTrue(lst.Update(p));
130
      Assert.AreEqual(3, lst[3].Key);
131
      Assert.AreEqual(78, lst[3].Value);
132
      p = new KeyValuePair<int, int>(13, 78);
133
      Assert.IsFalse(lst.Update(p));
134
    }
135

    
136

    
137
    [Test]
138
    public void UpdateOrAdd1()
139
    {
140
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
141

    
142
      Assert.IsTrue(lst.UpdateOrAdd(p));
143
      Assert.AreEqual(3, lst[3].Key);
144
      Assert.AreEqual(78, lst[3].Value);
145
      p = new KeyValuePair<int, int>(13, 79);
146
      Assert.IsFalse(lst.UpdateOrAdd(p));
147
      Assert.AreEqual(13, lst[10].Key);
148
      Assert.AreEqual(79, lst[10].Value);
149
    }
150

    
151
    [Test]
152
    public void UpdateOrAdd2()
153
    {
154
        ICollection<String> coll = new TreeBag<String>();
155
        // s1 and s2 are distinct objects but contain the same text:
156
        String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
157
        Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
158
        Assert.AreEqual(null, old);
159
        Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
160
        Assert.IsTrue(Object.ReferenceEquals(s1, old));
161
        Assert.IsFalse(Object.ReferenceEquals(s2, old));
162
    }
163

    
164
    [Test]
165
    public void RemoveWithReturn()
166
    {
167
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
168

    
169
      Assert.IsTrue(lst.Remove(p, out p));
170
      Assert.AreEqual(3, p.Key);
171
      Assert.AreEqual(33, p.Value);
172
      Assert.AreEqual(4, lst[3].Key);
173
      Assert.AreEqual(34, lst[3].Value);
174
      p = new KeyValuePair<int, int>(13, 78);
175
      Assert.IsFalse(lst.Remove(p, out p));
176
    }
177
  }
178

    
179

    
180
  [TestFixture]
181
  public class Simple
182
  {
183
    private TreeBag<string> bag;
184

    
185

    
186
    [SetUp]
187
    public void Init()
188
    {
189
      bag = new TreeBag<string>(new SC());
190
    }
191

    
192

    
193
    [TearDown]
194
    public void Dispose()
195
    {
196
      bag = null;
197
    }
198

    
199
    [Test]
200
    public void Initial()
201
    {
202
      bool res;
203

    
204
      Assert.IsFalse(bag.IsReadOnly);
205
      Assert.AreEqual(0, bag.Count, "new bag should be empty");
206
      Assert.AreEqual(0, bag.ContainsCount("A"));
207
      Assert.AreEqual(0, bag.ContainsCount("B"));
208
      Assert.AreEqual(0, bag.ContainsCount("C"));
209
      Assert.IsFalse(bag.Contains("A"));
210
      Assert.IsFalse(bag.Contains("B"));
211
      Assert.IsFalse(bag.Contains("C"));
212
      bag.Add("A");
213
      Assert.AreEqual(1, bag.Count);
214
      Assert.AreEqual(1, bag.ContainsCount("A"));
215
      Assert.AreEqual(0, bag.ContainsCount("B"));
216
      Assert.AreEqual(0, bag.ContainsCount("C"));
217
      Assert.IsTrue(bag.Contains("A"));
218
      Assert.IsFalse(bag.Contains("B"));
219
      Assert.IsFalse(bag.Contains("C"));
220
      bag.Add("C");
221
      Assert.AreEqual(2, bag.Count);
222
      Assert.AreEqual(1, bag.ContainsCount("A"));
223
      Assert.AreEqual(0, bag.ContainsCount("B"));
224
      Assert.AreEqual(1, bag.ContainsCount("C"));
225
      Assert.IsTrue(bag.Contains("A"));
226
      Assert.IsFalse(bag.Contains("B"));
227
      Assert.IsTrue(bag.Contains("C"));
228
      bag.Add("C");
229
      Assert.AreEqual(3, bag.Count);
230
      Assert.AreEqual(1, bag.ContainsCount("A"));
231
      Assert.AreEqual(0, bag.ContainsCount("B"));
232
      Assert.AreEqual(2, bag.ContainsCount("C"));
233
      Assert.IsTrue(bag.Contains("A"));
234
      Assert.IsFalse(bag.Contains("B"));
235
      Assert.IsTrue(bag.Contains("C"));
236
      bag.Add("B");
237
      bag.Add("C");
238
      Assert.AreEqual(5, bag.Count);
239
      Assert.AreEqual(1, bag.ContainsCount("A"));
240
      Assert.AreEqual(1, bag.ContainsCount("B"));
241
      Assert.AreEqual(3, bag.ContainsCount("C"));
242
      Assert.IsTrue(bag.Contains("A"));
243
      Assert.IsTrue(bag.Contains("B"));
244
      Assert.IsTrue(bag.Contains("C"));
245
      res = bag.Remove("C");
246
      Assert.AreEqual(4, bag.Count);
247
      Assert.AreEqual(1, bag.ContainsCount("A"));
248
      Assert.AreEqual(1, bag.ContainsCount("B"));
249
      Assert.AreEqual(2, bag.ContainsCount("C"));
250
      Assert.IsTrue(bag.Contains("A"));
251
      Assert.IsTrue(bag.Contains("B"));
252
      Assert.IsTrue(bag.Contains("C"));
253
      res = bag.Remove("A");
254
      Assert.AreEqual(3, bag.Count);
255
      Assert.AreEqual(0, bag.ContainsCount("A"));
256
      Assert.AreEqual(1, bag.ContainsCount("B"));
257
      Assert.AreEqual(2, bag.ContainsCount("C"));
258
      Assert.IsFalse(bag.Contains("A"));
259
      Assert.IsTrue(bag.Contains("B"));
260
      Assert.IsTrue(bag.Contains("C"));
261
      bag.RemoveAllCopies("C");
262
      Assert.AreEqual(1, bag.Count);
263
      Assert.AreEqual(0, bag.ContainsCount("A"));
264
      Assert.AreEqual(1, bag.ContainsCount("B"));
265
      Assert.AreEqual(0, bag.ContainsCount("C"));
266
      Assert.IsFalse(bag.Contains("A"));
267
      Assert.IsTrue(bag.Contains("B"));
268
      Assert.IsFalse(bag.Contains("C"));
269
      Assert.IsFalse(bag.Contains("Z"));
270
      Assert.IsFalse(bag.Remove("Z"));
271
      bag.RemoveAllCopies("Z");
272
      Assert.AreEqual(1, bag.Count);
273
      Assert.AreEqual(0, bag.ContainsCount("A"));
274
      Assert.AreEqual(1, bag.ContainsCount("B"));
275
      Assert.AreEqual(0, bag.ContainsCount("C"));
276
      Assert.IsFalse(bag.Contains("A"));
277
      Assert.IsTrue(bag.Contains("B"));
278
      Assert.IsFalse(bag.Contains("C"));
279
      Assert.IsFalse(bag.Contains("Z"));
280
    }
281
  }
282

    
283
  [TestFixture]
284
  public class FindOrAdd
285
  {
286
    private TreeBag<KeyValuePair<int, string>> bag;
287

    
288

    
289
    [SetUp]
290
    public void Init()
291
    {
292
      bag = new TreeBag<KeyValuePair<int, string>>(new KeyValuePairComparer<int, string>(new IC()));
293
    }
294

    
295

    
296
    [TearDown]
297
    public void Dispose()
298
    {
299
      bag = null;
300
    }
301

    
302

    
303
    [Test]
304
    public void Test()
305
    {
306
      KeyValuePair<int, string> p = new KeyValuePair<int, string>(3, "tre");
307
      Assert.IsFalse(bag.FindOrAdd(ref p));
308
      p.Value = "drei";
309
      Assert.IsTrue(bag.FindOrAdd(ref p));
310
      Assert.AreEqual("tre", p.Value);
311
      p.Value = "three";
312
      Assert.AreEqual(2, bag.ContainsCount(p));
313
      Assert.AreEqual("tre", bag[0].Value);
314
    }
315
  }
316

    
317

    
318
  [TestFixture]
319
  public class Enumerators
320
  {
321
    private TreeBag<string> bag;
322

    
323
    private SCG.IEnumerator<string> bagenum;
324

    
325

    
326
    [SetUp]
327
    public void Init()
328
    {
329
      bag = new TreeBag<string>(new SC());
330
      foreach (string s in new string[] { "A", "B", "A", "A", "B", "C", "D", "B" })
331
        bag.Add(s);
332

    
333
      bagenum = bag.GetEnumerator();
334
    }
335

    
336

    
337
    [TearDown]
338
    public void Dispose()
339
    {
340
      bagenum = null;
341
      bag = null;
342
    }
343

    
344

    
345
    [Test]
346
    [ExpectedException(typeof(CollectionModifiedException))]
347
    public void MoveNextOnModified()
348
    {
349
      //TODO: also problem before first MoveNext!!!!!!!!!!
350
      bagenum.MoveNext();
351
      bag.Add("T");
352
      bagenum.MoveNext();
353
    }
354

    
355

    
356
    [Test]
357
    public void NormalUse()
358
    {
359
      Assert.IsTrue(bagenum.MoveNext());
360
      Assert.AreEqual(bagenum.Current, "A");
361
      Assert.IsTrue(bagenum.MoveNext());
362
      Assert.AreEqual(bagenum.Current, "A");
363
      Assert.IsTrue(bagenum.MoveNext());
364
      Assert.AreEqual(bagenum.Current, "A");
365
      Assert.IsTrue(bagenum.MoveNext());
366
      Assert.AreEqual(bagenum.Current, "B");
367
      Assert.IsTrue(bagenum.MoveNext());
368
      Assert.AreEqual(bagenum.Current, "B");
369
      Assert.IsTrue(bagenum.MoveNext());
370
      Assert.AreEqual(bagenum.Current, "B");
371
      Assert.IsTrue(bagenum.MoveNext());
372
      Assert.AreEqual(bagenum.Current, "C");
373
      Assert.IsTrue(bagenum.MoveNext());
374
      Assert.AreEqual(bagenum.Current, "D");
375
      Assert.IsFalse(bagenum.MoveNext());
376
    }
377
  }
378

    
379
  [TestFixture]
380
  public class Ranges
381
  {
382
    private TreeBag<int> tree;
383

    
384
    private SCG.IComparer<int> c;
385

    
386

    
387
    [SetUp]
388
    public void Init()
389
    {
390
      c = new IC();
391
      tree = new TreeBag<int>(c);
392
      for (int i = 1; i <= 10; i++)
393
      {
394
        tree.Add(i * 2); tree.Add(i);
395
      }
396
    }
397

    
398

    
399
    [Test]
400
    public void Enumerator()
401
    {
402
      SCG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
403
      int i = 0;
404
      int[] all = new int[] { 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16 };
405
      while (e.MoveNext())
406
      {
407
        Assert.AreEqual(all[i++], e.Current);
408
      }
409

    
410
      Assert.AreEqual(12, i);
411
    }
412

    
413

    
414
    [Test]
415
    [ExpectedException(typeof(InvalidOperationException))]
416
    public void Enumerator2()
417
    {
418
      SCG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
419
      int i = e.Current;
420
    }
421

    
422

    
423
    [Test]
424
    [ExpectedException(typeof(InvalidOperationException))]
425
    public void Enumerator3()
426
    {
427
      SCG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
428

    
429
      while (e.MoveNext()) ;
430

    
431
      int i = e.Current;
432
    }
433

    
434

    
435
    [Test]
436
    public void Remove()
437
    {
438
      int[] all = new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 };
439

    
440
      tree.RemoveRangeFrom(18);
441
      Assert.IsTrue(IC.eq(tree, new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16 }));
442
      tree.RemoveRangeFrom(28);
443
      Assert.IsTrue(IC.eq(tree, new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16 }));
444
      tree.RemoveRangeFrom(1);
445
      Assert.IsTrue(IC.eq(tree));
446
      foreach (int i in all) tree.Add(i);
447

    
448
      tree.RemoveRangeTo(10);
449
      Assert.IsTrue(IC.eq(tree, new int[] { 10, 10, 12, 14, 16, 18, 20 }));
450
      tree.RemoveRangeTo(2);
451
      Assert.IsTrue(IC.eq(tree, new int[] { 10, 10, 12, 14, 16, 18, 20 }));
452
      tree.RemoveRangeTo(21);
453
      Assert.IsTrue(IC.eq(tree));
454
      foreach (int i in all) tree.Add(i);
455

    
456
      tree.RemoveRangeFromTo(4, 8);
457
      Assert.IsTrue(IC.eq(tree, 1, 2, 2, 3, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20));
458
      tree.RemoveRangeFromTo(14, 28);
459
      Assert.IsTrue(IC.eq(tree, 1, 2, 2, 3, 8, 8, 9, 10, 10, 12));
460
      tree.RemoveRangeFromTo(0, 9);
461
      Assert.IsTrue(IC.eq(tree, 9, 10, 10, 12));
462
      tree.RemoveRangeFromTo(0, 81);
463
      Assert.IsTrue(IC.eq(tree));
464
    }
465

    
466

    
467
    [Test]
468
    public void Normal()
469
    {
470
      int[] all = new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 };
471

    
472
      Assert.IsTrue(IC.eq(tree, all));
473
      Assert.IsTrue(IC.eq(tree.RangeAll(), all));
474
      Assert.AreEqual(20, tree.RangeAll().Count);
475
      Assert.IsTrue(IC.eq(tree.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
476
      Assert.AreEqual(5, tree.RangeFrom(11).Count);
477
      Assert.IsTrue(IC.eq(tree.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
478
      Assert.IsTrue(IC.eq(tree.RangeFrom(1), all));
479
      Assert.IsTrue(IC.eq(tree.RangeFrom(0), all));
480
      Assert.IsTrue(IC.eq(tree.RangeFrom(21), new int[] { }));
481
      Assert.IsTrue(IC.eq(tree.RangeFrom(20), new int[] { 20 }));
482
      Assert.IsTrue(IC.eq(tree.RangeTo(8), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7 }));
483
      Assert.IsTrue(IC.eq(tree.RangeTo(7), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6 }));
484
      Assert.AreEqual(9, tree.RangeTo(7).Count);
485
      Assert.IsTrue(IC.eq(tree.RangeTo(1), new int[] { }));
486
      Assert.IsTrue(IC.eq(tree.RangeTo(0), new int[] { }));
487
      Assert.IsTrue(IC.eq(tree.RangeTo(3), new int[] { 1, 2, 2 }));
488
      Assert.IsTrue(IC.eq(tree.RangeTo(20), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18 }));
489
      Assert.IsTrue(IC.eq(tree.RangeTo(21), all));
490
      Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12), new int[] { 7, 8, 8, 9, 10, 10 }));
491
      Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11), new int[] { 6, 6, 7, 8, 8, 9, 10, 10 }));
492
      Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10 }));
493
      Assert.AreEqual(15, tree.RangeFromTo(1, 12).Count);
494
      Assert.IsTrue(IC.eq(tree.RangeFromTo(2, 12), new int[] { 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10 }));
495
      Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21), new int[] { 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 }));
496
      Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20), new int[] { 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18 }));
497
    }
498

    
499

    
500
    [Test]
501
    public void Backwards()
502
    {
503
      int[] all = new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 };
504
      int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 };
505

    
506
      Assert.IsTrue(IC.eq(tree, all));
507
      Assert.IsTrue(IC.eq(tree.RangeAll().Backwards(), lla));
508
      Assert.IsTrue(IC.eq(tree.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
509
      Assert.IsTrue(IC.eq(tree.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
510
      Assert.IsTrue(IC.eq(tree.RangeFrom(1).Backwards(), lla));
511
      Assert.IsTrue(IC.eq(tree.RangeFrom(0).Backwards(), lla));
512
      Assert.IsTrue(IC.eq(tree.RangeFrom(21).Backwards(), new int[] { }));
513
      Assert.IsTrue(IC.eq(tree.RangeFrom(20).Backwards(), new int[] { 20 }));
514
      Assert.IsTrue(IC.eq(tree.RangeTo(8).Backwards(), new int[] { 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
515
      Assert.IsTrue(IC.eq(tree.RangeTo(7).Backwards(), new int[] { 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
516
      Assert.IsTrue(IC.eq(tree.RangeTo(1).Backwards(), new int[] { }));
517
      Assert.IsTrue(IC.eq(tree.RangeTo(0).Backwards(), new int[] { }));
518
      Assert.IsTrue(IC.eq(tree.RangeTo(3).Backwards(), new int[] { 2, 2, 1 }));
519
      Assert.IsTrue(IC.eq(tree.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
520
      Assert.IsTrue(IC.eq(tree.RangeTo(21).Backwards(), lla));
521
      Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12).Backwards(), new int[] { 10, 10, 9, 8, 8, 7 }));
522
      Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11).Backwards(), new int[] { 10, 10, 9, 8, 8, 7, 6, 6 }));
523
      Assert.IsTrue(IC.eq(tree.RangeFromTo(0, 12).Backwards(), new int[] { 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
524
      Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12).Backwards(), new int[] { 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
525
      Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6 }));
526
      Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6 }));
527
    }
528

    
529

    
530
    [Test]
531
    public void Direction()
532
    {
533
      Assert.AreEqual(EnumerationDirection.Forwards, tree.Direction);
534
      Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFrom(20).Direction);
535
      Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeTo(7).Direction);
536
      Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFromTo(1, 12).Direction);
537
      Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeAll().Direction);
538
      Assert.AreEqual(EnumerationDirection.Backwards, tree.Backwards().Direction);
539
      Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFrom(20).Backwards().Direction);
540
      Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeTo(7).Backwards().Direction);
541
      Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFromTo(1, 12).Backwards().Direction);
542
      Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeAll().Backwards().Direction);
543
    }
544

    
545

    
546
    [TearDown]
547
    public void Dispose()
548
    {
549
      tree = null;
550
      c = null;
551
    }
552
  }
553

    
554

    
555

    
556
  [TestFixture]
557
  public class BagItf
558
  {
559
    private TreeBag<int> tree;
560

    
561

    
562
    [SetUp]
563
    public void Init()
564
    {
565
      tree = new TreeBag<int>(new IC());
566
      for (int i = 10; i < 20; i++)
567
      {
568
        tree.Add(i);
569
        tree.Add(i + 5);
570
      }
571
    }
572

    
573

    
574
    [Test]
575
    public void Both()
576
    {
577
      Assert.AreEqual(0, tree.ContainsCount(7));
578
      Assert.AreEqual(1, tree.ContainsCount(10));
579
      Assert.AreEqual(2, tree.ContainsCount(17));
580
      tree.RemoveAllCopies(17);
581
      Assert.AreEqual(0, tree.ContainsCount(17));
582
      tree.RemoveAllCopies(7);
583
    }
584

    
585

    
586
    [TearDown]
587
    public void Dispose()
588
    {
589
      tree = null;
590
    }
591
  }
592

    
593

    
594

    
595
  [TestFixture]
596
  public class Div
597
  {
598
    private TreeBag<int> tree;
599

    
600

    
601
    [SetUp]
602
    public void Init()
603
    {
604
      tree = new TreeBag<int>(new IC());
605
    }
606

    
607

    
608
    private void loadup()
609
    {
610
      for (int i = 10; i < 20; i++)
611
      {
612
        tree.Add(i);
613
        tree.Add(i + 5);
614
      }
615
    }
616

    
617
    [Test]
618
    [ExpectedException(typeof(NullReferenceException))]
619
    public void NullEqualityComparerinConstructor1()
620
    {
621
      new TreeBag<int>(null);
622
    }
623

    
624
    [Test]
625
    [ExpectedException(typeof(NullReferenceException))]
626
    public void NullEqualityComparerinConstructor3()
627
    {
628
      new TreeBag<int>(null, EqualityComparer<int>.Default);
629
    }
630

    
631
    [Test]
632
    [ExpectedException(typeof(NullReferenceException))]
633
    public void NullEqualityComparerinConstructor4()
634
    {
635
      new TreeBag<int>(Comparer<int>.Default, null);
636
    }
637

    
638
    [Test]
639
    [ExpectedException(typeof(NullReferenceException))]
640
    public void NullEqualityComparerinConstructor5()
641
    {
642
      new TreeBag<int>(null, null);
643
    }
644

    
645
    [Test]
646
    public void Choose()
647
    {
648
      tree.Add(7);
649
      Assert.AreEqual(7, tree.Choose());
650
    }
651

    
652
    [Test]
653
    [ExpectedException(typeof(NoSuchItemException))]
654
    public void BadChoose()
655
    {
656
      tree.Choose();
657
    }
658

    
659

    
660
    [Test]
661
    public void NoDuplicates()
662
    {
663
      Assert.IsTrue(tree.AllowsDuplicates);
664
      loadup();
665
      Assert.IsTrue(tree.AllowsDuplicates);
666
    }
667

    
668

    
669
    [Test]
670
    public void Add()
671
    {
672
      Assert.IsTrue(tree.Add(17));
673
      Assert.IsTrue(tree.Add(17));
674
      Assert.IsTrue(tree.Add(18));
675
      Assert.IsTrue(tree.Add(18));
676
      Assert.AreEqual(4, tree.Count);
677
      Assert.IsTrue(IC.eq(tree, 17, 17, 18, 18));
678
    }
679

    
680

    
681
    [TearDown]
682
    public void Dispose()
683
    {
684
      tree = null;
685
    }
686
  }
687

    
688
  [TestFixture]
689
  public class FindPredicate
690
  {
691
    private TreeBag<int> list;
692
    Fun<int, bool> pred;
693

    
694
    [SetUp]
695
    public void Init()
696
    {
697
      list = new TreeBag<int>(TenEqualityComparer.Default);
698
      pred = delegate(int i) { return i % 5 == 0; };
699
    }
700

    
701
    [TearDown]
702
    public void Dispose() { list = null; }
703

    
704
    [Test]
705
    public void Find()
706
    {
707
      int i;
708
      Assert.IsFalse(list.Find(pred, out i));
709
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
710
      Assert.IsFalse(list.Find(pred, out i));
711
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
712
      Assert.IsTrue(list.Find(pred, out i));
713
      Assert.AreEqual(45, i);
714
    }
715

    
716
    [Test]
717
    public void FindLast()
718
    {
719
      int i;
720
      Assert.IsFalse(list.FindLast(pred, out i));
721
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
722
      Assert.IsFalse(list.FindLast(pred, out i));
723
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
724
      Assert.IsTrue(list.FindLast(pred, out i));
725
      Assert.AreEqual(675, i);
726
    }
727

    
728
    [Test]
729
    public void FindIndex()
730
    {
731
      Assert.IsFalse(0 <= list.FindIndex(pred));
732
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
733
      Assert.IsFalse(0 <= list.FindIndex(pred));
734
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
735
      Assert.AreEqual(3, list.FindIndex(pred));
736
    }
737

    
738
    [Test]
739
    public void FindLastIndex()
740
    {
741
      Assert.IsFalse(0 <= list.FindLastIndex(pred));
742
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
743
      Assert.IsFalse(0 <= list.FindLastIndex(pred));
744
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
745
      Assert.AreEqual(7, list.FindLastIndex(pred));
746
    }
747
  }
748

    
749
  [TestFixture]
750
  public class UniqueItems
751
  {
752
    private TreeBag<int> list;
753

    
754
    [SetUp]
755
    public void Init() { list = new TreeBag<int>(); }
756

    
757
    [TearDown]
758
    public void Dispose() { list = null; }
759

    
760
    [Test]
761
    public void Test()
762
    {
763
      Assert.IsTrue(IC.seteq(list.UniqueItems()));
764
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
765
      list.AddAll<int>(new int[] { 7, 9, 7 });
766
      Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
767
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 2, 9, 1));
768
    }
769
  }
770

    
771
  [TestFixture]
772
  public class ArrayTest
773
  {
774
    private TreeBag<int> tree;
775

    
776
    int[] a;
777

    
778

    
779
    [SetUp]
780
    public void Init()
781
    {
782
      tree = new TreeBag<int>(new IC());
783
      a = new int[10];
784
      for (int i = 0; i < 10; i++)
785
        a[i] = 1000 + i;
786
    }
787

    
788

    
789
    [TearDown]
790
    public void Dispose() { tree = null; }
791

    
792

    
793
    private string aeq(int[] a, params int[] b)
794
    {
795
      if (a.Length != b.Length)
796
        return "Lengths differ: " + a.Length + " != " + b.Length;
797

    
798
      for (int i = 0; i < a.Length; i++)
799
        if (a[i] != b[i])
800
          return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
801

    
802
      return "Alles klar";
803
    }
804

    
805

    
806
    [Test]
807
    public void ToArray()
808
    {
809
      Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
810
      tree.Add(4);
811
      tree.Add(7);
812
      tree.Add(4);
813
      Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 4, 7));
814
    }
815

    
816

    
817
    [Test]
818
    public void CopyTo()
819
    {
820
      tree.CopyTo(a, 1);
821
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
822
      tree.Add(6);
823
      tree.Add(6);
824
      tree.CopyTo(a, 2);
825
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 6, 1004, 1005, 1006, 1007, 1008, 1009));
826
      tree.Add(4);
827
      tree.Add(9);
828
      tree.CopyTo(a, 4);
829
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 6, 4, 6, 6, 9, 1008, 1009));
830
      tree.Clear();
831
      tree.Add(7);
832
      tree.CopyTo(a, 9);
833
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 6, 4, 6, 6, 9, 1008, 7));
834
    }
835

    
836

    
837
    [Test]
838
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
839
    public void CopyToBad()
840
    {
841
      tree.CopyTo(a, 11);
842
    }
843

    
844

    
845
    [Test]
846
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
847
    public void CopyToBad2()
848
    {
849
      tree.CopyTo(a, -1);
850
    }
851

    
852

    
853
    [Test]
854
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
855
    public void CopyToTooFar()
856
    {
857
      tree.Add(3);
858
      tree.Add(4);
859
      tree.CopyTo(a, 9);
860
    }
861
  }
862

    
863

    
864

    
865
  [TestFixture]
866
  public class Remove
867
  {
868
    private TreeBag<int> tree;
869

    
870

    
871
    [SetUp]
872
    public void Init()
873
    {
874
      tree = new TreeBag<int>(new IC());
875
      for (int i = 10; i < 20; i++)
876
      {
877
        tree.Add(i);
878
        tree.Add(i + 5);
879
      }
880
      //10,11,12,13,14,15,15,16,16,17,17,18,18,19,19,20,21,22,23,24
881
    }
882

    
883

    
884
    [Test]
885
    public void SmallTrees()
886
    {
887
      tree.Clear();
888
      tree.Add(9);
889
      tree.Add(7);
890
      tree.Add(9);
891
      Assert.IsTrue(tree.Remove(7));
892
      Assert.IsTrue(tree.Check(""));
893
    }
894

    
895

    
896
    [Test]
897
    public void ByIndex()
898
    {
899
      //Remove root!
900
      int n = tree.Count;
901
      int i = tree[10];
902

    
903
      Assert.AreEqual(17, tree.RemoveAt(10));
904
      Assert.IsTrue(tree.Check(""));
905
      Assert.IsTrue(tree.Contains(i));
906
      Assert.AreEqual(n - 1, tree.Count);
907
      Assert.AreEqual(17, tree.RemoveAt(9));
908
      Assert.IsTrue(tree.Check(""));
909
      Assert.IsFalse(tree.Contains(i));
910
      Assert.AreEqual(n - 2, tree.Count);
911

    
912
      //Low end
913
      i = tree.FindMin();
914
      tree.RemoveAt(0);
915
      Assert.IsTrue(tree.Check(""));
916
      Assert.IsFalse(tree.Contains(i));
917
      Assert.AreEqual(n - 3, tree.Count);
918

    
919
      //high end
920
      i = tree.FindMax();
921
      tree.RemoveAt(tree.Count - 1);
922
      Assert.IsTrue(tree.Check(""));
923
      Assert.IsFalse(tree.Contains(i));
924
      Assert.AreEqual(n - 4, tree.Count);
925

    
926
      //Some leaf
927
      //tree.dump();
928
      i = 18;
929
      Assert.AreEqual(i, tree.RemoveAt(9));
930
      Assert.IsTrue(tree.Check(""));
931
      Assert.AreEqual(i, tree.RemoveAt(8));
932
      Assert.IsTrue(tree.Check(""));
933
      Assert.IsFalse(tree.Contains(i));
934
      Assert.AreEqual(n - 6, tree.Count);
935
    }
936

    
937

    
938
    [Test]
939
    public void AlmostEmpty()
940
    {
941
      //Almost empty
942
      tree.Clear();
943
      tree.Add(3);
944
      tree.RemoveAt(0);
945
      Assert.IsTrue(tree.Check(""));
946
      Assert.IsFalse(tree.Contains(3));
947
      Assert.AreEqual(0, tree.Count);
948
    }
949

    
950

    
951
    [Test]
952
    [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
953
    public void Empty()
954
    {
955
      tree.Clear();
956
      tree.RemoveAt(0);
957
    }
958

    
959

    
960
    [Test]
961
    [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
962
    public void HighIndex()
963
    {
964
      tree.RemoveAt(tree.Count);
965
    }
966

    
967

    
968
    [Test]
969
    [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
970
    public void LowIndex()
971
    {
972
      tree.RemoveAt(-1);
973
    }
974

    
975

    
976
    [Test]
977
    public void Normal()
978
    {
979
      //Note: ids does not match for bag
980
      Assert.IsFalse(tree.Remove(-20));
981

    
982
      //1b
983
      Assert.IsTrue(tree.Remove(20));
984
      Assert.IsTrue(tree.Check("T1"));
985
      Assert.IsFalse(tree.Remove(20));
986

    
987
      //1b
988
      Assert.IsTrue(tree.Remove(10));
989
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
990

    
991
      //case 1c
992
      Assert.IsTrue(tree.Remove(24));
993
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
994

    
995
      //1a (terminating)
996
      Assert.IsTrue(tree.Remove(16));
997
      Assert.IsTrue(tree.Remove(16));
998
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
999

    
1000
      //2
1001
      Assert.IsTrue(tree.Remove(18));
1002
      Assert.IsTrue(tree.Remove(17));
1003
      Assert.IsTrue(tree.Remove(18));
1004
      Assert.IsTrue(tree.Remove(17));
1005
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1006

    
1007
      //2+1b
1008
      Assert.IsTrue(tree.Remove(15));
1009
      Assert.IsTrue(tree.Remove(15));
1010
      for (int i = 0; i < 5; i++) tree.Add(17 + i);
1011
      Assert.IsTrue(tree.Remove(23));
1012
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1013

    
1014
      //1a+1b
1015
      Assert.IsTrue(tree.Remove(11));
1016
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1017

    
1018
      //2+1c
1019
      for (int i = 0; i < 10; i++)
1020
        tree.Add(50 - 2 * i);
1021

    
1022
      Assert.IsTrue(tree.Remove(42));
1023
      Assert.IsTrue(tree.Remove(38));
1024
      Assert.IsTrue(tree.Remove(22));
1025
      Assert.IsTrue(tree.Remove(40));
1026

    
1027
      //
1028
      for (int i = 0; i < 48; i++)
1029
        tree.Remove(i);
1030

    
1031
      //Almost empty tree:*
1032
      Assert.IsFalse(tree.Remove(26));
1033
      Assert.IsTrue(tree.Remove(48));
1034
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1035

    
1036
      //Empty tree:*
1037
      Assert.IsFalse(tree.Remove(26));
1038
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1039
    }
1040

    
1041

    
1042
    [TearDown]
1043
    public void Dispose()
1044
    {
1045
      tree = null;
1046
    }
1047
  }
1048

    
1049

    
1050

    
1051
  [TestFixture]
1052
  public class PredecessorStructure
1053
  {
1054
    private TreeBag<int> tree;
1055

    
1056

    
1057
    [SetUp]
1058
    public void Init()
1059
    {
1060
      tree = new TreeBag<int>(new IC());
1061
    }
1062

    
1063

    
1064
    private void loadup()
1065
    {
1066
      for (int i = 0; i < 20; i++)
1067
        tree.Add(2 * i);
1068
      for (int i = 0; i < 10; i++)
1069
        tree.Add(4 * i);
1070
    }
1071

    
1072

    
1073
    [Test]
1074
    public void Predecessor()
1075
    {
1076
      loadup();
1077
      Assert.AreEqual(6, tree.Predecessor(7));
1078
      Assert.AreEqual(6, tree.Predecessor(8));
1079

    
1080
      //The bottom
1081
      Assert.AreEqual(0, tree.Predecessor(1));
1082

    
1083
      //The top
1084
      Assert.AreEqual(38, tree.Predecessor(39));
1085
    }
1086

    
1087

    
1088
    [Test]
1089
    [ExpectedException(typeof(NoSuchItemException))]
1090
    public void PredecessorTooLow1()
1091
    {
1092
      tree.Predecessor(-2);
1093
    }
1094

    
1095

    
1096
    [Test]
1097
    [ExpectedException(typeof(NoSuchItemException))]
1098
    public void PredecessorTooLow2()
1099
    {
1100
      tree.Predecessor(0);
1101
    }
1102

    
1103

    
1104
    [Test]
1105
    public void WeakPredecessor()
1106
    {
1107
      loadup();
1108
      Assert.AreEqual(6, tree.WeakPredecessor(7));
1109
      Assert.AreEqual(8, tree.WeakPredecessor(8));
1110

    
1111
      //The bottom
1112
      Assert.AreEqual(0, tree.WeakPredecessor(1));
1113
      Assert.AreEqual(0, tree.WeakPredecessor(0));
1114

    
1115
      //The top
1116
      Assert.AreEqual(38, tree.WeakPredecessor(39));
1117
      Assert.AreEqual(38, tree.WeakPredecessor(38));
1118
    }
1119

    
1120

    
1121
    [Test]
1122
    [ExpectedException(typeof(NoSuchItemException))]
1123
    public void WeakPredecessorTooLow1()
1124
    {
1125
      tree.WeakPredecessor(-2);
1126
    }
1127

    
1128

    
1129
    [Test]
1130
    public void Successor()
1131
    {
1132
      loadup();
1133
      Assert.AreEqual(8, tree.Successor(7));
1134
      Assert.AreEqual(10, tree.Successor(8));
1135

    
1136
      //The bottom
1137
      Assert.AreEqual(2, tree.Successor(0));
1138
      Assert.AreEqual(0, tree.Successor(-1));
1139

    
1140
      //The top
1141
      Assert.AreEqual(38, tree.Successor(37));
1142
    }
1143

    
1144

    
1145
    [Test]
1146
    [ExpectedException(typeof(NoSuchItemException))]
1147
    public void SuccessorTooHigh1()
1148
    {
1149
      tree.Successor(38);
1150
    }
1151

    
1152

    
1153
    [Test]
1154
    [ExpectedException(typeof(NoSuchItemException))]
1155
    public void SuccessorTooHigh2()
1156
    {
1157
      tree.Successor(39);
1158
    }
1159

    
1160

    
1161
    [Test]
1162
    public void WeakSuccessor()
1163
    {
1164
      loadup();
1165
      Assert.AreEqual(6, tree.WeakSuccessor(6));
1166
      Assert.AreEqual(8, tree.WeakSuccessor(7));
1167

    
1168
      //The bottom
1169
      Assert.AreEqual(0, tree.WeakSuccessor(-1));
1170
      Assert.AreEqual(0, tree.WeakSuccessor(0));
1171

    
1172
      //The top
1173
      Assert.AreEqual(38, tree.WeakSuccessor(37));
1174
      Assert.AreEqual(38, tree.WeakSuccessor(38));
1175
    }
1176

    
1177

    
1178
    [Test]
1179
    [ExpectedException(typeof(NoSuchItemException))]
1180
    public void WeakSuccessorTooHigh1()
1181
    {
1182
      tree.WeakSuccessor(39);
1183
    }
1184

    
1185

    
1186
    [TearDown]
1187
    public void Dispose()
1188
    {
1189
      tree = null;
1190
    }
1191
  }
1192

    
1193

    
1194

    
1195
  [TestFixture]
1196
  public class PriorityQueue
1197
  {
1198
    private TreeBag<int> tree;
1199

    
1200

    
1201
    [SetUp]
1202
    public void Init()
1203
    {
1204
      tree = new TreeBag<int>(new IC());
1205
    }
1206

    
1207

    
1208
    private void loadup()
1209
    {
1210
      foreach (int i in new int[] { 1, 2, 3, 4 })
1211
        tree.Add(i);
1212
      tree.Add(1);
1213
      tree.Add(3);
1214
    }
1215

    
1216

    
1217
    [Test]
1218
    public void Normal()
1219
    {
1220
      loadup();
1221
      Assert.AreEqual(1, tree.FindMin());
1222
      Assert.AreEqual(4, tree.FindMax());
1223
      Assert.AreEqual(1, tree.DeleteMin());
1224
      Assert.AreEqual(4, tree.DeleteMax());
1225
      Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1226
      Assert.AreEqual(1, tree.FindMin());
1227
      Assert.AreEqual(3, tree.FindMax());
1228
      Assert.AreEqual(1, tree.DeleteMin());
1229
      Assert.AreEqual(3, tree.DeleteMax());
1230
      Assert.IsTrue(tree.Check("Normal test 2"), "Bad tree");
1231
    }
1232

    
1233

    
1234
    [Test]
1235
    [ExpectedException(typeof(NoSuchItemException))]
1236
    public void Empty1()
1237
    {
1238
      tree.FindMin();
1239
    }
1240

    
1241

    
1242
    [Test]
1243
    [ExpectedException(typeof(NoSuchItemException))]
1244
    public void Empty2()
1245
    {
1246
      tree.FindMax();
1247
    }
1248

    
1249

    
1250
    [Test]
1251
    [ExpectedException(typeof(NoSuchItemException))]
1252
    public void Empty3()
1253
    {
1254
      tree.DeleteMin();
1255
    }
1256

    
1257

    
1258
    [Test]
1259
    [ExpectedException(typeof(NoSuchItemException))]
1260
    public void Empty4()
1261
    {
1262
      tree.DeleteMax();
1263
    }
1264

    
1265

    
1266
    [TearDown]
1267
    public void Dispose()
1268
    {
1269
      tree = null;
1270
    }
1271
  }
1272

    
1273

    
1274

    
1275
  [TestFixture]
1276
  public class IndexingAndCounting
1277
  {
1278
    private TreeBag<int> tree;
1279

    
1280

    
1281
    [SetUp]
1282
    public void Init()
1283
    {
1284
      tree = new TreeBag<int>(new IC());
1285
    }
1286

    
1287

    
1288
    private void populate()
1289
    {
1290
      tree.Add(30);
1291
      tree.Add(30);
1292
      tree.Add(50);
1293
      tree.Add(10);
1294
      tree.Add(70);
1295
      tree.Add(70);
1296
    }
1297

    
1298

    
1299
    [Test]
1300
    public void ToArray()
1301
    {
1302
      populate();
1303

    
1304
      int[] a = tree.ToArray();
1305

    
1306
      Assert.AreEqual(6, a.Length);
1307
      Assert.AreEqual(10, a[0]);
1308
      Assert.AreEqual(30, a[1]);
1309
      Assert.AreEqual(30, a[2]);
1310
      Assert.AreEqual(50, a[3]);
1311
      Assert.AreEqual(70, a[4]);
1312
      Assert.AreEqual(70, a[5]);
1313
    }
1314

    
1315

    
1316
    [Test]
1317
    public void GoodIndex()
1318
    {
1319
      Assert.AreEqual(-1, tree.IndexOf(20));
1320
      Assert.AreEqual(-1, tree.LastIndexOf(20));
1321
      populate();
1322
      Assert.AreEqual(10, tree[0]);
1323
      Assert.AreEqual(30, tree[1]);
1324
      Assert.AreEqual(30, tree[2]);
1325
      Assert.AreEqual(50, tree[3]);
1326
      Assert.AreEqual(70, tree[4]);
1327
      Assert.AreEqual(70, tree[5]);
1328
      Assert.AreEqual(0, tree.IndexOf(10));
1329
      Assert.AreEqual(1, tree.IndexOf(30));
1330
      Assert.AreEqual(3, tree.IndexOf(50));
1331
      Assert.AreEqual(4, tree.IndexOf(70));
1332
      Assert.AreEqual(~1, tree.IndexOf(20));
1333
      Assert.AreEqual(~0, tree.IndexOf(0));
1334
      Assert.AreEqual(~6, tree.IndexOf(90));
1335
      Assert.AreEqual(0, tree.LastIndexOf(10));
1336
      Assert.AreEqual(2, tree.LastIndexOf(30));
1337
      Assert.AreEqual(3, tree.LastIndexOf(50));
1338
      Assert.AreEqual(5, tree.LastIndexOf(70));
1339
      Assert.AreEqual(~1, tree.LastIndexOf(20));
1340
      Assert.AreEqual(~0, tree.LastIndexOf(0));
1341
      Assert.AreEqual(~6, tree.LastIndexOf(90));
1342
    }
1343

    
1344

    
1345
    [Test]
1346
    [ExpectedException(typeof(IndexOutOfRangeException))]
1347
    public void IndexTooLarge()
1348
    {
1349
      populate();
1350
      Console.WriteLine(tree[6]);
1351
    }
1352

    
1353

    
1354
    [Test]
1355
    [ExpectedException(typeof(IndexOutOfRangeException))]
1356
    public void IndexTooSmall()
1357
    {
1358
      populate();
1359
      Console.WriteLine(tree[-1]);
1360
    }
1361

    
1362

    
1363
    [Test]
1364
    public void FilledTreeOutsideInput()
1365
    {
1366
      populate();
1367
      Assert.AreEqual(0, tree.CountFrom(90));
1368
      Assert.AreEqual(0, tree.CountFromTo(-20, 0));
1369
      Assert.AreEqual(0, tree.CountFromTo(80, 100));
1370
      Assert.AreEqual(0, tree.CountTo(0));
1371
      Assert.AreEqual(6, tree.CountTo(90));
1372
      Assert.AreEqual(6, tree.CountFromTo(-20, 90));
1373
      Assert.AreEqual(6, tree.CountFrom(0));
1374
    }
1375

    
1376

    
1377
    [Test]
1378
    public void FilledTreeIntermediateInput()
1379
    {
1380
      populate();
1381
      Assert.AreEqual(5, tree.CountFrom(20));
1382
      Assert.AreEqual(2, tree.CountFromTo(20, 40));
1383
      Assert.AreEqual(3, tree.CountTo(40));
1384
    }
1385

    
1386

    
1387
    [Test]
1388
    public void FilledTreeMatchingInput()
1389
    {
1390
      populate();
1391
      Assert.AreEqual(5, tree.CountFrom(30));
1392
      Assert.AreEqual(3, tree.CountFromTo(30, 70));
1393
      Assert.AreEqual(0, tree.CountFromTo(50, 30));
1394
      Assert.AreEqual(0, tree.CountFromTo(50, 50));
1395
      Assert.AreEqual(0, tree.CountTo(10));
1396
      Assert.AreEqual(3, tree.CountTo(50));
1397
    }
1398

    
1399

    
1400
    [Test]
1401
    public void CountEmptyTree()
1402
    {
1403
      Assert.AreEqual(0, tree.CountFrom(20));
1404
      Assert.AreEqual(0, tree.CountFromTo(20, 40));
1405
      Assert.AreEqual(0, tree.CountTo(40));
1406
    }
1407

    
1408

    
1409
    [TearDown]
1410
    public void Dispose()
1411
    {
1412
      tree = null;
1413
    }
1414
  }
1415

    
1416

    
1417

    
1418

    
1419
  namespace ModificationCheck
1420
  {
1421
    [TestFixture]
1422
    public class Enumerator
1423
    {
1424
      private TreeBag<int> tree;
1425

    
1426
      private SCG.IEnumerator<int> e;
1427

    
1428

    
1429
      [SetUp]
1430
      public void Init()
1431
      {
1432
        tree = new TreeBag<int>(new IC());
1433
        for (int i = 0; i < 10; i++)
1434
          tree.Add(i);
1435
        tree.Add(3);
1436
        tree.Add(7);
1437
        e = tree.GetEnumerator();
1438
      }
1439

    
1440

    
1441
      [Test]
1442
      public void CurrentAfterModification()
1443
      {
1444
        e.MoveNext();
1445
        tree.Add(34);
1446
        Assert.AreEqual(0, e.Current);
1447
      }
1448

    
1449

    
1450
      [Test]
1451
      [ExpectedException(typeof(CollectionModifiedException))]
1452
      public void MoveNextAfterAdd()
1453
      {
1454
        tree.Add(34);
1455
        e.MoveNext();
1456
      }
1457

    
1458

    
1459
      [Test]
1460
      [ExpectedException(typeof(CollectionModifiedException))]
1461
      public void MoveNextAfterRemove()
1462
      {
1463
        tree.Remove(34);
1464
        e.MoveNext();
1465
      }
1466

    
1467

    
1468
      [Test]
1469
      [ExpectedException(typeof(CollectionModifiedException))]
1470
      public void MoveNextAfterClear()
1471
      {
1472
        tree.Clear();
1473
        e.MoveNext();
1474
      }
1475

    
1476

    
1477
      [TearDown]
1478
      public void Dispose()
1479
      {
1480
        tree = null;
1481
        e = null;
1482
      }
1483
    }
1484

    
1485

    
1486

    
1487
    [TestFixture]
1488
    public class RangeEnumerator
1489
    {
1490
      private TreeBag<int> tree;
1491

    
1492
      private SCG.IEnumerator<int> e;
1493

    
1494

    
1495
      [SetUp]
1496
      public void Init()
1497
      {
1498
        tree = new TreeBag<int>(new IC());
1499
        for (int i = 0; i < 10; i++)
1500
          tree.Add(i);
1501

    
1502
        e = tree.RangeFromTo(3, 7).GetEnumerator();
1503
      }
1504

    
1505

    
1506
      [Test]
1507
      public void CurrentAfterModification()
1508
      {
1509
        e.MoveNext();
1510
        tree.Add(34);
1511
        Assert.AreEqual(3, e.Current);
1512
      }
1513

    
1514

    
1515
      [Test]
1516
      [ExpectedException(typeof(CollectionModifiedException))]
1517
      public void MoveNextAfterAdd()
1518
      {
1519
        tree.Add(34);
1520
        e.MoveNext();
1521
      }
1522

    
1523

    
1524
      [Test]
1525
      [ExpectedException(typeof(CollectionModifiedException))]
1526
      public void MoveNextAfterRemove()
1527
      {
1528
        tree.Remove(34);
1529
        e.MoveNext();
1530
      }
1531

    
1532

    
1533
      [Test]
1534
      [ExpectedException(typeof(CollectionModifiedException))]
1535
      public void MoveNextAfterClear()
1536
      {
1537
        tree.Clear();
1538
        e.MoveNext();
1539
      }
1540

    
1541

    
1542
      [TearDown]
1543
      public void Dispose()
1544
      {
1545
        tree = null;
1546
        e = null;
1547
      }
1548
    }
1549
  }
1550

    
1551

    
1552

    
1553

    
1554
  namespace PathcopyPersistence
1555
  {
1556
    [TestFixture]
1557
    public class Navigation
1558
    {
1559
      private TreeBag<int> tree, snap;
1560

    
1561
      private SCG.IComparer<int> ic;
1562

    
1563

    
1564
      [SetUp]
1565
      public void Init()
1566
      {
1567
        ic = new IC();
1568
        tree = new TreeBag<int>(ic);
1569
        for (int i = 0; i <= 20; i++)
1570
          tree.Add(2 * i + 1);
1571
        tree.Add(13);
1572
        snap = (TreeBag<int>)tree.Snapshot();
1573
        for (int i = 0; i <= 10; i++)
1574
          tree.Remove(4 * i + 1);
1575
      }
1576

    
1577

    
1578
      private bool twomodeleven(int i)
1579
      {
1580
        return i % 11 == 2;
1581
      }
1582

    
1583

    
1584
      [Test]
1585
      public void InternalEnum()
1586
      {
1587
        Assert.IsTrue(IC.eq(snap.FindAll(new Fun<int, bool>(twomodeleven)), 13, 13, 35));
1588
      }
1589

    
1590

    
1591
      public void MoreCut()
1592
      {
1593
        //TODO: Assert.Fail("more tests of Cut needed");
1594
      }
1595

    
1596

    
1597
      [Test]
1598
      public void Cut()
1599
      {
1600
        int lo, hi;
1601
        bool lv, hv;
1602

    
1603
        Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(64), out lo, out lv, out hi, out hv));
1604
        Assert.IsTrue(lv && hv);
1605
        Assert.AreEqual(5, hi);
1606
        Assert.AreEqual(3, lo);
1607
        Assert.IsTrue(snap.Cut(new HigherOrder.CubeRoot(125), out lo, out lv, out hi, out hv));
1608
        Assert.IsTrue(lv && hv);
1609
        Assert.AreEqual(7, hi);
1610
        Assert.AreEqual(3, lo);
1611
        Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(125000), out lo, out lv, out hi, out hv));
1612
        Assert.IsTrue(lv && !hv);
1613
        Assert.AreEqual(41, lo);
1614
        Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(-27), out lo, out lv, out hi, out hv));
1615
        Assert.IsTrue(!lv && hv);
1616
        Assert.AreEqual(1, hi);
1617
      }
1618

    
1619

    
1620
      [Test]
1621
      public void Range()
1622
      {
1623
        Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 16), 5, 7, 9, 11, 13, 13, 15));
1624
        Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 17), 5, 7, 9, 11, 13, 13, 15));
1625
        Assert.IsTrue(IC.eq(snap.RangeFromTo(6, 16), 7, 9, 11, 13, 13, 15));
1626
      }
1627

    
1628

    
1629
      [Test]
1630
      public void Contains()
1631
      {
1632
        Assert.IsTrue(snap.Contains(5));
1633
        Assert.IsTrue(snap.Contains(13));
1634
        Assert.AreEqual(1, snap.ContainsCount(5));
1635
        Assert.AreEqual(2, snap.ContainsCount(13));
1636
      }
1637

    
1638

    
1639
      [Test]
1640
      public void FindMin()
1641
      {
1642
        Assert.AreEqual(1, snap.FindMin());
1643
      }
1644

    
1645

    
1646
      [Test]
1647
      public void FindMax()
1648
      {
1649
        Assert.AreEqual(41, snap.FindMax());
1650
      }
1651

    
1652

    
1653
      [Test]
1654
      public void Predecessor()
1655
      {
1656
        Assert.AreEqual(13, snap.Predecessor(15));
1657
        Assert.AreEqual(15, snap.Predecessor(16));
1658
        Assert.AreEqual(15, snap.Predecessor(17));
1659
        Assert.AreEqual(17, snap.Predecessor(18));
1660
      }
1661

    
1662

    
1663
      [Test]
1664
      public void Successor()
1665
      {
1666
        Assert.AreEqual(17, snap.Successor(15));
1667
        Assert.AreEqual(17, snap.Successor(16));
1668
        Assert.AreEqual(19, snap.Successor(17));
1669
        Assert.AreEqual(19, snap.Successor(18));
1670
      }
1671

    
1672

    
1673
      [Test]
1674
      public void WeakPredecessor()
1675
      {
1676
        Assert.AreEqual(15, snap.WeakPredecessor(15));
1677
        Assert.AreEqual(15, snap.WeakPredecessor(16));
1678
        Assert.AreEqual(17, snap.WeakPredecessor(17));
1679
        Assert.AreEqual(17, snap.WeakPredecessor(18));
1680
      }
1681

    
1682

    
1683
      [Test]
1684
      public void WeakSuccessor()
1685
      {
1686
        Assert.AreEqual(15, snap.WeakSuccessor(15));
1687
        Assert.AreEqual(17, snap.WeakSuccessor(16));
1688
        Assert.AreEqual(17, snap.WeakSuccessor(17));
1689
        Assert.AreEqual(19, snap.WeakSuccessor(18));
1690
      }
1691

    
1692

    
1693
      [Test]
1694
      [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
1695
      public void CountTo()
1696
      {
1697
        int j = snap.CountTo(15);
1698
      }
1699

    
1700

    
1701
      [Test]
1702
      [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
1703
      public void Indexing()
1704
      {
1705
        int j = snap[4];
1706
      }
1707

    
1708

    
1709
      [Test]
1710
      [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
1711
      public void Indexing2()
1712
      {
1713
        int j = snap.IndexOf(5);
1714
      }
1715

    
1716

    
1717
      [TearDown]
1718
      public void Dispose()
1719
      {
1720
        tree = null;
1721
        ic = null;
1722
      }
1723
    }
1724

    
1725

    
1726

    
1727
    [TestFixture]
1728
    public class Single
1729
    {
1730
      private TreeBag<int> tree;
1731

    
1732
      private SCG.IComparer<int> ic;
1733

    
1734

    
1735
      [SetUp]
1736
      public void Init()
1737
      {
1738
        ic = new IC();
1739
        tree = new TreeBag<int>(ic);
1740
        for (int i = 0; i < 10; i++)
1741
          tree.Add(2 * i + 1);
1742
      }
1743

    
1744

    
1745
      [Test]
1746
      public void EnumerationWithAdd()
1747
      {
1748
        int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1749
        int i = 0;
1750
        TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
1751

    
1752
        foreach (int j in snap)
1753
        {
1754
          Assert.AreEqual(1 + 2 * i++, j);
1755
          tree.Add(21 - j);
1756
          Assert.IsTrue(snap.Check("M"), "Bad snap!");
1757
          Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1758
          Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1759
        }
1760
      }
1761

    
1762

    
1763
      [Test]
1764
      public void Remove()
1765
      {
1766
        int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1767
        TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
1768

    
1769
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1770
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1771
        tree.Remove(19);
1772
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1773
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1774
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1775
      }
1776

    
1777

    
1778
      [Test]
1779
      public void RemoveNormal()
1780
      {
1781
        tree.Clear();
1782
        for (int i = 10; i < 20; i++)
1783
        {
1784
          tree.Add(i);
1785
          tree.Add(i + 10);
1786
        }
1787
        tree.Add(15);
1788

    
1789
        int[] orig = new int[] { 10, 11, 12, 13, 14, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 };
1790
        TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
1791

    
1792
        Assert.IsFalse(tree.Remove(-20));
1793
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1794
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1795

    
1796

    
1797
        //decrease items case
1798
        Assert.IsTrue(tree.Remove(15));
1799
        Assert.IsTrue(tree.Check("T1"));
1800
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1801
        //snap.dump();
1802
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1803

    
1804
        //No demote case, with move_item
1805
        Assert.IsTrue(tree.Remove(20));
1806
        Assert.IsTrue(tree.Check("T1"));
1807
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1808
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1809
        Assert.IsFalse(tree.Remove(20));
1810

    
1811
        //plain case 2
1812
        tree.Snapshot();
1813
        Assert.IsTrue(tree.Remove(14));
1814
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1815
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1816
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1817

    
1818
        //case 1b
1819
        Assert.IsTrue(tree.Remove(25));
1820
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1821
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1822
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1823

    
1824
        //case 1c
1825
        Assert.IsTrue(tree.Remove(29));
1826
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1827
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1828
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1829

    
1830
        //1a (terminating)
1831
        Assert.IsTrue(tree.Remove(10));
1832
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1833
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1834
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1835

    
1836
        //2+1b
1837
        Assert.IsTrue(tree.Remove(12));
1838
        tree.Snapshot();
1839
        Assert.IsTrue(tree.Remove(11));
1840
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1841
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1842
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1843

    
1844
        //1a+1b
1845
        Assert.IsTrue(tree.Remove(18));
1846
        Assert.IsTrue(tree.Remove(13));
1847
        Assert.IsTrue(tree.Remove(15));
1848
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1849
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1850
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1851

    
1852
        //2+1c
1853
        for (int i = 0; i < 10; i++)
1854
          tree.Add(50 - 2 * i);
1855

    
1856
        Assert.IsTrue(tree.Remove(42));
1857
        Assert.IsTrue(tree.Remove(38));
1858
        Assert.IsTrue(tree.Remove(28));
1859
        Assert.IsTrue(tree.Remove(40));
1860
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1861
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1862
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1863

    
1864
        //
1865
        Assert.IsTrue(tree.Remove(16));
1866
        Assert.IsTrue(tree.Remove(23));
1867
        Assert.IsTrue(tree.Remove(17));
1868
        Assert.IsTrue(tree.Remove(19));
1869
        Assert.IsTrue(tree.Remove(50));
1870
        Assert.IsTrue(tree.Remove(26));
1871
        Assert.IsTrue(tree.Remove(21));
1872
        Assert.IsTrue(tree.Remove(22));
1873
        Assert.IsTrue(tree.Remove(24));
1874
        for (int i = 0; i < 48; i++)
1875
          tree.Remove(i);
1876

    
1877
        //Almost empty tree:
1878
        Assert.IsFalse(tree.Remove(26));
1879
        Assert.IsTrue(tree.Remove(48));
1880
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1881
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1882
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1883

    
1884
        //Empty tree:
1885
        Assert.IsFalse(tree.Remove(26));
1886
        Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1887
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1888
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1889
      }
1890

    
1891

    
1892
      [Test]
1893
      public void Add()
1894
      {
1895
        int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1896
        TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
1897

    
1898
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1899
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1900
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1901
        tree.Add(10);
1902
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1903
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1904
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1905
        tree.Add(16);
1906
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1907
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1908
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1909

    
1910
        tree.Add(9);
1911
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1912
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1913
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1914

    
1915
        //Promote+zigzig
1916
        tree.Add(40);
1917
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1918
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1919
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1920
        for (int i = 1; i < 4; i++)
1921
          tree.Add(40 - 2 * i);
1922

    
1923
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1924
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1925
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1926

    
1927
        //Zigzag:
1928
        tree.Add(32);
1929
        Assert.IsTrue(snap.Check("M"), "Bad snap!");
1930
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1931
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1932
      }
1933

    
1934

    
1935
      [Test]
1936
      public void Clear()
1937
      {
1938
        int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1939
        TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
1940

    
1941
        tree.Clear();
1942
        Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1943
        Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1944
        Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1945
        Assert.AreEqual(0, tree.Count);
1946
      }
1947

    
1948

    
1949
      [Test]
1950
      [ExpectedException(typeof(InvalidOperationException), "Cannot snapshot a snapshot")]
1951
      public void SnapSnap()
1952
      {
1953
        TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
1954

    
1955
        snap.Snapshot();
1956
      }
1957

    
1958

    
1959
      [TearDown]
1960
      public void Dispose()
1961
      {
1962
        tree = null;
1963
        ic = null;
1964
      }
1965
    }
1966

    
1967

    
1968

    
1969
    [TestFixture]
1970
    public class Multiple
1971
    {
1972
      private TreeBag<int> tree;
1973

    
1974
      private SCG.IComparer<int> ic;
1975

    
1976

    
1977
      private bool eq(SCG.IEnumerable<int> me, int[] that)
1978
      {
1979
        int i = 0, maxind = that.Length - 1;
1980

    
1981
        foreach (int item in me)
1982
          if (i > maxind || ic.Compare(item, that[i++]) != 0)
1983
            return false;
1984

    
1985
        return true;
1986
      }
1987

    
1988

    
1989
      [SetUp]
1990
      public void Init()
1991
      {
1992
        ic = new IC();
1993
        tree = new TreeBag<int>(ic);
1994
        for (int i = 0; i < 10; i++)
1995
          tree.Add(2 * i + 1);
1996
      }
1997

    
1998

    
1999
      [Test]
2000
      public void First()
2001
      {
2002
        TreeBag<int>[] snaps = new TreeBag<int>[10];
2003

    
2004
        for (int i = 0; i < 10; i++)
2005
        {
2006
          snaps[i] = (TreeBag<int>)(tree.Snapshot());
2007
          tree.Add(2 * i);
2008
        }
2009

    
2010
        for (int i = 0; i < 10; i++)
2011
        {
2012
          Assert.AreEqual(i + 10, snaps[i].Count);
2013
        }
2014

    
2015
        snaps[5] = null;
2016
        snaps[9] = null;
2017
        GC.Collect();
2018
        snaps[8].Dispose();
2019
        tree.Remove(14);
2020

    
2021
        int[] res = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19 };
2022
        int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
2023
        int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
2024

    
2025
        Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
2026
        Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
2027
        Assert.IsTrue(IC.eq(tree, res));
2028
        Assert.IsTrue(tree.Check("B"));
2029
        Assert.IsTrue(snaps[3].Check("B"));
2030
        Assert.IsTrue(snaps[7].Check("B"));
2031
      }
2032

    
2033

    
2034
      [Test]
2035
      public void CollectingTheMaster()
2036
      {
2037
        TreeBag<int>[] snaps = new TreeBag<int>[10];
2038

    
2039
        for (int i = 0; i < 10; i++)
2040
        {
2041
          snaps[i] = (TreeBag<int>)(tree.Snapshot());
2042
          tree.Add(2 * i);
2043
        }
2044

    
2045
        tree = null;
2046
        GC.Collect();
2047
        for (int i = 0; i < 10; i++)
2048
        {
2049
          Assert.AreEqual(i + 10, snaps[i].Count);
2050
        }
2051

    
2052
        snaps[5] = null;
2053
        snaps[9] = null;
2054
        GC.Collect();
2055
        snaps[8].Dispose();
2056

    
2057
        int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
2058
        int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
2059

    
2060
        Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
2061
        Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
2062
        Assert.IsTrue(snaps[3].Check("B"));
2063
        Assert.IsTrue(snaps[7].Check("B"));
2064
      }
2065

    
2066

    
2067
      [TearDown]
2068
      public void Dispose()
2069
      {
2070
        tree = null;
2071
        ic = null;
2072
      }
2073
    }
2074
  }
2075

    
2076

    
2077

    
2078

    
2079
  namespace HigherOrder
2080
  {
2081
    internal class CubeRoot : IComparable<int>
2082
    {
2083
      private int c;
2084

    
2085

    
2086
      internal CubeRoot(int c) { this.c = c; }
2087

    
2088

    
2089
      public int CompareTo(int that) { return c - that * that * that; }
2090
      public bool Equals(int that) { return c == that * that * that; }
2091
    }
2092

    
2093

    
2094

    
2095
    class Interval : IComparable<int>
2096
    {
2097
      private int b, t;
2098

    
2099

    
2100
      internal Interval(int b, int t) { this.b = b; this.t = t; }
2101

    
2102

    
2103
      public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
2104
      public bool Equals(int that) { return that >= b && that <= t; }
2105
    }
2106

    
2107

    
2108

    
2109
    [TestFixture]
2110
    public class Simple
2111
    {
2112
      private TreeBag<int> tree;
2113

    
2114
      private SCG.IComparer<int> ic;
2115

    
2116

    
2117
      [SetUp]
2118
      public void Init()
2119
      {
2120
        ic = new IC();
2121
        tree = new TreeBag<int>(ic);
2122
      }
2123

    
2124

    
2125
      private bool never(int i) { return false; }
2126

    
2127

    
2128
      private bool always(int i) { return true; }
2129

    
2130

    
2131
      private bool even(int i) { return i % 2 == 0; }
2132

    
2133

    
2134
      private string themap(int i) { return String.Format("AA {0,4} BB", i); }
2135

    
2136

    
2137
      private string badmap(int i) { return String.Format("AA {0} BB", i); }
2138

    
2139

    
2140
      private int appfield1;
2141

    
2142
      private int appfield2;
2143

    
2144

    
2145
      private void apply(int i) { appfield1++; appfield2 += i * i; }
2146

    
2147

    
2148
      [Test]
2149
      public void Apply()
2150
      {
2151
        Simple simple1 = new Simple();
2152

    
2153
        tree.Apply(new Act<int>(simple1.apply));
2154
        Assert.AreEqual(0, simple1.appfield1);
2155
        Assert.AreEqual(0, simple1.appfield2);
2156

    
2157
        Simple simple2 = new Simple();
2158

    
2159
        for (int i = 0; i < 10; i++) tree.Add(i);
2160
        tree.Add(2);
2161

    
2162
        tree.Apply(new Act<int>(simple2.apply));
2163
        Assert.AreEqual(11, simple2.appfield1);
2164
        Assert.AreEqual(289, simple2.appfield2);
2165
      }
2166

    
2167

    
2168
      [Test]
2169
      public void All()
2170
      {
2171
        Assert.IsTrue(tree.All(new Fun<int, bool>(never)));
2172
        Assert.IsTrue(tree.All(new Fun<int, bool>(even)));
2173
        Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2174
        for (int i = 0; i < 10; i++) tree.Add(i);
2175

    
2176
        Assert.IsFalse(tree.All(new Fun<int, bool>(never)));
2177
        Assert.IsFalse(tree.All(new Fun<int, bool>(even)));
2178
        Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2179
        tree.Clear();
2180
        for (int i = 0; i < 10; i++) tree.Add(i * 2);
2181

    
2182
        Assert.IsFalse(tree.All(new Fun<int, bool>(never)));
2183
        Assert.IsTrue(tree.All(new Fun<int, bool>(even)));
2184
        Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2185
        tree.Clear();
2186
        for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1);
2187

    
2188
        Assert.IsFalse(tree.All(new Fun<int, bool>(never)));
2189
        Assert.IsFalse(tree.All(new Fun<int, bool>(even)));
2190
        Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2191
      }
2192

    
2193

    
2194
      [Test]
2195
      public void Exists()
2196
      {
2197
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2198
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(even)));
2199
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(always)));
2200
        for (int i = 0; i < 10; i++) tree.Add(i);
2201

    
2202
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2203
        Assert.IsTrue(tree.Exists(new Fun<int, bool>(even)));
2204
        Assert.IsTrue(tree.Exists(new Fun<int, bool>(always)));
2205
        tree.Clear();
2206
        for (int i = 0; i < 10; i++) tree.Add(i * 2);
2207

    
2208
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2209
        Assert.IsTrue(tree.Exists(new Fun<int, bool>(even)));
2210
        Assert.IsTrue(tree.Exists(new Fun<int, bool>(always)));
2211
        tree.Clear();
2212
        for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1);
2213

    
2214
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2215
        Assert.IsFalse(tree.Exists(new Fun<int, bool>(even)));
2216
        Assert.IsTrue(tree.Exists(new Fun<int, bool>(always)));
2217
      }
2218

    
2219

    
2220
      [Test]
2221
      public void FindAll()
2222
      {
2223
        Assert.AreEqual(0, tree.FindAll(new Fun<int, bool>(never)).Count);
2224
        for (int i = 0; i < 10; i++)
2225
          tree.Add(i);
2226
        tree.Add(2);
2227

    
2228
        Assert.AreEqual(0, tree.FindAll(new Fun<int, bool>(never)).Count);
2229
        Assert.AreEqual(11, tree.FindAll(new Fun<int, bool>(always)).Count);
2230
        Assert.AreEqual(6, tree.FindAll(new Fun<int, bool>(even)).Count);
2231
        Assert.IsTrue(((TreeBag<int>)tree.FindAll(new Fun<int, bool>(even))).Check("R"));
2232
      }
2233

    
2234

    
2235
      [Test]
2236
      public void Map()
2237
      {
2238
        Assert.AreEqual(0, tree.Map(new Fun<int, string>(themap), new SC()).Count);
2239
        for (int i = 0; i < 14; i++)
2240
          tree.Add(i * i * i);
2241
        tree.Add(1);
2242

    
2243
        IIndexedSorted<string> res = tree.Map(new Fun<int, string>(themap), new SC());
2244

    
2245
        Assert.IsTrue(((TreeBag<string>)res).Check("R"));
2246
        Assert.AreEqual(15, res.Count);
2247
        Assert.AreEqual("AA    0 BB", res[0]);
2248
        Assert.AreEqual("AA    1 BB", res[1]);
2249
        Assert.AreEqual("AA    1 BB", res[2]);
2250
        Assert.AreEqual("AA    8 BB", res[3]);
2251
        Assert.AreEqual("AA   27 BB", res[4]);
2252
        Assert.AreEqual("AA  125 BB", res[6]);
2253
        Assert.AreEqual("AA 1000 BB", res[11]);
2254
      }
2255

    
2256

    
2257
      [Test]
2258
      [ExpectedException(typeof(ArgumentException), "mapper not monotonic")]
2259
      public void BadMap()
2260
      {
2261
        for (int i = 0; i < 11; i++)
2262
          tree.Add(i * i * i);
2263

    
2264
        ISorted<string> res = tree.Map(new Fun<int, string>(badmap), new SC());
2265
      }
2266

    
2267

    
2268
      [Test]
2269
      public void Cut()
2270
      {
2271
        for (int i = 0; i < 10; i++)
2272
          tree.Add(i);
2273
        tree.Add(3);
2274

    
2275
        int low, high;
2276
        bool lval, hval;
2277

    
2278
        Assert.IsTrue(tree.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
2279
        Assert.IsTrue(lval && hval);
2280
        Assert.AreEqual(4, high);
2281
        Assert.AreEqual(2, low);
2282
        Assert.IsFalse(tree.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
2283
        Assert.IsTrue(lval && hval);
2284
        Assert.AreEqual(4, high);
2285
        Assert.AreEqual(3, low);
2286
      }
2287

    
2288

    
2289
      [Test]
2290
      public void CutInt()
2291
      {
2292
        for (int i = 0; i < 10; i++)
2293
          tree.Add(2 * i);
2294

    
2295
        int low, high;
2296
        bool lval, hval;
2297

    
2298
        Assert.IsFalse(tree.Cut(new IC(3), out low, out lval, out high, out hval));
2299
        Assert.IsTrue(lval && hval);
2300
        Assert.AreEqual(4, high);
2301
        Assert.AreEqual(2, low);
2302
        Assert.IsTrue(tree.Cut(new IC(6), out low, out lval, out high, out hval));
2303
        Assert.IsTrue(lval && hval);
2304
        Assert.AreEqual(8, high);
2305
        Assert.AreEqual(4, low);
2306
      }
2307

    
2308

    
2309
      [Test]
2310
      public void CutInterval()
2311
      {
2312
        for (int i = 0; i < 10; i++)
2313
          tree.Add(2 * i);
2314

    
2315
        int lo, hi;
2316
        bool lv, hv;
2317

    
2318
        Assert.IsTrue(tree.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
2319
        Assert.IsTrue(lv && hv);
2320
        Assert.AreEqual(10, hi);
2321
        Assert.AreEqual(4, lo);
2322
        Assert.IsTrue(tree.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
2323
        Assert.IsTrue(lv && hv);
2324
        Assert.AreEqual(12, hi);
2325
        Assert.AreEqual(4, lo);
2326
        for (int i = 0; i < 100; i++)
2327
          tree.Add(2 * i);
2328

    
2329
        tree.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
2330
        Assert.IsTrue(lv && hv);
2331
        Assert.AreEqual(106, hi);
2332
        Assert.AreEqual(76, lo);
2333
        tree.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
2334
        Assert.IsTrue(lv && hv);
2335
        Assert.AreEqual(8, hi);
2336
        Assert.AreEqual(4, lo);
2337
        tree.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
2338
        Assert.IsTrue(lv && hv);
2339
        Assert.AreEqual(112, hi);
2340
        Assert.AreEqual(78, lo);
2341
      }
2342

    
2343

    
2344
      [Test]
2345
      public void UpperCut()
2346
      {
2347
        for (int i = 0; i < 10; i++)
2348
          tree.Add(i);
2349

    
2350
        int l, h;
2351
        bool lv, hv;
2352

    
2353
        Assert.IsFalse(tree.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
2354
        Assert.IsTrue(lv && !hv);
2355
        Assert.AreEqual(9, l);
2356
        Assert.IsFalse(tree.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
2357
        Assert.IsTrue(!lv && hv);
2358
        Assert.AreEqual(0, h);
2359
      }
2360

    
2361

    
2362
      [TearDown]
2363
      public void Dispose() { ic = null; tree = null; }
2364
    }
2365
  }
2366

    
2367

    
2368

    
2369

    
2370
  namespace MultiOps
2371
  {
2372
    [TestFixture]
2373
    public class AddAll
2374
    {
2375
      private int sqr(int i) { return i * i; }
2376

    
2377

    
2378
      TreeBag<int> tree;
2379

    
2380

    
2381
      [SetUp]
2382
      public void Init() { tree = new TreeBag<int>(new IC()); }
2383

    
2384

    
2385
      [Test]
2386
      public void EmptyEmpty()
2387
      {
2388
        tree.AddAll(new FunEnumerable(0, new Fun<int, int>(sqr)));
2389
        Assert.AreEqual(0, tree.Count);
2390
        Assert.IsTrue(tree.Check());
2391
      }
2392

    
2393

    
2394
      [Test]
2395
      public void SomeEmpty()
2396
      {
2397
        for (int i = 4; i < 9; i++) tree.Add(i);
2398

    
2399
        tree.AddAll(new FunEnumerable(0, new Fun<int, int>(sqr)));
2400
        Assert.AreEqual(5, tree.Count);
2401
        Assert.IsTrue(tree.Check());
2402
      }
2403

    
2404

    
2405
      [Test]
2406
      public void EmptySome()
2407
      {
2408
        tree.AddAll(new FunEnumerable(4, new Fun<int, int>(sqr)));
2409
        Assert.AreEqual(4, tree.Count);
2410
        Assert.IsTrue(tree.Check());
2411
        Assert.AreEqual(0, tree[0]);
2412
        Assert.AreEqual(1, tree[1]);
2413
        Assert.AreEqual(4, tree[2]);
2414
        Assert.AreEqual(9, tree[3]);
2415
      }
2416

    
2417

    
2418
      [Test]
2419
      public void SomeSome()
2420
      {
2421
        for (int i = 5; i < 9; i++) tree.Add(i);
2422
        tree.Add(1);
2423

    
2424
        tree.AddAll(new FunEnumerable(4, new Fun<int, int>(sqr)));
2425
        Assert.AreEqual(9, tree.Count);
2426
        Assert.IsTrue(tree.Check());
2427
        Assert.IsTrue(IC.eq(tree, 0, 1, 1, 4, 5, 6, 7, 8, 9));
2428
      }
2429

    
2430

    
2431
      [TearDown]
2432
      public void Dispose() { tree = null; }
2433
    }
2434

    
2435

    
2436

    
2437
    [TestFixture]
2438
    public class AddSorted
2439
    {
2440
      private int sqr(int i) { return i * i; }
2441

    
2442
      private int step(int i) { return i / 3; }
2443

    
2444

    
2445
      private int bad(int i) { return i * (5 - i); }
2446

    
2447

    
2448
      TreeBag<int> tree;
2449

    
2450

    
2451
      [SetUp]
2452
      public void Init() { tree = new TreeBag<int>(new IC()); }
2453

    
2454

    
2455
      [Test]
2456
      public void EmptyEmpty()
2457
      {
2458
        tree.AddSorted(new FunEnumerable(0, new Fun<int, int>(sqr)));
2459
        Assert.AreEqual(0, tree.Count);
2460
        Assert.IsTrue(tree.Check());
2461
      }
2462

    
2463

    
2464
      [Test]
2465
      public void SomeEmpty()
2466
      {
2467
        for (int i = 4; i < 9; i++) tree.Add(i);
2468

    
2469
        tree.AddSorted(new FunEnumerable(0, new Fun<int, int>(sqr)));
2470
        Assert.AreEqual(5, tree.Count);
2471
        Assert.IsTrue(tree.Check());
2472
      }
2473

    
2474

    
2475
      [Test]
2476
      public void EmptySome()
2477
      {
2478
        tree.AddSorted(new FunEnumerable(4, new Fun<int, int>(sqr)));
2479
        Assert.AreEqual(4, tree.Count);
2480
        Assert.IsTrue(tree.Check());
2481
        Assert.AreEqual(0, tree[0]);
2482
        Assert.AreEqual(1, tree[1]);
2483
        Assert.AreEqual(4, tree[2]);
2484
        Assert.AreEqual(9, tree[3]);
2485
      }
2486

    
2487
      [Test]
2488
      public void EmptySome2()
2489
      {
2490
        tree.AddSorted(new FunEnumerable(4, new Fun<int, int>(step)));
2491
        //tree.dump();
2492
        Assert.AreEqual(4, tree.Count);
2493
        Assert.IsTrue(tree.Check());
2494
        Assert.AreEqual(0, tree[0]);
2495
        Assert.AreEqual(0, tree[1]);
2496
        Assert.AreEqual(0, tree[2]);
2497
        Assert.AreEqual(1, tree[3]);
2498
      }
2499

    
2500

    
2501
      [Test]
2502
      public void SomeSome()
2503
      {
2504
        for (int i = 5; i < 9; i++) tree.Add(i);
2505
        tree.Add(1);
2506

    
2507
        tree.AddSorted(new FunEnumerable(4, new Fun<int, int>(sqr)));
2508
        Assert.AreEqual(9, tree.Count);
2509
        Assert.IsTrue(tree.Check());
2510
        Assert.IsTrue(IC.eq(tree, 0, 1, 1, 4, 5, 6, 7, 8, 9));
2511
      }
2512

    
2513

    
2514
      [Test]
2515
      [ExpectedException(typeof(ArgumentException), "Argument not sorted")]
2516
      public void EmptyBad()
2517
      {
2518
        tree.AddSorted(new FunEnumerable(9, new Fun<int, int>(bad)));
2519
      }
2520

    
2521

    
2522
      [TearDown]
2523
      public void Dispose() { tree = null; }
2524
    }
2525

    
2526

    
2527

    
2528
    [TestFixture]
2529
    public class Rest
2530
    {
2531
      TreeBag<int> tree, tree2;
2532

    
2533

    
2534
      [SetUp]
2535
      public void Init()
2536
      {
2537
        tree = new TreeBag<int>(new IC());
2538
        tree2 = new TreeBag<int>(new IC());
2539
        for (int i = 0; i < 10; i++)
2540
          tree.Add(i);
2541
        tree.Add(4);
2542

    
2543
        for (int i = 0; i < 10; i++)
2544
          tree2.Add(2 * i);
2545
      }
2546

    
2547

    
2548
      [Test]
2549
      public void RemoveAll()
2550
      {
2551
        tree.RemoveAll(tree2.RangeFromTo(3, 7));
2552
        Assert.AreEqual(9, tree.Count);
2553
        Assert.IsTrue(tree.Check());
2554
        Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 4, 5, 7, 8, 9));
2555
        tree.RemoveAll(tree2.RangeFromTo(3, 7));
2556
        Assert.AreEqual(8, tree.Count);
2557
        Assert.IsTrue(tree.Check());
2558
        Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
2559
        tree.RemoveAll(tree2.RangeFromTo(13, 17));
2560
        Assert.AreEqual(8, tree.Count);
2561
        Assert.IsTrue(tree.Check());
2562
        Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
2563
        tree.RemoveAll(tree2.RangeFromTo(3, 17));
2564
        Assert.AreEqual(7, tree.Count);
2565
        Assert.IsTrue(tree.Check());
2566
        Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 9));
2567
        for (int i = 0; i < 10; i++) tree2.Add(i);
2568

    
2569
        tree.RemoveAll(tree2.RangeFromTo(-1, 10));
2570
        Assert.AreEqual(0, tree.Count);
2571
        Assert.IsTrue(tree.Check());
2572
        Assert.IsTrue(IC.eq(tree));
2573
      }
2574

    
2575
      private void pint<T>(SCG.IEnumerable<T> e)
2576
      {
2577
        foreach (T i in e)
2578
          Console.Write("{0} ", i);
2579

    
2580
        Console.WriteLine();
2581
      }
2582

    
2583
      [Test]
2584
      public void RetainAll()
2585
      {
2586
        tree.Add(8); tree2.Add(6);
2587
        //pint<int>(tree);
2588
        //pint<int>(tree2);
2589
        tree.RetainAll(tree2.RangeFromTo(3, 17));
2590
        Assert.AreEqual(3, tree.Count);
2591
        Assert.IsTrue(tree.Check());
2592
        Assert.IsTrue(IC.eq(tree, 4, 6, 8));
2593
        tree.RetainAll(tree2.RangeFromTo(1, 17));
2594
        Assert.AreEqual(3, tree.Count);
2595
        Assert.IsTrue(tree.Check());
2596
        Assert.IsTrue(IC.eq(tree, 4, 6, 8));
2597
        tree.RetainAll(tree2.RangeFromTo(3, 5));
2598
        Assert.AreEqual(1, tree.Count);
2599
        Assert.IsTrue(tree.Check());
2600
        Assert.IsTrue(IC.eq(tree, 4));
2601
        tree.RetainAll(tree2.RangeFromTo(7, 17));
2602
        Assert.AreEqual(0, tree.Count);
2603
        Assert.IsTrue(tree.Check());
2604
        Assert.IsTrue(IC.eq(tree));
2605
        for (int i = 0; i < 10; i++) tree.Add(i);
2606

    
2607
        tree.RetainAll(tree2.RangeFromTo(5, 5));
2608
        Assert.AreEqual(0, tree.Count);
2609
        Assert.IsTrue(tree.Check());
2610
        Assert.IsTrue(IC.eq(tree));
2611
        for (int i = 0; i < 10; i++) tree.Add(i);
2612

    
2613
        tree.RetainAll(tree2.RangeFromTo(15, 25));
2614
        Assert.AreEqual(0, tree.Count);
2615
        Assert.IsTrue(tree.Check());
2616
        Assert.IsTrue(IC.eq(tree));
2617
      }
2618

    
2619

    
2620
      [Test]
2621
      public void ContainsAll()
2622
      {
2623
        Assert.IsFalse(tree.ContainsAll(tree2));
2624
        Assert.IsTrue(tree.ContainsAll(tree));
2625
        tree2.Clear();
2626
        Assert.IsTrue(tree.ContainsAll(tree2));
2627
        tree.Clear();
2628
        Assert.IsTrue(tree.ContainsAll(tree2));
2629
        tree2.Add(8);
2630
        Assert.IsFalse(tree.ContainsAll(tree2));
2631
      }
2632

    
2633

    
2634
      [Test]
2635
      public void RemoveInterval()
2636
      {
2637
        tree.RemoveInterval(3, 4);
2638
        Assert.IsTrue(tree.Check());
2639
        Assert.AreEqual(7, tree.Count);
2640
        Assert.IsTrue(IC.eq(tree, 0, 1, 2, 6, 7, 8, 9));
2641
        tree.RemoveInterval(2, 3);
2642
        Assert.IsTrue(tree.Check());
2643
        Assert.AreEqual(4, tree.Count);
2644
        Assert.IsTrue(IC.eq(tree, 0, 1, 8, 9));
2645
        tree.RemoveInterval(0, 4);
2646
        Assert.IsTrue(tree.Check());
2647
        Assert.AreEqual(0, tree.Count);
2648
        Assert.IsTrue(IC.eq(tree));
2649
      }
2650

    
2651

    
2652
      [Test]
2653
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2654
      public void RemoveRangeBad1()
2655
      {
2656
        tree.RemoveInterval(-3, 8);
2657
      }
2658

    
2659

    
2660
      [Test]
2661
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2662
      public void RemoveRangeBad2()
2663
      {
2664
        tree.RemoveInterval(3, -8);
2665
      }
2666

    
2667

    
2668
      [Test]
2669
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2670
      public void RemoveRangeBad3()
2671
      {
2672
        tree.RemoveInterval(3, 9);
2673
      }
2674

    
2675

    
2676
      [Test]
2677
      public void GetRange()
2678
      {
2679
        Assert.IsTrue(IC.eq(tree[3, 3]));
2680
        Assert.IsTrue(IC.eq(tree[3, 4], 3));
2681
        Assert.IsTrue(IC.eq(tree[3, 5], 3, 4));
2682
        Assert.IsTrue(IC.eq(tree[3, 6], 3, 4, 4));
2683
        Assert.IsTrue(IC.eq(tree[3, 7], 3, 4, 4, 5));
2684
        Assert.IsTrue(IC.eq(tree[4, 4]));
2685
        Assert.IsTrue(IC.eq(tree[4, 5], 4));
2686
        Assert.IsTrue(IC.eq(tree[4, 6], 4, 4));
2687
        Assert.IsTrue(IC.eq(tree[4, 7], 4, 4, 5));
2688
        Assert.IsTrue(IC.eq(tree[4, 8], 4, 4, 5, 6));
2689
        Assert.IsTrue(IC.eq(tree[5, 5]));
2690
        Assert.IsTrue(IC.eq(tree[5, 6], 4));
2691
        Assert.IsTrue(IC.eq(tree[5, 7], 4, 5));
2692
        Assert.IsTrue(IC.eq(tree[5, 8], 4, 5, 6));
2693
        Assert.IsTrue(IC.eq(tree[5, 9], 4, 5, 6, 7));
2694
        Assert.IsTrue(IC.eq(tree[5, 11], 4, 5, 6, 7, 8, 9));
2695
      }
2696

    
2697

    
2698
      [Test]
2699
      public void GetRangeBackwards()
2700
      {
2701
        Assert.IsTrue(IC.eq(tree[3, 3].Backwards()));
2702
        Assert.IsTrue(IC.eq(tree[3, 4].Backwards(), 3));
2703
        Assert.IsTrue(IC.eq(tree[3, 5].Backwards(), 4, 3));
2704
        Assert.IsTrue(IC.eq(tree[3, 6].Backwards(), 4, 4, 3));
2705
        Assert.IsTrue(IC.eq(tree[3, 7].Backwards(), 5, 4, 4, 3));
2706
        Assert.IsTrue(IC.eq(tree[4, 4].Backwards()));
2707
        Assert.IsTrue(IC.eq(tree[4, 5].Backwards(), 4));
2708
        Assert.IsTrue(IC.eq(tree[4, 6].Backwards(), 4, 4));
2709
        Assert.IsTrue(IC.eq(tree[4, 7].Backwards(), 5, 4, 4));
2710
        Assert.IsTrue(IC.eq(tree[4, 8].Backwards(), 6, 5, 4, 4));
2711
        Assert.IsTrue(IC.eq(tree[5, 5].Backwards()));
2712
        Assert.IsTrue(IC.eq(tree[5, 6].Backwards(), 4));
2713
        Assert.IsTrue(IC.eq(tree[5, 7].Backwards(), 5, 4));
2714
        Assert.IsTrue(IC.eq(tree[5, 8].Backwards(), 6, 5, 4));
2715
        Assert.IsTrue(IC.eq(tree[5, 9].Backwards(), 7, 6, 5, 4));
2716
      }
2717

    
2718

    
2719
      [Test]
2720
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2721
      public void GetRangeBad1()
2722
      {
2723
        object foo = tree[-3, 0];
2724
      }
2725

    
2726

    
2727
      [Test]
2728
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2729
      public void GetRangeBad2()
2730
      {
2731
        object foo = tree[3, 2];
2732
      }
2733

    
2734

    
2735
      [Test]
2736
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2737
      public void GetRangeBad3()
2738
      {
2739
        object foo = tree[3, 12];
2740
      }
2741

    
2742

    
2743
      [TearDown]
2744
      public void Dispose() { tree = null; tree2 = null; }
2745
    }
2746
  }
2747

    
2748

    
2749
  namespace Hashing
2750
  {
2751
    [TestFixture]
2752
    public class ISequenced
2753
    {
2754
      private ISequenced<int> dit, dat, dut;
2755

    
2756

    
2757
      [SetUp]
2758
      public void Init()
2759
      {
2760
        dit = new TreeBag<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2761
        dat = new TreeBag<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2762
        dut = new TreeBag<int>(new RevIC(), EqualityComparer<int>.Default);
2763
      }
2764

    
2765

    
2766
      [Test]
2767
      public void EmptyEmpty()
2768
      {
2769
        Assert.IsTrue(dit.SequencedEquals(dat));
2770
      }
2771

    
2772

    
2773
      [Test]
2774
      public void EmptyNonEmpty()
2775
      {
2776
        dit.Add(3);
2777
        Assert.IsFalse(dit.SequencedEquals(dat));
2778
        Assert.IsFalse(dat.SequencedEquals(dit));
2779
      }
2780

    
2781
      [Test]
2782
      public void HashVal()
2783
      {
2784
        Assert.AreEqual(CHC.sequencedhashcode(), dit.GetSequencedHashCode());
2785
        dit.Add(3);
2786
        Assert.AreEqual(CHC.sequencedhashcode(3), dit.GetSequencedHashCode());
2787
        dit.Add(7);
2788
        Assert.AreEqual(CHC.sequencedhashcode(3, 7), dit.GetSequencedHashCode());
2789
        Assert.AreEqual(CHC.sequencedhashcode(), dut.GetSequencedHashCode());
2790
        dut.Add(3);
2791
        Assert.AreEqual(CHC.sequencedhashcode(3), dut.GetSequencedHashCode());
2792
        dut.Add(7);
2793
        Assert.AreEqual(CHC.sequencedhashcode(7, 3), dut.GetSequencedHashCode());
2794
      }
2795

    
2796

    
2797
      [Test]
2798
      public void Normal()
2799
      {
2800
        dit.Add(3);
2801
        dit.Add(7);
2802
        dat.Add(3);
2803
        Assert.IsFalse(dit.SequencedEquals(dat));
2804
        Assert.IsFalse(dat.SequencedEquals(dit));
2805
        dat.Add(7);
2806
        Assert.IsTrue(dit.SequencedEquals(dat));
2807
        Assert.IsTrue(dat.SequencedEquals(dit));
2808
      }
2809

    
2810

    
2811
      [Test]
2812
      public void WrongOrder()
2813
      {
2814
        dit.Add(3);
2815
        dut.Add(3);
2816
        Assert.IsTrue(dit.SequencedEquals(dut));
2817
        Assert.IsTrue(dut.SequencedEquals(dit));
2818
        dit.Add(7);
2819
        dut.Add(7);
2820
        Assert.IsFalse(dit.SequencedEquals(dut));
2821
        Assert.IsFalse(dut.SequencedEquals(dit));
2822
      }
2823

    
2824

    
2825
      [Test]
2826
      public void Reflexive()
2827
      {
2828
        Assert.IsTrue(dit.SequencedEquals(dit));
2829
        dit.Add(3);
2830
        Assert.IsTrue(dit.SequencedEquals(dit));
2831
        dit.Add(7);
2832
        Assert.IsTrue(dit.SequencedEquals(dit));
2833
      }
2834

    
2835

    
2836
      [TearDown]
2837
      public void Dispose()
2838
      {
2839
        dit = null;
2840
        dat = null;
2841
        dut = null;
2842
      }
2843
    }
2844

    
2845

    
2846

    
2847
    [TestFixture]
2848
    public class IEditableCollection
2849
    {
2850
      private ICollection<int> dit, dat, dut;
2851

    
2852

    
2853
      [SetUp]
2854
      public void Init()
2855
      {
2856
        dit = new TreeBag<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2857
        dat = new TreeBag<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2858
        dut = new TreeBag<int>(new RevIC(), EqualityComparer<int>.Default);
2859
      }
2860

    
2861

    
2862
      [Test]
2863
      public void EmptyEmpty()
2864
      {
2865
        Assert.IsTrue(dit.UnsequencedEquals(dat));
2866
      }
2867

    
2868

    
2869
      [Test]
2870
      public void EmptyNonEmpty()
2871
      {
2872
        dit.Add(3);
2873
        Assert.IsFalse(dit.UnsequencedEquals(dat));
2874
        Assert.IsFalse(dat.UnsequencedEquals(dit));
2875
      }
2876

    
2877

    
2878
      [Test]
2879
      public void HashVal()
2880
      {
2881
        Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
2882
        dit.Add(3);
2883
        Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
2884
        dit.Add(7);
2885
        Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
2886
        Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
2887
        dut.Add(3);
2888
        Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
2889
        dut.Add(7);
2890
        Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
2891
      }
2892

    
2893

    
2894
      [Test]
2895
      public void Normal()
2896
      {
2897
        dit.Add(3);
2898
        dit.Add(7);
2899
        dat.Add(3);
2900
        Assert.IsFalse(dit.UnsequencedEquals(dat));
2901
        Assert.IsFalse(dat.UnsequencedEquals(dit));
2902
        dat.Add(7);
2903
        Assert.IsTrue(dit.UnsequencedEquals(dat));
2904
        Assert.IsTrue(dat.UnsequencedEquals(dit));
2905
      }
2906

    
2907

    
2908
      [Test]
2909
      public void WrongOrder()
2910
      {
2911
        dit.Add(3);
2912
        dut.Add(3);
2913
        Assert.IsTrue(dit.UnsequencedEquals(dut));
2914
        Assert.IsTrue(dut.UnsequencedEquals(dit));
2915
        dit.Add(7);
2916
        dut.Add(7);
2917
        Assert.IsTrue(dit.UnsequencedEquals(dut));
2918
        Assert.IsTrue(dut.UnsequencedEquals(dit));
2919
      }
2920

    
2921

    
2922
      [Test]
2923
      public void Reflexive()
2924
      {
2925
        Assert.IsTrue(dit.UnsequencedEquals(dit));
2926
        dit.Add(3);
2927
        Assert.IsTrue(dit.UnsequencedEquals(dit));
2928
        dit.Add(7);
2929
        Assert.IsTrue(dit.UnsequencedEquals(dit));
2930
      }
2931

    
2932

    
2933
      [TearDown]
2934
      public void Dispose()
2935
      {
2936
        dit = null;
2937
        dat = null;
2938
        dut = null;
2939
      }
2940
    }
2941
  }
2942

    
2943
}