Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / trees / RedBlackTreeSetTests.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
namespace C5UnitTests.trees.TreeSet
28
{
29
  using CollectionOfInt = TreeSet<int>;
30

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

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

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

    
54

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

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

    
81

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

    
90

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

    
94

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

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

    
107

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

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

    
122

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

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

    
135

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

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

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

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

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

    
178

    
179
	[TestFixture]
180
	public class Ranges
181
	{
182
		private TreeSet<int> tree;
183

    
184
		private SCG.IComparer<int> c;
185

    
186

    
187
		[SetUp]
188
		public void Init()
189
		{
190
			c = new IC();
191
			tree = new TreeSet<int>(c);
192
			for (int i = 1; i <= 10; i++)
193
			{
194
				tree.Add(i * 2);
195
			}
196
		}
197

    
198

    
199
		[Test]
200
		public void Enumerator()
201
		{
202
			SCG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
203
			int i = 3;
204

    
205
			while (e.MoveNext())
206
			{
207
				Assert.AreEqual(2 * i++, e.Current);
208
			}
209

    
210
			Assert.AreEqual(9, i);
211
		}
212

    
213

    
214
		[Test]
215
		[ExpectedException(typeof(InvalidOperationException))]
216
		public void Enumerator2()
217
		{
218
			SCG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
219
			int i = e.Current;
220
		}
221

    
222

    
223
		[Test]
224
		[ExpectedException(typeof(InvalidOperationException))]
225
		public void Enumerator3()
226
		{
227
			SCG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
228

    
229
			while (e.MoveNext());
230

    
231
			int i = e.Current;
232
		}
233

    
234

    
235
		[Test]
236
		public void Remove()
237
		{
238
			int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
239

    
240
			tree.RemoveRangeFrom(18);
241
			Assert.IsTrue(IC.eq(tree, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
242
			tree.RemoveRangeFrom(28);
243
			Assert.IsTrue(IC.eq(tree, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
244
			tree.RemoveRangeFrom(2);
245
			Assert.IsTrue(IC.eq(tree));
246
			foreach (int i in all) tree.Add(i);
247

    
248
			tree.RemoveRangeTo(10);
249
			Assert.IsTrue(IC.eq(tree, new int[] { 10, 12, 14, 16, 18, 20 }));
250
			tree.RemoveRangeTo(2);
251
			Assert.IsTrue(IC.eq(tree, new int[] { 10, 12, 14, 16, 18, 20 }));
252
			tree.RemoveRangeTo(21);
253
			Assert.IsTrue(IC.eq(tree));
254
			foreach (int i in all) tree.Add(i);
255

    
256
			tree.RemoveRangeFromTo(4, 8);
257
			Assert.IsTrue(IC.eq(tree, 2, 8, 10, 12, 14, 16, 18, 20));
258
			tree.RemoveRangeFromTo(14, 28);
259
			Assert.IsTrue(IC.eq(tree, 2, 8, 10, 12));
260
			tree.RemoveRangeFromTo(0, 9);
261
			Assert.IsTrue(IC.eq(tree, 10, 12));
262
			tree.RemoveRangeFromTo(0, 81);
263
			Assert.IsTrue(IC.eq(tree));
264
		}
265

    
266
		[Test]
267
		public void Normal()
268
		{
269
			int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
270

    
271
			Assert.IsTrue(IC.eq(tree, all));
272
			Assert.IsTrue(IC.eq(tree.RangeAll(), all));
273
			Assert.AreEqual(10, tree.RangeAll().Count);
274
			Assert.IsTrue(IC.eq(tree.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
275
			Assert.AreEqual(5, tree.RangeFrom(11).Count);
276
			Assert.IsTrue(IC.eq(tree.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
277
			Assert.IsTrue(IC.eq(tree.RangeFrom(2), all));
278
			Assert.IsTrue(IC.eq(tree.RangeFrom(1), all));
279
			Assert.IsTrue(IC.eq(tree.RangeFrom(21), new int[] { }));
280
			Assert.IsTrue(IC.eq(tree.RangeFrom(20), new int[] { 20 }));
281
			Assert.IsTrue(IC.eq(tree.RangeTo(8), new int[] { 2, 4, 6 }));
282
			Assert.IsTrue(IC.eq(tree.RangeTo(7), new int[] { 2, 4, 6 }));
283
			Assert.AreEqual(3, tree.RangeTo(7).Count);
284
			Assert.IsTrue(IC.eq(tree.RangeTo(2), new int[] { }));
285
			Assert.IsTrue(IC.eq(tree.RangeTo(1), new int[] {  }));
286
			Assert.IsTrue(IC.eq(tree.RangeTo(3), new int[] { 2 }));
287
			Assert.IsTrue(IC.eq(tree.RangeTo(20), new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18 }));
288
			Assert.IsTrue(IC.eq(tree.RangeTo(21), all));
289
			Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12), new int[] { 8, 10 }));
290
			Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11), new int[] { 6, 8, 10 }));
291
			Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12), new int[] { 2, 4, 6, 8, 10 }));
292
			Assert.AreEqual(5, tree.RangeFromTo(1, 12).Count);
293
			Assert.IsTrue(IC.eq(tree.RangeFromTo(2, 12), new int[] { 2, 4, 6, 8, 10 }));
294
			Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21), new int[] { 6, 8, 10, 12, 14, 16, 18, 20 }));
295
			Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20), new int[] { 6, 8, 10, 12, 14, 16, 18 }));
296
		}
297

    
298

    
299
		[Test]
300
		public void Backwards()
301
		{
302
			int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
303
			int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 8, 6, 4, 2 };
304

    
305
			Assert.IsTrue(IC.eq(tree, all));
306
			Assert.IsTrue(IC.eq(tree.RangeAll().Backwards(), lla));
307
			Assert.IsTrue(IC.eq(tree.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
308
            Assert.IsTrue(IC.eq(tree.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
309
			Assert.IsTrue(IC.eq(tree.RangeFrom(2).Backwards(), lla));
310
			Assert.IsTrue(IC.eq(tree.RangeFrom(1).Backwards(), lla));
311
			Assert.IsTrue(IC.eq(tree.RangeFrom(21).Backwards(), new int[] { }));
312
			Assert.IsTrue(IC.eq(tree.RangeFrom(20).Backwards(), new int[] { 20 }));
313
			Assert.IsTrue(IC.eq(tree.RangeTo(8).Backwards(), new int[] { 6, 4, 2 }));
314
			Assert.IsTrue(IC.eq(tree.RangeTo(7).Backwards(), new int[] { 6, 4, 2 }));
315
			Assert.IsTrue(IC.eq(tree.RangeTo(2).Backwards(), new int[] { }));
316
			Assert.IsTrue(IC.eq(tree.RangeTo(1).Backwards(), new int[] {  }));
317
			Assert.IsTrue(IC.eq(tree.RangeTo(3).Backwards(), new int[] { 2 }));
318
			Assert.IsTrue(IC.eq(tree.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6, 4, 2}));
319
			Assert.IsTrue(IC.eq(tree.RangeTo(21).Backwards(), lla));
320
			Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12).Backwards(), new int[] { 10, 8 }));
321
			Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11).Backwards(), new int[] { 10, 8, 6 }));
322
			Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
323
			Assert.IsTrue(IC.eq(tree.RangeFromTo(2, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
324
			Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 8, 6 }));
325
			Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6 }));
326
		}
327

    
328
		[Test]
329
		public void Direction()
330
		{
331
			Assert.AreEqual(EnumerationDirection.Forwards, tree.Direction);
332
			Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFrom(20).Direction);
333
			Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeTo(7).Direction);
334
			Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFromTo(1, 12).Direction);
335
			Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeAll().Direction);
336
			Assert.AreEqual(EnumerationDirection.Backwards, tree.Backwards().Direction);
337
			Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFrom(20).Backwards().Direction);
338
			Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeTo(7).Backwards().Direction);
339
			Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFromTo(1, 12).Backwards().Direction);
340
			Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeAll().Backwards().Direction);
341
		}
342

    
343

    
344
		[TearDown]
345
		public void Dispose()
346
		{
347
			tree = null;
348
			c = null;
349
		}
350
	}
351

    
352
	[TestFixture]
353
	public class BagItf
354
	{
355
		private TreeSet<int> tree;
356

    
357

    
358
		[SetUp]
359
		public void Init()
360
		{
361
			tree = new TreeSet<int>(new IC());
362
			for (int i = 10; i < 20; i++)
363
			{
364
				tree.Add(i);
365
				tree.Add(i + 10);
366
			}
367
		}
368

    
369

    
370
		[Test]
371
		public void Both()
372
		{
373
			Assert.AreEqual(0, tree.ContainsCount(7));
374
			Assert.AreEqual(1, tree.ContainsCount(10));
375
			tree.RemoveAllCopies(10);
376
			Assert.AreEqual(0, tree.ContainsCount(10));
377
			tree.RemoveAllCopies(7);
378
		}
379

    
380

    
381
		[TearDown]
382
		public void Dispose()
383
		{
384
			tree = null;
385
		}
386
	}
387

    
388

    
389
	[TestFixture]
390
	public class Div
391
	{
392
		private TreeSet<int> tree;
393

    
394

    
395
		[SetUp]
396
		public void Init()
397
		{
398
			tree = new TreeSet<int>(new IC());
399
		}
400

    
401

    
402
		private void loadup()
403
		{
404
			for (int i = 10; i < 20; i++)
405
			{
406
				tree.Add(i);
407
				tree.Add(i + 10);
408
			}
409
		}
410

    
411
    [Test]
412
    [ExpectedException(typeof(NullReferenceException))]
413
    public void NullEqualityComparerinConstructor1()
414
    {
415
      new TreeSet<int>(null);
416
    }
417

    
418
    [Test]
419
    [ExpectedException(typeof(NullReferenceException))]
420
    public void NullEqualityComparerinConstructor3()
421
    {
422
      new TreeSet<int>(null, EqualityComparer<int>.Default);
423
    }
424

    
425
    [Test]
426
    [ExpectedException(typeof(NullReferenceException))]
427
    public void NullEqualityComparerinConstructor4()
428
    {
429
      new TreeSet<int>(Comparer<int>.Default, null);
430
    }
431

    
432
    [Test]
433
    [ExpectedException(typeof(NullReferenceException))]
434
    public void NullEqualityComparerinConstructor5()
435
    {
436
      new TreeSet<int>(null, null);
437
    }
438

    
439
    [Test]
440
    public void Choose()
441
    {
442
      tree.Add(7);
443
      Assert.AreEqual(7, tree.Choose());
444
    }
445

    
446
    [Test]
447
    [ExpectedException(typeof(NoSuchItemException))]
448
    public void BadChoose()
449
    {
450
      tree.Choose();
451
    }
452

    
453

    
454
    [Test]
455
		public void NoDuplicates()
456
		{
457
			Assert.IsFalse(tree.AllowsDuplicates);
458
			loadup();
459
			Assert.IsFalse(tree.AllowsDuplicates);
460
		}
461

    
462
		[Test]
463
		public void Add()
464
		{
465
			Assert.IsTrue(tree.Add(17));
466
			Assert.IsFalse(tree.Add(17));
467
			Assert.IsTrue(tree.Add(18));
468
			Assert.IsFalse(tree.Add(18));
469
			Assert.AreEqual(2, tree.Count);
470
		}
471

    
472

    
473
		[TearDown]
474
		public void Dispose()
475
		{
476
			tree = null;
477
		}
478
	}
479

    
480

    
481
	[TestFixture]
