Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / arrays / SortedArrayTests.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.arrays.sorted
28
{
29
  using CollectionOfInt = SortedArray<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 SortedArray<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 Ranges
78
	{
79
		private SortedArray<int> array;
80

    
81
		private SCG.IComparer<int> c;
82

    
83

    
84
		[SetUp]
85
		public void Init()
86
		{
87
			c = new IC();
88
			array = new SortedArray<int>(c);
89
			for (int i = 1; i <= 10; i++)
90
			{
91
				array.Add(i * 2);
92
			}
93
		}
94

    
95

    
96
		[Test]
97
		public void Enumerator()
98
		{
99
			SCG.IEnumerator<int> e = array.RangeFromTo(5, 17).GetEnumerator();
100
			int i = 3;
101

    
102
			while (e.MoveNext())
103
			{
104
				Assert.AreEqual(2 * i++, e.Current);
105
			}
106

    
107
			Assert.AreEqual(9, i);
108
		}
109

    
110

    
111
		[Test]
112
		[ExpectedException(typeof(CollectionModifiedException))]
113
		public void Enumerator3()
114
		{
115
			SCG.IEnumerator<int> e = array.RangeFromTo(5, 17).GetEnumerator();
116

    
117
			e.MoveNext();
118
			array.Add(67);
119
			e.MoveNext();
120
		}
121

    
122

    
123
		[Test]
124
		public void Remove()
125
		{
126
			int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
127

    
128
			array.RemoveRangeFrom(18);
129
			Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
130
			array.RemoveRangeFrom(28);
131
			Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
132
			array.RemoveRangeFrom(13);
133
			Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12 }));
134
			array.RemoveRangeFrom(2);
135
			Assert.IsTrue(IC.eq(array));
136
			foreach (int i in all) array.Add(i);
137

    
138
			array.RemoveRangeTo(10);
139
			Assert.IsTrue(IC.eq(array, new int[] { 10, 12, 14, 16, 18, 20 }));
140
			array.RemoveRangeTo(2);
141
			Assert.IsTrue(IC.eq(array, new int[] { 10, 12, 14, 16, 18, 20 }));
142
			array.RemoveRangeTo(21);
143
			Assert.IsTrue(IC.eq(array));
144
			foreach (int i in all) array.Add(i);
145

    
146
			array.RemoveRangeFromTo(4, 8);
147
			Assert.IsTrue(IC.eq(array, 2, 8, 10, 12, 14, 16, 18, 20));
148
			array.RemoveRangeFromTo(14, 28);
149
			Assert.IsTrue(IC.eq(array, 2, 8, 10, 12));
150
			array.RemoveRangeFromTo(0, 9);
151
			Assert.IsTrue(IC.eq(array, 10, 12));
152
			array.RemoveRangeFromTo(0, 81);
153
			Assert.IsTrue(IC.eq(array));
154
		}
155

    
156
		[Test]
157
		public void Normal()