482
	public class FindOrAdd
483
	{
484
		private TreeSet<KeyValuePair<int,string>> bag;
485

    
486

    
487
		[SetUp]
488
		public void Init()
489
		{
490
			bag = new TreeSet<KeyValuePair<int,string>>(new KeyValuePairComparer<int,string>(new IC()));
491
		}
492

    
493

    
494
		[TearDown]
495
		public void Dispose()
496
		{
497
			bag = null;
498
		}
499

    
500

    
501
		[Test]
502
		public void Test()
503
		{
504
			KeyValuePair<int,string> p = new KeyValuePair<int,string>(3, "tre");
505

    
506
			Assert.IsFalse(bag.FindOrAdd(ref p));
507
			p.Value = "drei";
508
			Assert.IsTrue(bag.FindOrAdd(ref p));
509
			Assert.AreEqual("tre", p.Value);
510
			p.Value = "three";
511
			Assert.AreEqual(1, bag.ContainsCount(p));
512
			Assert.AreEqual("tre", bag[0].Value);
513
		}
514
	}
515

    
516
  [TestFixture]
517
  public class FindPredicate
518
  {
519
    private TreeSet<int> list;
520
    Fun<int, bool> pred;
521

    
522
    [SetUp]
523
    public void Init()
524
    {
525
      list = new TreeSet<int>(TenEqualityComparer.Default);
526
      pred = delegate(int i) { return i % 5 == 0; };
527
    }
528

    
529
    [TearDown]
530
    public void Dispose() { list = null; }
531

    
532
    [Test]
533
    public void Find()
534
    {
535
      int i;
536
      Assert.IsFalse(list.Find(pred, out i));
537
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
538
      Assert.IsFalse(list.Find(pred, out i));
539
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
540
      Assert.IsTrue(list.Find(pred, out i));
541
      Assert.AreEqual(45, i);
542
    }
543

    
544
    [Test]
545
    public void FindLast()
546
    {
547
      int i;
548
      Assert.IsFalse(list.FindLast(pred, out i));
549
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
550
      Assert.IsFalse(list.FindLast(pred, out i));
551
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
552
      Assert.IsTrue(list.FindLast(pred, out i));
553
      Assert.AreEqual(675, i);
554
    }
555

    
556
    [Test]
557
    public void FindIndex()
558
    {
559
      Assert.IsFalse(0 <= list.FindIndex(pred));
560
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
561
      Assert.IsFalse(0 <= list.FindIndex(pred));
562
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
563
      Assert.AreEqual(3, list.FindIndex(pred));
564
    }
565

    
566
    [Test]
567
    public void FindLastIndex()
568
    {
569
      Assert.IsFalse(0 <= list.FindLastIndex(pred));
570
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
571
      Assert.IsFalse(0 <= list.FindLastIndex(pred));
572
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
573
      Assert.AreEqual(7, list.FindLastIndex(pred));
574
    }
575
  }
576

    
577
  [TestFixture]
578
  public class UniqueItems
579
  {
580
    private TreeSet<int> list;
581

    
582
    [SetUp]
583
    public void Init() { list = new TreeSet<int>(); }
584

    
585
    [TearDown]
586
    public void Dispose() { list = null; }
587

    
588
    [Test]
589
    public void Test()
590
    {
591
      Assert.IsTrue(IC.seteq(list.UniqueItems()));
592
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
593
      list.AddAll<int>(new int[] { 7, 9, 7 });
594
      Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
595
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 1, 9, 1));
596
    }
597
  }
598

    
599
  [TestFixture]
600
  public class ArrayTest
601
	{
602
		private TreeSet<int> tree;
603

    
604
		int[] a;
605

    
606

    
607
		[SetUp]
608
		public void Init()
609
		{
610
			tree = new TreeSet<int>(new IC());
611
			a = new int[10];
612
			for (int i = 0; i < 10; i++)
613
				a[i] = 1000 + i;
614
		}
615

    
616

    
617
		[TearDown]
618
		public void Dispose() { tree = null; }
619

    
620

    
621
		private string aeq(int[] a, params int[] b)
622
		{
623
			if (a.Length != b.Length)
624
				return "Lengths differ: " + a.Length + " != " + b.Length;
625

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

    
630
			return "Alles klar";
631
		}
632

    
633

    
634
		[Test]
635
		public void ToArray()
636
		{
637
			Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
638
			tree.Add(7);
639
			tree.Add(4);
640
			Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 7));
641
		}
642

    
643

    
644
		[Test]
645
		public void CopyTo()
646
		{
647
			tree.CopyTo(a, 1);
648
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
649
			tree.Add(6);
650
			tree.CopyTo(a, 2);
651
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
652
			tree.Add(4);
653
			tree.Add(9);
654
			tree.CopyTo(a, 4);
655
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 1009));
656
			tree.Clear();
657
			tree.Add(7);
658
			tree.CopyTo(a, 9);
659
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 7));
660
		}
661

    
662

    
663
		[Test]
664
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
665
    public void CopyToBad()
666
		{
667
			tree.CopyTo(a, 11);
668
		}
669

    
670

    
671
		[Test]
672
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
673
		public void CopyToBad2()
674
		{
675
			tree.CopyTo(a, -1);
676
		}
677

    
678

    
679
		[Test]
680
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
681
    public void CopyToTooFar()
682
		{
683
			tree.Add(3);
684
			tree.Add(4);
685
			tree.CopyTo(a, 9);
686
		}
687
	}
688

    
689

    
690

    
691

    
692
	[TestFixture]
693
	public class Remove
694
	{
695
		private TreeSet<int> tree;
696

    
697

    
698
		[SetUp]
699
		public void Init()
700
		{
701
			tree = new TreeSet<int>(new IC());
702
			for (int i = 10; i < 20; i++)
703
			{
704
				tree.Add(i);
705
				tree.Add(i + 10);
706
			}
707
		}
708

    
709

    
710
		[Test]
711
		public void SmallTrees()
712
		{
713
			tree.Clear();
714
			tree.Add(7);
715
			tree.Add(9);
716
			Assert.IsTrue(tree.Remove(7));
717
			Assert.IsTrue(tree.Check(""));
718
		}
719

    
720

    
721
		[Test]
722
		public void ByIndex()
723
		{
724
			//Remove root!
725
			int n = tree.Count;
726
			int i = tree[10];
727

    
728
			tree.RemoveAt(10);
729
			Assert.IsTrue(tree.Check(""));
730
			Assert.IsFalse(tree.Contains(i));
731
			Assert.AreEqual(n - 1, tree.Count);
732

    
733
			//Low end
734
			i = tree.FindMin();
735
			tree.RemoveAt(0);
736
			Assert.IsTrue(tree.Check(""));
737
			Assert.IsFalse(tree.Contains(i));
738
			Assert.AreEqual(n - 2, tree.Count);
739

    
740
			//high end
741
			i = tree.FindMax();
742
			tree.RemoveAt(tree.Count - 1);
743
			Assert.IsTrue(tree.Check(""));
744
			Assert.IsFalse(tree.Contains(i));
745
			Assert.AreEqual(n - 3, tree.Count);
746

    
747
			//Some leaf
748
			i = 18;
749
			tree.RemoveAt(7);
750
			Assert.IsTrue(tree.Check(""));
751
			Assert.IsFalse(tree.Contains(i));
752
			Assert.AreEqual(n - 4, tree.Count);
753
		}
754

    
755

    
756
		[Test]
757
		public void AlmostEmpty()
758
		{
759
			//Almost empty
760
			tree.Clear();
761
			tree.Add(3);
762
			tree.RemoveAt(0);
763
			Assert.IsTrue(tree.Check(""));
764
			Assert.IsFalse(tree.Contains(3));
765
			Assert.AreEqual(0, tree.Count);
766
		}
767

    
768

    
769
		[Test]
770
		[ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
771
		public void Empty()
772
		{
773
			tree.Clear();
774
			tree.RemoveAt(0);
775
		}
776

    
777

    
778
		[Test]
779
		[ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
780
		public void HighIndex()
781
		{
782
			tree.RemoveAt(tree.Count);
783
		}
784

    
785

    
786
		[Test]
787
		[ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
788
		public void LowIndex()
789
		{
790
			tree.RemoveAt(-1);
791
		}
792

    
793

    
794
		[Test]
795
		public void Normal()
796
		{
797
			Assert.IsFalse(tree.Remove(-20));
798

    
799
			//No demote case, with move_item
800
			Assert.IsTrue(tree.Remove(20));
801
			Assert.IsTrue(tree.Check("T1"));
802
			Assert.IsFalse(tree.Remove(20));
803

    
804
			//plain case 2
805
			Assert.IsTrue(tree.Remove(14));
806
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
807

    
808
			//case 1b
809
			Assert.IsTrue(tree.Remove(25));
810
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
811

    
812
			//case 1c
813
			Assert.IsTrue(tree.Remove(29));
814
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
815

    
816
			//1a (terminating)
817
			Assert.IsTrue(tree.Remove(10));
818
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
819

    
820
			//2+1b
821
			Assert.IsTrue(tree.Remove(12));
822
			Assert.IsTrue(tree.Remove(11));
823

    
824
			//1a+1b
825
			Assert.IsTrue(tree.Remove(18));
826
			Assert.IsTrue(tree.Remove(13));
827
			Assert.IsTrue(tree.Remove(15));
828

    
829
			//2+1c
830
			for (int i = 0; i < 10; i++)
831
				tree.Add(50 - 2 * i);
832

    
833
			Assert.IsTrue(tree.Remove(42));
834
			Assert.IsTrue(tree.Remove(38));
835
			Assert.IsTrue(tree.Remove(28));
836
			Assert.IsTrue(tree.Remove(40));
837

    
838
			//
839
			Assert.IsTrue(tree.Remove(16));
840
			Assert.IsTrue(tree.Remove(23));
841
			Assert.IsTrue(tree.Remove(17));
842
			Assert.IsTrue(tree.Remove(19));
843
			Assert.IsTrue(tree.Remove(50));
844
			Assert.IsTrue(tree.Remove(26));
845
			Assert.IsTrue(tree.Remove(21));
846
			Assert.IsTrue(tree.Remove(22));
847
			Assert.IsTrue(tree.Remove(24));
848
			for (int i = 0; i < 48; i++)
849
				tree.Remove(i);
850

    
851
			//Almost empty tree:
852
			Assert.IsFalse(tree.Remove(26));
853
			Assert.IsTrue(tree.Remove(48));
854
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
855

    
856
			//Empty tree:
857
			Assert.IsFalse(tree.Remove(26));
858
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
859
		}
860

    
861

    
862
		[TearDown]
863
		public void Dispose()
864
		{
865
			tree = null;
866
		}
867
	}
868

    
869

    
870

    
871
	[TestFixture]
872
	public class PredecessorStructure
873
	{
874
		private TreeSet<int> tree;
875

    
876

    
877
		[SetUp]
878
		public void Init()
879
		{
880
			tree = new TreeSet<int>(new IC());
881
		}
882

    
883

    
884
		private void loadup()
885
		{
886
			for (int i = 0; i < 20; i++)
887
				tree.Add(2 * i);
888
		}
889

    
890
        [Test]
891
        public void FindPredecessor()
892
        {
893
            loadup();
894
            int res;
895
            Assert.IsTrue(tree.TryPredecessor(7, out res) && res == 6);
896
            Assert.IsTrue(tree.TryPredecessor(8, out res) && res == 6);
897

    
898
            //The bottom
899
            Assert.IsTrue(tree.TryPredecessor(1, out res) && res == 0);
900

    
901
            //The top
902
            Assert.IsTrue(tree.TryPredecessor(39, out res) && res == 38);
903
        }
904

    
905
        [Test]
906
        public void FindPredecessorTooLow1()
907
        {
908
            int res;
909
            Assert.IsFalse(tree.TryPredecessor(-2, out res));
910
            Assert.AreEqual(0, res);
911
            Assert.IsFalse(tree.TryPredecessor(0, out res));
912
            Assert.AreEqual(0, res);
913
        }
914

    
915
        [Test]
916
        public void FindWeakPredecessor()
917
        {
918
            loadup();
919
            int res;
920
            Assert.IsTrue(tree.TryWeakPredecessor(7, out res) && res == 6);
921
            Assert.IsTrue(tree.TryWeakPredecessor(8, out res) && res == 8);
922

    
923
            //The bottom
924
            Assert.IsTrue(tree.TryWeakPredecessor(1, out res) && res == 0);
925
            Assert.IsTrue(tree.TryWeakPredecessor(0, out res) && res == 0);
926

    
927
            //The top
928
            Assert.IsTrue(tree.TryWeakPredecessor(39, out res) && res == 38);
929
            Assert.IsTrue(tree.TryWeakPredecessor(38, out res) && res == 38);
930
        }
931

    
932
        [Test]
933
        public void FindWeakPredecessorTooLow1()
934
        {
935
            int res;
936
            Assert.IsFalse(tree.TryWeakPredecessor(-1, out res));
937
            Assert.AreEqual(0, res);
938
        }
939

    
940
        [Test]
941
        public void FindSuccessor()
942
        {
943
            loadup();
944
            int res;
945
            Assert.IsTrue(tree.TrySuccessor(7, out res) && res == 8);
946
            Assert.IsTrue(tree.TrySuccessor(8, out res) && res == 10);
947

    
948
            //The bottom
949
            Assert.IsTrue(tree.TrySuccessor(0, out res) && res == 2);
950
            Assert.IsTrue(tree.TrySuccessor(-1, out res) && res == 0);
951

    
952
            //The top
953
            Assert.IsTrue(tree.TrySuccessor(37, out res) && res == 38);
954
        }
955

    
956
        [Test]
957
        public void FindSuccessorTooHigh()
958
        {
959
            int res;
960
            Assert.IsFalse(tree.TrySuccessor(38, out res));
961
            Assert.AreEqual(0, res);
962
            Assert.IsFalse(tree.TrySuccessor(39, out res));
963
            Assert.AreEqual(0, res);
964
        }
965

    
966
        [Test]
967
        public void FindWeakSuccessor()
968
        {
969
            loadup();
970
            int res;
971
            Assert.IsTrue(tree.TryWeakSuccessor(6, out res) && res == 6);
972
            Assert.IsTrue(tree.TryWeakSuccessor(7, out res) && res == 8);
973

    
974
            //The bottom
975
            Assert.IsTrue(tree.TryWeakSuccessor(-1, out res) && res == 0);
976
            Assert.IsTrue(tree.TryWeakSuccessor(0, out res) && res == 0);
977

    
978
            //The top
979
            Assert.IsTrue(tree.TryWeakSuccessor(37, out res) && res == 38);
980
            Assert.IsTrue(tree.TryWeakSuccessor(38, out res) && res == 38);
981
        }
982

    
983
        [Test]
984
        public void FindWeakSuccessorTooHigh1()
985
        {
986
            int res;
987
            Assert.IsFalse(tree.TryWeakSuccessor(39, out res));
988
            Assert.AreEqual(0, res);
989
        }
990

    
991

    
992
		[Test]
993
		public void Predecessor()
994
		{
995
			loadup();
996
			Assert.AreEqual(6, tree.Predecessor(7));
997
			Assert.AreEqual(6, tree.Predecessor(8));
998

    
999
			//The bottom
1000
			Assert.AreEqual(0, tree.Predecessor(1));
1001

    
1002
			//The top
1003
			Assert.AreEqual(38, tree.Predecessor(39));
1004
		}
1005

    
1006

    
1007
		[Test]
1008
        [ExpectedException(typeof(NoSuchItemException))]
1009
        public void PredecessorTooLow1()
1010
		{
1011
			tree.Predecessor(-2);
1012
		}
1013

    
1014

    
1015
		[Test]
1016
        [ExpectedException(typeof(NoSuchItemException))]
1017
        public void PredecessorTooLow2()
1018
		{
1019
			tree.Predecessor(0);
1020
		}
1021

    
1022

    
1023
		[Test]
1024
		public void WeakPredecessor()
1025
		{
1026
			loadup();
1027
			Assert.AreEqual(6, tree.WeakPredecessor(7));
1028
			Assert.AreEqual(8, tree.WeakPredecessor(8));
1029

    
1030
			//The bottom
1031
			Assert.AreEqual(0, tree.WeakPredecessor(1));
1032
			Assert.AreEqual(0, tree.WeakPredecessor(0));
1033

    
1034
			//The top
1035
			Assert.AreEqual(38, tree.WeakPredecessor(39));
1036
			Assert.AreEqual(38, tree.WeakPredecessor(38));
1037
		}
1038

    
1039

    
1040
		[Test]
1041
        [ExpectedException(typeof(NoSuchItemException))]
1042
        public void WeakPredecessorTooLow1()
1043
		{
1044
			tree.WeakPredecessor(-2);
1045
		}
1046

    
1047

    
1048
		[Test]
1049
		public void Successor()
1050
		{
1051
			loadup();
1052
			Assert.AreEqual(8, tree.Successor(7));
1053
			Assert.AreEqual(10, tree.Successor(8));
1054

    
1055
			//The bottom
1056
			Assert.AreEqual(2, tree.Successor(0));
1057
			Assert.AreEqual(0, tree.Successor(-1));
1058

    
1059
			//The top
1060
			Assert.AreEqual(38, tree.Successor(37));
1061
		}
1062

    
1063

    
1064
		[Test]
1065
        [ExpectedException(typeof(NoSuchItemException))]
1066
        public void SuccessorTooHigh1()
1067
		{
1068
			tree.Successor(38);
1069
		}
1070

    
1071

    
1072
		[Test]
1073
        [ExpectedException(typeof(NoSuchItemException))]
1074
        public void SuccessorTooHigh2()
1075
		{
1076
			tree.Successor(39);
1077
		}
1078

    
1079

    
1080
		[Test]
1081
		public void WeakSuccessor()
1082
		{
1083
			loadup();
1084
			Assert.AreEqual(6, tree.WeakSuccessor(6));
1085
			Assert.AreEqual(8, tree.WeakSuccessor(7));
1086

    
1087
			//The bottom
1088
			Assert.AreEqual(0, tree.WeakSuccessor(-1));
1089
			Assert.AreEqual(0, tree.WeakSuccessor(0));
1090

    
1091
			//The top
1092
			Assert.AreEqual(38, tree.WeakSuccessor(37));
1093
			Assert.AreEqual(38, tree.WeakSuccessor(38));
1094
		}
1095

    
1096

    
1097
		[Test]
1098
        [ExpectedException(typeof(NoSuchItemException))]
1099
        public void WeakSuccessorTooHigh1()
1100
		{
1101
			tree.WeakSuccessor(39);
1102
		}
1103

    
1104

    
1105
		[TearDown]
1106
		public void Dispose()
1107
		{
1108
			tree = null;
1109
		}
1110
	}
1111

    
1112

    
1113

    
1114
	[TestFixture]
1115
	public class PriorityQueue
1116
	{
1117
		private TreeSet<int> tree;
1118

    
1119

    
1120
		[SetUp]
1121
		public void Init()
1122
		{
1123
			tree = new TreeSet<int>(new IC());
1124
		}
1125

    
1126

    
1127
		private void loadup()
1128
		{
1129
			foreach (int i in new int[] { 1, 2, 3, 4 })
1130
				tree.Add(i);
1131
		}
1132

    
1133

    
1134
		[Test]
1135
		public void Normal()
1136
		{
1137
			loadup();
1138
			Assert.AreEqual(1, tree.FindMin());
1139
			Assert.AreEqual(4, tree.FindMax());
1140
			Assert.AreEqual(1, tree.DeleteMin());
1141
			Assert.AreEqual(4, tree.DeleteMax());
1142
			Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1143
			Assert.AreEqual(2, tree.FindMin());
1144
			Assert.AreEqual(3, tree.FindMax());
1145
			Assert.AreEqual(2, tree.DeleteMin());
1146
			Assert.AreEqual(3, tree.DeleteMax());
1147
			Assert.IsTrue(tree.Check("Normal test 2"), "Bad tree");
1148
		}
1149

    
1150

    
1151
		[Test]
1152
		[ExpectedException(typeof(NoSuchItemException))]
1153
		public void Empty1()
1154
		{
1155
			tree.FindMin();
1156
		}
1157

    
1158

    
1159
		[Test]
1160
		[ExpectedException(typeof(NoSuchItemException))]
1161
		public void Empty2()
1162
		{
1163
			tree.FindMax();
1164
		}
1165

    
1166

    
1167
		[Test]
1168
		[ExpectedException(typeof(NoSuchItemException))]
1169
		public void Empty3()
1170
		{
1171
			tree.DeleteMin();
1172
		}
1173

    
1174

    
1175
		[Test]
1176
		[ExpectedException(typeof(NoSuchItemException))]
1177
		public void Empty4()
1178
		{
1179
			tree.DeleteMax();
1180
		}
1181

    
1182

    
1183
		[TearDown]
1184
		public void Dispose()
1185
		{
1186
			tree = null;
1187
		}
1188
	}
1189

    
1190

    
1191

    
1192
	[TestFixture]
1193
	public class IndexingAndCounting
1194
	{
1195
		private TreeSet<int> tree;
1196

    
1197

    
1198
		[SetUp]
1199
		public void Init()
1200
		{
1201
			tree = new TreeSet<int>(new IC());
1202
		}
1203

    
1204

    
1205
		private void populate()
1206
		{
1207
			tree.Add(30);
1208
			tree.Add(50);
1209
			tree.Add(10);
1210
			tree.Add(70);
1211
		}
1212

    
1213

    
1214
		[Test]
1215
		public void ToArray()
1216
		{
1217
			populate();
1218

    
1219
			int[] a = tree.ToArray();
1220

    
1221
			Assert.AreEqual(4, a.Length);
1222
			Assert.AreEqual(10, a[0]);
1223
			Assert.AreEqual(30, a[1]);
1224
			Assert.AreEqual(50, a[2]);
1225
			Assert.AreEqual(70, a[3]);
1226
		}
1227

    
1228

    
1229
		[Test]
1230
		public void GoodIndex()
1231
		{
1232
			Assert.AreEqual(-1, tree.IndexOf(20));
1233
			Assert.AreEqual(-1, tree.LastIndexOf(20));
1234
			populate();
1235
			Assert.AreEqual(10, tree[0]);
1236
			Assert.AreEqual(30, tree[1]);
1237
			Assert.AreEqual(50, tree[2]);
1238
			Assert.AreEqual(70, tree[3]);
1239
			Assert.AreEqual(0, tree.IndexOf(10));
1240
			Assert.AreEqual(1, tree.IndexOf(30));
1241
			Assert.AreEqual(2, tree.IndexOf(50));
1242
			Assert.AreEqual(3, tree.IndexOf(70));
1243
			Assert.AreEqual(~1, tree.IndexOf(20));
1244
			Assert.AreEqual(~0, tree.IndexOf(0));
1245
			Assert.AreEqual(~4, tree.IndexOf(90));
1246
			Assert.AreEqual(0, tree.LastIndexOf(10));
1247
			Assert.AreEqual(1, tree.LastIndexOf(30));
1248
			Assert.AreEqual(2, tree.LastIndexOf(50));
1249
			Assert.AreEqual(3, tree.LastIndexOf(70));
1250
			Assert.AreEqual(~1, tree.LastIndexOf(20));
1251
			Assert.AreEqual(~0, tree.LastIndexOf(0));
1252
			Assert.AreEqual(~4, tree.LastIndexOf(90));
1253
		}
1254

    
1255

    
1256
		[Test]
1257
		[ExpectedException(typeof(IndexOutOfRangeException))]
1258
		public void IndexTooLarge()
1259
		{
1260
			populate();
1261
			Console.WriteLine(tree[4]);
1262
		}
1263

    
1264

    
1265
		[Test]
1266
		[ExpectedException(typeof(IndexOutOfRangeException))]
1267
		public void IndexTooSmall()
1268
		{
1269
			populate();
1270
			Console.WriteLine(tree[-1]);
1271
		}
1272

    
1273

    
1274
		[Test]
1275
		public void FilledTreeOutsideInput()
1276
		{
1277
			populate();
1278
			Assert.AreEqual(0, tree.CountFrom(90));
1279
			Assert.AreEqual(0, tree.CountFromTo(-20, 0));
1280
			Assert.AreEqual(0, tree.CountFromTo(80, 100));
1281
			Assert.AreEqual(0, tree.CountTo(0));
1282
			Assert.AreEqual(4, tree.CountTo(90));
1283
			Assert.AreEqual(4, tree.CountFromTo(-20, 90));
1284
			Assert.AreEqual(4, tree.CountFrom(0));
1285
		}
1286

    
1287

    
1288
		[Test]
1289
		public void FilledTreeIntermediateInput()
1290
		{
1291
			populate();
1292
			Assert.AreEqual(3, tree.CountFrom(20));
1293
			Assert.AreEqual(1, tree.CountFromTo(20, 40));
1294
			Assert.AreEqual(2, tree.CountTo(40));
1295
		}
1296

    
1297

    
1298
		[Test]
1299
		public void FilledTreeMatchingInput()
1300
		{
1301
			populate();
1302
			Assert.AreEqual(3, tree.CountFrom(30));
1303
			Assert.AreEqual(2, tree.CountFromTo(30, 70));
1304
			Assert.AreEqual(0, tree.CountFromTo(50, 30));
1305
			Assert.AreEqual(0, tree.CountFromTo(50, 50));
1306
			Assert.AreEqual(0, tree.CountTo(10));
1307
			Assert.AreEqual(2, tree.CountTo(50));
1308
		}
1309

    
1310

    
1311
		[Test]
1312
		public void CountEmptyTree()
1313
		{
1314
			Assert.AreEqual(0, tree.CountFrom(20));
1315
			Assert.AreEqual(0, tree.CountFromTo(20, 40));
1316
			Assert.AreEqual(0, tree.CountTo(40));
1317
		}
1318

    
1319

    
1320
		[TearDown]
1321
		public void Dispose()
1322
		{
1323
			tree = null;
1324
		}
1325
	}
1326

    
1327

    
1328

    
1329

    
1330
	namespace ModificationCheck
1331
	{
1332
		[TestFixture]
1333
		public class Enumerator
1334
		{
1335
			private TreeSet<int> tree;
1336

    
1337
			private SCG.IEnumerator<int> e;
1338

    
1339

    
1340
			[SetUp]
1341
			public void Init()
1342
			{
1343
				tree = new TreeSet<int>(new IC());
1344
				for (int i = 0; i < 10; i++)
1345
					tree.Add(i);
1346

    
1347
				e = tree.GetEnumerator();
1348
			}
1349

    
1350

    
1351
			[Test]
1352
			public void CurrentAfterModification()
1353
			{
1354
				e.MoveNext();
1355
				tree.Add(34);
1356
				Assert.AreEqual(0, e.Current);
1357
			}
1358

    
1359

    
1360
			[Test]
1361
      [ExpectedException(typeof(CollectionModifiedException))]
1362
      public void MoveNextAfterAdd()
1363
			{
1364
				tree.Add(34);
1365
				e.MoveNext();
1366
			}
1367

    
1368

    
1369

    
1370

    
1371
			[Test]
1372
      [ExpectedException(typeof(CollectionModifiedException))]
1373
      public void MoveNextAfterRemove()
1374
			{
1375
				tree.Remove(34);
1376
				e.MoveNext();
1377
			}
1378

    
1379

    
1380
			[Test]
1381
      [ExpectedException(typeof(CollectionModifiedException))]
1382
      public void MoveNextAfterClear()
1383
			{
1384
				tree.Clear();
1385
				e.MoveNext();
1386
			}
1387

    
1388

    
1389
			[TearDown]
1390
			public void Dispose()
1391
			{
1392
				tree = null;
1393
				e = null;
1394
			}
1395
		}
1396

    
1397

    
1398

    
1399
		[TestFixture]
1400
		public class RangeEnumerator
1401
		{
1402
			private TreeSet<int> tree;
1403

    
1404
			private SCG.IEnumerator<int> e;
1405

    
1406

    
1407
			[SetUp]
1408
			public void Init()
1409
			{
1410
				tree = new TreeSet<int>(new IC());
1411
				for (int i = 0; i < 10; i++)
1412
					tree.Add(i);
1413

    
1414
				e = tree.RangeFromTo(3, 7).GetEnumerator();
1415
			}
1416

    
1417

    
1418
			[Test]
1419
			public void CurrentAfterModification()
1420
			{
1421
				e.MoveNext();
1422
				tree.Add(34);
1423
				Assert.AreEqual(3, e.Current);
1424
			}
1425

    
1426

    
1427
			[Test]
1428
      [ExpectedException(typeof(CollectionModifiedException))]
1429
      public void MoveNextAfterAdd()
1430
			{
1431
				tree.Add(34);
1432
				e.MoveNext();
1433
			}
1434

    
1435

    
1436

    
1437

    
1438
			[Test]
1439
      [ExpectedException(typeof(CollectionModifiedException))]
1440
      public void MoveNextAfterRemove()
1441
			{
1442
				tree.Remove(34);
1443
				e.MoveNext();
1444
			}
1445

    
1446

    
1447
			[Test]
1448
      [ExpectedException(typeof(CollectionModifiedException))]
1449
      public void MoveNextAfterClear()
1450
			{
1451
				tree.Clear();
1452
				e.MoveNext();
1453
			}
1454

    
1455

    
1456
			[TearDown]
1457
			public void Dispose()
1458
			{
1459
				tree = null;
1460
				e = null;
1461
			}
1462
		}
1463
	}
1464

    
1465

    
1466

    
1467

    
1468
	namespace PathcopyPersistence
1469
	{
1470
		[TestFixture]
1471
		public class Navigation
1472
		{
1473
			private TreeSet<int> tree, snap;
1474

    
1475
			private SCG.IComparer<int> ic;
1476

    
1477

    
1478
			[SetUp]
1479
			public void Init()
1480
			{
1481
				ic = new IC();
1482
				tree = new TreeSet<int>(ic);
1483
				for (int i = 0; i <= 20; i++)
1484
					tree.Add(2 * i + 1);
1485

    
1486
				snap = (TreeSet<int>)tree.Snapshot();
1487
				for (int i = 0; i <= 10; i++)
1488
					tree.Remove(4 * i + 1);
1489
			}
1490

    
1491

    
1492
			private bool twomodeleven(int i)
1493
			{
1494
				return i % 11 == 2;
1495
			}
1496

    
1497

    
1498
			[Test]
1499
			public void InternalEnum()
1500
			{
1501
				Assert.IsTrue(IC.eq(snap.FindAll(new Fun<int, bool>(twomodeleven)), 13, 35));
1502
			}
1503

    
1504
			
1505
			public void MoreCut() { }
1506

    
1507
			[Test]
1508
			public void Cut()
1509
			{
1510
				int lo, hi;
1511
				bool lv, hv;
1512

    
1513
				Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(64), out lo, out lv, out hi, out hv));
1514
				Assert.IsTrue(lv && hv);
1515
				Assert.AreEqual(5, hi);
1516
				Assert.AreEqual(3, lo);
1517
				Assert.IsTrue(snap.Cut(new HigherOrder.CubeRoot(125), out lo, out lv, out hi, out hv));
1518
				Assert.IsTrue(lv && hv);
1519
				Assert.AreEqual(7, hi);
1520
				Assert.AreEqual(3, lo);
1521
				Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(125000), out lo, out lv, out hi, out hv));
1522
				Assert.IsTrue(lv && !hv);
1523
				Assert.AreEqual(41, lo);
1524
				Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(-27), out lo, out lv, out hi, out hv));