158
		{
159
			int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
160

    
161
			Assert.IsTrue(IC.eq(array, all));
162
			Assert.IsTrue(IC.eq(array.RangeAll(), all));
163
			Assert.AreEqual(10, array.RangeAll().Count);
164
			Assert.IsTrue(IC.eq(array.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
165
			Assert.AreEqual(5, array.RangeFrom(11).Count);
166
			Assert.IsTrue(IC.eq(array.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
167
			Assert.IsTrue(IC.eq(array.RangeFrom(2), all));
168
			Assert.IsTrue(IC.eq(array.RangeFrom(1), all));
169
			Assert.IsTrue(IC.eq(array.RangeFrom(21), new int[] { }));
170
			Assert.IsTrue(IC.eq(array.RangeFrom(20), new int[] { 20 }));
171
			Assert.IsTrue(IC.eq(array.RangeTo(8), new int[] { 2, 4, 6 }));
172
			Assert.IsTrue(IC.eq(array.RangeTo(7), new int[] { 2, 4, 6 }));
173
			Assert.AreEqual(3, array.RangeTo(7).Count);
174
			Assert.IsTrue(IC.eq(array.RangeTo(2), new int[] { }));
175
			Assert.IsTrue(IC.eq(array.RangeTo(1), new int[] {  }));
176
			Assert.IsTrue(IC.eq(array.RangeTo(3), new int[] { 2 }));
177
			Assert.IsTrue(IC.eq(array.RangeTo(20), new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18 }));
178
			Assert.IsTrue(IC.eq(array.RangeTo(21), all));
179
			Assert.IsTrue(IC.eq(array.RangeFromTo(7, 12), new int[] { 8, 10 }));
180
			Assert.IsTrue(IC.eq(array.RangeFromTo(6, 11), new int[] { 6, 8, 10 }));
181
			Assert.IsTrue(IC.eq(array.RangeFromTo(1, 12), new int[] { 2, 4, 6, 8, 10 }));
182
			Assert.AreEqual(5, array.RangeFromTo(1, 12).Count);
183
			Assert.IsTrue(IC.eq(array.RangeFromTo(2, 12), new int[] { 2, 4, 6, 8, 10 }));
184
			Assert.IsTrue(IC.eq(array.RangeFromTo(6, 21), new int[] { 6, 8, 10, 12, 14, 16, 18, 20 }));
185
			Assert.IsTrue(IC.eq(array.RangeFromTo(6, 20), new int[] { 6, 8, 10, 12, 14, 16, 18 }));
186
		}
187

    
188

    
189
		[Test]
190
		public void Backwards()
191
		{
192
			int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
193
			int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 8, 6, 4, 2 };
194

    
195
			Assert.IsTrue(IC.eq(array, all));
196
			Assert.IsTrue(IC.eq(array.RangeAll().Backwards(), lla));
197
			Assert.IsTrue(IC.eq(array.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
198
            Assert.IsTrue(IC.eq(array.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
199
			Assert.IsTrue(IC.eq(array.RangeFrom(2).Backwards(), lla));
200
			Assert.IsTrue(IC.eq(array.RangeFrom(1).Backwards(), lla));
201
			Assert.IsTrue(IC.eq(array.RangeFrom(21).Backwards(), new int[] { }));
202
			Assert.IsTrue(IC.eq(array.RangeFrom(20).Backwards(), new int[] { 20 }));
203
			Assert.IsTrue(IC.eq(array.RangeTo(8).Backwards(), new int[] { 6, 4, 2 }));
204
			Assert.IsTrue(IC.eq(array.RangeTo(7).Backwards(), new int[] { 6, 4, 2 }));
205
			Assert.IsTrue(IC.eq(array.RangeTo(2).Backwards(), new int[] { }));
206
			Assert.IsTrue(IC.eq(array.RangeTo(1).Backwards(), new int[] {  }));
207
			Assert.IsTrue(IC.eq(array.RangeTo(3).Backwards(), new int[] { 2 }));
208
			Assert.IsTrue(IC.eq(array.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6, 4, 2}));
209
			Assert.IsTrue(IC.eq(array.RangeTo(21).Backwards(), lla));
210
			Assert.IsTrue(IC.eq(array.RangeFromTo(7, 12).Backwards(), new int[] { 10, 8 }));
211
			Assert.IsTrue(IC.eq(array.RangeFromTo(6, 11).Backwards(), new int[] { 10, 8, 6 }));
212
			Assert.IsTrue(IC.eq(array.RangeFromTo(1, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
213
			Assert.IsTrue(IC.eq(array.RangeFromTo(2, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
214
			Assert.IsTrue(IC.eq(array.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 8, 6 }));
215
			Assert.IsTrue(IC.eq(array.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6 }));
216
		}
217

    
218
		[Test]
219
		public void Direction()
220
		{
221
			Assert.AreEqual(EnumerationDirection.Forwards, array.Direction);
222
			Assert.AreEqual(EnumerationDirection.Forwards, array.RangeFrom(20).Direction);
223
			Assert.AreEqual(EnumerationDirection.Forwards, array.RangeTo(7).Direction);
224
			Assert.AreEqual(EnumerationDirection.Forwards, array.RangeFromTo(1, 12).Direction);
225
			Assert.AreEqual(EnumerationDirection.Forwards, array.RangeAll().Direction);
226
			Assert.AreEqual(EnumerationDirection.Backwards, array.Backwards().Direction);
227
			Assert.AreEqual(EnumerationDirection.Backwards, array.RangeFrom(20).Backwards().Direction);
228
			Assert.AreEqual(EnumerationDirection.Backwards, array.RangeTo(7).Backwards().Direction);
229
			Assert.AreEqual(EnumerationDirection.Backwards, array.RangeFromTo(1, 12).Backwards().Direction);
230
			Assert.AreEqual(EnumerationDirection.Backwards, array.RangeAll().Backwards().Direction);
231
		}
232

    
233

    
234
		[TearDown]
235
		public void Dispose()
236
		{
237
			array = null;
238
			c = null;
239
		}
240
	}
241

    
242
	[TestFixture]
243
	public class BagItf
244
	{
245
		private SortedArray<int> array;
246

    
247

    
248
		[SetUp]
249
		public void Init()
250
		{
251
			array = new SortedArray<int>(new IC());
252
			for (int i = 10; i < 20; i++)
253
			{
254
				array.Add(i);
255
				array.Add(i + 10);
256
			}
257
		}
258

    
259

    
260
		[Test]
261
		public void Both()
262
		{
263
			Assert.AreEqual(0, array.ContainsCount(7));
264
			Assert.AreEqual(1, array.ContainsCount(10));
265
			array.RemoveAllCopies(10);
266
			Assert.AreEqual(0, array.ContainsCount(10));
267
			array.RemoveAllCopies(7);
268
		}
269

    
270

    
271
		[TearDown]
272
		public void Dispose()
273
		{
274
			array = null;
275
		}
276
	}
277

    
278

    
279
	[TestFixture]
280
	public class Div
281
	{
282
		private SortedArray<int> array;
283

    
284

    
285
		[SetUp]
286
		public void Init()
287
		{
288
			array = new SortedArray<int>(new IC());
289
		}
290

    
291
    [Test]
292
    [ExpectedException(typeof(NullReferenceException))]
293
    public void NullEqualityComparerinConstructor1()
294
    {
295
      new SortedArray<int>(null);
296
    }
297

    
298
    [Test]
299
    [ExpectedException(typeof(NullReferenceException))]
300
    public void NullEqualityComparerinConstructor2()
301
    {
302
      new SortedArray<int>(5, null);
303
    }
304

    
305
    [Test]
306
    [ExpectedException(typeof(NullReferenceException))]
307
    public void NullEqualityComparerinConstructor3()
308
    {
309
      new SortedArray<int>(5, null, EqualityComparer<int>.Default);
310
    }
311

    
312
    [Test]
313
    [ExpectedException(typeof(NullReferenceException))]
314
    public void NullEqualityComparerinConstructor4()
315
    {
316
      new SortedArray<int>(5, Comparer<int>.Default, null);
317
    }
318

    
319
    [Test]
320
    [ExpectedException(typeof(NullReferenceException))]
321
    public void NullEqualityComparerinConstructor5()
322
    {
323
      new SortedArray<int>(5, null, null);
324
    }
325

    
326
    [Test]
327
    public void Choose()
328
    {
329
      array.Add(7);
330
      Assert.AreEqual(7, array.Choose());
331
    }
332

    
333
    [Test]
334
    [ExpectedException(typeof(NoSuchItemException))]
335
    public void BadChoose()
336
    {
337
      array.Choose();
338
    }
339

    
340

    
341

    
342
    private void loadup()
343
		{
344
			for (int i = 10; i < 20; i++)
345
			{
346
				array.Add(i);
347
				array.Add(i + 10);
348
			}
349
		}
350

    
351

    
352
		[Test]
353
		public void NoDuplicatesEtc()
354
		{
355
			Assert.IsFalse(array.AllowsDuplicates);
356
			loadup();
357
			Assert.IsFalse(array.AllowsDuplicates);
358
			Assert.AreEqual(Speed.Log, array.ContainsSpeed);
359
			Assert.IsTrue(array.Comparer.Compare(2, 3) < 0);
360
            Assert.IsTrue(array.Comparer.Compare(4, 3) > 0);
361
            Assert.IsTrue(array.Comparer.Compare(3, 3) == 0);
362
        }
363

    
364
		[Test]
365
		public void Add()
366
		{
367
			Assert.IsTrue(array.Add(17));
368
			Assert.IsFalse(array.Add(17));
369
			Assert.IsTrue(array.Add(18));
370
			Assert.IsFalse(array.Add(18));
371
			Assert.AreEqual(2, array.Count);
372
		}
373

    
374

    
375
		[TearDown]
376
		public void Dispose()
377
		{
378
			array = null;
379
		}
380
	}
381

    
382

    
383
	[TestFixture]
384
	public class FindOrAdd
385
	{
386
		private SortedArray<KeyValuePair<int,string>> bag;
387

    
388

    
389
		[SetUp]
390
		public void Init()
391
		{
392
			bag = new SortedArray<KeyValuePair<int,string>>(new KeyValuePairComparer<int,string>(new IC()));
393
		}
394

    
395

    
396
		[TearDown]
397
		public void Dispose()
398
		{
399
			bag = null;
400
		}
401

    
402

    
403
		[Test]
404
		public void Test()
405
		{
406
			KeyValuePair<int,string> p = new KeyValuePair<int,string>(3, "tre");
407

    
408
			Assert.IsFalse(bag.FindOrAdd(ref p));
409
			p.Value = "drei";
410
			Assert.IsTrue(bag.FindOrAdd(ref p));
411
			Assert.AreEqual("tre", p.Value);
412
			p.Value = "three";
413
			Assert.AreEqual(1, bag.ContainsCount(p));
414
			Assert.AreEqual("tre", bag[0].Value);
415
		}
416
	}
417

    
418
  [TestFixture]
419
  public class FindPredicate
420
  {
421
    private SortedArray<int> list;
422
    Fun<int, bool> pred;
423

    
424
    [SetUp]
425
    public void Init()
426
    {
427
      list = new SortedArray<int>(TenEqualityComparer.Default);
428
      pred = delegate(int i) { return i % 5 == 0; };
429
    }
430

    
431
    [TearDown]
432
    public void Dispose() { list = null; }
433

    
434
    [Test]
435
    public void Find()
436
    {
437
      int i;
438
      Assert.IsFalse(list.Find(pred, out i));
439
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
440
      Assert.IsFalse(list.Find(pred, out i));
441
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
442
      Assert.IsTrue(list.Find(pred, out i));
443
      Assert.AreEqual(45, i);
444
    }
445

    
446
    [Test]
447
    public void FindLast()
448
    {
449
      int i;
450
      Assert.IsFalse(list.FindLast(pred, out i));
451
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
452
      Assert.IsFalse(list.FindLast(pred, out i));
453
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
454
      Assert.IsTrue(list.FindLast(pred, out i));
455
      Assert.AreEqual(675, i);
456
    }
457

    
458
    [Test]
459
    public void FindIndex()
460
    {
461
      Assert.IsFalse(0 <= list.FindIndex(pred));
462
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
463
      Assert.IsFalse(0 <= list.FindIndex(pred));
464
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
465
      Assert.AreEqual(3, list.FindIndex(pred));
466
    }
467

    
468
    [Test]
469
    public void FindLastIndex()
470
    {
471
      Assert.IsFalse(0 <= list.FindLastIndex(pred));
472
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
473
      Assert.IsFalse(0 <= list.FindLastIndex(pred));
474
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
475
      Assert.AreEqual(7, list.FindLastIndex(pred));
476
    }
477
  }
478

    
479
  [TestFixture]
480
  public class UniqueItems
481
  {
482
    private SortedArray<int> list;
483

    
484
    [SetUp]
485
    public void Init() { list = new SortedArray<int>(); }
486

    
487
    [TearDown]
488
    public void Dispose() { list = null; }
489

    
490
    [Test]
491
    public void Test()
492
    {
493
      Assert.IsTrue(IC.seteq(list.UniqueItems()));
494
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
495
      list.AddAll<int>(new int[] { 7, 9, 7 });
496
      Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
497
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 1, 9, 1));
498
    }
499
  }
500

    
501
  [TestFixture]
502
  public class ArrayTest
503
	{
504
		private SortedArray<int> tree;
505

    
506
		int[] a;
507

    
508

    
509
		[SetUp]
510
		public void Init()
511
		{
512
			tree = new SortedArray<int>(new IC());
513
			a = new int[10];
514
			for (int i = 0; i < 10; i++)
515
				a[i] = 1000 + i;
516
		}
517

    
518

    
519
		[TearDown]
520
		public void Dispose() { tree = null; }
521

    
522

    
523
		private string aeq(int[] a, params int[] b)
524
		{
525
			if (a.Length != b.Length)
526
				return "Lengths differ: " + a.Length + " != " + b.Length;
527

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

    
532
			return "Alles klar";
533
		}
534

    
535

    
536
		[Test]
537
		public void ToArray()
538
		{
539
			Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
540
			tree.Add(7);
541
			tree.Add(4);
542
			Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 7));
543
		}
544

    
545

    
546
		[Test]
547
		public void CopyTo()
548
		{
549
			tree.CopyTo(a, 1);
550
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
551
			tree.Add(6);
552
			tree.CopyTo(a, 2);
553
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
554
			tree.Add(4);
555
			tree.Add(9);
556
			tree.CopyTo(a, 4);
557
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 1009));
558
			tree.Clear();
559
			tree.Add(7);
560
			tree.CopyTo(a, 9);
561
			Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 7));
562
		}
563

    
564

    
565
		[Test]
566
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
567
    public void CopyToBad()
568
		{
569
			tree.CopyTo(a, 11);
570
		}
571

    
572

    
573
		[Test]
574
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
575
		public void CopyToBad2()
576
		{
577
			tree.CopyTo(a, -1);
578
		}
579

    
580

    
581
		[Test]
582
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
583
    public void CopyToTooFar()
584
		{
585
			tree.Add(3);
586
			tree.Add(4);
587
			tree.CopyTo(a, 9);
588
		}
589
	}
590

    
591

    
592
	[TestFixture]
593
	public class Combined