1525
				Assert.IsTrue(!lv && hv);
1526
				Assert.AreEqual(1, hi);
1527
			}
1528

    
1529

    
1530
			[Test]
1531
			public void Range()
1532
			{
1533
				Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 16), 5, 7, 9, 11, 13, 15));
1534
				Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 17), 5, 7, 9, 11, 13, 15));
1535
				Assert.IsTrue(IC.eq(snap.RangeFromTo(6, 16), 7, 9, 11, 13, 15));
1536
				//Assert.AreEqual(snap.RangeFromTo(6, 16).Count, 5);
1537
			}
1538

    
1539

    
1540
			[Test]
1541
			public void Contains()
1542
			{
1543
				Assert.IsTrue(snap.Contains(5));
1544
			}
1545

    
1546

    
1547
			[Test]
1548
			public void FindMin()
1549
			{
1550
				Assert.AreEqual(1, snap.FindMin());
1551
			}
1552

    
1553

    
1554
			[Test]
1555
			public void FindMax()
1556
			{
1557
				Assert.AreEqual(41, snap.FindMax());
1558
			}
1559

    
1560
            [Test]
1561
            public void FindPredecessor()
1562
            {
1563
                int res;
1564
                Assert.IsTrue(snap.TryPredecessor(15, out res) && res == 13);
1565
                Assert.IsTrue(snap.TryPredecessor(16, out res) && res == 15);
1566
                Assert.IsTrue(snap.TryPredecessor(17, out res) && res == 15);
1567
                Assert.IsTrue(snap.TryPredecessor(18, out res) && res == 17);
1568

    
1569
                Assert.IsTrue(snap.TryPredecessor(2, out res) && res == 1);
1570

    
1571
                Assert.IsFalse(snap.TryPredecessor(1, out res));
1572
                Assert.AreEqual(0, res);
1573
            }
1574

    
1575

    
1576
            [Test]
1577
            public void FindSuccessor()
1578
            {
1579
                int res;
1580
                Assert.IsTrue(snap.TrySuccessor(15, out res) && res == 17);
1581
                Assert.IsTrue(snap.TrySuccessor(16, out res) && res == 17);
1582
                Assert.IsTrue(snap.TrySuccessor(17, out res) && res == 19);
1583
                Assert.IsTrue(snap.TrySuccessor(18, out res) && res == 19);
1584

    
1585
                Assert.IsTrue(snap.TrySuccessor(40, out res) && res == 41);
1586
                
1587
                Assert.IsFalse(snap.TrySuccessor(41, out res));
1588
                Assert.AreEqual(0, res);
1589
            }
1590

    
1591

    
1592
            [Test]
1593
            public void FindWeakPredecessor()
1594
            {
1595
                int res;
1596
                Assert.IsTrue(snap.TryWeakPredecessor(15, out res) && res == 15);
1597
                Assert.IsTrue(snap.TryWeakPredecessor(16, out res) && res == 15);
1598
                Assert.IsTrue(snap.TryWeakPredecessor(17, out res) && res == 17);
1599
                Assert.IsTrue(snap.TryWeakPredecessor(18, out res) && res == 17);
1600

    
1601
                Assert.IsTrue(snap.TryWeakPredecessor(1, out res) && res == 1);
1602

    
1603
                Assert.IsFalse(snap.TryWeakPredecessor(0, out res));
1604
                Assert.AreEqual(0, res);
1605
            }
1606

    
1607

    
1608
            [Test]
1609
            public void FindWeakSuccessor()
1610
            {
1611
                int res;
1612
                Assert.IsTrue(snap.TryWeakSuccessor(15, out res) && res == 15);
1613
                Assert.IsTrue(snap.TryWeakSuccessor(16, out res) && res == 17);
1614
                Assert.IsTrue(snap.TryWeakSuccessor(17, out res) && res == 17);
1615
                Assert.IsTrue(snap.TryWeakSuccessor(18, out res) && res == 19);
1616

    
1617
                Assert.IsTrue(snap.TryWeakSuccessor(41, out res) && res == 41);
1618

    
1619
                Assert.IsFalse(snap.TryWeakSuccessor(42, out res));
1620
                Assert.AreEqual(0, res);
1621
            }
1622

    
1623

    
1624
			[Test]
1625
			public void Predecessor()
1626
			{
1627
				Assert.AreEqual(13, snap.Predecessor(15));
1628
				Assert.AreEqual(15, snap.Predecessor(16));
1629
				Assert.AreEqual(15, snap.Predecessor(17));
1630
				Assert.AreEqual(17, snap.Predecessor(18));
1631
			}
1632

    
1633

    
1634
			[Test]
1635
			public void Successor()
1636
			{
1637
				Assert.AreEqual(17, snap.Successor(15));
1638
				Assert.AreEqual(17, snap.Successor(16));
1639
				Assert.AreEqual(19, snap.Successor(17));
1640
				Assert.AreEqual(19, snap.Successor(18));
1641
			}
1642

    
1643

    
1644
			[Test]
1645
			public void WeakPredecessor()
1646
			{
1647
				Assert.AreEqual(15, snap.WeakPredecessor(15));
1648
				Assert.AreEqual(15, snap.WeakPredecessor(16));
1649
				Assert.AreEqual(17, snap.WeakPredecessor(17));
1650
				Assert.AreEqual(17, snap.WeakPredecessor(18));
1651
			}
1652

    
1653

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

    
1663

    
1664
			[Test]
1665
			[ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
1666
			public void CountTo()
1667
			{
1668
				int j = snap.CountTo(15);
1669
			}
1670

    
1671

    
1672
			[Test]
1673
			[ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
1674
			public void Indexing()
1675
			{
1676
				int j = snap[4];
1677
			}
1678

    
1679

    
1680
			[Test]
1681
			[ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
1682
			public void Indexing2()
1683
			{
1684
				int j = snap.IndexOf(5);
1685
			}
1686

    
1687

    
1688
			[TearDown]
1689
			public void Dispose()
1690
			{
1691
				tree = null;
1692
				ic = null;
1693
			}
1694
		}
1695

    
1696

    
1697

    
1698
		[TestFixture]
1699
		public class Single
1700
		{
1701
			private TreeSet<int> tree;
1702

    
1703
			private SCG.IComparer<int> ic;
1704

    
1705

    
1706
			[SetUp]
1707
			public void Init()
1708
			{
1709
				ic = new IC();
1710
				tree = new TreeSet<int>(ic);
1711
				for (int i = 0; i < 10; i++)
1712
					tree.Add(2 * i + 1);
1713
			}
1714

    
1715

    
1716
			[Test]
1717
			public void EnumerationWithAdd()
1718
			{
1719
				int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1720
				int i = 0;
1721
				TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
1722

    
1723
				foreach (int j in snap)
1724
				{
1725
					Assert.AreEqual(1 + 2 * i++, j);
1726
					tree.Add(21 - j);
1727
					Assert.IsTrue(snap.Check("M"), "Bad snap!");
1728
					Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1729
					Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1730
				}
1731
			}
1732

    
1733

    
1734
			[Test]
1735
			public void Remove()
1736
			{
1737
				int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1738
				TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
1739

    
1740
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1741
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1742
				tree.Remove(19);
1743
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1744
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1745
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1746
			}
1747

    
1748

    
1749
			[Test]
1750
			public void RemoveNormal()
1751
			{
1752
				tree.Clear();
1753
				for (int i = 10; i < 20; i++)
1754
				{
1755
					tree.Add(i);
1756
					tree.Add(i + 10);
1757
				}
1758

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

    
1762
				Assert.IsFalse(tree.Remove(-20));
1763
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1764
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1765

    
1766
				//No demote case, with move_item
1767
				Assert.IsTrue(tree.Remove(20));
1768
				Assert.IsTrue(tree.Check("T1"));
1769
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1770
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1771
				Assert.IsFalse(tree.Remove(20));
1772

    
1773
				//plain case 2
1774
				tree.Snapshot();
1775
				Assert.IsTrue(tree.Remove(14));
1776
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1777
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1778
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1779

    
1780
				//case 1b
1781
				Assert.IsTrue(tree.Remove(25));
1782
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1783
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1784
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1785

    
1786
				//case 1c
1787
				Assert.IsTrue(tree.Remove(29));
1788
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1789
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1790
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1791

    
1792
				//1a (terminating)
1793
				Assert.IsTrue(tree.Remove(10));
1794
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1795
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1796
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1797

    
1798
				//2+1b
1799
				Assert.IsTrue(tree.Remove(12));
1800
				tree.Snapshot();
1801
				Assert.IsTrue(tree.Remove(11));
1802
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1803
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1804
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1805

    
1806
				//1a+1b
1807
				Assert.IsTrue(tree.Remove(18));
1808
				Assert.IsTrue(tree.Remove(13));
1809
				Assert.IsTrue(tree.Remove(15));
1810
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1811
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1812
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1813

    
1814
				//2+1c
1815
				for (int i = 0; i < 10; i++)
1816
					tree.Add(50 - 2 * i);
1817

    
1818
				Assert.IsTrue(tree.Remove(42));
1819
				Assert.IsTrue(tree.Remove(38));
1820
				Assert.IsTrue(tree.Remove(28));
1821
				Assert.IsTrue(tree.Remove(40));
1822
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1823
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1824
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1825

    
1826
				//
1827
				Assert.IsTrue(tree.Remove(16));
1828
				Assert.IsTrue(tree.Remove(23));
1829
				Assert.IsTrue(tree.Remove(17));
1830
				Assert.IsTrue(tree.Remove(19));
1831
				Assert.IsTrue(tree.Remove(50));
1832
				Assert.IsTrue(tree.Remove(26));
1833
				Assert.IsTrue(tree.Remove(21));
1834
				Assert.IsTrue(tree.Remove(22));
1835
				Assert.IsTrue(tree.Remove(24));
1836
				for (int i = 0; i < 48; i++)
1837
					tree.Remove(i);
1838

    
1839
				//Almost empty tree:
1840
				Assert.IsFalse(tree.Remove(26));
1841
				Assert.IsTrue(tree.Remove(48));
1842
				Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
1843
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1844
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1845

    
1846
				//Empty tree:
1847
				Assert.IsFalse(tree.Remove(26));
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

    
1853

    
1854
			[Test]
1855
			public void Add()
1856
			{
1857
				int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
1858
				TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
1859

    
1860
				Assert.IsTrue(snap.Check("M"), "Bad snap!");
1861
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1862
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1863
				tree.Add(10);
1864
				Assert.IsTrue(snap.Check("M"), "Bad snap!");
1865
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1866
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1867
				tree.Add(16);
1868
				Assert.IsTrue(snap.Check("M"), "Bad snap!");
1869
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1870
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1871

    
1872
				//Promote+zigzig
1873
				tree.Add(40);
1874
				Assert.IsTrue(snap.Check("M"), "Bad snap!");
1875
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1876
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1877
				for (int i = 1; i < 4; i++)
1878
					tree.Add(40 - 2 * i);
1879

    
1880
				Assert.IsTrue(snap.Check("M"), "Bad snap!");
1881
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1882
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1883

    
1884
				//Zigzag:
1885
				tree.Add(32);
1886
				Assert.IsTrue(snap.Check("M"), "Bad snap!");
1887
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1888
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1889
			}
1890

    
1891

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

    
1898
				tree.Clear();
1899
				Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
1900
				Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
1901
				Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
1902
				Assert.AreEqual(0, tree.Count);
1903
			}
1904

    
1905

    
1906
			[Test]
1907
			[ExpectedException(typeof(InvalidOperationException), "Cannot snapshot a snapshot")]
1908
			public void SnapSnap()
1909
			{
1910
				TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
1911

    
1912
				snap.Snapshot();
1913
			}
1914

    
1915

    
1916
			[TearDown]
1917
			public void Dispose()
1918
			{
1919
				tree = null;
1920
				ic = null;
1921
			}
1922
		}
1923

    
1924

    
1925

    
1926
		[TestFixture]
1927
		public class Multiple
1928
		{
1929
			private TreeSet<int> tree;
1930

    
1931
			private SCG.IComparer<int> ic;
1932

    
1933

    
1934
			private bool eq(SCG.IEnumerable<int> me, int[] that)
1935
			{
1936
				int i = 0, maxind = that.Length - 1;
1937

    
1938
				foreach (int item in me)
1939
					if (i > maxind || ic.Compare(item, that[i++]) != 0)
1940
						return false;
1941

    
1942
				return true;
1943
			}
1944

    
1945

    
1946
			[SetUp]
1947
			public void Init()
1948
			{
1949
				ic = new IC();
1950
				tree = new TreeSet<int>(ic);
1951
				for (int i = 0; i < 10; i++)
1952
					tree.Add(2 * i + 1);
1953
			}
1954

    
1955

    
1956
			[Test]
1957
			public void First()
1958
			{
1959
				TreeSet<int>[] snaps = new TreeSet<int>[10];
1960

    
1961
				for (int i = 0; i < 10; i++)
1962
				{
1963
					snaps[i] = (TreeSet<int>)(tree.Snapshot());
1964
					tree.Add(2 * i);
1965
				}
1966

    
1967
				for (int i = 0; i < 10; i++)
1968
				{
1969
					Assert.AreEqual(i + 10, snaps[i].Count);
1970
				}
1971

    
1972
				snaps[5] = null;
1973
				snaps[9] = null;
1974
				GC.Collect();
1975
				snaps[8].Dispose();
1976
				tree.Remove(14);
1977

    
1978
				int[] res = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19 };
1979
				int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
1980
				int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
1981

    
1982
				Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
1983
				Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
1984
				Assert.IsTrue(IC.eq(tree, res));
1985
				Assert.IsTrue(tree.Check("B"));
1986
				Assert.IsTrue(snaps[3].Check("B"));
1987
				Assert.IsTrue(snaps[7].Check("B"));
1988
			}
1989

    
1990

    
1991
			[Test]
1992
			public void CollectingTheMaster()
1993
			{
1994
				TreeSet<int>[] snaps = new TreeSet<int>[10];
1995

    
1996
				for (int i = 0; i < 10; i++)
1997
				{
1998
					snaps[i] = (TreeSet<int>)(tree.Snapshot());
1999
					tree.Add(2 * i);
2000
				}
2001

    
2002
				tree = null;
2003
				GC.Collect();
2004
				for (int i = 0; i < 10; i++)
2005
				{
2006
					Assert.AreEqual(i + 10, snaps[i].Count);
2007
				}
2008

    
2009
				snaps[5] = null;
2010
				snaps[9] = null;
2011
				GC.Collect();
2012
				snaps[8].Dispose();
2013

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

    
2017
				Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
2018
				Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
2019
				Assert.IsTrue(snaps[3].Check("B"));
2020
				Assert.IsTrue(snaps[7].Check("B"));
2021
			}
2022

    
2023

    
2024
			[TearDown]
2025
			public void Dispose()
2026
			{
2027
				tree = null;
2028
				ic = null;
2029
			}
2030
		}
2031
	}