594
	{
595
		private IIndexedSorted<KeyValuePair<int,int>> lst;
596

    
597

    
598
		[SetUp]
599
		public void Init()
600
		{
601
			lst = new SortedArray<KeyValuePair<int,int>>(new KeyValuePairComparer<int,int>(new IC()));
602
			for (int i = 0; i < 10; i++)
603
				lst.Add(new KeyValuePair<int,int>(i, i + 30));
604
		}
605

    
606

    
607
		[TearDown]
608
		public void Dispose() { lst = null; }
609

    
610

    
611
		[Test]
612
		public void Find()
613
		{
614
			KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
615

    
616
			Assert.IsTrue(lst.Find(ref p));
617
			Assert.AreEqual(3, p.Key);
618
			Assert.AreEqual(33, p.Value);
619
			p = new KeyValuePair<int,int>(13, 78);
620
			Assert.IsFalse(lst.Find(ref p));
621
		}
622

    
623

    
624
		[Test]
625
		public void FindOrAdd()
626
		{
627
			KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
628

    
629
			Assert.IsTrue(lst.FindOrAdd(ref p));
630
			Assert.AreEqual(3, p.Key);
631
			Assert.AreEqual(33, p.Value);
632
			p = new KeyValuePair<int,int>(13, 79);
633
			Assert.IsFalse(lst.FindOrAdd(ref p));
634
			Assert.AreEqual(13, lst[10].Key);
635
			Assert.AreEqual(79, lst[10].Value);
636
		}
637

    
638

    
639
		[Test]
640
		public void Update()
641
		{
642
			KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
643

    
644
			Assert.IsTrue(lst.Update(p));
645
			Assert.AreEqual(3, lst[3].Key);
646
			Assert.AreEqual(78, lst[3].Value);
647
			p = new KeyValuePair<int,int>(13, 78);
648
			Assert.IsFalse(lst.Update(p));
649
		}
650

    
651

    
652
		[Test]
653
		public void UpdateOrAdd1()
654
		{
655
			KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
656

    
657
			Assert.IsTrue(lst.UpdateOrAdd(p));
658
			Assert.AreEqual(3, lst[3].Key);
659
			Assert.AreEqual(78, lst[3].Value);
660
			p = new KeyValuePair<int,int>(13, 79);
661
			Assert.IsFalse(lst.UpdateOrAdd(p));
662
			Assert.AreEqual(13, lst[10].Key);
663
			Assert.AreEqual(79, lst[10].Value);
664
		}
665

    
666
        [Test]
667
        public void UpdateOrAdd2()
668
        {
669
            ICollection<String> coll = new SortedArray<String>();
670
            // s1 and s2 are distinct objects but contain the same text:
671
            String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
672
            Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
673
            Assert.AreEqual(null, old);
674
            Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
675
            Assert.IsTrue(Object.ReferenceEquals(s1, old));
676
            Assert.IsFalse(Object.ReferenceEquals(s2, old));
677
        }
678

    
679
        [Test]
680
        public void UpdateOrAddWithExpand()
681
        {
682
            // bug20071217
683
            SortedArray<double> arr = new SortedArray<double>();
684
            for (int i = 0; i < 50; i++)
685
            {
686
                arr.UpdateOrAdd(i + 0.1);
687
                arr.Add(i + 0.2);
688
            }
689
            Assert.IsTrue(arr.Count == 100);
690
        }
691

    
692
        [Test]
693
        public void FindOrAddWithExpand()
694
        {
695
            // bug20071217
696
            SortedArray<double> arr = new SortedArray<double>();
697
            for (int i = 0; i < 50; i++)
698
            {
699
                double iVar = i + 0.1;
700
                arr.FindOrAdd(ref iVar);
701
                arr.Add(i * 0.2);
702
            }
703
            Assert.IsTrue(arr.Count == 100);
704
        }
705

    
706
		[Test]
707
		public void RemoveWithReturn()
708
		{
709
			KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
710

    
711
            Assert.IsTrue(lst.Remove(p, out p));
712
            Assert.AreEqual(3, p.Key);
713
			Assert.AreEqual(33, p.Value);
714
			Assert.AreEqual(4, lst[3].Key);
715
			Assert.AreEqual(34, lst[3].Value);
716
			p = new KeyValuePair<int,int>(13, 78);
717
            Assert.IsFalse(lst.Remove(p, out p));
718
        }
719
	}
720

    
721

    
722
	[TestFixture]
723
	public class Remove