2032

    
2033

    
2034

    
2035

    
2036
	namespace HigherOrder
2037
	{
2038
		internal class CubeRoot: IComparable<int>
2039
		{
2040
			private int c;
2041

    
2042

    
2043
			internal CubeRoot(int c) { this.c = c; }
2044

    
2045

    
2046
			public int CompareTo(int that) { return c - that * that * that; }
2047
            public bool Equals(int that) { return c == that * that * that; }
2048
        }
2049

    
2050

    
2051

    
2052
        class Interval: IComparable<int>
2053
		{
2054
			private int b, t;
2055

    
2056

    
2057
			internal Interval(int b, int t) { this.b = b; this.t = t; }
2058

    
2059

    
2060
			public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
2061
            public bool Equals(int that) { return that >= b && that <= t; }
2062
        }
2063

    
2064

    
2065

    
2066
        [TestFixture]
2067
		public class Simple
2068
		{
2069
			private TreeSet<int> tree;
2070

    
2071
			private SCG.IComparer<int> ic;
2072

    
2073

    
2074
			[SetUp]
2075
			public void Init()
2076
			{
2077
				ic = new IC();
2078
				tree = new TreeSet<int>(ic);
2079
			}
2080

    
2081

    
2082
			private bool never(int i) { return false; }
2083

    
2084

    
2085
			private bool always(int i) { return true; }
2086

    
2087

    
2088
			private bool even(int i) { return i % 2 == 0; }
2089

    
2090

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

    
2093

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

    
2096

    
2097
			private int appfield1;
2098

    
2099
			private int appfield2;
2100

    
2101

    
2102
			private void apply(int i) { appfield1++; appfield2 += i * i; }
2103

    
2104

    
2105
			[Test]
2106
			public void Apply()
2107
			{
2108
				Simple simple1 = new Simple();
2109

    
2110
				tree.Apply(new Act<int>(simple1.apply));
2111
				Assert.AreEqual(0, simple1.appfield1);
2112
				Assert.AreEqual(0, simple1.appfield2);
2113

    
2114
				Simple simple2 = new Simple();
2115

    
2116
				for (int i = 0; i < 10; i++) tree.Add(i);
2117

    
2118
				tree.Apply(new Act<int>(simple2.apply));
2119
				Assert.AreEqual(10, simple2.appfield1);
2120
				Assert.AreEqual(285, simple2.appfield2);
2121
			}
2122

    
2123

    
2124
			[Test]
2125
			public void All()
2126
			{
2127
				Assert.IsTrue(tree.All(new Fun<int, bool>(never)));
2128
				Assert.IsTrue(tree.All(new Fun<int, bool>(even)));
2129
				Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2130
				for (int i = 0; i < 10; i++)					tree.Add(i);
2131

    
2132
				Assert.IsFalse(tree.All(new Fun<int, bool>(never)));
2133
				Assert.IsFalse(tree.All(new Fun<int, bool>(even)));
2134
				Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2135
				tree.Clear();
2136
				for (int i = 0; i < 10; i++)					tree.Add(i * 2);
2137

    
2138
				Assert.IsFalse(tree.All(new Fun<int, bool>(never)));
2139
				Assert.IsTrue(tree.All(new Fun<int, bool>(even)));
2140
				Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2141
				tree.Clear();
2142
				for (int i = 0; i < 10; i++)					tree.Add(i * 2 + 1);
2143

    
2144
				Assert.IsFalse(tree.All(new Fun<int, bool>(never)));
2145
				Assert.IsFalse(tree.All(new Fun<int, bool>(even)));
2146
				Assert.IsTrue(tree.All(new Fun<int, bool>(always)));
2147
			}
2148

    
2149

    
2150
			[Test]
2151
			public void Exists()
2152
			{
2153
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2154
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(even)));
2155
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(always)));
2156
				for (int i = 0; i < 10; i++)					tree.Add(i);
2157

    
2158
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2159
				Assert.IsTrue(tree.Exists(new Fun<int, bool>(even)));
2160
				Assert.IsTrue(tree.Exists(new Fun<int, bool>(always)));
2161
				tree.Clear();
2162
				for (int i = 0; i < 10; i++)					tree.Add(i * 2);
2163

    
2164
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2165
				Assert.IsTrue(tree.Exists(new Fun<int, bool>(even)));
2166
				Assert.IsTrue(tree.Exists(new Fun<int, bool>(always)));
2167
				tree.Clear();
2168
				for (int i = 0; i < 10; i++)					tree.Add(i * 2 + 1);
2169

    
2170
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(never)));
2171
				Assert.IsFalse(tree.Exists(new Fun<int, bool>(even)));
2172
				Assert.IsTrue(tree.Exists(new Fun<int, bool>(always)));
2173
			}
2174

    
2175

    
2176
			[Test]
2177
			public void FindAll()
2178
			{
2179
				Assert.AreEqual(0, tree.FindAll(new Fun<int, bool>(never)).Count);
2180
				for (int i = 0; i < 10; i++)
2181
					tree.Add(i);
2182

    
2183
				Assert.AreEqual(0, tree.FindAll(new Fun<int, bool>(never)).Count);
2184
				Assert.AreEqual(10, tree.FindAll(new Fun<int, bool>(always)).Count);
2185
				Assert.AreEqual(5, tree.FindAll(new Fun<int, bool>(even)).Count);
2186
				Assert.IsTrue(((TreeSet<int>)tree.FindAll(new Fun<int, bool>(even))).Check("R"));
2187
			}
2188

    
2189

    
2190
			[Test]
2191
			public void Map()
2192
			{
2193
				Assert.AreEqual(0, tree.Map(new Fun<int,string>(themap), new SC()).Count);
2194
				for (int i = 0; i < 11; i++)
2195
					tree.Add(i * i * i);
2196

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

    
2199
				Assert.IsTrue(((TreeSet<string>)res).Check("R"));
2200
				Assert.AreEqual(11, res.Count);
2201
				Assert.AreEqual("AA    0 BB", res[0]);
2202
				Assert.AreEqual("AA   27 BB", res[3]);
2203
				Assert.AreEqual("AA  125 BB", res[5]);
2204
				Assert.AreEqual("AA 1000 BB", res[10]);
2205
			}
2206

    
2207

    
2208
			[Test]
2209
			[ExpectedException(typeof(ArgumentException), "mapper not monotonic")]
2210
			public void BadMap()
2211
			{
2212
				for (int i = 0; i < 11; i++)
2213
					tree.Add(i * i * i);
2214

    
2215
				ISorted<string> res = tree.Map(new Fun<int,string>(badmap), new SC());
2216
			}
2217

    
2218

    
2219
			[Test]
2220
			public void Cut()
2221
			{
2222
				for (int i = 0; i < 10; i++)
2223
					tree.Add(i);
2224

    
2225
				int low, high;
2226
				bool lval, hval;
2227

    
2228
				Assert.IsTrue(tree.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
2229
				Assert.IsTrue(lval && hval);
2230
				Assert.AreEqual(4, high);
2231
				Assert.AreEqual(2, low);
2232
				Assert.IsFalse(tree.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
2233
				Assert.IsTrue(lval && hval);
2234
				Assert.AreEqual(4, high);
2235
				Assert.AreEqual(3, low);
2236
			}
2237

    
2238

    
2239
			[Test]
2240
			public void CutInt()
2241
			{
2242
				for (int i = 0; i < 10; i++)
2243
					tree.Add(2 * i);
2244

    
2245
				int low, high;
2246
				bool lval, hval;
2247

    
2248
				Assert.IsFalse(tree.Cut(new IC(3), out low, out lval, out high, out hval));
2249
				Assert.IsTrue(lval && hval);
2250
				Assert.AreEqual(4, high);
2251
				Assert.AreEqual(2, low);
2252
				Assert.IsTrue(tree.Cut(new IC(6), out low, out lval, out high, out hval));
2253
				Assert.IsTrue(lval && hval);
2254
				Assert.AreEqual(8, high);
2255
				Assert.AreEqual(4, low);
2256
			}
2257

    
2258

    
2259
			[Test]
2260
			public void CutInterval()
2261
			{
2262
				for (int i = 0; i < 10; i++)
2263
					tree.Add(2 * i);
2264

    
2265
				int lo, hi;
2266
				bool lv, hv;
2267

    
2268
				Assert.IsTrue(tree.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
2269
				Assert.IsTrue(lv && hv);
2270
				Assert.AreEqual(10, hi);
2271
				Assert.AreEqual(4, lo);
2272
				Assert.IsTrue(tree.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
2273
				Assert.IsTrue(lv && hv);
2274
				Assert.AreEqual(12, hi);
2275
				Assert.AreEqual(4, lo);
2276
				for (int i = 0; i < 100; i++)
2277
					tree.Add(2 * i);
2278

    
2279
				tree.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
2280
				Assert.IsTrue(lv && hv);
2281
				Assert.AreEqual(106, hi);
2282
				Assert.AreEqual(76, lo);
2283
				tree.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
2284
				Assert.IsTrue(lv && hv);
2285
				Assert.AreEqual(8, hi);
2286
				Assert.AreEqual(4, lo);
2287
				tree.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
2288
				Assert.IsTrue(lv && hv);
2289
				Assert.AreEqual(112, hi);
2290
				Assert.AreEqual(78, lo);
2291
			}
2292

    
2293

    
2294
			[Test]
2295
			public void UpperCut()
2296
			{
2297
				for (int i = 0; i < 10; i++)
2298
					tree.Add(i);
2299

    
2300
				int l, h;
2301
				bool lv, hv;
2302

    
2303
				Assert.IsFalse(tree.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
2304
				Assert.IsTrue(lv && !hv);
2305
				Assert.AreEqual(9, l);
2306
				Assert.IsFalse(tree.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
2307
				Assert.IsTrue(!lv && hv);
2308
				Assert.AreEqual(0, h);
2309
			}
2310

    
2311

    
2312
			[TearDown]
2313
			public void Dispose() { ic = null; tree = null; }
2314
		}
2315
	}
2316

    
2317

    
2318

    
2319

    
2320
	namespace MultiOps
2321
	{
2322
		[TestFixture]
2323
		public class AddAll
2324
		{
2325
			private int sqr(int i) { return i * i; }
2326

    
2327

    
2328
			TreeSet<int> tree;
2329

    
2330

    
2331
			[SetUp]
2332
			public void Init() { tree = new TreeSet<int>(new IC()); }
2333

    
2334

    
2335
			[Test]
2336
			public void EmptyEmpty()
2337
			{
2338
				tree.AddAll(new FunEnumerable(0, new Fun<int,int>(sqr)));
2339
				Assert.AreEqual(0, tree.Count);
2340
				Assert.IsTrue(tree.Check());
2341
			}
2342

    
2343

    
2344
			[Test]
2345
			public void SomeEmpty()
2346
			{
2347
				for (int i = 4; i < 9; i++) tree.Add(i);
2348

    
2349
				tree.AddAll(new FunEnumerable(0, new Fun<int,int>(sqr)));
2350
				Assert.AreEqual(5, tree.Count);
2351
				Assert.IsTrue(tree.Check());
2352
			}
2353

    
2354

    
2355
			[Test]
2356
			public void EmptySome()
2357
			{
2358
				tree.AddAll(new FunEnumerable(4, new Fun<int,int>(sqr)));
2359
				Assert.AreEqual(4, tree.Count);
2360
				Assert.IsTrue(tree.Check());
2361
				Assert.AreEqual(0, tree[0]);
2362
				Assert.AreEqual(1, tree[1]);
2363
				Assert.AreEqual(4, tree[2]);
2364
				Assert.AreEqual(9, tree[3]);
2365
			}
2366

    
2367

    
2368
			[Test]
2369
			public void SomeSome()
2370
			{
2371
				for (int i = 5; i < 9; i++) tree.Add(i);
2372

    
2373
				tree.AddAll(new FunEnumerable(4, new Fun<int,int>(sqr)));
2374
				Assert.AreEqual(8, tree.Count);
2375
				Assert.IsTrue(tree.Check());
2376
				Assert.IsTrue(IC.eq(tree, 0, 1, 4, 5, 6, 7, 8, 9));
2377
			}
2378

    
2379

    
2380
			[TearDown]
2381
			public void Dispose() { tree = null; }
2382
		}
2383

    
2384

    
2385

    
2386
		[TestFixture]
2387
		public class AddSorted
2388
		{
2389
			private int sqr(int i) { return i * i; }
2390

    
2391

    
2392
			private int bad(int i) { return i * (5 - i); }
2393

    
2394

    
2395
			TreeSet<int> tree;
2396

    
2397

    
2398
			[SetUp]
2399
			public void Init() { tree = new TreeSet<int>(new IC()); }
2400

    
2401

    
2402
			[Test]
2403
			public void EmptyEmpty()
2404
			{
2405
				tree.AddSorted(new FunEnumerable(0, new Fun<int,int>(sqr)));
2406
				Assert.AreEqual(0, tree.Count);
2407
				Assert.IsTrue(tree.Check());
2408
			}
2409

    
2410

    
2411

    
2412
			[Test]
2413
			public void SomeEmpty()
2414
			{
2415
				for (int i = 4; i < 9; i++) tree.Add(i);
2416

    
2417
				tree.AddSorted(new FunEnumerable(0, new Fun<int,int>(sqr)));
2418
				Assert.AreEqual(5, tree.Count);
2419
				Assert.IsTrue(tree.Check());
2420
			}
2421

    
2422

    
2423

    
2424
			[Test]
2425
			public void EmptySome()
2426
			{
2427
				tree.AddSorted(new FunEnumerable(4, new Fun<int,int>(sqr)));
2428
				Assert.AreEqual(4, tree.Count);
2429
				Assert.IsTrue(tree.Check());
2430
				Assert.AreEqual(0, tree[0]);
2431
				Assert.AreEqual(1, tree[1]);
2432
				Assert.AreEqual(4, tree[2]);
2433
				Assert.AreEqual(9, tree[3]);
2434
			}
2435

    
2436

    
2437

    
2438
			[Test]
2439
			public void SomeSome()
2440
			{
2441
				for (int i = 5; i < 9; i++) tree.Add(i);
2442

    
2443
				tree.AddSorted(new FunEnumerable(4, new Fun<int,int>(sqr)));
2444
				Assert.AreEqual(8, tree.Count);
2445
				Assert.IsTrue(tree.Check());
2446
				Assert.IsTrue(IC.eq(tree, 0, 1, 4, 5, 6, 7, 8, 9));
2447
			}
2448

    
2449
			[Test]
2450
			[ExpectedException(typeof(ArgumentException), "Argument not sorted")]
2451
			public void EmptyBad()
2452
			{
2453
				tree.AddSorted(new FunEnumerable(9, new Fun<int,int>(bad)));
2454
			}
2455

    
2456

    
2457
			[TearDown]
2458
			public void Dispose() { tree = null; }
2459
		}
2460

    
2461
		[TestFixture]
2462
		public class Rest
2463
		{
2464
			TreeSet<int> tree, tree2;
2465

    
2466

    
2467
			[SetUp]
2468
			public void Init()
2469
			{
2470
				tree = new TreeSet<int>(new IC());
2471
				tree2 = new TreeSet<int>(new IC());
2472
				for (int i = 0; i < 10; i++)
2473
					tree.Add(i);
2474

    
2475
				for (int i = 0; i < 10; i++)
2476
					tree2.Add(2 * i);
2477
			}
2478

    
2479

    
2480
			[Test]
2481
			public void RemoveAll()
2482
			{
2483
				tree.RemoveAll(tree2.RangeFromTo(3, 7));
2484
				Assert.AreEqual(8, tree.Count);
2485
				Assert.IsTrue(tree.Check());
2486
				Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
2487
				tree.RemoveAll(tree2.RangeFromTo(3, 7));
2488
				Assert.AreEqual(8, tree.Count);
2489
				Assert.IsTrue(tree.Check());
2490
				Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
2491
				tree.RemoveAll(tree2.RangeFromTo(13, 17));
2492
				Assert.AreEqual(8, tree.Count);
2493
				Assert.IsTrue(tree.Check());
2494
				Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
2495
				tree.RemoveAll(tree2.RangeFromTo(3, 17));
2496
				Assert.AreEqual(7, tree.Count);
2497
				Assert.IsTrue(tree.Check());
2498
				Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 9));
2499
				for (int i = 0; i < 10; i++) tree2.Add(i);
2500

    
2501
				tree.RemoveAll(tree2.RangeFromTo(-1, 10));
2502
				Assert.AreEqual(0, tree.Count);
2503
				Assert.IsTrue(tree.Check());
2504
				Assert.IsTrue(IC.eq(tree));
2505
			}
2506

    
2507

    
2508
			[Test]
2509
			public void RetainAll()
2510
			{
2511
				tree.RetainAll(tree2.RangeFromTo(3, 17));
2512
				Assert.AreEqual(3, tree.Count);
2513
				Assert.IsTrue(tree.Check());
2514
				Assert.IsTrue(IC.eq(tree, 4, 6, 8));
2515
				tree.RetainAll(tree2.RangeFromTo(1, 17));
2516
				Assert.AreEqual(3, tree.Count);
2517
				Assert.IsTrue(tree.Check());
2518
				Assert.IsTrue(IC.eq(tree, 4, 6, 8));
2519
				tree.RetainAll(tree2.RangeFromTo(3, 5));
2520
				Assert.AreEqual(1, tree.Count);
2521
				Assert.IsTrue(tree.Check());
2522
				Assert.IsTrue(IC.eq(tree, 4));
2523
				tree.RetainAll(tree2.RangeFromTo(7, 17));
2524
				Assert.AreEqual(0, tree.Count);
2525
				Assert.IsTrue(tree.Check());
2526
				Assert.IsTrue(IC.eq(tree));
2527
				for (int i = 0; i < 10; i++) tree.Add(i);
2528

    
2529
				tree.RetainAll(tree2.RangeFromTo(5, 5));
2530
				Assert.AreEqual(0, tree.Count);
2531
				Assert.IsTrue(tree.Check());
2532
				Assert.IsTrue(IC.eq(tree));
2533
				for (int i = 0; i < 10; i++) tree.Add(i);
2534

    
2535
				tree.RetainAll(tree2.RangeFromTo(15, 25));
2536
				Assert.AreEqual(0, tree.Count);
2537
				Assert.IsTrue(tree.Check());
2538
				Assert.IsTrue(IC.eq(tree));
2539
			}
2540

    
2541

    
2542
			[Test]
2543
			public void ContainsAll()
2544
			{
2545
				Assert.IsFalse(tree.ContainsAll(tree2));
2546
				Assert.IsTrue(tree.ContainsAll(tree));
2547
				tree2.Clear();
2548
				Assert.IsTrue(tree.ContainsAll(tree2));
2549
				tree.Clear();
2550
				Assert.IsTrue(tree.ContainsAll(tree2));
2551
				tree2.Add(8);
2552
				Assert.IsFalse(tree.ContainsAll(tree2));
2553
			}
2554

    
2555

    
2556
			[Test]
2557
			public void RemoveInterval()
2558
			{
2559
				tree.RemoveInterval(3, 4);
2560
				Assert.IsTrue(tree.Check());
2561
				Assert.AreEqual(6, tree.Count);
2562
				Assert.IsTrue(IC.eq(tree, 0, 1, 2, 7, 8, 9));
2563
				tree.RemoveInterval(2, 3);
2564
				Assert.IsTrue(tree.Check());
2565
				Assert.AreEqual(3, tree.Count);
2566
				Assert.IsTrue(IC.eq(tree, 0, 1, 9));
2567
				tree.RemoveInterval(0, 3);
2568
				Assert.IsTrue(tree.Check());
2569
				Assert.AreEqual(0, tree.Count);
2570
				Assert.IsTrue(IC.eq(tree));
2571
			}
2572

    
2573

    
2574
			[Test]
2575
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2576
			public void RemoveRangeBad1()
2577
			{
2578
				tree.RemoveInterval(-3, 8);
2579
			}
2580

    
2581

    
2582
			[Test]
2583
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2584
			public void RemoveRangeBad2()
2585
			{
2586
				tree.RemoveInterval(3, -8);
2587
			}
2588

    
2589

    
2590
			[Test]
2591
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2592
      public void RemoveRangeBad3()
2593
			{
2594
				tree.RemoveInterval(3, 8);
2595
			}
2596

    
2597

    
2598
			[Test]
2599
			public void GetRange()
2600
			{
2601
				SCG.IEnumerable<int> e = tree[3, 6];
2602

    
2603
				Assert.IsTrue(IC.eq(e, 3, 4, 5));
2604
				e = tree[3, 3];
2605
				Assert.IsTrue(IC.eq(e));
2606
			}
2607

    
2608

    
2609
			[Test]
2610
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2611
			public void GetRangeBad1()
2612
			{
2613
				object foo = tree[-3, 0];
2614
			}
2615

    
2616

    
2617
			[Test]
2618
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2619
			public void GetRangeBad2()
2620
			{
2621
				object foo = tree[3, 2];
2622
			}
2623

    
2624

    
2625
			[Test]
2626
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2627
      public void GetRangeBad3()
2628
			{
2629
				object foo = tree[3, 11];
2630
			}
2631

    
2632

    
2633
			[TearDown]
2634
			public void Dispose() { tree = null; tree2 = null; }
2635
		}
2636
	}
2637

    
2638

    
2639

    
2640

    
2641
	namespace Sync
2642
	{
2643
		[TestFixture]
2644
		public class SyncRoot
2645
		{
2646
			private TreeSet<int> tree;
2647
      private readonly Object mySyncRoot = new Object();
2648
			int sz = 5000;
2649

    
2650

    
2651
			[Test]
2652
			public void Safe()
2653
			{
2654
				System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(safe1));
2655
				System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(safe2));
2656

    
2657
				t1.Start();
2658
				t2.Start();
2659
				t1.Join();
2660
				t2.Join();
2661
				Assert.AreEqual(2 * sz + 1, tree.Count);
2662
				Assert.IsTrue(tree.Check());
2663
			}
2664

    
2665

    
2666
			//[Test]
2667
			public void UnSafe()
2668
			{
2669
				bool bad = false;
2670

    
2671
				for (int i = 0; i < 10; i++)
2672
				{
2673
					System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
2674
					System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
2675

    
2676
					t1.Start();
2677
					t2.Start();
2678
					t1.Join();
2679
					t2.Join();
2680
					if (bad = 2 * sz + 1 != tree.Count)
2681
					{
2682
						Console.WriteLine("{0}::Unsafe(): bad at {1}", GetType(), i);
2683
						break;
2684
					}
2685
				}
2686

    
2687
				Assert.IsTrue(bad, "No sync problems!");
2688
			}
2689

    
2690

    
2691
			[Test]
2692
			public void SafeUnSafe()
2693
			{
2694
				System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
2695
				System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
2696

    
2697
				t1.Start();
2698
				t1.Join();
2699
				t2.Start();
2700
				t2.Join();
2701
				Assert.AreEqual(2 * sz + 1, tree.Count);
2702
			}
2703

    
2704

    
2705
			[SetUp]
2706
			public void Init() { tree = new TreeSet<int>(new IC()); }
2707

    
2708

    
2709
			private void unsafe1()
2710
			{
2711
				for (int i = 0; i < 2 * sz; i++)
2712
					tree.Add(i * 2);
2713

    
2714
				for (int i = 1; i < sz; i++)
2715
					tree.Remove(i * 4);
2716
			}
2717

    
2718

    
2719
			private void safe1()
2720
			{
2721
				for (int i = 0; i < 2 * sz; i++)
2722
					lock (mySyncRoot)
2723
						tree.Add(i * 2);
2724

    
2725
				for (int i = 1; i < sz; i++)
2726
					lock (mySyncRoot)
2727
						tree.Remove(i * 4);
2728
			}
2729

    
2730

    
2731
			private void unsafe2()
2732
			{
2733
				for (int i = 2 * sz; i > 0; i--)
2734
					tree.Add(i * 2 + 1);
2735

    
2736
				for (int i = sz; i > 0; i--)
2737
					tree.Remove(i * 4 + 1);
2738
			}
2739

    
2740

    
2741
			private void safe2()
2742
			{
2743
				for (int i = 2 * sz; i > 0; i--)
2744
					lock (mySyncRoot)
2745
						tree.Add(i * 2 + 1);
2746

    
2747
				for (int i = sz; i > 0; i--)
2748
					lock (mySyncRoot)
2749
						tree.Remove(i * 4 + 1);
2750
			}
2751

    
2752

    
2753
			[TearDown]
2754
			public void Dispose() { tree = null; }
2755
		}
2756

    
2757

    
2758

    
2759
		//[TestFixture]
2760
		public class ConcurrentQueries
2761
		{
2762
			private TreeSet<int> tree;
2763

    
2764
			int sz = 500000;
2765

    
2766

    
2767
			[SetUp]
2768
			public void Init()
2769
			{
2770
				tree = new TreeSet<int>(new IC());
2771
				for (int i = 0; i < sz; i++)
2772
				{
2773
					tree.Add(i);
2774
				}
2775
			}
2776

    
2777

    
2778

    
2779
			class A
2780
			{
2781
				public int count = 0;
2782

    
2783
				TreeSet<int> t;
2784

    
2785

    
2786
				public A(TreeSet<int> t) { this.t = t; }
2787

    
2788

    
2789
				public void a(int i) { count++; }
2790

    
2791

    
2792
				public void traverse() { t.Apply(new Act<int>(a)); }
2793
			}
2794

    
2795

    
2796

    
2797

    
2798
			[Test]
2799
			public void Safe()
2800
			{
2801
				A a = new A(tree);
2802

    
2803
				a.traverse();
2804
				Assert.AreEqual(sz, a.count);
2805
			}
2806

    
2807

    
2808
			[Test]
2809
			public void RegrettablyUnsafe()
2810
			{
2811
				System.Threading.Thread[] t = new System.Threading.Thread[10];
2812
				A[] a = new A[10];
2813
				for (int i = 0; i < 10; i++)
2814
				{
2815
					a[i] = new A(tree);
2816
					t[i] = new System.Threading.Thread(new System.Threading.ThreadStart(a[i].traverse));
2817
				}
2818

    
2819
				for (int i = 0; i < 10; i++)
2820
					t[i].Start();
2821
				for (int i = 0; i < 10; i++)
2822
					t[i].Join();
2823
				for (int i = 0; i < 10; i++)
2824
					Assert.AreEqual(sz,a[i].count);
2825

    
2826
			}
2827

    
2828

    
2829
			[TearDown]
2830
			public void Dispose() { tree = null; }
2831
		}
2832
	}