724
	{
725
		private SortedArray<int> array;
726

    
727

    
728
		[SetUp]
729
		public void Init()
730
		{
731
			array = new SortedArray<int>(new IC());
732
			for (int i = 10; i < 20; i++)
733
			{
734
				array.Add(i);
735
				array.Add(i + 10);
736
			}
737
		}
738

    
739

    
740
		[Test]
741
		public void SmallTrees()
742
		{
743
			array.Clear();
744
			array.Add(7);
745
			array.Add(9);
746
			Assert.IsTrue(array.Remove(7));
747
			Assert.IsTrue(array.Check());
748
		}
749

    
750

    
751
		[Test]
752
		public void ByIndex()
753
		{
754
			//Remove root!
755
			int n = array.Count;
756
			int i = array[10];
757

    
758
			array.RemoveAt(10);
759
			Assert.IsTrue(array.Check());
760
			Assert.IsFalse(array.Contains(i));
761
			Assert.AreEqual(n - 1, array.Count);
762

    
763
			//Low end
764
			i = array.FindMin();
765
			array.RemoveAt(0);
766
			Assert.IsTrue(array.Check());
767
			Assert.IsFalse(array.Contains(i));
768
			Assert.AreEqual(n - 2, array.Count);
769

    
770
			//high end
771
			i = array.FindMax();
772
			array.RemoveAt(array.Count - 1);
773
			Assert.IsTrue(array.Check());
774
			Assert.IsFalse(array.Contains(i));
775
			Assert.AreEqual(n - 3, array.Count);
776

    
777
			//Some leaf
778
			i = 18;
779
			array.RemoveAt(7);
780
			Assert.IsTrue(array.Check());
781
			Assert.IsFalse(array.Contains(i));
782
			Assert.AreEqual(n - 4, array.Count);
783
		}
784

    
785

    
786
		[Test]
787
		public void AlmostEmpty()
788
		{
789
			//Almost empty
790
			array.Clear();
791
			array.Add(3);
792
			array.RemoveAt(0);
793
			Assert.IsTrue(array.Check());
794
			Assert.IsFalse(array.Contains(3));
795
			Assert.AreEqual(0, array.Count);
796
		}
797

    
798

    
799
		[Test]
800
		[ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
801
		public void Empty()
802
		{
803
			array.Clear();
804
			array.RemoveAt(0);
805
		}
806

    
807

    
808
		[Test]
809
		[ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
810
		public void HighIndex()
811
		{
812
			array.RemoveAt(array.Count);
813
		}
814

    
815

    
816
		[Test]
817
		[ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collectionvalue")]
818
		public void LowIndex()
819
		{
820
			array.RemoveAt(-1);
821
		}
822

    
823

    
824
		[Test]
825
		public void Normal()
826
		{
827
			Assert.IsFalse(array.Remove(-20));
828

    
829
			//No demote case, with move_item
830
			Assert.IsTrue(array.Remove(20));
831
			Assert.IsTrue(array.Check());
832
			Assert.IsFalse(array.Remove(20));
833

    
834
			//plain case 2
835
			Assert.IsTrue(array.Remove(14));
836
			Assert.IsTrue(array.Check(), "Bad tree");
837

    
838
			//case 1b
839
			Assert.IsTrue(array.Remove(25));
840
			Assert.IsTrue(array.Check(), "Bad tree");
841

    
842
			//case 1c
843
			Assert.IsTrue(array.Remove(29));
844
			Assert.IsTrue(array.Check(), "Bad tree");
845

    
846
			//1a (terminating)
847
			Assert.IsTrue(array.Remove(10));
848
			Assert.IsTrue(array.Check(), "Bad tree");
849

    
850
			//2+1b
851
			Assert.IsTrue(array.Remove(12));
852
			Assert.IsTrue(array.Remove(11));
853

    
854
			//1a+1b
855
			Assert.IsTrue(array.Remove(18));
856
			Assert.IsTrue(array.Remove(13));
857
			Assert.IsTrue(array.Remove(15));
858

    
859
			//2+1c
860
			for (int i = 0; i < 10; i++)
861
				array.Add(50 - 2 * i);
862

    
863
			Assert.IsTrue(array.Remove(42));
864
			Assert.IsTrue(array.Remove(38));
865
			Assert.IsTrue(array.Remove(28));
866
			Assert.IsTrue(array.Remove(40));
867

    
868
			//
869
			Assert.IsTrue(array.Remove(16));
870
			Assert.IsTrue(array.Remove(23));
871
			Assert.IsTrue(array.Remove(17));
872
			Assert.IsTrue(array.Remove(19));
873
			Assert.IsTrue(array.Remove(50));
874
			Assert.IsTrue(array.Remove(26));
875
			Assert.IsTrue(array.Remove(21));
876
			Assert.IsTrue(array.Remove(22));
877
			Assert.IsTrue(array.Remove(24));
878
			for (int i = 0; i < 48; i++)
879
				array.Remove(i);
880

    
881
			//Almost empty tree:
882
			Assert.IsFalse(array.Remove(26));
883
			Assert.IsTrue(array.Remove(48));
884
			Assert.IsTrue(array.Check(), "Bad tree");
885

    
886
			//Empty tree:
887
			Assert.IsFalse(array.Remove(26));
888
			Assert.IsTrue(array.Check(), "Bad tree");
889
		}
890

    
891

    
892
		[TearDown]
893
		public void Dispose()
894
		{
895
			array = null;
896
		}
897
	}
898

    
899

    
900

    
901
	[TestFixture]
902
	public class PredecessorStructure
903
	{
904
		private SortedArray<int> tree;
905

    
906

    
907
		[SetUp]
908
		public void Init()
909
		{
910
			tree = new SortedArray<int>(new IC());
911
		}
912

    
913

    
914
		private void loadup()
915
		{
916
			for (int i = 0; i < 20; i++)
917
				tree.Add(2 * i);
918
		}
919

    
920
        [Test]
921
        public void FindPredecessor()
922
        {
923
            loadup();
924
            int res;
925
            Assert.IsTrue(tree.TryPredecessor(7, out res) && res == 6);
926
            Assert.IsTrue(tree.TryPredecessor(8, out res) && res == 6);
927

    
928
            //The bottom
929
            Assert.IsTrue(tree.TryPredecessor(1, out res) && res == 0);
930

    
931
            //The top
932
            Assert.IsTrue(tree.TryPredecessor(39, out res) && res == 38);
933
        }
934

    
935
        [Test]
936
        public void FindPredecessorTooLow1()
937
        {
938
            int res;
939
            Assert.IsFalse(tree.TryPredecessor(-2, out res));
940
            Assert.AreEqual(0, res);
941
            Assert.IsFalse(tree.TryPredecessor(0, out res));
942
            Assert.AreEqual(0, res);
943
        }
944

    
945
        [Test]
946
        public void FindWeakPredecessor()
947
        {
948
            loadup();
949
            int res;
950
            Assert.IsTrue(tree.TryWeakPredecessor(7, out res) && res == 6);
951
            Assert.IsTrue(tree.TryWeakPredecessor(8, out res) && res == 8);
952

    
953
            //The bottom
954
            Assert.IsTrue(tree.TryWeakPredecessor(1, out res) && res == 0);
955
            Assert.IsTrue(tree.TryWeakPredecessor(0, out res) && res == 0);
956

    
957
            //The top
958
            Assert.IsTrue(tree.TryWeakPredecessor(39, out res) && res == 38);
959
            Assert.IsTrue(tree.TryWeakPredecessor(38, out res) && res == 38);
960
        }
961

    
962
        [Test]
963
        public void FindWeakPredecessorTooLow1()
964
        {
965
            int res;
966
            Assert.IsFalse(tree.TryWeakPredecessor(-1, out res));
967
            Assert.AreEqual(0, res);
968
        }
969

    
970
        [Test]
971
        public void FindSuccessor()
972
        {
973
            loadup();
974
            int res;
975
            Assert.IsTrue(tree.TrySuccessor(7, out res) && res == 8);
976
            Assert.IsTrue(tree.TrySuccessor(8, out res) && res == 10);
977

    
978
            //The bottom
979
            Assert.IsTrue(tree.TrySuccessor(0, out res) && res == 2);
980
            Assert.IsTrue(tree.TrySuccessor(-1, out res) && res == 0);
981

    
982
            //The top
983
            Assert.IsTrue(tree.TrySuccessor(37, out res) && res == 38);
984
        }
985

    
986
        [Test]
987
        public void FindSuccessorTooHigh()
988
        {
989
            int res;
990
            Assert.IsFalse(tree.TrySuccessor(38, out res));
991
            Assert.AreEqual(0, res);
992
            Assert.IsFalse(tree.TrySuccessor(39, out res));
993
            Assert.AreEqual(0, res);
994
        }
995

    
996
        [Test]
997
        public void FindWeakSuccessor()
998
        {
999
            loadup();
1000
            int res;
1001
            Assert.IsTrue(tree.TryWeakSuccessor(6, out res) && res == 6);
1002
            Assert.IsTrue(tree.TryWeakSuccessor(7, out res) && res == 8);
1003

    
1004
            //The bottom
1005
            Assert.IsTrue(tree.TryWeakSuccessor(-1, out res) && res == 0);
1006
            Assert.IsTrue(tree.TryWeakSuccessor(0, out res) && res == 0);
1007

    
1008
            //The top
1009
            Assert.IsTrue(tree.TryWeakSuccessor(37, out res) && res == 38);
1010
            Assert.IsTrue(tree.TryWeakSuccessor(38, out res) && res == 38);
1011
        }
1012

    
1013
        [Test]
1014
        public void FindWeakSuccessorTooHigh1()
1015
        {
1016
            int res;
1017
            Assert.IsFalse(tree.TryWeakSuccessor(39, out res));
1018
            Assert.AreEqual(0, res);
1019
        }
1020

    
1021

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

    
1029
			//The bottom
1030
			Assert.AreEqual(0, tree.Predecessor(1));
1031

    
1032
			//The top
1033
			Assert.AreEqual(38, tree.Predecessor(39));
1034
		}
1035

    
1036
		[Test]
1037
		[ExpectedException(typeof(NoSuchItemException))]
1038
		public void PredecessorTooLow1()
1039
		{
1040
			tree.Predecessor(-2);
1041
		}
1042

    
1043

    
1044
		[Test]
1045
		[ExpectedException(typeof(NoSuchItemException))]
1046
		public void PredecessorTooLow2()
1047
		{
1048
			tree.Predecessor(0);
1049
		}
1050

    
1051
		[Test]
1052
		public void WeakPredecessor()
1053
		{
1054
			loadup();
1055
			Assert.AreEqual(6, tree.WeakPredecessor(7));
1056
			Assert.AreEqual(8, tree.WeakPredecessor(8));
1057

    
1058
			//The bottom
1059
			Assert.AreEqual(0, tree.WeakPredecessor(1));
1060
			Assert.AreEqual(0, tree.WeakPredecessor(0));
1061

    
1062
			//The top
1063
			Assert.AreEqual(38, tree.WeakPredecessor(39));
1064
			Assert.AreEqual(38, tree.WeakPredecessor(38));
1065
		}
1066

    
1067
		[Test]
1068
        [ExpectedException(typeof(NoSuchItemException))]
1069
        public void WeakPredecessorTooLow1()
1070
		{
1071
			tree.WeakPredecessor(-1);
1072
		}
1073

    
1074

    
1075
		[Test]
1076
		public void Successor()
1077
		{
1078
			loadup();
1079
			Assert.AreEqual(8, tree.Successor(7));
1080
			Assert.AreEqual(10, tree.Successor(8));
1081

    
1082
			//The bottom
1083
			Assert.AreEqual(2, tree.Successor(0));
1084
			Assert.AreEqual(0, tree.Successor(-1));
1085

    
1086
			//The top
1087
			Assert.AreEqual(38, tree.Successor(37));
1088
		}
1089

    
1090

    
1091
		[Test]
1092
        [ExpectedException(typeof(NoSuchItemException))]
1093
        public void SuccessorTooHigh1()
1094
		{
1095
			tree.Successor(38);
1096
		}
1097

    
1098

    
1099
		[Test]
1100
        [ExpectedException(typeof(NoSuchItemException))]
1101
        public void SuccessorTooHigh2()
1102
		{
1103
			tree.Successor(39);
1104
		}
1105

    
1106

    
1107
		[Test]
1108
		public void WeakSuccessor()
1109
		{
1110
			loadup();
1111
			Assert.AreEqual(6, tree.WeakSuccessor(6));
1112
			Assert.AreEqual(8, tree.WeakSuccessor(7));
1113

    
1114
			//The bottom
1115
			Assert.AreEqual(0, tree.WeakSuccessor(-1));
1116
			Assert.AreEqual(0, tree.WeakSuccessor(0));
1117

    
1118
			//The top
1119
			Assert.AreEqual(38, tree.WeakSuccessor(37));
1120
			Assert.AreEqual(38, tree.WeakSuccessor(38));
1121
		}
1122

    
1123
		[Test]
1124
        [ExpectedException(typeof(NoSuchItemException))]
1125
        public void WeakSuccessorTooHigh1()
1126
		{
1127
			tree.WeakSuccessor(39);
1128
		}
1129

    
1130

    
1131
		[TearDown]
1132
		public void Dispose()
1133
		{
1134
			tree = null;
1135
		}
1136
	}
1137

    
1138

    
1139

    
1140
	[TestFixture]
1141
	public class PriorityQueue
1142
	{
1143
		private SortedArray<int> tree;
1144

    
1145

    
1146
		[SetUp]
1147
		public void Init()
1148
		{
1149
			tree = new SortedArray<int>(new IC());
1150
		}
1151

    
1152

    
1153
		private void loadup()
1154
		{
1155
			foreach (int i in new int[] { 1, 2, 3, 4 })
1156
				tree.Add(i);
1157
		}
1158

    
1159

    
1160
		[Test]
1161
		public void Normal()
1162
		{
1163
			loadup();
1164
			Assert.AreEqual(1, tree.FindMin());
1165
			Assert.AreEqual(4, tree.FindMax());
1166
			Assert.AreEqual(1, tree.DeleteMin());
1167
			Assert.AreEqual(4, tree.DeleteMax());
1168
			Assert.IsTrue(tree.Check(), "Bad tree");
1169
			Assert.AreEqual(2, tree.FindMin());
1170
			Assert.AreEqual(3, tree.FindMax());
1171
			Assert.AreEqual(2, tree.DeleteMin());
1172
			Assert.AreEqual(3, tree.DeleteMax());
1173
			Assert.IsTrue(tree.Check(), "Bad tree");
1174
		}
1175

    
1176

    
1177
		[Test]
1178
		[ExpectedException(typeof(NoSuchItemException))]
1179
		public void Empty1()
1180
		{
1181
			tree.FindMin();
1182
		}
1183

    
1184

    
1185
		[Test]
1186
		[ExpectedException(typeof(NoSuchItemException))]
1187
		public void Empty2()
1188
		{
1189
			tree.FindMax();
1190
		}
1191

    
1192

    
1193
		[Test]
1194
		[ExpectedException(typeof(NoSuchItemException))]
1195
		public void Empty3()
1196
		{
1197
			tree.DeleteMin();
1198
		}
1199

    
1200

    
1201
		[Test]
1202
		[ExpectedException(typeof(NoSuchItemException))]
1203
		public void Empty4()
1204
		{
1205
			tree.DeleteMax();
1206
		}
1207

    
1208

    
1209
		[TearDown]
1210
		public void Dispose()
1211
		{
1212
			tree = null;
1213
		}
1214
	}
1215

    
1216

    
1217

    
1218
	[TestFixture]
1219
	public class IndexingAndCounting
1220
	{
1221
		private SortedArray<int> array;
1222

    
1223

    
1224
		[SetUp]
1225
		public void Init()
1226
		{
1227
			array = new SortedArray<int>(new IC());
1228
		}
1229

    
1230

    
1231
		private void populate()
1232
		{
1233
			array.Add(30);
1234
			array.Add(50);
1235
			array.Add(10);
1236
			array.Add(70);
1237
		}
1238

    
1239

    
1240
		[Test]
1241
		public void ToArray()
1242
		{
1243
			populate();
1244

    
1245
			int[] a = array.ToArray();
1246

    
1247
			Assert.AreEqual(4, a.Length);
1248
			Assert.AreEqual(10, a[0]);
1249
			Assert.AreEqual(30, a[1]);
1250
			Assert.AreEqual(50, a[2]);
1251
			Assert.AreEqual(70, a[3]);
1252
		}
1253

    
1254

    
1255
		[Test]
1256
		public void GoodIndex()
1257
		{
1258
			Assert.AreEqual(~0, array.IndexOf(20));
1259
			Assert.AreEqual(~0, array.LastIndexOf(20));
1260
			populate();
1261
			Assert.AreEqual(10, array[0]);
1262
			Assert.AreEqual(30, array[1]);
1263
			Assert.AreEqual(50, array[2]);
1264
			Assert.AreEqual(70, array[3]);
1265
			Assert.AreEqual(0, array.IndexOf(10));
1266
			Assert.AreEqual(1, array.IndexOf(30));
1267
			Assert.AreEqual(2, array.IndexOf(50));
1268
			Assert.AreEqual(3, array.IndexOf(70));
1269
			Assert.AreEqual(~1, array.IndexOf(20));
1270
			Assert.AreEqual(~0, array.IndexOf(0));
1271
			Assert.AreEqual(~4, array.IndexOf(90));
1272
			Assert.AreEqual(0, array.LastIndexOf(10));
1273
			Assert.AreEqual(1, array.LastIndexOf(30));
1274
			Assert.AreEqual(2, array.LastIndexOf(50));
1275
			Assert.AreEqual(3, array.LastIndexOf(70));
1276
			Assert.AreEqual(~1, array.LastIndexOf(20));
1277
			Assert.AreEqual(~0, array.LastIndexOf(0));
1278
			Assert.AreEqual(~4, array.LastIndexOf(90));
1279
		}
1280

    
1281

    
1282
		[Test]
1283
		[ExpectedException(typeof(IndexOutOfRangeException))]
1284
		public void IndexTooLarge()
1285
		{
1286
			populate();
1287
			Console.WriteLine(array[4]);
1288
		}
1289

    
1290

    
1291
		[Test]
1292
		[ExpectedException(typeof(IndexOutOfRangeException))]
1293
		public void IndexTooSmall()
1294
		{
1295
			populate();
1296
			Console.WriteLine(array[-1]);
1297
		}
1298

    
1299

    
1300
		[Test]
1301
		public void FilledTreeOutsideInput()
1302
		{
1303
			populate();
1304
			Assert.AreEqual(0, array.CountFrom(90));
1305
			Assert.AreEqual(0, array.CountFromTo(-20, 0));
1306
			Assert.AreEqual(0, array.CountFromTo(80, 100));
1307
			Assert.AreEqual(0, array.CountTo(0));
1308
			Assert.AreEqual(4, array.CountTo(90));
1309
			Assert.AreEqual(4, array.CountFromTo(-20, 90));
1310
			Assert.AreEqual(4, array.CountFrom(0));
1311
		}
1312

    
1313

    
1314
		[Test]
1315
		public void FilledTreeIntermediateInput()
1316
		{
1317
			populate();
1318
			Assert.AreEqual(3, array.CountFrom(20));
1319
			Assert.AreEqual(1, array.CountFromTo(20, 40));
1320
			Assert.AreEqual(2, array.CountTo(40));
1321
		}
1322

    
1323

    
1324
		[Test]
1325
		public void FilledTreeMatchingInput()
1326
		{
1327
			populate();
1328
			Assert.AreEqual(3, array.CountFrom(30));
1329
			Assert.AreEqual(2, array.CountFromTo(30, 70));
1330
			Assert.AreEqual(0, array.CountFromTo(50, 30));
1331
			Assert.AreEqual(0, array.CountFromTo(50, 50));
1332
			Assert.AreEqual(0, array.CountTo(10));
1333
			Assert.AreEqual(2, array.CountTo(50));
1334
		}
1335

    
1336

    
1337
		[Test]
1338
		public void CountEmptyTree()
1339
		{
1340
			Assert.AreEqual(0, array.CountFrom(20));
1341
			Assert.AreEqual(0, array.CountFromTo(20, 40));
1342
			Assert.AreEqual(0, array.CountTo(40));
1343
		}
1344

    
1345

    
1346
		[TearDown]
1347
		public void Dispose()
1348
		{
1349
			array = null;
1350
		}
1351
	}
1352

    
1353

    
1354

    
1355

    
1356
	namespace ModificationCheck
1357
	{
1358
		[TestFixture]
1359
		public class Enumerator
1360
		{
1361
			private SortedArray<int> tree;
1362

    
1363
			private SCG.IEnumerator<int> e;
1364

    
1365

    
1366
			[SetUp]
1367
			public void Init()
1368
			{
1369
				tree = new SortedArray<int>(new IC());
1370
				for (int i = 0; i < 10; i++)
1371
					tree.Add(i);
1372

    
1373
				e = tree.GetEnumerator();
1374
			}
1375

    
1376

    
1377
			[Test]
1378
			public void CurrentAfterModification()
1379
			{
1380
				e.MoveNext();
1381
				tree.Add(34);
1382
				Assert.AreEqual(0, e.Current);
1383
			}
1384

    
1385

    
1386
			[Test]
1387
      [ExpectedException(typeof(CollectionModifiedException))]
1388
      public void MoveNextAfterAdd()
1389
			{
1390
				e.MoveNext();
1391
				tree.Add(34);
1392
				e.MoveNext();
1393
			}
1394

    
1395

    
1396

    
1397

    
1398
			[Test]
1399
      [ExpectedException(typeof(CollectionModifiedException))]
1400
      public void MoveNextAfterRemove()
1401
			{
1402
				e.MoveNext();
1403
				tree.Remove(34);
1404
				e.MoveNext();
1405
			}
1406

    
1407

    
1408
			[Test]
1409
      [ExpectedException(typeof(CollectionModifiedException))]
1410
      public void MoveNextAfterClear()
1411
			{
1412
				e.MoveNext();
1413
				tree.Clear();
1414
				e.MoveNext();
1415
			}
1416

    
1417

    
1418
			[TearDown]
1419
			public void Dispose()
1420
			{
1421
				tree = null;
1422
				e = null;
1423
			}
1424
		}
1425

    
1426

    
1427

    
1428
		[TestFixture]
1429
		public class RangeEnumerator
1430
		{
1431
			private SortedArray<int> tree;
1432

    
1433
			private SCG.IEnumerator<int> e;
1434

    
1435

    
1436
			[SetUp]
1437
			public void Init()
1438
			{
1439
				tree = new SortedArray<int>(new IC());
1440
				for (int i = 0; i < 10; i++)
1441
					tree.Add(i);
1442

    
1443
				e = tree.RangeFromTo(3, 7).GetEnumerator();
1444
			}
1445

    
1446

    
1447
			[Test]
1448
			public void CurrentAfterModification()
1449
			{
1450
				e.MoveNext();
1451
				tree.Add(34);
1452
				Assert.AreEqual(3, e.Current);
1453
			}
1454

    
1455

    
1456
			[Test]
1457
      [ExpectedException(typeof(CollectionModifiedException))]
1458
      public void MoveNextAfterAdd()
1459
			{
1460
				tree.Add(34);
1461
				e.MoveNext();
1462
			}
1463

    
1464

    
1465

    
1466

    
1467
			[Test]
1468
      [ExpectedException(typeof(CollectionModifiedException))]
1469
      public void MoveNextAfterRemove()
1470
			{
1471
				tree.Remove(34);
1472
				e.MoveNext();
1473
			}
1474

    
1475

    
1476
			[Test]
1477
      [ExpectedException(typeof(CollectionModifiedException))]
1478
      public void MoveNextAfterClear()
1479
			{
1480
				tree.Clear();
1481
				e.MoveNext();
1482
			}
1483

    
1484

    
1485
			[TearDown]
1486
			public void Dispose()
1487
			{
1488
				tree = null;
1489
				e = null;
1490
			}
1491
		}
1492
	}
1493

    
1494
	namespace HigherOrder
1495
	{
1496
		internal class CubeRoot: IComparable<int>
1497
		{
1498
			private int c;
1499

    
1500

    
1501
			internal CubeRoot(int c) { this.c = c; }
1502

    
1503

    
1504
			public int CompareTo(int that) { return c - that * that * that; }
1505

    
1506
            public bool Equals(int that) { return c == that * that * that; }
1507

    
1508
		}
1509

    
1510

    
1511

    
1512
		class Interval: IComparable<int>
1513
		{
1514
			private int b, t;
1515

    
1516

    
1517
			internal Interval(int b, int t) { this.b = b; this.t = t; }
1518

    
1519

    
1520
			public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
1521

    
1522
            public bool Equals(int that) { return that >= b && that <= t; }
1523
        }
1524

    
1525

    
1526

    
1527
		[TestFixture]
1528
		public class Simple
1529
		{
1530
			private SortedArray<int> array;
1531

    
1532
			private SCG.IComparer<int> ic;
1533

    
1534

    
1535
			[SetUp]
1536
			public void Init()
1537
			{
1538
				ic = new IC();
1539
				array = new SortedArray<int>(ic);
1540
			}
1541

    
1542

    
1543
			private bool never(int i) { return false; }
1544

    
1545

    
1546
			private bool always(int i) { return true; }
1547

    
1548

    
1549
			private bool even(int i) { return i % 2 == 0; }
1550

    
1551

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

    
1554

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

    
1557

    
1558
			private int appfield1;
1559

    
1560
			private int appfield2;
1561

    
1562

    
1563
			private void apply(int i) { appfield1++; appfield2 += i * i; }
1564

    
1565

    
1566
			[Test]
1567
			public void Apply()
1568
			{
1569
				Simple simple1 = new Simple();
1570

    
1571
				array.Apply(new Act<int>(simple1.apply));
1572
				Assert.AreEqual(0, simple1.appfield1);
1573
				Assert.AreEqual(0, simple1.appfield2);
1574

    
1575
				Simple simple2 = new Simple();
1576

    
1577
				for (int i = 0; i < 10; i++) array.Add(i);
1578

    
1579
				array.Apply(new Act<int>(simple2.apply));
1580
				Assert.AreEqual(10, simple2.appfield1);
1581
				Assert.AreEqual(285, simple2.appfield2);
1582
			}
1583

    
1584

    
1585
			[Test]
1586
			public void All()
1587
			{
1588
				Assert.IsTrue(array.All(new Fun<int, bool>(never)));
1589
				Assert.IsTrue(array.All(new Fun<int, bool>(even)));
1590
				Assert.IsTrue(array.All(new Fun<int, bool>(always)));
1591
				for (int i = 0; i < 10; i++)					array.Add(i);
1592

    
1593
				Assert.IsFalse(array.All(new Fun<int, bool>(never)));
1594
				Assert.IsFalse(array.All(new Fun<int, bool>(even)));
1595
				Assert.IsTrue(array.All(new Fun<int, bool>(always)));
1596
				array.Clear();
1597
				for (int i = 0; i < 10; i++)					array.Add(i * 2);
1598

    
1599
				Assert.IsFalse(array.All(new Fun<int, bool>(never)));
1600
				Assert.IsTrue(array.All(new Fun<int, bool>(even)));
1601
				Assert.IsTrue(array.All(new Fun<int, bool>(always)));
1602
				array.Clear();
1603
				for (int i = 0; i < 10; i++)					array.Add(i * 2 + 1);
1604

    
1605
				Assert.IsFalse(array.All(new Fun<int, bool>(never)));
1606
				Assert.IsFalse(array.All(new Fun<int, bool>(even)));
1607
				Assert.IsTrue(array.All(new Fun<int, bool>(always)));
1608
			}
1609

    
1610

    
1611
			[Test]
1612
			public void Exists()
1613
			{
1614
				Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
1615
				Assert.IsFalse(array.Exists(new Fun<int, bool>(even)));
1616
				Assert.IsFalse(array.Exists(new Fun<int, bool>(always)));
1617
				for (int i = 0; i < 10; i++)					array.Add(i);
1618

    
1619
				Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
1620
				Assert.IsTrue(array.Exists(new Fun<int, bool>(even)));
1621
				Assert.IsTrue(array.Exists(new Fun<int, bool>(always)));
1622
				array.Clear();
1623
				for (int i = 0; i < 10; i++)					array.Add(i * 2);
1624

    
1625
				Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
1626
				Assert.IsTrue(array.Exists(new Fun<int, bool>(even)));
1627
				Assert.IsTrue(array.Exists(new Fun<int, bool>(always)));
1628
				array.Clear();
1629
				for (int i = 0; i < 10; i++)					array.Add(i * 2 + 1);
1630

    
1631
				Assert.IsFalse(array.Exists(new Fun<int, bool>(never)));
1632
				Assert.IsFalse(array.Exists(new Fun<int, bool>(even)));
1633
				Assert.IsTrue(array.Exists(new Fun<int, bool>(always)));
1634
			}
1635

    
1636

    
1637
			[Test]
1638
			public void FindAll()
1639
			{
1640
				Assert.AreEqual(0, array.FindAll(new Fun<int, bool>(never)).Count);
1641
				for (int i = 0; i < 10; i++)
1642
					array.Add(i);
1643

    
1644
				Assert.AreEqual(0, array.FindAll(new Fun<int, bool>(never)).Count);
1645
				Assert.AreEqual(10, array.FindAll(new Fun<int, bool>(always)).Count);
1646
				Assert.AreEqual(5, array.FindAll(new Fun<int, bool>(even)).Count);
1647
				Assert.IsTrue(((SortedArray<int>)array.FindAll(new Fun<int, bool>(even))).Check());
1648
			}
1649

    
1650

    
1651
			[Test]
1652
			public void Map()
1653
			{
1654
				Assert.AreEqual(0, array.Map(new Fun<int,string>(themap), new SC()).Count);
1655
				for (int i = 0; i < 11; i++)
1656
					array.Add(i * i * i);
1657

    
1658
				IIndexedSorted<string> res = array.Map(new Fun<int,string>(themap), new SC());
1659

    
1660
				Assert.IsTrue(((SortedArray<string>)res).Check());
1661
				Assert.AreEqual(11, res.Count);
1662
				Assert.AreEqual("AA    0 BB", res[0]);
1663
				Assert.AreEqual("AA   27 BB", res[3]);
1664
				Assert.AreEqual("AA  125 BB", res[5]);
1665
				Assert.AreEqual("AA 1000 BB", res[10]);
1666
			}
1667

    
1668

    
1669
			[Test]
1670
			[ExpectedException(typeof(ArgumentException), "mapper not monotonic")]
1671
			public void BadMap()
1672
			{
1673
				for (int i = 0; i < 11; i++)
1674
					array.Add(i * i * i);
1675

    
1676
				ISorted<string> res = array.Map(new Fun<int,string>(badmap), new SC());
1677
			}
1678

    
1679

    
1680
			[Test]
1681
			public void Cut()
1682
			{
1683
				for (int i = 0; i < 10; i++)
1684
					array.Add(i);
1685

    
1686
				int low, high;
1687
				bool lval, hval;
1688

    
1689
				Assert.IsTrue(array.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
1690
				Assert.IsTrue(lval && hval);
1691
				Assert.AreEqual(4, high);
1692
				Assert.AreEqual(2, low);
1693
				Assert.IsFalse(array.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
1694
				Assert.IsTrue(lval && hval);
1695
				Assert.AreEqual(4, high);
1696
				Assert.AreEqual(3, low);
1697
			}
1698

    
1699

    
1700
			[Test]
1701
			public void CutInt()
1702
			{
1703
				for (int i = 0; i < 10; i++)
1704
					array.Add(2 * i);
1705

    
1706
				int low, high;
1707
				bool lval, hval;
1708

    
1709
				Assert.IsFalse(array.Cut(new IC(3), out low, out lval, out high, out hval));
1710
				Assert.IsTrue(lval && hval);
1711
				Assert.AreEqual(4, high);
1712
				Assert.AreEqual(2, low);
1713
				Assert.IsTrue(array.Cut(new IC(6), out low, out lval, out high, out hval));
1714
				Assert.IsTrue(lval && hval);
1715
				Assert.AreEqual(8, high);
1716
				Assert.AreEqual(4, low);
1717
			}
1718

    
1719

    
1720
			[Test]
1721
			public void CutInterval()
1722
			{
1723
				for (int i = 0; i < 10; i++)
1724
					array.Add(2 * i);
1725

    
1726
				int lo, hi;
1727
				bool lv, hv;
1728

    
1729
				Assert.IsTrue(array.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
1730
				Assert.IsTrue(lv && hv);
1731
				Assert.AreEqual(10, hi);
1732
				Assert.AreEqual(4, lo);
1733
				Assert.IsTrue(array.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
1734
				Assert.IsTrue(lv && hv);
1735
				Assert.AreEqual(12, hi);
1736
				Assert.AreEqual(4, lo);
1737
				for (int i = 0; i < 100; i++)
1738
					array.Add(2 * i);
1739

    
1740
				array.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
1741
				Assert.IsTrue(lv && hv);
1742
				Assert.AreEqual(106, hi);
1743
				Assert.AreEqual(76, lo);
1744
				array.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
1745
				Assert.IsTrue(lv && hv);
1746
				Assert.AreEqual(8, hi);
1747
				Assert.AreEqual(4, lo);
1748
				array.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
1749
				Assert.IsTrue(lv && hv);
1750
				Assert.AreEqual(112, hi);
1751
				Assert.AreEqual(78, lo);
1752
			}
1753

    
1754

    
1755
			[Test]
1756
			public void UpperCut()
1757
			{
1758
				for (int i = 0; i < 10; i++)
1759
					array.Add(i);
1760

    
1761
				int l, h;
1762
				bool lv, hv;
1763

    
1764
				Assert.IsFalse(array.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
1765
				Assert.IsTrue(lv && !hv);
1766
				Assert.AreEqual(9, l);
1767
				Assert.IsFalse(array.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
1768
				Assert.IsTrue(!lv && hv);
1769
				Assert.AreEqual(0, h);
1770
			}
1771

    
1772

    
1773
			[TearDown]
1774
			public void Dispose() { ic = null; array = null; }
1775
		}
1776
	}
1777

    
1778

    
1779

    
1780

    
1781
	namespace MultiOps
1782
	{
1783
		[TestFixture]
1784
		public class AddAll
1785
		{
1786
			private int sqr(int i) { return i * i; }
1787

    
1788

    
1789
			SortedArray<int> array;
1790

    
1791

    
1792
			[SetUp]
1793
			public void Init() { array = new SortedArray<int>(new IC()); }
1794

    
1795

    
1796
			[Test]
1797
			public void EmptyEmpty()
1798
			{
1799
				array.AddAll(new FunEnumerable(0, new Fun<int,int>(sqr)));
1800
				Assert.AreEqual(0, array.Count);
1801
				Assert.IsTrue(array.Check());
1802
			}
1803

    
1804

    
1805
			[Test]
1806
			public void SomeEmpty()
1807
			{
1808
				for (int i = 4; i < 9; i++) array.Add(i);
1809

    
1810
				array.AddAll(new FunEnumerable(0, new Fun<int,int>(sqr)));
1811
				Assert.AreEqual(5, array.Count);
1812
				Assert.IsTrue(array.Check());
1813
			}
1814

    
1815

    
1816
			[Test]
1817
			public void EmptySome()
1818
			{
1819
				array.AddAll(new FunEnumerable(4, new Fun<int,int>(sqr)));
1820
				Assert.AreEqual(4, array.Count);
1821
				Assert.IsTrue(array.Check());
1822
				Assert.AreEqual(0, array[0]);
1823
				Assert.AreEqual(1, array[1]);
1824
				Assert.AreEqual(4, array[2]);
1825
				Assert.AreEqual(9, array[3]);
1826
			}
1827

    
1828

    
1829
			[Test]
1830
			public void SomeSome()
1831
			{
1832
				for (int i = 3; i < 9; i++) array.Add(i);
1833

    
1834
				array.AddAll(new FunEnumerable(4, new Fun<int,int>(sqr)));
1835
				Assert.AreEqual(9, array.Count);
1836
				Assert.IsTrue(array.Check());
1837
				Assert.IsTrue(IC.eq(array, 0, 1, 3,4, 5, 6, 7, 8, 9));
1838
			}
1839

    
1840

    
1841
			[TearDown]
1842
			public void Dispose() { array = null; }
1843
		}
1844

    
1845

    
1846

    
1847
		[TestFixture]
1848
		public class AddSorted
1849
		{
1850
			private int sqr(int i) { return i * i; }
1851

    
1852

    
1853
			private int bad(int i) { return i * (5 - i); }
1854

    
1855

    
1856
			SortedArray<int> array;
1857

    
1858

    
1859
			[SetUp]
1860
			public void Init() { array = new SortedArray<int>(new IC()); }
1861

    
1862

    
1863
			[Test]
1864
			public void EmptyEmpty()
1865
			{
1866
				array.AddSorted(new FunEnumerable(0, new Fun<int,int>(sqr)));
1867
				Assert.AreEqual(0, array.Count);
1868
				Assert.IsTrue(array.Check());
1869
			}
1870

    
1871

    
1872

    
1873
			[Test]
1874
			public void SomeEmpty()
1875
			{
1876
				for (int i = 4; i < 9; i++) array.Add(i);
1877

    
1878
				array.AddSorted(new FunEnumerable(0, new Fun<int,int>(sqr)));
1879
				Assert.AreEqual(5, array.Count);
1880
				Assert.IsTrue(array.Check());
1881
			}
1882

    
1883

    
1884

    
1885
			[Test]
1886
			public void EmptySome()
1887
			{
1888
				array.AddSorted(new FunEnumerable(4, new Fun<int,int>(sqr)));
1889
				Assert.AreEqual(4, array.Count);
1890
				Assert.IsTrue(array.Check());
1891
				Assert.AreEqual(0, array[0]);
1892
				Assert.AreEqual(1, array[1]);
1893
				Assert.AreEqual(4, array[2]);
1894
				Assert.AreEqual(9, array[3]);
1895
			}
1896

    
1897

    
1898

    
1899
			[Test]
1900
			public void SomeSome()
1901
			{
1902
				for (int i = 3; i < 9; i++) array.Add(i);
1903

    
1904
				array.AddSorted(new FunEnumerable(4, new Fun<int,int>(sqr)));
1905
				Assert.AreEqual(9, array.Count);
1906
				Assert.IsTrue(array.Check());
1907
				Assert.IsTrue(IC.eq(array, 0, 1, 3, 4, 5, 6, 7, 8, 9));
1908
			}
1909

    
1910
			[Test]
1911
			[ExpectedException(typeof(ArgumentException), "Argument not sorted")]
1912
			public void EmptyBad()
1913
			{
1914
				array.AddSorted(new FunEnumerable(9, new Fun<int,int>(bad)));
1915
			}
1916

    
1917

    
1918
			[TearDown]
1919
			public void Dispose() { array = null; }
1920
		}
1921

    
1922
		[TestFixture]
1923
		public class Rest
1924
		{
1925
			SortedArray<int> array, array2;
1926

    
1927

    
1928
			[SetUp]
1929
			public void Init()
1930
			{
1931
				array = new SortedArray<int>(new IC());
1932
				array2 = new SortedArray<int>(new IC());
1933
				for (int i = 0; i < 10; i++)
1934
					array.Add(i);
1935

    
1936
				for (int i = 0; i < 10; i++)
1937
					array2.Add(2 * i);
1938
			}
1939

    
1940

    
1941
			[Test]
1942
			public void RemoveAll()
1943
			{
1944
				array.RemoveAll(array2.RangeFromTo(3, 7));
1945
				Assert.AreEqual(8, array.Count);
1946
				Assert.IsTrue(array.Check());
1947
				Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
1948
				array.RemoveAll(array2.RangeFromTo(3, 7));
1949
				Assert.AreEqual(8, array.Count);
1950
				Assert.IsTrue(array.Check());
1951
				Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
1952
				array.RemoveAll(array2.RangeFromTo(13, 17));
1953
				Assert.AreEqual(8, array.Count);
1954
				Assert.IsTrue(array.Check());
1955
				Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
1956
				array.RemoveAll(array2.RangeFromTo(3, 17));
1957
				Assert.AreEqual(7, array.Count);
1958
				Assert.IsTrue(array.Check());
1959
				Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 9));
1960
				for (int i = 0; i < 10; i++) array2.Add(i);
1961

    
1962
				array.RemoveAll(array2.RangeFromTo(-1, 10));
1963
				Assert.AreEqual(0, array.Count);
1964
				Assert.IsTrue(array.Check());
1965
				Assert.IsTrue(IC.eq(array));
1966
			}
1967

    
1968

    
1969
			[Test]
1970
			public void RetainAll()
1971
			{
1972
				array.RetainAll(array2.RangeFromTo(3, 17));
1973
				Assert.AreEqual(3, array.Count);
1974
				Assert.IsTrue(array.Check());
1975
				Assert.IsTrue(IC.eq(array, 4, 6, 8));
1976
				array.RetainAll(array2.RangeFromTo(1, 17));
1977
				Assert.AreEqual(3, array.Count);
1978
				Assert.IsTrue(array.Check());
1979
				Assert.IsTrue(IC.eq(array, 4, 6, 8));
1980
				array.RetainAll(array2.RangeFromTo(3, 5));
1981
				Assert.AreEqual(1, array.Count);
1982
				Assert.IsTrue(array.Check());
1983
				Assert.IsTrue(IC.eq(array, 4));
1984
				array.RetainAll(array2.RangeFromTo(7, 17));
1985
				Assert.AreEqual(0, array.Count);
1986
				Assert.IsTrue(array.Check());
1987
				Assert.IsTrue(IC.eq(array));
1988
				for (int i = 0; i < 10; i++) array.Add(i);
1989

    
1990
				array.RetainAll(array2.RangeFromTo(5, 5));
1991
				Assert.AreEqual(0, array.Count);
1992
				Assert.IsTrue(array.Check());
1993
				Assert.IsTrue(IC.eq(array));
1994
				for (int i = 0; i < 10; i++) array.Add(i);
1995

    
1996
				array.RetainAll(array2.RangeFromTo(15, 25));
1997
				Assert.AreEqual(0, array.Count);
1998
				Assert.IsTrue(array.Check());
1999
				Assert.IsTrue(IC.eq(array));
2000
			}
2001

    
2002

    
2003
			[Test]
2004
			public void ContainsAll()
2005
			{
2006
				Assert.IsFalse(array.ContainsAll(array2));
2007
				Assert.IsTrue(array.ContainsAll(array));
2008
				array2.Clear();
2009
				Assert.IsTrue(array.ContainsAll(array2));
2010
				array.Clear();
2011
				Assert.IsTrue(array.ContainsAll(array2));
2012
				array2.Add(8);
2013
				Assert.IsFalse(array.ContainsAll(array2));
2014
			}
2015

    
2016

    
2017
			[Test]
2018
			public void RemoveInterval()
2019
			{
2020
				array.RemoveInterval(3, 4);
2021
				Assert.IsTrue(array.Check());
2022
				Assert.AreEqual(6, array.Count);
2023
				Assert.IsTrue(IC.eq(array, 0, 1, 2, 7, 8, 9));
2024
				array.RemoveInterval(2, 3);
2025
				Assert.IsTrue(array.Check());
2026
				Assert.AreEqual(3, array.Count);
2027
				Assert.IsTrue(IC.eq(array, 0, 1, 9));
2028
				array.RemoveInterval(0, 3);
2029
				Assert.IsTrue(array.Check());
2030
				Assert.AreEqual(0, array.Count);
2031
				Assert.IsTrue(IC.eq(array));
2032
			}
2033

    
2034

    
2035
			[Test]
2036
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2037
			public void RemoveRangeBad1()
2038
			{
2039
				array.RemoveInterval(-3, 8);
2040
			}
2041

    
2042

    
2043
			[Test]
2044
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2045
			public void RemoveRangeBad2()
2046
			{
2047
				array.RemoveInterval(3, -8);
2048
			}
2049

    
2050

    
2051
			[Test]
2052
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2053
      public void RemoveRangeBad3()
2054
			{
2055
				array.RemoveInterval(3, 8);
2056
			}
2057

    
2058

    
2059
			[Test]
2060
			public void GetRange()
2061
			{
2062
				SCG.IEnumerable<int> e = array[3, 3];
2063

    
2064
				Assert.IsTrue(IC.eq(e, 3, 4, 5));
2065
				e = array[3, 0];
2066
				Assert.IsTrue(IC.eq(e));
2067
			}
2068

    
2069

    
2070
			[Test]
2071
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2072
			public void GetRangeBad1()
2073
			{
2074
				object foo = array[-3, 0];
2075
			}
2076

    
2077

    
2078
			[Test]
2079
			[ExpectedException(typeof(ArgumentOutOfRangeException))]
2080
			public void GetRangeBad2()
2081
			{
2082
				object foo = array[3, -1];
2083
			}
2084

    
2085

    
2086
			[Test]
2087
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
2088
      public void GetRangeBad3()
2089
			{
2090
				object foo = array[3, 8];
2091
			}
2092

    
2093

    
2094
			[TearDown]
2095
			public void Dispose() { array = null; array2 = null; }
2096
		}
2097
	}
2098

    
2099

    
2100

    
2101

    
2102
	namespace Sync
2103
	{
2104
		[TestFixture]
2105
		public class SyncRoot
2106
		{
2107
			private SortedArray<int> tree;
2108
      private readonly Object mySyncRoot = new Object();
2109
			int sz = 5000;
2110

    
2111

    
2112
			[Test]
2113
			public void Safe()
2114
			{
2115
				System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(safe1));
2116
				System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(safe2));
2117

    
2118
				t1.Start();
2119
				t2.Start();
2120
				t1.Join();
2121
				t2.Join();
2122
				Assert.AreEqual(2 * sz + 1, tree.Count);
2123
				Assert.IsTrue(tree.Check());
2124
			}
2125

    
2126

    
2127
			//[Test]
2128
			public void UnSafe()
2129
			{
2130
				bool bad = false;
2131

    
2132
				for (int i = 0; i < 10; i++)
2133
				{
2134
					System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
2135
					System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
2136

    
2137
					t1.Start();
2138
					t2.Start();
2139
					t1.Join();
2140
					t2.Join();
2141
					if (bad = 2 * sz + 1 != tree.Count)
2142
					{
2143
						Console.WriteLine("{0}::Unsafe(): bad at {1}", GetType(), i);
2144
						break;
2145
					}
2146
				}
2147

    
2148
				Assert.IsTrue(bad, "No sync problems!");
2149
			}
2150

    
2151

    
2152
			[Test]
2153
			public void SafeUnSafe()
2154
			{
2155
				System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
2156
				System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
2157

    
2158
				t1.Start();
2159
				t1.Join();
2160
				t2.Start();
2161
				t2.Join();
2162
				Assert.AreEqual(2 * sz + 1, tree.Count);
2163
			}
2164

    
2165

    
2166
			[SetUp]
2167
			public void Init() { tree = new SortedArray<int>(new IC()); }
2168

    
2169

    
2170
			private void unsafe1()
2171
			{
2172
				for (int i = 0; i < 2 * sz; i++)
2173
					tree.Add(i * 2);
2174

    
2175
				for (int i = 1; i < sz; i++)
2176
					tree.Remove(i * 4);
2177
			}
2178

    
2179

    
2180
			private void safe1()
2181
			{
2182
				for (int i = 0; i < 2 * sz; i++)
2183
					lock (mySyncRoot)
2184
						tree.Add(i * 2);
2185

    
2186
				for (int i = 1; i < sz; i++)
2187
					lock (mySyncRoot)
2188
						tree.Remove(i * 4);
2189
			}
2190

    
2191

    
2192
			private void unsafe2()
2193
			{
2194
				for (int i = 2 * sz; i > 0; i--)
2195
					tree.Add(i * 2 + 1);
2196

    
2197
				for (int i = sz; i > 0; i--)
2198
					tree.Remove(i * 4 + 1);
2199
			}
2200

    
2201

    
2202
			private void safe2()
2203
			{
2204
				for (int i = 2 * sz; i > 0; i--)
2205
					lock (mySyncRoot)
2206
						tree.Add(i * 2 + 1);
2207

    
2208
				for (int i = sz; i > 0; i--)
2209
					lock (mySyncRoot)
2210
						tree.Remove(i * 4 + 1);
2211
			}
2212

    
2213

    
2214
			[TearDown]
2215
			public void Dispose() { tree = null; }
2216
		}
2217

    
2218

    
2219

    
2220
		//[TestFixture]
2221
		public class ConcurrentQueries
2222
		{
2223
			private SortedArray<int> tree;
2224

    
2225
			int sz = 500000;
2226

    
2227

    
2228
			[SetUp]
2229
			public void Init()
2230
			{
2231
				tree = new SortedArray<int>(new IC());
2232
				for (int i = 0; i < sz; i++)
2233
				{
2234
					tree.Add(i);
2235
				}
2236
			}
2237

    
2238

    
2239

    
2240
			class A
2241
			{
2242
				public int count = 0;
2243

    
2244
				SortedArray<int> t;
2245

    
2246

    
2247
				public A(SortedArray<int> t) { this.t = t; }
2248

    
2249

    
2250
				public void a(int i) { count++; }
2251

    
2252

    
2253
				public void traverse() { t.Apply(new Act<int>(a)); }
2254
			}
2255

    
2256

    
2257

    
2258

    
2259
			[Test]
2260
			public void Safe()
2261
			{
2262
				A a = new A(tree);
2263

    
2264
				a.traverse();
2265
				Assert.AreEqual(sz, a.count);
2266
			}
2267

    
2268

    
2269
			[Test]
2270
			public void RegrettablyUnsafe()
2271
			{
2272
				System.Threading.Thread[] t = new System.Threading.Thread[10];
2273
				A[] a = new A[10];
2274
				for (int i = 0; i < 10; i++)
2275
				{
2276
					a[i] = new A(tree);
2277
					t[i] = new System.Threading.Thread(new System.Threading.ThreadStart(a[i].traverse));
2278
				}
2279

    
2280
				for (int i = 0; i < 10; i++)
2281
					t[i].Start();
2282
				for (int i = 0; i < 10; i++)
2283
					t[i].Join();
2284
				for (int i = 0; i < 10; i++)
2285
					Assert.AreEqual(sz,a[i].count);
2286

    
2287
			}
2288

    
2289

    
2290
			[TearDown]
2291
			public void Dispose() { tree = null; }
2292
		}
2293
	}
2294

    
2295

    
2296

    
2297

    
2298
	namespace Hashing
2299
	{
2300
		[TestFixture]
2301
		public class ISequenced
2302
		{
2303
			private ISequenced<int> dit, dat, dut;
2304

    
2305

    
2306
			[SetUp]
2307
			public void Init()
2308
			{
2309
                dit = new SortedArray<int>(8,Comparer<int>.Default, EqualityComparer<int>.Default);
2310
                dat = new SortedArray<int>(8,Comparer<int>.Default, EqualityComparer<int>.Default);
2311
                dut = new SortedArray<int>(8,new RevIC(), EqualityComparer<int>.Default);
2312
			}
2313

    
2314

    
2315
			[Test]
2316
			public void EmptyEmpty()
2317
			{
2318
				Assert.IsTrue(dit.SequencedEquals(dat));
2319
			}
2320

    
2321

    
2322
			[Test]
2323
			public void EmptyNonEmpty()
2324
			{
2325
				dit.Add(3);
2326
				Assert.IsFalse(dit.SequencedEquals(dat));
2327
				Assert.IsFalse(dat.SequencedEquals(dit));
2328
			}
2329

    
2330
			[Test]
2331
			public void HashVal()
2332
			{
2333
				Assert.AreEqual(CHC.sequencedhashcode(), dit.GetSequencedHashCode());
2334
				dit.Add(3);
2335
				Assert.AreEqual(CHC.sequencedhashcode(3), dit.GetSequencedHashCode());
2336
				dit.Add(7);
2337
				Assert.AreEqual(CHC.sequencedhashcode(3, 7), dit.GetSequencedHashCode());
2338
				Assert.AreEqual(CHC.sequencedhashcode(), dut.GetSequencedHashCode());
2339
				dut.Add(3);
2340
				Assert.AreEqual(CHC.sequencedhashcode(3), dut.GetSequencedHashCode());
2341
				dut.Add(7);
2342
				Assert.AreEqual(CHC.sequencedhashcode(7, 3), dut.GetSequencedHashCode());
2343
			}
2344

    
2345

    
2346
			[Test]
2347
			public void Normal()
2348
			{
2349
				dit.Add(3);
2350
				dit.Add(7);
2351
				dat.Add(3);
2352
				Assert.IsFalse(dit.SequencedEquals(dat));
2353
				Assert.IsFalse(dat.SequencedEquals(dit));
2354
				dat.Add(7);
2355
				Assert.IsTrue(dit.SequencedEquals(dat));
2356
				Assert.IsTrue(dat.SequencedEquals(dit));
2357
			}
2358

    
2359

    
2360
			[Test]
2361
			public void WrongOrder()
2362
			{
2363
				dit.Add(3);
2364
				dut.Add(3);
2365
				Assert.IsTrue(dit.SequencedEquals(dut));
2366
				Assert.IsTrue(dut.SequencedEquals(dit));
2367
				dit.Add(7);
2368
				dut.Add(7);
2369
				Assert.IsFalse(dit.SequencedEquals(dut));
2370
				Assert.IsFalse(dut.SequencedEquals(dit));
2371
			}
2372

    
2373

    
2374
			[Test]
2375
			public void Reflexive()
2376
			{
2377
				Assert.IsTrue(dit.SequencedEquals(dit));
2378
				dit.Add(3);
2379
				Assert.IsTrue(dit.SequencedEquals(dit));
2380
				dit.Add(7);
2381
				Assert.IsTrue(dit.SequencedEquals(dit));
2382
			}
2383

    
2384

    
2385
			[TearDown]
2386
			public void Dispose()
2387
			{
2388
				dit = null;
2389
				dat = null;
2390
				dut = null;
2391
			}
2392
		}
2393

    
2394

    
2395

    
2396
		[TestFixture]
2397
		public class IEditableCollection
2398
		{
2399
			private ICollection<int> dit, dat, dut;
2400

    
2401

    
2402
			[SetUp]
2403
			public void Init()
2404
			{
2405
                dit = new SortedArray<int>(8,Comparer<int>.Default, EqualityComparer<int>.Default);
2406
                dat = new SortedArray<int>(8,Comparer<int>.Default, EqualityComparer<int>.Default);
2407
                dut = new SortedArray<int>(8,new RevIC(), EqualityComparer<int>.Default);
2408
			}
2409

    
2410

    
2411
			[Test]
2412
			public void EmptyEmpty()
2413
			{
2414
				Assert.IsTrue(dit.UnsequencedEquals(dat));
2415
			}
2416

    
2417

    
2418
			[Test]
2419
			public void EmptyNonEmpty()
2420
			{
2421
				dit.Add(3);
2422
				Assert.IsFalse(dit.UnsequencedEquals(dat));
2423
				Assert.IsFalse(dat.UnsequencedEquals(dit));
2424
			}
2425

    
2426

    
2427
			[Test]
2428
			public void HashVal()
2429
			{
2430
                Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
2431
                dit.Add(3);
2432
                Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
2433
                dit.Add(7);
2434
                Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
2435
                Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
2436
                dut.Add(3);
2437
                Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
2438
                dut.Add(7);
2439
                Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
2440
            }
2441

    
2442

    
2443
			[Test]
2444
			public void Normal()
2445
			{
2446
				dit.Add(3);
2447
				dit.Add(7);
2448
				dat.Add(3);
2449
				Assert.IsFalse(dit.UnsequencedEquals(dat));
2450
				Assert.IsFalse(dat.UnsequencedEquals(dit));
2451
				dat.Add(7);
2452
				Assert.IsTrue(dit.UnsequencedEquals(dat));
2453
				Assert.IsTrue(dat.UnsequencedEquals(dit));
2454
			}
2455

    
2456

    
2457
			[Test]
2458
			public void WrongOrder()
2459
			{
2460
				dit.Add(3);
2461
				dut.Add(3);
2462
				Assert.IsTrue(dit.UnsequencedEquals(dut));
2463
				Assert.IsTrue(dut.UnsequencedEquals(dit));
2464
				dit.Add(7);
2465
				dut.Add(7);
2466
				Assert.IsTrue(dit.UnsequencedEquals(dut));
2467
				Assert.IsTrue(dut.UnsequencedEquals(dit));
2468
			}
2469

    
2470

    
2471
			[Test]
2472
			public void Reflexive()
2473
			{
2474
				Assert.IsTrue(dit.UnsequencedEquals(dit));
2475
				dit.Add(3);
2476
				Assert.IsTrue(dit.UnsequencedEquals(dit));
2477
				dit.Add(7);
2478
				Assert.IsTrue(dit.UnsequencedEquals(dit));
2479
			}
2480

    
2481

    
2482
			[TearDown]
2483
			public void Dispose()
2484
			{
2485
				dit = null;
2486
				dat = null;
2487
				dut = null;
2488
			}
2489
		}
2490

    
2491
	}
2492
}