2833

    
2834

    
2835

    
2836

    
2837
	namespace Hashing
2838
	{
2839
		[TestFixture]
2840
		public class ISequenced
2841
		{
2842
			private ISequenced<int> dit, dat, dut;
2843

    
2844

    
2845
			[SetUp]
2846
			public void Init()
2847
			{
2848
                dit = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2849
                dat = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2850
                dut = new TreeSet<int>(new RevIC(), EqualityComparer<int>.Default);
2851
			}
2852

    
2853

    
2854
			[Test]
2855
			public void EmptyEmpty()
2856
			{
2857
				Assert.IsTrue(dit.SequencedEquals(dat));
2858
			}
2859

    
2860

    
2861
			[Test]
2862
			public void EmptyNonEmpty()
2863
			{
2864
				dit.Add(3);
2865
				Assert.IsFalse(dit.SequencedEquals(dat));
2866
				Assert.IsFalse(dat.SequencedEquals(dit));
2867
			}
2868

    
2869
			[Test]
2870
			public void HashVal()
2871
			{
2872
				Assert.AreEqual(CHC.sequencedhashcode(), dit.GetSequencedHashCode());
2873
				dit.Add(3);
2874
				Assert.AreEqual(CHC.sequencedhashcode(3), dit.GetSequencedHashCode());
2875
				dit.Add(7);
2876
				Assert.AreEqual(CHC.sequencedhashcode(3, 7), dit.GetSequencedHashCode());
2877
				Assert.AreEqual(CHC.sequencedhashcode(), dut.GetSequencedHashCode());
2878
				dut.Add(3);
2879
				Assert.AreEqual(CHC.sequencedhashcode(3), dut.GetSequencedHashCode());
2880
				dut.Add(7);
2881
				Assert.AreEqual(CHC.sequencedhashcode(7, 3), dut.GetSequencedHashCode());
2882
			}
2883

    
2884

    
2885
			[Test]
2886
			public void Normal()
2887
			{
2888
				dit.Add(3);
2889
				dit.Add(7);
2890
				dat.Add(3);
2891
				Assert.IsFalse(dit.SequencedEquals(dat));
2892
				Assert.IsFalse(dat.SequencedEquals(dit));
2893
				dat.Add(7);
2894
				Assert.IsTrue(dit.SequencedEquals(dat));
2895
				Assert.IsTrue(dat.SequencedEquals(dit));
2896
			}
2897

    
2898

    
2899
			[Test]
2900
			public void WrongOrder()
2901
			{
2902
				dit.Add(3);
2903
				dut.Add(3);
2904
				Assert.IsTrue(dit.SequencedEquals(dut));
2905
				Assert.IsTrue(dut.SequencedEquals(dit));
2906
				dit.Add(7);
2907
				dut.Add(7);
2908
				Assert.IsFalse(dit.SequencedEquals(dut));
2909
				Assert.IsFalse(dut.SequencedEquals(dit));
2910
			}
2911

    
2912

    
2913
			[Test]
2914
			public void Reflexive()
2915
			{
2916
				Assert.IsTrue(dit.SequencedEquals(dit));
2917
				dit.Add(3);
2918
				Assert.IsTrue(dit.SequencedEquals(dit));
2919
				dit.Add(7);
2920
				Assert.IsTrue(dit.SequencedEquals(dit));
2921
			}
2922

    
2923

    
2924
			[TearDown]
2925
			public void Dispose()
2926
			{
2927
				dit = null;
2928
				dat = null;
2929
				dut = null;
2930
			}
2931
		}
2932

    
2933

    
2934

    
2935
		[TestFixture]
2936
		public class IEditableCollection
2937
		{
2938
			private ICollection<int> dit, dat, dut;
2939

    
2940

    
2941
			[SetUp]
2942
			public void Init()
2943
			{
2944
                dit = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2945
                dat = new TreeSet<int>(Comparer<int>.Default, EqualityComparer<int>.Default);
2946
                dut = new TreeSet<int>(new RevIC(), EqualityComparer<int>.Default);
2947
			}
2948

    
2949

    
2950
			[Test]
2951
			public void EmptyEmpty()
2952
			{
2953
				Assert.IsTrue(dit.UnsequencedEquals(dat));
2954
			}
2955

    
2956

    
2957
			[Test]
2958
			public void EmptyNonEmpty()
2959
			{
2960
				dit.Add(3);
2961
				Assert.IsFalse(dit.UnsequencedEquals(dat));
2962
				Assert.IsFalse(dat.UnsequencedEquals(dit));
2963
			}
2964

    
2965

    
2966
			[Test]
2967
			public void HashVal()
2968
			{
2969
                Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
2970
                dit.Add(3);
2971
                Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
2972
                dit.Add(7);
2973
                Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
2974
                Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
2975
                dut.Add(3);
2976
                Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
2977
                dut.Add(7);
2978
                Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
2979
            }
2980

    
2981

    
2982
			[Test]
2983
			public void Normal()
2984
			{
2985
				dit.Add(3);
2986
				dit.Add(7);
2987
				dat.Add(3);
2988
				Assert.IsFalse(dit.UnsequencedEquals(dat));
2989
				Assert.IsFalse(dat.UnsequencedEquals(dit));
2990
				dat.Add(7);
2991
				Assert.IsTrue(dit.UnsequencedEquals(dat));
2992
				Assert.IsTrue(dat.UnsequencedEquals(dit));
2993
			}
2994

    
2995

    
2996
			[Test]
2997
			public void WrongOrder()
2998
			{
2999
				dit.Add(3);
3000
				dut.Add(3);
3001
				Assert.IsTrue(dit.UnsequencedEquals(dut));
3002
				Assert.IsTrue(dut.UnsequencedEquals(dit));
3003
				dit.Add(7);
3004
				dut.Add(7);
3005
				Assert.IsTrue(dit.UnsequencedEquals(dut));
3006
				Assert.IsTrue(dut.UnsequencedEquals(dit));
3007
			}
3008

    
3009

    
3010
			[Test]
3011
			public void Reflexive()
3012
			{
3013
				Assert.IsTrue(dit.UnsequencedEquals(dit));
3014
				dit.Add(3);
3015
				Assert.IsTrue(dit.UnsequencedEquals(dit));
3016
				dit.Add(7);
3017
				Assert.IsTrue(dit.UnsequencedEquals(dit));
3018
			}
3019

    
3020

    
3021
			[TearDown]
3022
			public void Dispose()
3023
			{
3024
				dit = null;
3025
				dat = null;
3026
				dut = null;
3027
			}
3028
		}
3029

    
3030
	}
3031
}