Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Baulig <martin@novell.com>2004-11-15 03:12:52 +0300
committerMartin Baulig <martin@novell.com>2004-11-15 03:12:52 +0300
commitd91cda984ce03930ee4ed38fdd8a5407592dafd8 (patch)
tree1489dc4741078491e6cbf680b8b2dde81b388efc /mcs/class/Mono.C5
parent8422e31df9009b4f9e39442721fb99061b7c13e7 (diff)
2004-11-15 Martin Baulig <martin@ximian.com>
Importing version 0.5 of C5's test suite, http://www.itu.dk/research/c5/. svn path=/trunk/mcs/; revision=36110
Diffstat (limited to 'mcs/class/Mono.C5')
-rw-r--r--mcs/class/Mono.C5/Test/.cvsignore3
-rw-r--r--mcs/class/Mono.C5/Test/AssemblyInfo.cs79
-rw-r--r--mcs/class/Mono.C5/Test/BasesTest.cs256
-rw-r--r--mcs/class/Mono.C5/Test/ChangeLog4
-rw-r--r--mcs/class/Mono.C5/Test/Sorting.cs268
-rw-r--r--mcs/class/Mono.C5/Test/SupportClasses.cs156
-rw-r--r--mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs2453
-rw-r--r--mcs/class/Mono.C5/Test/arrays/HashedArrayListTest.cs2004
-rw-r--r--mcs/class/Mono.C5/Test/arrays/SortedArrayTests.cs2202
-rw-r--r--mcs/class/Mono.C5/Test/hashing/HashBagTests.cs464
-rw-r--r--mcs/class/Mono.C5/Test/hashing/HashDictionaryTests.cs250
-rw-r--r--mcs/class/Mono.C5/Test/hashing/HashTableTests.cs1058
-rw-r--r--mcs/class/Mono.C5/Test/heaps/HeapTests.cs191
-rw-r--r--mcs/class/Mono.C5/Test/linkedlists/HashedLinkedListTest.cs2035
-rw-r--r--mcs/class/Mono.C5/Test/linkedlists/LinkedListTest.cs2404
-rw-r--r--mcs/class/Mono.C5/Test/nunit.csproj127
-rw-r--r--mcs/class/Mono.C5/Test/nunit.csproj.user58
-rw-r--r--mcs/class/Mono.C5/Test/trees/Bag.cs2785
-rw-r--r--mcs/class/Mono.C5/Test/trees/Dictionary.cs264
-rw-r--r--mcs/class/Mono.C5/Test/trees/RedBlackTreeSetTests.cs2709
20 files changed, 19770 insertions, 0 deletions
diff --git a/mcs/class/Mono.C5/Test/.cvsignore b/mcs/class/Mono.C5/Test/.cvsignore
new file mode 100644
index 00000000000..69eecaa3ab4
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/.cvsignore
@@ -0,0 +1,3 @@
+bin
+obj
+Project \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/AssemblyInfo.cs b/mcs/class/Mono.C5/Test/AssemblyInfo.cs
new file mode 100644
index 00000000000..e08ead5c6e5
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/AssemblyInfo.cs
@@ -0,0 +1,79 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+//
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+//
+[assembly: AssemblyTitle("")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("")]
+[assembly: AssemblyCopyright("")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+//
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Revision and Build Numbers
+// by using the '*' as shown below:
+
+[assembly: AssemblyVersion("1.0.*")]
+
+//
+// In order to sign your assembly you must specify a key to use. Refer to the
+// Microsoft .NET Framework documentation for more information on assembly signing.
+//
+// Use the attributes below to control which key is used for signing.
+//
+// Notes:
+// (*) If no key is specified, the assembly is not signed.
+// (*) KeyName refers to a key that has been installed in the Crypto Service
+// Provider (CSP) on your machine. KeyFile refers to a file which contains
+// a key.
+// (*) If the KeyFile and the KeyName values are both specified, the
+// following processing occurs:
+// (1) If the KeyName can be found in the CSP, that key is used.
+// (2) If the KeyName does not exist and the KeyFile does exist, the key
+// in the KeyFile is installed into the CSP and used.
+// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
+// When specifying the KeyFile, the location of the KeyFile should be
+// relative to the project output directory which is
+// %Project Directory%\obj\<configuration>. For example, if your KeyFile is
+// located in the project directory, you would specify the AssemblyKeyFile
+// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
+// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
+// documentation for more information on this.
+//
+[assembly: AssemblyDelaySign(false)]
+[assembly: AssemblyKeyFile("")]
+[assembly: AssemblyKeyName("")]
diff --git a/mcs/class/Mono.C5/Test/BasesTest.cs b/mcs/class/Mono.C5/Test/BasesTest.cs
new file mode 100644
index 00000000000..e4def19044b
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/BasesTest.cs
@@ -0,0 +1,256 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+
+namespace nunit.support
+{
+ namespace bases
+ {
+ [TestFixture]
+ public class ArrayBaseTest
+ {
+ class ABT: ArrayBase<string>
+ {
+ public ABT() : base(8,new DefaultReferenceTypeHasher<string>()) { }
+
+
+ public string this[int i] { get { return array[i]; } set { array[i] = value; } }
+
+
+ public int thesize { get { return size; } set { size = value; } }
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ ABT abt = new ABT();
+
+ abt.thesize = 3;
+ abt[2] = "aaa";
+ Assert.IsFalse(abt.Check());
+ abt[0] = "##";
+ abt[1] = "##";
+ Assert.IsTrue(abt.Check());
+ }
+ }
+ }
+
+
+
+
+ namespace itemops
+ {
+ [TestFixture]
+ public class Comparers
+ {
+ class dbl: IComparable<dbl>
+ {
+ double d;
+
+ public dbl(double din) { d = din; }
+
+ public int CompareTo(dbl that)
+ {
+ return d < that.d ? -1 : d == that.d ? 0 : 1;
+ }
+ public bool Equals(dbl that) { return d == that.d; }
+ }
+
+ [Test]
+ public void GenericC()
+ {
+ IComparer<dbl> h = new NaturalComparer<dbl>();
+ dbl s = new dbl(3.4);
+ dbl t = new dbl(3.4);
+ dbl u = new dbl(7.4);
+
+ Assert.AreEqual(0, h.Compare(s, t));
+ Assert.IsTrue(h.Compare(s, u) < 0);
+ }
+
+
+ [Test]
+ public void OrdinaryC()
+ {
+ IComparer<string> h = new NaturalComparerO<string>();
+ string s = "bamse";
+ string t = "bamse";
+ string u = "bimse";
+
+ Assert.AreEqual(0, h.Compare(s, t));
+ Assert.IsTrue(h.Compare(s, u) < 0);
+ }
+
+
+ [Test]
+ public void GenericCViaBuilder()
+ {
+ IComparer<dbl> h = C5.ComparerBuilder.FromComparable<dbl>.Examine();
+ dbl s = new dbl(3.4);
+ dbl t = new dbl(3.4);
+ dbl u = new dbl(7.4);
+
+ Assert.AreEqual(0, h.Compare(s, t));
+ Assert.IsTrue(h.Compare(s, u) < 0);
+ }
+
+
+ [Test]
+ public void OrdinaryCViaBuilder()
+ {
+ IComparer<string> h = C5.ComparerBuilder.FromComparable<string>.Examine();
+ string s = "bamse";
+ string t = "bamse";
+ string u = "bimse";
+
+ Assert.AreEqual(0, h.Compare(s, t));
+ Assert.IsTrue(h.Compare(s, u) < 0);
+ }
+
+
+ [Test]
+ public void ICViaBuilder()
+ {
+ IComparer<int> h = C5.ComparerBuilder.FromComparable<int>.Examine();
+ int s = 4;
+ int t = 4;
+ int u = 5;
+
+ Assert.AreEqual(0, h.Compare(s, t));
+ Assert.IsTrue(h.Compare(s, u) < 0);
+ }
+ }
+
+ [TestFixture]
+ public class Hashers
+ {
+ [Test]
+ public void Reftypehasher()
+ {
+ IHasher<string> h = new DefaultReferenceTypeHasher<string>();
+ string s = "bamse";
+ string t = "bamse";
+ string u = "bimse";
+
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+
+ [Test]
+ public void Valuetypehasher()
+ {
+ IHasher<double> h = new DefaultValueTypeHasher<double>();
+ double s = 3.4;
+ double t = 3.4;
+ double u = 5.7;
+
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+
+ [Test]
+ public void ReftypehasherViaBuilder()
+ {
+ IHasher<string> h = C5.HasherBuilder.ByPrototype<string>.Examine();
+ string s = "bamse";
+ string t = "bamse";
+ string u = "bimse";
+
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+
+ [Test]
+ public void ValuetypehasherViaBuilder()
+ {
+ IHasher<double> h = C5.HasherBuilder.ByPrototype<double>.Examine();
+ double s = 3.4;
+ double t = 3.4;
+ double u = 5.7;
+
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+
+ [Test]
+ public void InthasherViaBuilder()
+ {
+ IHasher<int> h = C5.HasherBuilder.ByPrototype<int>.Examine();
+ int s = 3;
+ int t = 3;
+ int u = 5;
+
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+ [Test]
+ public void UnseqhasherViaBuilder()
+ {
+ IHasher<ICollection<int>> h = C5.HasherBuilder.ByPrototype<ICollection<int>>.Examine();
+ ICollection<int> s = new LinkedList<int>();
+ ICollection<int> t = new LinkedList<int>();
+ ICollection<int> u = new LinkedList<int>();
+ s.Add(1);s.Add(2);s.Add(3);
+ t.Add(3);t.Add(2);t.Add(1);
+ u.Add(3);u.Add(2);u.Add(4);
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+
+ [Test]
+ public void SeqhasherViaBuilder()
+ {
+ IHasher<ISequenced<int>> h = C5.HasherBuilder.ByPrototype<ISequenced<int>>.Examine();
+ ISequenced<int> s = new LinkedList<int>();
+ ISequenced<int> t = new LinkedList<int>();
+ ISequenced<int> u = new LinkedList<int>();
+ s.Add(1);s.Add(2);s.Add(3);
+ t.Add(1);t.Add(2);t.Add(3);
+ u.Add(3);u.Add(2);u.Add(1);
+ Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s));
+ Assert.IsTrue(h.Equals(s, t));
+ Assert.IsFalse(h.Equals(s, u));
+ }
+
+
+ }
+
+
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/ChangeLog b/mcs/class/Mono.C5/Test/ChangeLog
new file mode 100644
index 00000000000..83eda4d4860
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/ChangeLog
@@ -0,0 +1,4 @@
+2004-11-15 Martin Baulig <martin@ximian.com>
+
+ Importing version 0.5 of C5's test suite, http://www.itu.dk/research/c5/.
+
diff --git a/mcs/class/Mono.C5/Test/Sorting.cs b/mcs/class/Mono.C5/Test/Sorting.cs
new file mode 100644
index 00000000000..33595712955
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/Sorting.cs
@@ -0,0 +1,268 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+namespace nunit.SortingTests
+{
+ [TestFixture]
+ public class SortRandom
+ {
+ IC ic;
+
+ Random ran;
+
+ int[] a;
+
+ int length;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ ran = new Random(3456);
+ length = 100000;
+ a = new int[length];
+ for (int i = 0; i < length; i++)
+ a[i] = ran.Next();
+ }
+
+
+ [Test]
+ public void HeapSort()
+ {
+ Sorting.HeapSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void IntroSort()
+ {
+ Sorting.IntroSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void InsertionSort()
+ {
+ length = 1000;
+ Sorting.InsertionSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+
+ Sorting.InsertionSort<int>(a, length, 2 * length, ic);
+ for (int i = length + 1; i < 2 * length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; }
+ }
+
+
+
+ [TestFixture]
+ public class SortRandomDuplicates
+ {
+ IC ic;
+
+ Random ran;
+
+ int[] a;
+
+ int length;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ ran = new Random(3456);
+ length = 100000;
+ a = new int[length];
+ for (int i = 0; i < length; i++)
+ a[i] = ran.Next(3, 23);
+ }
+
+
+ [Test]
+ public void HeapSort()
+ {
+ Sorting.HeapSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void IntroSort()
+ {
+ Sorting.IntroSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void InsertionSort()
+ {
+ length = 1000;
+ Sorting.InsertionSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+
+ Sorting.InsertionSort<int>(a, length, 2 * length, ic);
+ for (int i = length + 1; i < 2 * length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; a = null; ran = null; }
+ }
+
+
+
+ [TestFixture]
+ public class SortIncreasing
+ {
+ IC ic;
+
+ int[] a;
+
+ int length;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ length = 100000;
+ a = new int[length];
+ for (int i = 0; i < length; i++)
+ a[i] = i;
+ }
+
+
+ [Test]
+ public void HeapSort()
+ {
+ Sorting.HeapSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void IntroSort()
+ {
+ Sorting.IntroSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void InsertionSort()
+ {
+ length = 1000;
+ Sorting.InsertionSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+
+ Sorting.InsertionSort<int>(a, length, 2 * length, ic);
+ for (int i = length + 1; i < 2 * length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; a = null; }
+ }
+
+
+
+ [TestFixture]
+ public class SortDecreasing
+ {
+ IC ic;
+
+ int[] a;
+
+ int length;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ length = 100000;
+ a = new int[length];
+ for (int i = 0; i < length; i++)
+ a[i] = -i;
+ }
+
+
+ [Test]
+ public void HeapSort()
+ {
+ Sorting.HeapSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void IntroSort()
+ {
+ Sorting.IntroSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [Test]
+ public void InsertionSort()
+ {
+ length = 1000;
+ Sorting.InsertionSort<int>(a, 0, length, ic);
+ for (int i = 1; i < length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+
+ Sorting.InsertionSort<int>(a, length, 2 * length, ic);
+ for (int i = length + 1; i < 2 * length; i++)
+ Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; a = null; }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/SupportClasses.cs b/mcs/class/Mono.C5/Test/SupportClasses.cs
new file mode 100644
index 00000000000..3487afd9c91
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/SupportClasses.cs
@@ -0,0 +1,156 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+
+namespace nunit
+{
+ using System;
+ using C5;
+ using NUnit.Framework;
+ class SC: IComparer<string>
+ {
+ public int Compare(string a, string b)
+ {
+ return a.CompareTo(b);
+ }
+
+
+ public void appl(String s)
+ {
+ System.Console.WriteLine("--{0}", s);
+ }
+ }
+
+
+
+ class IC: IComparer<int>, IComparable<int>, IComparer<IC>, IComparable<IC>
+ {
+ public int Compare(int a, int b)
+ {
+ return a > b ? 1 : a < b ? -1 : 0;
+ }
+
+
+ public int Compare(IC a, IC b)
+ {
+ return a._i > b._i ? 1 : a._i < b._i ? -1 : 0;
+ }
+
+
+ private int _i;
+
+
+ public int i
+ {
+ get { return _i; }
+ set { _i = value; }
+ }
+
+
+ public IC() { }
+
+
+ public IC(int i) { _i = i; }
+
+
+ public int CompareTo(int that) { return _i > that ? 1 : _i < that ? -1 : 0; }
+
+ public bool Equals(int that) { return _i == that; }
+
+
+ public int CompareTo(IC that) { return _i > that._i ? 1 : _i < that._i ? -1 : 0; }
+ public bool Equals(IC that) { return _i == that._i; }
+
+
+ public static bool eq(MSG.IEnumerable<int> me, params int[] that)
+ {
+ int i = 0, maxind = that.Length - 1;
+
+ foreach (int item in me)
+ if (i > maxind || item != that[i++])
+ return false;
+
+ return i == maxind + 1;
+ }
+ public static bool seq(ICollection<int> me, params int[] that)
+ {
+ int[] me2 = me.ToArray();
+
+ Array.Sort(me2);
+
+ int i = 0, maxind = that.Length - 1;
+
+ foreach (int item in me2)
+ if (i > maxind || item != that[i++])
+ return false;
+
+ return i == maxind + 1;
+ }
+ }
+
+
+ class RevIC: IComparer<int>
+ {
+ public int Compare(int a, int b)
+ {
+ return a > b ? -1 : a < b ? 1 : 0;
+ }
+ }
+
+
+
+ public delegate int Int2Int(int i);
+
+
+ public class FunEnumerable: MSG.IEnumerable<int>
+ {
+ int size;
+
+ Int2Int f;
+
+
+ public FunEnumerable(int size, Int2Int f)
+ {
+ this.size = size; this.f = f;
+ }
+
+
+ public bool Exists(Filter<int> filter) { return false; }
+
+
+ public bool All(Filter<int> filter) { return false; }
+
+
+ public MSG.IEnumerator<int> GetEnumerator()
+ {
+ for (int i = 0; i < size; i++)
+ yield return f(i);
+ }
+
+ public void Apply(Applier<int> a) { }
+ }
+
+}
diff --git a/mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs b/mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs
new file mode 100644
index 00000000000..f7c6304c750
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs
@@ -0,0 +1,2453 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+
+namespace nunit.arrays.list
+{
+ namespace Enumerable
+ {
+ [TestFixture]
+ public class Multiops
+ {
+ private ArrayList<int> list;
+
+ private Filter<int> always, never, even;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new ArrayList<int>();
+ always = delegate{return true;};
+ never = delegate{return false;};
+ even = delegate(int i){return i%2==0;};
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(list.All(always));
+ Assert.IsTrue(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(8);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(5);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsFalse(list.All(even));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(5);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(8);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsTrue(list.Exists(even));
+ }
+
+
+ [Test]
+ public void Apply()
+ {
+ int sum = 0;
+ Applier<int> a = delegate(int i){sum=i+10*sum;};
+
+ list.Apply(a);
+ Assert.AreEqual(0, sum);
+ sum = 0;
+ list.Add(5);list.Add(8);list.Add(7);list.Add(5);
+ list.Apply(a);
+ Assert.AreEqual(5875, sum);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class GetEnumerator
+ {
+ private ArrayList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new ArrayList<int>(); }
+
+
+ [Test]
+ public void Empty()
+ {
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+ list.Add(5);
+ list.Add(10);
+ list.Add(1);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(8, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(10, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(1, e.Current);
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void DoDispose()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.Dispose();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ list.Add(99);
+ e.MoveNext();
+ }
+
+
+/*
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void TooEarlyCurrent()
+ {
+ int none = list.GetEnumerator().Current;
+ }
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void TooLateMoveNext()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void TooLateCurrent()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+
+ int i = e.Current;
+ }
+
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterDispose()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.Dispose();
+ e.MoveNext();
+ }
+
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void CurrentAfterDispose()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.Dispose();
+
+ int i = e.Current;
+ }
+*/
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace CollectionOrSink
+ {
+ [TestFixture]
+ public class CollectionOrSink
+ {
+ private ArrayList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new ArrayList<int>(); }
+
+
+ [Test]
+ public void CountEtAl()
+ {
+ Assert.AreEqual(0, list.Count);
+ Assert.IsTrue(list.IsEmpty);
+ Assert.IsTrue(list.AllowsDuplicates);
+ list.Add(5);
+ Assert.AreEqual(1, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ list.Add(5);
+ Assert.AreEqual(2, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ list.Add(8);
+ Assert.AreEqual(3, list.Count);
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ list.Add(3);list.Add(4);list.Add(5);
+
+ ArrayList<int> list2 = new ArrayList<int>();
+
+ list2.AddAll(list);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ list.AddAll(list2);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ Assert.IsTrue(IC.eq(list, 3, 4, 5, 3, 4, 5));
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private ArrayList<int> list;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new ArrayList<int>();
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(list.ToArray()));
+ list.Add(7);
+ list.Add(7);
+ Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 7));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ list.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(6);
+ list.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(4);
+ list.Add(4);
+ list.Add(9);
+ list.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 4, 9, 1008, 1009));
+ list.Clear();
+ list.Add(7);
+ list.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 4, 9, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ list.Add(3);
+ list.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ list.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ list.Add(3);
+ list.Add(3);
+ list.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sync
+ {
+ private ArrayList<int> list;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new ArrayList<int>();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ [Test]
+ public void Get()
+ {
+ Assert.IsNotNull(list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace EditableCollection
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private ArrayList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new ArrayList<int>(); }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsFalse(list.Contains(5));
+ list.Add(5);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ list.Add(8);
+ list.Add(10);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsTrue(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ list.Remove(8);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsFalse(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ }
+
+
+ [Test]
+ public void ContainsCount()
+ {
+ Assert.AreEqual(0, list.ContainsCount(5));
+ list.Add(5);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ list.Add(8);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ Assert.AreEqual(1, list.ContainsCount(8));
+ list.Add(5);
+ Assert.AreEqual(2, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ Assert.AreEqual(1, list.ContainsCount(8));
+ }
+
+
+ [Test]
+ public void RemoveAllCopies()
+ {
+ list.Add(5);list.Add(7);list.Add(5);
+ Assert.AreEqual(2, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.RemoveAllCopies(5);
+ Assert.AreEqual(0, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.Add(5);list.Add(8);list.Add(5);
+ list.RemoveAllCopies(8);
+ Assert.IsTrue(IC.eq(list, 7, 5, 5));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Filter<int> f = delegate(int i){return i%2==0;};
+
+ Assert.IsTrue(list.FindAll(f).IsEmpty);
+ list.Add(5);list.Add(8);list.Add(5);list.Add(10);list.Add(8);
+ Assert.IsTrue(((ArrayList<int>)list.FindAll(f)).Check());
+ Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10, 8));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ ArrayList<int> list2 = new ArrayList<int>();
+
+ Assert.IsTrue(list.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(list.ContainsAll(list2));
+ list.Add(4);
+ Assert.IsTrue(list.ContainsAll(list2));
+ list.Add(5);
+ Assert.IsTrue(list.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(list.ContainsAll(list2));
+ list.Add(4);
+ Assert.IsTrue(list.ContainsAll(list2));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ ArrayList<int> list2 = new ArrayList<int>();
+
+ list.Add(4);list.Add(4);list.Add(5);list.Add(4);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);list2.Add(7);list2.Add(4);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 4, 5));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(5);list2.Add(5);list2.Add(6);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 5, 6));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list));
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ ArrayList<int> list2 = new ArrayList<int>();
+
+ list.Add(4);list.Add(4);list.Add(5);list.Add(4);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);list2.Add(7);list2.Add(4);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 6));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(6);list2.Add(5);list2.Add(5);list2.Add(6);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 4));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 4));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ list.Add(4);list.Add(4);list.Add(5);list.Add(4);list.Add(6);
+ Assert.IsFalse(list.Remove(2));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(list.Remove(4));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 5, 4, 6));
+ Assert.AreEqual(6, list.RemoveLast());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 5, 4));
+ list.Add(7);
+ Assert.AreEqual(4, list.RemoveFirst());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 4, 7));
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ list.Add(7);list.Add(7);
+ list.Clear();
+ Assert.IsTrue(list.IsEmpty);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace Indexed
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ }
+
+
+ [Test]
+ public void IndexOf()
+ {
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(-1, dit.LastIndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ dit.Add(5);dit.Add(7);dit.Add(8);dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ Assert.AreEqual(4, dit.LastIndexOf(7));
+ Assert.AreEqual(3, dit.IndexOf(8));
+ Assert.AreEqual(1, dit.LastIndexOf(5));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Removing
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ }
+
+
+ [Test]
+ public void RemoveAt()
+ {
+ dit.Add(5);dit.Add(7);dit.Add(9);dit.Add(1);dit.Add(2);
+ Assert.AreEqual(7, dit.RemoveAt(1));
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
+ Assert.AreEqual(5, dit.RemoveAt(0));
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1, 2));
+ Assert.AreEqual(2, dit.RemoveAt(2));
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad0()
+ {
+ dit.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBadM1()
+ {
+ dit.RemoveAt(-1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad1()
+ {
+ dit.Add(8);
+ dit.RemoveAt(1);
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ dit.RemoveInterval(0, 0);
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(3, 0);
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
+ dit.RemoveInterval(3, 1);
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
+ dit.RemoveInterval(1, 3);
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 60));
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit));
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
+ dit.RemoveInterval(2, 2);
+ Assert.IsTrue(((ArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+ }
+
+
+
+
+ namespace List
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new ArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void FirstBad()
+ {
+ int f = lst.First;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void LastBad()
+ {
+ int f = lst.Last;
+ }
+
+
+ [Test]
+ public void FirstLast()
+ {
+ lst.Add(19);
+ Assert.AreEqual(19, lst.First);
+ Assert.AreEqual(19, lst.Last);
+ lst.Add(34);lst.InsertFirst(12);
+ Assert.AreEqual(12, lst.First);
+ Assert.AreEqual(34, lst.Last);
+ }
+
+
+ [Test]
+ public void This()
+ {
+ lst.Add(34);
+ Assert.AreEqual(34, lst[0]);
+ lst[0] = 56;
+ Assert.AreEqual(56, lst.First);
+ lst.Add(7);lst.Add(7);lst.Add(7);lst.Add(7);
+ lst[0] = 45;lst[2] = 78;lst[4] = 101;
+ Assert.IsTrue(IC.eq(lst, 45, 7, 78, 7, 101));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptyGet()
+ {
+ int f = lst[0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowGet()
+ {
+ lst.Add(7);
+
+ int f = lst[-1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiGet()
+ {
+ lst.Add(6);
+
+ int f = lst[1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptySet()
+ {
+ lst[0] = 4;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowSet()
+ {
+ lst.Add(7);
+ lst[-1] = 9;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiSet()
+ {
+ lst.Add(6);
+ lst[1] = 11;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Inserting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new ArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Insert()
+ {
+ lst.Insert(0, 5);
+ Assert.IsTrue(IC.eq(lst, 5));
+ lst.Insert(0, 7);
+ Assert.IsTrue(IC.eq(lst, 7, 5));
+ lst.Insert(1, 4);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5));
+ lst.Insert(3, 2);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertLow()
+ {
+ lst.Add(7);
+ lst.Insert(-1, 9);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertHi()
+ {
+ lst.Add(6);
+ lst.Insert(2, 11);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ for (int i = 0; i < 7; i++)
+ lst.Add(2 * i);
+
+ Assert.IsTrue(lst.FIFO);
+ Assert.AreEqual(0, lst.Remove());
+ Assert.AreEqual(2, lst.Remove());
+ lst.FIFO = false;
+ Assert.AreEqual(12, lst.Remove());
+ Assert.AreEqual(10, lst.Remove());
+ lst.FIFO = true;
+ Assert.AreEqual(4, lst.Remove());
+ Assert.AreEqual(6, lst.Remove());
+ }
+
+
+ [Test]
+ public void InsertFirstLast()
+ {
+ lst.InsertFirst(4);
+ lst.InsertLast(5);
+ lst.InsertFirst(14);
+ lst.InsertLast(15);
+ lst.InsertFirst(24);
+ lst.InsertLast(25);
+ lst.InsertFirst(34);
+ lst.InsertLast(55);
+ Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
+ }
+
+
+ [Test]
+ public void InsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertBefore(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 3, 2, 5));
+ lst.InsertBefore(8, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 2, 5));
+ lst.InsertBefore(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 2, 9, 5));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertBefore(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAfter()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertAfter(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 3, 2, 7, 5));
+ lst.InsertAfter(8, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 3, 2, 7, 5));
+ lst.InsertAfter(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 3, 2, 7, 5, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfter()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertAfter(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAll()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+
+ IList<int> lst2 = new ArrayList<int>();
+
+ lst2.Add(7);lst2.Add(8);lst2.Add(9);
+ lst.InsertAll(0, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
+ lst.InsertAll(7, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4, 7, 8, 9));
+ lst.InsertAll(5, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 7, 8, 9, 3, 4, 7, 8, 9));
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Mapper<int,string> m = delegate(int i){return "<<"+i+">>";};
+ IList<string> r = lst.Map(m);
+
+ Assert.IsTrue(r.Check());
+ Assert.IsTrue(r.IsEmpty);
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ r = lst.Map(m);
+ Assert.IsTrue(r.Check());
+ Assert.AreEqual(4, r.Count);
+ for (int i = 0; i < 4; i++)
+ Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
+ }
+
+
+ [Test]
+ public void RemoveFirstLast()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ Assert.AreEqual(1, lst.RemoveFirst());
+ Assert.AreEqual(4, lst.RemoveLast());
+ Assert.AreEqual(2, lst.RemoveFirst());
+ Assert.AreEqual(3, lst.RemoveLast());
+ Assert.IsTrue(lst.IsEmpty);
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse();
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(0, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 0);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ lst.Reverse(5, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void BadReverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse(8, 3);
+ }
+ }
+
+ [TestFixture]
+ public class Combined
+ {
+ private IList<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new ArrayList<KeyValuePair<int,int>>();
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i,i+30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Sorting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new ArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Sort()
+ {
+ lst.Add(5);lst.Add(6);lst.Add(5);lst.Add(7);lst.Add(3);
+ Assert.IsFalse(lst.IsSorted(new IC()));
+ lst.Sort(new IC());
+ Assert.IsTrue(lst.IsSorted(new IC()));
+ Assert.IsTrue(IC.eq(lst, 3, 5, 5, 6, 7));
+ }
+
+
+ [Test]
+ public void Stability()
+ {
+ IList<KeyValuePair<int,string>> lst2 = new ArrayList<KeyValuePair<int,string>>();
+ IComparer<KeyValuePair<int,string>> c = new KeyValuePairComparer<int,string>(new IC());
+
+ lst2.Add(new KeyValuePair<int,string>(5, "a"));
+ lst2.Add(new KeyValuePair<int,string>(5, "b"));
+ lst2.Add(new KeyValuePair<int,string>(6, "c"));
+ lst2.Add(new KeyValuePair<int,string>(4, "d"));
+ lst2.Add(new KeyValuePair<int,string>(3, "e"));
+ lst2.Add(new KeyValuePair<int,string>(4, "f"));
+ lst2.Add(new KeyValuePair<int,string>(5, "h"));
+ Assert.IsFalse(lst2.IsSorted(c));
+ lst2.Sort(c);
+ Assert.IsTrue(lst2.IsSorted(c));
+
+ KeyValuePair<int,string> p = lst2.RemoveFirst();
+
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual("e", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(4, p.key);
+ Assert.AreEqual("d", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(4, p.key);
+ Assert.AreEqual("f", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(5, p.key);
+ Assert.AreEqual("a", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(5, p.key);
+ Assert.AreEqual("b", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(5, p.key);
+ Assert.AreEqual("h", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(6, p.key);
+ Assert.AreEqual("c", p.value);
+ Assert.IsTrue(lst2.IsEmpty);
+ }
+ }
+ }
+
+
+ namespace IStackQueue
+ {
+ [TestFixture]
+ public class Stack
+ {
+ private IStack<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new ArrayList<int>(); }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.Push(7);
+ list.Push(5);
+ list.Push(7);
+ list.Push(8);
+ list.Push(9);
+ Assert.AreEqual(9, list.Pop());
+ Assert.AreEqual(8, list.Pop());
+ Assert.AreEqual(7, list.Pop());
+ Assert.AreEqual(5, list.Pop());
+ Assert.AreEqual(7, list.Pop());
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void PopEmpty()
+ {
+ list.Push(5);
+ Assert.AreEqual(5, list.Pop());
+ list.Pop();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ [TestFixture]
+ public class Queue
+ {
+ private IQueue<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new ArrayList<int>(); }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.EnQueue(7);
+ list.EnQueue(5);
+ list.EnQueue(7);
+ list.EnQueue(8);
+ list.EnQueue(9);
+ Assert.AreEqual(7, list.DeQueue());
+ Assert.AreEqual(5, list.DeQueue());
+ Assert.AreEqual(7, list.DeQueue());
+ Assert.AreEqual(8, list.DeQueue());
+ Assert.AreEqual(9, list.DeQueue());
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void DeQueueEmpty()
+ {
+ list.EnQueue(5);
+ Assert.AreEqual(5, list.DeQueue());
+ list.DeQueue();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+ namespace Range
+ {
+ [TestFixture]
+ public class Range
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new ArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void GetRange()
+ {
+ //Assert.IsTrue(IC.eq(lst[0, 0)));
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
+ Assert.IsTrue(IC.eq(lst[3, 4], 3, 4, 5, 6));
+ Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void BadGetRange()
+ {
+ object foo= lst[0, 11];
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[0, 4].Backwards(), 3, 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[3, 4].Backwards(), 6, 5, 4, 3));
+ Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
+ }
+
+
+ [Test]
+ public void DirectionAndCount()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 4].Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 4].Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
+ Assert.AreEqual(4, lst[3, 4].Count);
+ Assert.AreEqual(4, lst[3, 4].Backwards().Count);
+ Assert.AreEqual(10, lst.Backwards().Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ foreach (int i in lst)
+ {
+ lst.Add(45 + i);
+ }
+ }
+ }
+ }
+
+
+
+
+ namespace View
+ {
+ [TestFixture]
+ public class Simple
+ {
+ ArrayList<int> list, view;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new ArrayList<int>();
+ list.Add(0);list.Add(1);list.Add(2);list.Add(3);
+ view = (ArrayList<int>)list.View(1, 2);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ list = view = null;
+ }
+
+
+ void check()
+ {
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(view.Check());
+ }
+
+
+ //static void pint(IEnumerable<int> l) { foreach (int i in l) Console.WriteLine(i); }
+
+ [Test]
+ public void Add()
+ {
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2));
+ view.InsertFirst(10);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 10, 1, 2));
+ view.Clear();
+ Assert.IsFalse(view.IsReadOnly);
+ Assert.IsTrue(view.AllowsDuplicates);
+ Assert.IsTrue(view.IsEmpty);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 3));
+ Assert.IsTrue(IC.eq(view));
+ view.Add(8);
+ Assert.IsFalse(view.IsEmpty);
+ Assert.IsTrue(view.AllowsDuplicates);
+ Assert.IsFalse(view.IsReadOnly);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 3));
+ Assert.IsTrue(IC.eq(view, 8));
+ view.Add(12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12));
+ view.InsertAfter(15, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12, 15));
+ view.InsertBefore(18, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
+
+ ArrayList<int> lst2 = new ArrayList<int>();
+
+ lst2.Add(90);lst2.Add(92);
+ view.AddAll(lst2);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
+ view.InsertLast(66);
+ check();
+
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
+ }
+
+
+ [Test]
+ public void Bxxx()
+ {
+ Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
+ Assert.AreSame(list, view.Underlying);
+ Assert.IsNull(list.Underlying);
+ Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
+ Assert.AreEqual(0,list.Offset);
+ Assert.AreEqual(1,view.Offset);
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsTrue(view.Contains(1));
+ Assert.IsFalse(view.Contains(0));
+
+ ArrayList<int> lst2 = new ArrayList<int>();
+
+ lst2.Add(2);
+ Assert.IsTrue(view.ContainsAll(lst2));
+ lst2.Add(3);
+ Assert.IsFalse(view.ContainsAll(lst2));
+ Assert.AreEqual(Speed.Linear, view.ContainsSpeed);
+ Assert.AreEqual(2, view.Count);
+ view.Add(1);
+ Assert.AreEqual(1, view.ContainsCount(2));
+ Assert.AreEqual(2, view.ContainsCount(1));
+ Assert.AreEqual(3, view.Count);
+ }
+
+
+ [Test]
+ public void CreateView()
+ {
+ ArrayList<int> view2 = (ArrayList<int>)view.View(1, 0);
+
+ Assert.AreSame(list, view2.Underlying);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ Assert.IsTrue(view.FIFO);
+ view.Add(23);view.Add(24);view.Add(25);
+ check();
+ Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
+ Assert.AreEqual(1, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
+ view.FIFO = false;
+ Assert.IsFalse(view.FIFO);
+ Assert.AreEqual(25, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24));
+ }
+
+
+ [Test]
+ public void MapEtc()
+ {
+ ArrayList<double> dbl = (ArrayList<double>)view.Map(new Mapper<int,double>(delegate(int i){return i/10.0;}));
+
+ Assert.IsTrue(dbl.Check());
+ Assert.AreEqual(0.1, dbl[0]);
+ Assert.AreEqual(0.2, dbl[1]);
+ for (int i = 0; i < 10; i++) view.Add(i);
+
+ list = (ArrayList<int>)view.FindAll(new Filter<int>(delegate(int i){return i%4==1;}));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 1, 1, 5, 9));
+ }
+
+
+ [Test]
+ public void FL()
+ {
+ Assert.AreEqual(1, view.First);
+ Assert.AreEqual(2, view.Last);
+ }
+
+
+ [Test]
+ public void Indexing()
+ {
+ list.Clear();
+ for (int i = 0; i < 20; i++) list.Add(i);
+
+ view = (ArrayList<int>)list.View(5, 7);
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.IndexOf(i + 5));
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
+ }
+
+
+ [Test]
+ public void Insert()
+ {
+ view.Insert(0, 34);
+ view.Insert(1, 35);
+ view.Insert(4, 36);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
+
+ IList<int> list2 = new ArrayList<int>();
+
+ list2.AddAll(view);
+ view.InsertAll(3, list2);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 34, 35, 1, 2, 36, 2, 36));
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void RangeCheck1()
+ {
+ IList<int> lst = new ArrayList<int>();
+ lst.Add(2);
+ lst = lst.View(1, 1);
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RangeCheck2()
+ {
+ IList<int> lst = new ArrayList<int>();
+ lst.Add(2);
+ lst = lst.View(1, -1);
+ }
+
+
+ [Test]
+ public void Sort()
+ {
+ view.Add(45);view.Add(47);view.Add(46);view.Add(48);
+ Assert.IsFalse(view.IsSorted(new IC()));
+ view.Sort(new IC());
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 1, 2, 1, 5, 3, 1, 3, 0));
+ Assert.IsTrue(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 1, 5, 3, 1, 3, 0));
+ Assert.IsTrue(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5, 3, 1, 3, 0));
+ Assert.IsTrue(view.Remove(0));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5, 3, 1, 3));
+ view.RemoveAllCopies(3);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5, 1));
+ Assert.IsTrue(IC.eq(list, 0, 2, 5, 1, 3));
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 2, 5, 1, 1, 5, 3, 1, 3, 0));
+
+ ArrayList<int> l2 = new ArrayList<int>();
+
+ l2.Add(1);l2.Add(2);l2.Add(2);l2.Add(3);l2.Add(1);
+ view.RemoveAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view, 5, 5, 1, 3, 0));
+ view.RetainAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view, 1, 3));
+ view.Add(2);view.Add(4);view.Add(5);
+ Assert.AreEqual(1, view.RemoveAt(0));
+ Assert.AreEqual(5, view.RemoveAt(3));
+ Assert.AreEqual(2, view.RemoveAt(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 3, 4));
+ view.Add(8);
+ Assert.AreEqual(3, view.RemoveFirst());
+ Assert.AreEqual(8, view.RemoveLast());
+ view.Add(2);view.Add(5);view.Add(3);view.Add(1);
+ view.RemoveInterval(1, 2);
+ check();
+ Assert.IsTrue(IC.eq(view, 4, 3, 1));
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ view.Clear();
+ for (int i = 0; i < 10; i++) view.Add(10 + i);
+
+ view.Reverse(3, 4);
+ check();
+ Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
+ view.Reverse();
+ Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
+ Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
+ }
+
+
+ [Test]
+ public void Slide()
+ {
+ view.Slide(1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 3));
+ view.Slide(-2);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1));
+ view.Slide(0, 3);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1, 2));
+ view.Slide(2, 1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2));
+ view.Slide(-1, 0);
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(28);
+ Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
+ }
+ [Test]
+ public void Iterate()
+ {
+ list.Clear();
+ view = null;
+ foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 2, 7 }) list.Add(i);
+
+ view = (ArrayList<int>)list.View(list.Count - 2, 2);
+ while (true)
+ {
+ if ((view.Last - view.First) % 2 == 1)
+ view.Insert(1, 666);
+ check();
+ if (view.Offset == 0)
+ break;
+ else
+ view.Slide(-1,2);
+ }
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 2, 4, 8, 666, 13, 6, 1, 666, 2, 666, 7));
+ }
+
+
+ [Test]
+ public void SyncRoot()
+ {
+ Assert.AreSame(view.SyncRoot, list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace ArrayListOfTreesORLists
+ {
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new TreeSet<int>(new IC());
+ dut = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new ArrayList<ICollection<int>>();
+ Dat = new ArrayList<ICollection<int>>();
+ Dut = new ArrayList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new TreeSet<int>(new IC());
+ dut = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new ArrayList<ICollection<int>>();
+ Dat = new ArrayList<ICollection<int>>();
+ Dut = new ArrayList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeSet<int>(new IC());
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ dot = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(2);dat.Add(1);
+ dut.Add(3);
+ dot.Add(1);dot.Add(2);
+ Dit = new ArrayList<ISequenced<int>>();
+ Dat = new ArrayList<ISequenced<int>>();
+ Dut = new ArrayList<ISequenced<int>>();
+ Dot = new ArrayList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeSet<int>(new IC());
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ dot = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(2);dat.Add(1);
+ dut.Add(3);
+ dot.Add(1);dot.Add(2);
+ Dit = new ArrayList<ISequenced<int>>();
+ Dat = new ArrayList<ISequenced<int>>();
+ Dut = new ArrayList<ISequenced<int>>();
+ Dot = new ArrayList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+
+
+
+
+ namespace HashingAndEquals
+ {
+ [TestFixture]
+ public class ISequenced
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(0);dit.Add(31);
+ dat.Add(1);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ ((ArrayList<int>)dut).InsertFirst(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count, params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count << 16) + retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1, 3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2, 3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1, 3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2, 7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(2);dit.Add(1);
+ dat.Add(3);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new ArrayList<ICollection<int>>();
+ Dat = new ArrayList<ICollection<int>>();
+ Dut = new ArrayList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new ArrayList<ICollection<int>>();
+ Dat = new ArrayList<ICollection<int>>();
+ Dut = new ArrayList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ dot = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new ArrayList<ISequenced<int>>();
+ Dat = new ArrayList<ISequenced<int>>();
+ Dut = new ArrayList<ISequenced<int>>();
+ Dot = new ArrayList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new ArrayList<int>();
+ dat = new ArrayList<int>();
+ dut = new ArrayList<int>();
+ dot = new ArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new ArrayList<ISequenced<int>>();
+ Dat = new ArrayList<ISequenced<int>>();
+ Dut = new ArrayList<ISequenced<int>>();
+ Dot = new ArrayList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/arrays/HashedArrayListTest.cs b/mcs/class/Mono.C5/Test/arrays/HashedArrayListTest.cs
new file mode 100644
index 00000000000..7a5e402a21e
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/arrays/HashedArrayListTest.cs
@@ -0,0 +1,2004 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+
+namespace nunit.arrays.hashed
+{
+ namespace Enumerable
+ {
+ [TestFixture]
+ public class Multiops
+ {
+ private HashedArrayList<int> list;
+
+ private Filter<int> always, never, even;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedArrayList<int>();
+ always = delegate{return true;};
+ never = delegate{return false;};
+ even = delegate(int i){return i%2==0;};
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(list.All(always));
+ Assert.IsTrue(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(8);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(5);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsFalse(list.All(even));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(5);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(8);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsTrue(list.Exists(even));
+ }
+
+
+ [Test]
+ public void Apply()
+ {
+ int sum = 0;
+ Applier<int> a = delegate(int i){sum=i+10*sum;};
+
+ list.Apply(a);
+ Assert.AreEqual(0, sum);
+ sum = 0;
+ list.Add(5);list.Add(8);list.Add(7);list.Add(5);
+ list.Apply(a);
+ Assert.AreEqual(587, sum);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class GetEnumerator
+ {
+ private HashedArrayList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new HashedArrayList<int>(); }
+
+
+ [Test]
+ public void Empty()
+ {
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+ list.Add(5);
+ list.Add(10);
+ list.Add(1);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(8, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(10, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(1, e.Current);
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void DoDispose()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.Dispose();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ list.Add(99);
+ e.MoveNext();
+ }
+
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace CollectionOrSink
+ {
+ [TestFixture]
+ public class CollectionOrSink
+ {
+ private HashedArrayList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new HashedArrayList<int>(); }
+
+
+ [Test]
+ public void CountEtAl()
+ {
+ Assert.AreEqual(0, list.Count);
+ Assert.IsTrue(list.IsEmpty);
+ Assert.IsFalse(list.AllowsDuplicates);
+ Assert.IsTrue(list.Add(5));
+ Assert.AreEqual(1, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ Assert.IsFalse(list.Add(5));
+ Assert.AreEqual(1, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ Assert.IsTrue(list.Add(8));
+ Assert.AreEqual(2, list.Count);
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ list.Add(3);list.Add(4);list.Add(5);
+
+ HashedArrayList<int> list2 = new HashedArrayList<int>();
+
+ list2.AddAll(list);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ list.AddAll(list2);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ Assert.IsTrue(IC.eq(list, 3, 4, 5));
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private HashedArrayList<int> list;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedArrayList<int>();
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(list.ToArray()));
+ list.Add(7);
+ list.Add(8);
+ Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 8));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ list.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(6);
+ list.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(4);
+ list.Add(5);
+ list.Add(9);
+ list.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 1009));
+ list.Clear();
+ list.Add(7);
+ list.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ list.Add(3);
+ list.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ list.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ list.Add(3);
+ list.Add(4);
+ list.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sync
+ {
+ private HashedArrayList<int> list;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedArrayList<int>();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ [Test]
+ public void Get()
+ {
+ Assert.IsNotNull(list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace EditableCollection
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private HashedArrayList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new HashedArrayList<int>(); }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsFalse(list.Contains(5));
+ list.Add(5);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ list.Add(8);
+ list.Add(10);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsTrue(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ list.Remove(8);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsFalse(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ }
+
+ [Test]
+ public void BadAdd()
+ {
+ Assert.IsTrue(list.Add(5));
+ Assert.IsTrue(list.Add(8));
+ Assert.IsFalse(list.Add(5));
+ }
+
+
+ [Test]
+ public void ContainsCount()
+ {
+ Assert.AreEqual(0, list.ContainsCount(5));
+ list.Add(5);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ list.Add(8);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ Assert.AreEqual(1, list.ContainsCount(8));
+ }
+
+
+ [Test]
+ public void RemoveAllCopies()
+ {
+ list.Add(5);list.Add(7);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.RemoveAllCopies(5);
+ Assert.AreEqual(0, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.Add(5);list.Add(8);
+ list.RemoveAllCopies(8);
+ Assert.IsTrue(IC.eq(list, 7, 5));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Filter<int> f = delegate(int i){return i%2==0;};
+
+ Assert.IsTrue(list.FindAll(f).IsEmpty);
+ list.Add(5);list.Add(8);list.Add(10);
+ Assert.IsTrue(((ArrayList<int>)list.FindAll(f)).Check());
+ Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ HashedArrayList<int> list2 = new HashedArrayList<int>();
+
+ Assert.IsTrue(list.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(list.ContainsAll(list2));
+ list.Add(4);
+ Assert.IsTrue(list.ContainsAll(list2));
+ list.Add(5);
+ Assert.IsTrue(list.ContainsAll(list2));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ HashedArrayList<int> list2 = new HashedArrayList<int>();
+
+ list.Add(4);list.Add(5);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 5));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(5);list2.Add(5);list2.Add(6);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 6));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list));
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ HashedArrayList<int> list2 = new HashedArrayList<int>();
+
+ list.Add(4);list.Add(5);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 6));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(6);list2.Add(5);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ list.Add(4);list.Add(5);list.Add(6);
+ Assert.IsFalse(list.Remove(2));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(list.Remove(4));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 6));
+ Assert.AreEqual(6, list.RemoveLast());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5));
+ list.Add(7);
+ Assert.AreEqual(5, list.RemoveFirst());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 7));
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ list.Add(7);list.Add(6);
+ list.Clear();
+ Assert.IsTrue(list.IsEmpty);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace IIndexed
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ }
+
+
+ [Test]
+ public void IndexOf()
+ {
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(-1, dit.LastIndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ dit.Add(5);dit.Add(7);dit.Add(8);dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ Assert.AreEqual(0, dit.LastIndexOf(7));
+ Assert.AreEqual(2, dit.IndexOf(8));
+ Assert.AreEqual(1, dit.LastIndexOf(5));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Removing
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ }
+
+
+ [Test]
+ public void RemoveAt()
+ {
+ dit.Add(5);dit.Add(7);dit.Add(9);dit.Add(1);dit.Add(2);
+ Assert.AreEqual(7, dit.RemoveAt(1));
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
+ Assert.AreEqual(5, dit.RemoveAt(0));
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1, 2));
+ Assert.AreEqual(2, dit.RemoveAt(2));
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad0()
+ {
+ dit.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBadM1()
+ {
+ dit.RemoveAt(-1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad1()
+ {
+ dit.Add(8);
+ dit.RemoveAt(1);
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ dit.RemoveInterval(0, 0);
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(3, 0);
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
+ dit.RemoveInterval(3, 1);
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
+ dit.RemoveInterval(1, 3);
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 60));
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit));
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
+ dit.RemoveInterval(2, 2);
+ Assert.IsTrue(((HashedArrayList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+ }
+
+
+
+
+ namespace IList
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void FirstBad()
+ {
+ int f = lst.First;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void LastBad()
+ {
+ int f = lst.Last;
+ }
+
+
+ [Test]
+ public void FirstLast()
+ {
+ lst.Add(19);
+ Assert.AreEqual(19, lst.First);
+ Assert.AreEqual(19, lst.Last);
+ lst.Add(34);lst.InsertFirst(12);
+ Assert.AreEqual(12, lst.First);
+ Assert.AreEqual(34, lst.Last);
+ }
+
+
+ [Test]
+ public void This()
+ {
+ lst.Add(34);
+ Assert.AreEqual(34, lst[0]);
+ lst[0] = 56;
+ Assert.AreEqual(56, lst.First);
+ lst.Add(7);lst.Add(77);lst.Add(777);lst.Add(7777);
+ lst[0] = 45;lst[2] = 78;lst[4] = 101;
+ Assert.IsTrue(IC.eq(lst, 45, 7, 78, 777, 101));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptyGet()
+ {
+ int f = lst[0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowGet()
+ {
+ lst.Add(7);
+
+ int f = lst[-1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiGet()
+ {
+ lst.Add(6);
+
+ int f = lst[1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptySet()
+ {
+ lst[0] = 4;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowSet()
+ {
+ lst.Add(7);
+ lst[-1] = 9;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiSet()
+ {
+ lst.Add(6);
+ lst[1] = 11;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Inserting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Insert()
+ {
+ lst.Insert(0, 5);
+ Assert.IsTrue(IC.eq(lst, 5));
+ lst.Insert(0, 7);
+ Assert.IsTrue(IC.eq(lst, 7, 5));
+ lst.Insert(1, 4);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5));
+ lst.Insert(3, 2);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertLow()
+ {
+ lst.Add(7);
+ lst.Insert(-1, 9);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertHi()
+ {
+ lst.Add(6);
+ lst.Insert(2, 11);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ for (int i = 0; i < 7; i++)
+ lst.Add(2 * i);
+
+ Assert.IsTrue(lst.FIFO);
+ Assert.AreEqual(0, lst.Remove());
+ Assert.AreEqual(2, lst.Remove());
+ lst.FIFO = false;
+ Assert.AreEqual(12, lst.Remove());
+ Assert.AreEqual(10, lst.Remove());
+ lst.FIFO = true;
+ Assert.AreEqual(4, lst.Remove());
+ Assert.AreEqual(6, lst.Remove());
+ }
+
+
+ [Test]
+ public void InsertFirstLast()
+ {
+ lst.InsertFirst(4);
+ lst.InsertLast(5);
+ lst.InsertFirst(14);
+ lst.InsertLast(15);
+ lst.InsertFirst(24);
+ lst.InsertLast(25);
+ lst.InsertFirst(34);
+ lst.InsertLast(55);
+ Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
+ }
+
+
+ [Test]
+ public void InsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ lst.Add(5);
+ lst.InsertBefore(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 3, 4, 5));
+ lst.InsertBefore(8, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 5));
+ lst.InsertBefore(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 9, 5));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertBefore(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAfter()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ lst.Add(5);
+ lst.InsertAfter(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 7, 3, 4, 5));
+ lst.InsertAfter(8, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5));
+ lst.InsertAfter(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfter()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(6);
+ lst.Add(5);
+ lst.InsertAfter(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAll()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+
+ IList<int> lst2 = new HashedArrayList<int>();
+
+ lst2.Add(7);lst2.Add(8);lst2.Add(9);
+ lst.InsertAll(0, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
+ lst.RemoveAll(lst2);
+ lst.InsertAll(4, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 3, 4, 7, 8, 9));
+ lst.RemoveAll(lst2);
+ lst.InsertAll(2, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 7, 8, 9, 3, 4));
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Mapper<int,string> m = delegate(int i){return "<<"+i+">>";};
+ IList<string> r = lst.Map(m);
+
+ Assert.IsTrue(((ArrayList<string>)r).Check());
+ Assert.IsTrue(r.IsEmpty);
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ r = lst.Map(m);
+ Assert.IsTrue(((ArrayList<string>)r).Check());
+ Assert.AreEqual(4, r.Count);
+ for (int i = 0; i < 4; i++)
+ Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
+ }
+
+
+ [Test]
+ public void RemoveFirstLast()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ Assert.AreEqual(1, lst.RemoveFirst());
+ Assert.AreEqual(4, lst.RemoveLast());
+ Assert.AreEqual(2, lst.RemoveFirst());
+ Assert.AreEqual(3, lst.RemoveLast());
+ Assert.IsTrue(lst.IsEmpty);
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse();
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(0, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 0);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ lst.Reverse(5, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void BadReverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse(8, 3);
+ }
+ }
+
+
+ [TestFixture]
+ public class Combined
+ {
+ private IList<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new HashedArrayList<KeyValuePair<int,int>>();
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Sorting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Sort()
+ {
+ lst.Add(5);lst.Add(6);lst.Add(55);lst.Add(7);lst.Add(3);
+ Assert.IsFalse(lst.IsSorted(new IC()));
+ lst.Sort(new IC());
+ Assert.IsTrue(lst.IsSorted(new IC()));
+ Assert.IsTrue(IC.eq(lst, 3, 5, 6, 7, 55));
+ }
+ }
+ }
+
+
+
+
+ namespace Range
+ {
+ [TestFixture]
+ public class Range
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedArrayList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void GetRange()
+ {
+ //Assert.IsTrue(IC.eq(lst[0, 0)));
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
+ Assert.IsTrue(IC.eq(lst[3, 3], 3, 4, 5));
+ Assert.IsTrue(IC.eq(lst[6, 3], 6, 7, 8));
+ Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[0, 3].Backwards(), 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[3, 3].Backwards(), 5, 4, 3));
+ Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
+ }
+
+
+ [Test]
+ public void DirectionAndCount()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 4].Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 4].Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
+ Assert.AreEqual(4, lst[3, 4].Count);
+ Assert.AreEqual(4, lst[3, 4].Backwards().Count);
+ Assert.AreEqual(10, lst.Backwards().Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ foreach (int i in lst)
+ {
+ lst.Add(45 + i);
+ }
+ }
+ }
+ }
+
+
+
+
+ namespace View
+ {
+ [TestFixture]
+ public class Simple
+ {
+ HashedArrayList<int> list;
+ HashedArrayList<int> view;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedArrayList<int>();
+ list.Add(0);list.Add(1);list.Add(2);list.Add(3);
+ view = (HashedArrayList<int>)list.View(1, 2);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ list = null;
+ view = null;
+ }
+
+
+ void check()
+ {
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(view.Check());
+ }
+
+
+ [Test]
+ public void Add()
+ {
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2));
+ view.InsertFirst(10);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 10, 1, 2));
+ view.Clear();
+ Assert.IsFalse(view.IsReadOnly);
+ Assert.IsFalse(view.AllowsDuplicates);
+ Assert.IsTrue(view.IsEmpty);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 3));
+ Assert.IsTrue(IC.eq(view));
+ view.Add(8);
+ Assert.IsFalse(view.IsEmpty);
+ Assert.IsFalse(view.AllowsDuplicates);
+ Assert.IsFalse(view.IsReadOnly);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 3));
+ Assert.IsTrue(IC.eq(view, 8));
+ view.Add(12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12));
+ view.InsertAfter(15, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12, 15));
+ view.InsertBefore(18, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
+
+ HashedArrayList<int> lst2 = new HashedArrayList<int>();
+
+ lst2.Add(90);lst2.Add(92);
+ view.AddAll(lst2);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
+ view.InsertLast(66);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
+ }
+
+
+ [Test]
+ public void Bxxx()
+ {
+ Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
+ Assert.AreSame(list, view.Underlying);
+ Assert.IsNull(list.Underlying);
+ Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
+ Assert.AreEqual(0,list.Offset);
+ Assert.AreEqual(1,view.Offset);
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsTrue(view.Contains(1));
+ Assert.IsFalse(view.Contains(0));
+
+ HashedArrayList<int> lst2 = new HashedArrayList<int>();
+
+ lst2.Add(2);
+ Assert.IsTrue(view.ContainsAll(lst2));
+ lst2.Add(3);
+ Assert.IsFalse(view.ContainsAll(lst2));
+ Assert.AreEqual(Speed.Constant, view.ContainsSpeed);
+ Assert.AreEqual(2, view.Count);
+ view.Add(1);
+ Assert.AreEqual(1, view.ContainsCount(2));
+ Assert.AreEqual(1, view.ContainsCount(1));
+ Assert.AreEqual(2, view.Count);
+ }
+
+
+ [Test]
+ public void CreateView()
+ {
+ HashedArrayList<int> view2 = (HashedArrayList<int>)view.View(1, 0);
+
+ Assert.AreSame(list, view2.Underlying);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ Assert.IsTrue(view.FIFO);
+ view.Add(23);view.Add(24);view.Add(25);
+ check();
+ Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
+ Assert.AreEqual(1, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
+ view.FIFO = false;
+ Assert.IsFalse(view.FIFO);
+ Assert.AreEqual(25, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24));
+ }
+
+
+ [Test]
+ public void MapEtc()
+ {
+ ArrayList<double> dbl = (ArrayList<double>)view.Map(new Mapper<int,double>(delegate(int i){return i/10.0;}));
+
+ Assert.IsTrue(dbl.Check());
+ Assert.AreEqual(0.1, dbl[0]);
+ Assert.AreEqual(0.2, dbl[1]);
+ for (int i = 0; i < 10; i++) view.Add(i);
+
+ ArrayList<int> list2 = (ArrayList<int>)view.FindAll(new Filter<int>(delegate(int i){return i%4==1;}));
+
+ Assert.IsTrue(list2.Check());
+ Assert.IsTrue(IC.eq(list2, 1, 5, 9));
+ }
+
+
+ [Test]
+ public void FL()
+ {
+ Assert.AreEqual(1, view.First);
+ Assert.AreEqual(2, view.Last);
+ }
+
+
+ [Test]
+ public void Indexing()
+ {
+ list.Clear();
+ for (int i = 0; i < 20; i++) list.Add(i);
+
+ view = (HashedArrayList<int>)list.View(5, 7);
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.IndexOf(i + 5));
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
+ }
+
+
+ [Test]
+ public void Insert()
+ {
+ view.Insert(0, 34);
+ view.Insert(1, 35);
+ view.Insert(4, 36);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
+
+ IList<int> list2 = new HashedArrayList<int>();
+
+ list2.Add(40);list2.Add(41);
+ view.InsertAll(3, list2);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 40, 41, 2, 36));
+ }
+
+
+ [Test]
+ public void Sort()
+ {
+ view.Add(45);view.Add(47);view.Add(46);view.Add(48);
+ Assert.IsFalse(view.IsSorted(new IC()));
+ view.Sort(new IC());
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 1, 2, 5));
+ Assert.IsTrue(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ Assert.IsFalse(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ Assert.IsFalse(view.Remove(0));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ view.RemoveAllCopies(3);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ Assert.IsTrue(IC.eq(list, 0, 2, 5, 3));
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 2, 5, 1));
+
+ HashedArrayList<int> l2 = new HashedArrayList<int>();
+
+ l2.Add(1);l2.Add(2);l2.Add(2);l2.Add(3);l2.Add(1);
+ view.RemoveAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view, 5));
+ view.RetainAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(2);view.Add(4);view.Add(5);
+ Assert.AreEqual(2, view.RemoveAt(0));
+ Assert.AreEqual(5, view.RemoveAt(1));
+ Assert.AreEqual(4, view.RemoveAt(0));
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(8);view.Add(6);view.Add(78);
+ Assert.AreEqual(8, view.RemoveFirst());
+ Assert.AreEqual(78, view.RemoveLast());
+ view.Add(2);view.Add(5);view.Add(3);view.Add(1);
+ view.RemoveInterval(1, 2);
+ check();
+ Assert.IsTrue(IC.eq(view, 6, 1));
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ view.Clear();
+ for (int i = 0; i < 10; i++) view.Add(10 + i);
+
+ view.Reverse(3, 4);
+ check();
+ Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
+ view.Reverse();
+ Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
+ Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
+ }
+
+
+ [Test]
+ public void Slide()
+ {
+ view.Slide(1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 3));
+ view.Slide(-2);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1));
+ view.Slide(0, 3);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1, 2));
+ view.Slide(2, 1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2));
+ view.Slide(-1, 0);
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(28);
+ Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
+ }
+ [Test]
+ public void Iterate()
+ {
+ list.Clear();
+ view = null;
+ foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 10, 11 }) list.Add(i);
+
+ view = (HashedArrayList<int>)list.View(list.Count - 2, 2);
+ int j = 666;
+ while (true)
+ {
+ //Console.WriteLine("View: {0}: {1} --> {2}", view.Count, view.First, view.Last);
+ if ((view.Last - view.First) % 2 == 1)
+ view.Insert(1, j++);
+ check();
+ if (view.Offset == 0)
+ break;
+ else
+ view.Slide(-1,2);
+ }
+ //foreach (int i in list) Console.Write(" " + i);
+ //Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 2, 4, 8, 668, 13, 6, 1, 667, 10, 666, 11));
+ }
+
+
+ [Test]
+ public void SyncRoot()
+ {
+ Assert.AreSame(view.SyncRoot, list.SyncRoot);
+ }
+ }
+ }
+
+ namespace HashingAndEquals
+ {
+ [TestFixture]
+ public class IIndexed
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ dat = new HashedArrayList<int>();
+ dut = new HashedArrayList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(0);dit.Add(31);
+ dat.Add(1);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ ((HashedArrayList<int>)dut).InsertFirst(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ dat = new HashedArrayList<int>();
+ dut = new HashedArrayList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count, params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count << 16) + retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1, 3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2, 3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1, 3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2, 7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(2);dit.Add(1);
+ dat.Add(3);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ dat = new HashedArrayList<int>();
+ dut = new HashedArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new HashedArrayList<ICollection<int>>();
+ Dat = new HashedArrayList<ICollection<int>>();
+ Dut = new HashedArrayList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ dat = new HashedArrayList<int>();
+ dut = new HashedArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new HashedArrayList<ICollection<int>>();
+ Dat = new HashedArrayList<ICollection<int>>();
+ Dut = new HashedArrayList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ dat = new HashedArrayList<int>();
+ dut = new HashedArrayList<int>();
+ dot = new HashedArrayList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new HashedArrayList<ISequenced<int>>();
+ Dat = new HashedArrayList<ISequenced<int>>();
+ Dut = new HashedArrayList<ISequenced<int>>();
+ Dot = new HashedArrayList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedArrayList<int>();
+ dat = new HashedArrayList<int>();
+ dut = new HashedArrayList<int>();
+ dot = new HashedArrayList<int>();
+ dit.Add(2);dit.Add(1); //{2,1}
+ dat.Add(1);dat.Add(2); //{1,2}
+ dut.Add(3); //{3}
+ dot.Add(2);dot.Add(1); //{2,1}
+ Dit = new HashedArrayList<ISequenced<int>>();
+ Dat = new HashedArrayList<ISequenced<int>>();
+ Dut = new HashedArrayList<ISequenced<int>>();
+ Dot = new HashedArrayList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit); // {{2,1},{3}}
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat); // {{3},{2,1},{1,2}}
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit); // {{2,1},{3}}
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut); // {{2,1},{3}}
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/arrays/SortedArrayTests.cs b/mcs/class/Mono.C5/Test/arrays/SortedArrayTests.cs
new file mode 100644
index 00000000000..62b2bf7f410
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/arrays/SortedArrayTests.cs
@@ -0,0 +1,2202 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+namespace nunit.arrays.sorted
+{
+ [TestFixture]
+ public class Ranges
+ {
+ private SortedArray<int> array;
+
+ private IComparer<int> c;
+
+
+ [SetUp]
+ public void Init()
+ {
+ c = new IC();
+ array = new SortedArray<int>(c);
+ for (int i = 1; i <= 10; i++)
+ {
+ array.Add(i * 2);
+ }
+ }
+
+
+ [Test]
+ public void Enumerator()
+ {
+ MSG.IEnumerator<int> e = array.RangeFromTo(5, 17).GetEnumerator();
+ int i = 3;
+
+ while (e.MoveNext())
+ {
+ Assert.AreEqual(2 * i++, e.Current);
+ }
+
+ Assert.AreEqual(9, i);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void Enumerator3()
+ {
+ MSG.IEnumerator<int> e = array.RangeFromTo(5, 17).GetEnumerator();
+
+ e.MoveNext();
+ array.Add(67);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
+
+ array.RemoveRangeFrom(18);
+ Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
+ array.RemoveRangeFrom(28);
+ Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
+ array.RemoveRangeFrom(13);
+ Assert.IsTrue(IC.eq(array, new int[] { 2, 4, 6, 8, 10, 12 }));
+ array.RemoveRangeFrom(2);
+ Assert.IsTrue(IC.eq(array));
+ foreach (int i in all) array.Add(i);
+
+ array.RemoveRangeTo(10);
+ Assert.IsTrue(IC.eq(array, new int[] { 10, 12, 14, 16, 18, 20 }));
+ array.RemoveRangeTo(2);
+ Assert.IsTrue(IC.eq(array, new int[] { 10, 12, 14, 16, 18, 20 }));
+ array.RemoveRangeTo(21);
+ Assert.IsTrue(IC.eq(array));
+ foreach (int i in all) array.Add(i);
+
+ array.RemoveRangeFromTo(4, 8);
+ Assert.IsTrue(IC.eq(array, 2, 8, 10, 12, 14, 16, 18, 20));
+ array.RemoveRangeFromTo(14, 28);
+ Assert.IsTrue(IC.eq(array, 2, 8, 10, 12));
+ array.RemoveRangeFromTo(0, 9);
+ Assert.IsTrue(IC.eq(array, 10, 12));
+ array.RemoveRangeFromTo(0, 81);
+ Assert.IsTrue(IC.eq(array));
+ }
+
+ [Test]
+ public void Normal()
+ {
+ int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
+
+ Assert.IsTrue(IC.eq(array, all));
+ Assert.IsTrue(IC.eq(array.RangeAll(), all));
+ Assert.AreEqual(10, array.RangeAll().Count);
+ Assert.IsTrue(IC.eq(array.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
+ Assert.AreEqual(5, array.RangeFrom(11).Count);
+ Assert.IsTrue(IC.eq(array.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
+ Assert.IsTrue(IC.eq(array.RangeFrom(2), all));
+ Assert.IsTrue(IC.eq(array.RangeFrom(1), all));
+ Assert.IsTrue(IC.eq(array.RangeFrom(21), new int[] { }));
+ Assert.IsTrue(IC.eq(array.RangeFrom(20), new int[] { 20 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(8), new int[] { 2, 4, 6 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(7), new int[] { 2, 4, 6 }));
+ Assert.AreEqual(3, array.RangeTo(7).Count);
+ Assert.IsTrue(IC.eq(array.RangeTo(2), new int[] { }));
+ Assert.IsTrue(IC.eq(array.RangeTo(1), new int[] { }));
+ Assert.IsTrue(IC.eq(array.RangeTo(3), new int[] { 2 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(20), new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(21), all));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(7, 12), new int[] { 8, 10 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(6, 11), new int[] { 6, 8, 10 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(1, 12), new int[] { 2, 4, 6, 8, 10 }));
+ Assert.AreEqual(5, array.RangeFromTo(1, 12).Count);
+ Assert.IsTrue(IC.eq(array.RangeFromTo(2, 12), new int[] { 2, 4, 6, 8, 10 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(6, 21), new int[] { 6, 8, 10, 12, 14, 16, 18, 20 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(6, 20), new int[] { 6, 8, 10, 12, 14, 16, 18 }));
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
+ int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 8, 6, 4, 2 };
+
+ Assert.IsTrue(IC.eq(array, all));
+ Assert.IsTrue(IC.eq(array.RangeAll().Backwards(), lla));
+ Assert.IsTrue(IC.eq(array.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
+ Assert.IsTrue(IC.eq(array.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
+ Assert.IsTrue(IC.eq(array.RangeFrom(2).Backwards(), lla));
+ Assert.IsTrue(IC.eq(array.RangeFrom(1).Backwards(), lla));
+ Assert.IsTrue(IC.eq(array.RangeFrom(21).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(array.RangeFrom(20).Backwards(), new int[] { 20 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(8).Backwards(), new int[] { 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(7).Backwards(), new int[] { 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(2).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(array.RangeTo(1).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(array.RangeTo(3).Backwards(), new int[] { 2 }));
+ Assert.IsTrue(IC.eq(array.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6, 4, 2}));
+ Assert.IsTrue(IC.eq(array.RangeTo(21).Backwards(), lla));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(7, 12).Backwards(), new int[] { 10, 8 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(6, 11).Backwards(), new int[] { 10, 8, 6 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(1, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(2, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 8, 6 }));
+ Assert.IsTrue(IC.eq(array.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6 }));
+ }
+
+ [Test]
+ public void Direction()
+ {
+ Assert.AreEqual(EnumerationDirection.Forwards, array.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, array.RangeFrom(20).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, array.RangeTo(7).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, array.RangeFromTo(1, 12).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, array.RangeAll().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, array.Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, array.RangeFrom(20).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, array.RangeTo(7).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, array.RangeFromTo(1, 12).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, array.RangeAll().Backwards().Direction);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ array = null;
+ c = null;
+ }
+ }
+
+ [TestFixture]
+ public class BagItf
+ {
+ private SortedArray<int> array;
+
+
+ [SetUp]
+ public void Init()
+ {
+ array = new SortedArray<int>(new IC());
+ for (int i = 10; i < 20; i++)
+ {
+ array.Add(i);
+ array.Add(i + 10);
+ }
+ }
+
+
+ [Test]
+ public void Both()
+ {
+ Assert.AreEqual(0, array.ContainsCount(7));
+ Assert.AreEqual(1, array.ContainsCount(10));
+ array.RemoveAllCopies(10);
+ Assert.AreEqual(0, array.ContainsCount(10));
+ array.RemoveAllCopies(7);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ array = null;
+ }
+ }
+
+
+ [TestFixture]
+ public class Div
+ {
+ private SortedArray<int> array;
+
+
+ [SetUp]
+ public void Init()
+ {
+ array = new SortedArray<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ for (int i = 10; i < 20; i++)
+ {
+ array.Add(i);
+ array.Add(i + 10);
+ }
+ }
+
+
+ [Test]
+ public void NoDuplicatesEtc()
+ {
+ Assert.IsFalse(array.AllowsDuplicates);
+ loadup();
+ Assert.IsFalse(array.AllowsDuplicates);
+ Assert.AreEqual(Speed.Log, array.ContainsSpeed);
+ Assert.IsTrue(array.Comparer.Compare(2, 3) < 0);
+ Assert.IsTrue(array.Comparer.Compare(4, 3) > 0);
+ Assert.IsTrue(array.Comparer.Compare(3, 3) == 0);
+ }
+
+ [Test]
+ public void Add()
+ {
+ Assert.IsTrue(array.Add(17));
+ Assert.IsFalse(array.Add(17));
+ Assert.IsTrue(array.Add(18));
+ Assert.IsFalse(array.Add(18));
+ Assert.AreEqual(2, array.Count);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ array = null;
+ }
+ }
+
+
+ [TestFixture]
+ public class FindOrAdd
+ {
+ private SortedArray<KeyValuePair<int,string>> bag;
+
+
+ [SetUp]
+ public void Init()
+ {
+ bag = new SortedArray<KeyValuePair<int,string>>(new KeyValuePairComparer<int,string>(new IC()));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ bag = null;
+ }
+
+
+ [Test]
+ public void Test()
+ {
+ KeyValuePair<int,string> p = new KeyValuePair<int,string>(3, "tre");
+
+ Assert.IsFalse(bag.FindOrAdd(ref p));
+ p.value = "drei";
+ Assert.IsTrue(bag.FindOrAdd(ref p));
+ Assert.AreEqual("tre", p.value);
+ p.value = "three";
+ Assert.AreEqual(1, bag.ContainsCount(p));
+ Assert.AreEqual("tre", bag[0].value);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private SortedArray<int> tree;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new SortedArray<int>(new IC());
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
+ tree.Add(7);
+ tree.Add(4);
+ Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 7));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ tree.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ tree.Add(6);
+ tree.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ tree.Add(4);
+ tree.Add(9);
+ tree.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 1009));
+ tree.Clear();
+ tree.Add(7);
+ tree.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ tree.Add(3);
+ tree.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ tree.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ tree.Add(3);
+ tree.Add(4);
+ tree.CopyTo(a, 9);
+ }
+ }
+
+
+ [TestFixture]
+ public class Combined
+ {
+ private IIndexedSorted<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new SortedArray<KeyValuePair<int,int>>(new KeyValuePairComparer<int,int>(new IC()));
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Remove
+ {
+ private SortedArray<int> array;
+
+
+ [SetUp]
+ public void Init()
+ {
+ array = new SortedArray<int>(new IC());
+ for (int i = 10; i < 20; i++)
+ {
+ array.Add(i);
+ array.Add(i + 10);
+ }
+ }
+
+
+ [Test]
+ public void SmallTrees()
+ {
+ array.Clear();
+ array.Add(7);
+ array.Add(9);
+ Assert.IsTrue(array.Remove(7));
+ Assert.IsTrue(array.Check());
+ }
+
+
+ [Test]
+ public void ByIndex()
+ {
+ //Remove root!
+ int n = array.Count;
+ int i = array[10];
+
+ array.RemoveAt(10);
+ Assert.IsTrue(array.Check());
+ Assert.IsFalse(array.Contains(i));
+ Assert.AreEqual(n - 1, array.Count);
+
+ //Low end
+ i = array.FindMin();
+ array.RemoveAt(0);
+ Assert.IsTrue(array.Check());
+ Assert.IsFalse(array.Contains(i));
+ Assert.AreEqual(n - 2, array.Count);
+
+ //high end
+ i = array.FindMax();
+ array.RemoveAt(array.Count - 1);
+ Assert.IsTrue(array.Check());
+ Assert.IsFalse(array.Contains(i));
+ Assert.AreEqual(n - 3, array.Count);
+
+ //Some leaf
+ i = 18;
+ array.RemoveAt(7);
+ Assert.IsTrue(array.Check());
+ Assert.IsFalse(array.Contains(i));
+ Assert.AreEqual(n - 4, array.Count);
+ }
+
+
+ [Test]
+ public void AlmostEmpty()
+ {
+ //Almost empty
+ array.Clear();
+ array.Add(3);
+ array.RemoveAt(0);
+ Assert.IsTrue(array.Check());
+ Assert.IsFalse(array.Contains(3));
+ Assert.AreEqual(0, array.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void Empty()
+ {
+ array.Clear();
+ array.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void HighIndex()
+ {
+ array.RemoveAt(array.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void LowIndex()
+ {
+ array.RemoveAt(-1);
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ Assert.IsFalse(array.Remove(-20));
+
+ //No demote case, with move_item
+ Assert.IsTrue(array.Remove(20));
+ Assert.IsTrue(array.Check());
+ Assert.IsFalse(array.Remove(20));
+
+ //plain case 2
+ Assert.IsTrue(array.Remove(14));
+ Assert.IsTrue(array.Check(), "Bad tree");
+
+ //case 1b
+ Assert.IsTrue(array.Remove(25));
+ Assert.IsTrue(array.Check(), "Bad tree");
+
+ //case 1c
+ Assert.IsTrue(array.Remove(29));
+ Assert.IsTrue(array.Check(), "Bad tree");
+
+ //1a (terminating)
+ Assert.IsTrue(array.Remove(10));
+ Assert.IsTrue(array.Check(), "Bad tree");
+
+ //2+1b
+ Assert.IsTrue(array.Remove(12));
+ Assert.IsTrue(array.Remove(11));
+
+ //1a+1b
+ Assert.IsTrue(array.Remove(18));
+ Assert.IsTrue(array.Remove(13));
+ Assert.IsTrue(array.Remove(15));
+
+ //2+1c
+ for (int i = 0; i < 10; i++)
+ array.Add(50 - 2 * i);
+
+ Assert.IsTrue(array.Remove(42));
+ Assert.IsTrue(array.Remove(38));
+ Assert.IsTrue(array.Remove(28));
+ Assert.IsTrue(array.Remove(40));
+
+ //
+ Assert.IsTrue(array.Remove(16));
+ Assert.IsTrue(array.Remove(23));
+ Assert.IsTrue(array.Remove(17));
+ Assert.IsTrue(array.Remove(19));
+ Assert.IsTrue(array.Remove(50));
+ Assert.IsTrue(array.Remove(26));
+ Assert.IsTrue(array.Remove(21));
+ Assert.IsTrue(array.Remove(22));
+ Assert.IsTrue(array.Remove(24));
+ for (int i = 0; i < 48; i++)
+ array.Remove(i);
+
+ //Almost empty tree:
+ Assert.IsFalse(array.Remove(26));
+ Assert.IsTrue(array.Remove(48));
+ Assert.IsTrue(array.Check(), "Bad tree");
+
+ //Empty tree:
+ Assert.IsFalse(array.Remove(26));
+ Assert.IsTrue(array.Check(), "Bad tree");
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ array = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class PredecessorStructure
+ {
+ private SortedArray<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new SortedArray<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ for (int i = 0; i < 20; i++)
+ tree.Add(2 * i);
+ }
+
+
+ [Test]
+ public void Predecessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.Predecessor(7));
+ Assert.AreEqual(6, tree.Predecessor(8));
+
+ //The bottom
+ Assert.AreEqual(0, tree.Predecessor(1));
+
+ //The top
+ Assert.AreEqual(38, tree.Predecessor(39));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was -2.")]
+ public void PredecessorTooLow1()
+ {
+ tree.Predecessor(-2);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was 0.")]
+ public void PredecessorTooLow2()
+ {
+ tree.Predecessor(0);
+ }
+
+
+ [Test]
+ public void WeakPredecessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.WeakPredecessor(7));
+ Assert.AreEqual(8, tree.WeakPredecessor(8));
+
+ //The bottom
+ Assert.AreEqual(0, tree.WeakPredecessor(1));
+ Assert.AreEqual(0, tree.WeakPredecessor(0));
+
+ //The top
+ Assert.AreEqual(38, tree.WeakPredecessor(39));
+ Assert.AreEqual(38, tree.WeakPredecessor(38));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was -1.")]
+ public void WeakPredecessorTooLow1()
+ {
+ tree.WeakPredecessor(-1);
+ }
+
+
+ [Test]
+ public void Successor()
+ {
+ loadup();
+ Assert.AreEqual(8, tree.Successor(7));
+ Assert.AreEqual(10, tree.Successor(8));
+
+ //The bottom
+ Assert.AreEqual(2, tree.Successor(0));
+ Assert.AreEqual(0, tree.Successor(-1));
+
+ //The top
+ Assert.AreEqual(38, tree.Successor(37));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 38.")]
+ public void SuccessorTooHigh1()
+ {
+ tree.Successor(38);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 39.")]
+ public void SuccessorTooHigh2()
+ {
+ tree.Successor(39);
+ }
+
+
+ [Test]
+ public void WeakSuccessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.WeakSuccessor(6));
+ Assert.AreEqual(8, tree.WeakSuccessor(7));
+
+ //The bottom
+ Assert.AreEqual(0, tree.WeakSuccessor(-1));
+ Assert.AreEqual(0, tree.WeakSuccessor(0));
+
+ //The top
+ Assert.AreEqual(38, tree.WeakSuccessor(37));
+ Assert.AreEqual(38, tree.WeakSuccessor(38));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 39.")]
+ public void WeakSuccessorTooHigh1()
+ {
+ tree.WeakSuccessor(39);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class PriorityQueue
+ {
+ private SortedArray<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new SortedArray<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ foreach (int i in new int[] { 1, 2, 3, 4 })
+ tree.Add(i);
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ loadup();
+ Assert.AreEqual(1, tree.FindMin());
+ Assert.AreEqual(4, tree.FindMax());
+ Assert.AreEqual(1, tree.DeleteMin());
+ Assert.AreEqual(4, tree.DeleteMax());
+ Assert.IsTrue(tree.Check(), "Bad tree");
+ Assert.AreEqual(2, tree.FindMin());
+ Assert.AreEqual(3, tree.FindMax());
+ Assert.AreEqual(2, tree.DeleteMin());
+ Assert.AreEqual(3, tree.DeleteMax());
+ Assert.IsTrue(tree.Check(), "Bad tree");
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty1()
+ {
+ tree.FindMin();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty2()
+ {
+ tree.FindMax();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty3()
+ {
+ tree.DeleteMin();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty4()
+ {
+ tree.DeleteMax();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IndexingAndCounting
+ {
+ private SortedArray<int> array;
+
+
+ [SetUp]
+ public void Init()
+ {
+ array = new SortedArray<int>(new IC());
+ }
+
+
+ private void populate()
+ {
+ array.Add(30);
+ array.Add(50);
+ array.Add(10);
+ array.Add(70);
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ populate();
+
+ int[] a = array.ToArray();
+
+ Assert.AreEqual(4, a.Length);
+ Assert.AreEqual(10, a[0]);
+ Assert.AreEqual(30, a[1]);
+ Assert.AreEqual(50, a[2]);
+ Assert.AreEqual(70, a[3]);
+ }
+
+
+ [Test]
+ public void GoodIndex()
+ {
+ Assert.AreEqual(-1, array.IndexOf(20));
+ Assert.AreEqual(-1, array.LastIndexOf(20));
+ populate();
+ Assert.AreEqual(10, array[0]);
+ Assert.AreEqual(30, array[1]);
+ Assert.AreEqual(50, array[2]);
+ Assert.AreEqual(70, array[3]);
+ Assert.AreEqual(0, array.IndexOf(10));
+ Assert.AreEqual(1, array.IndexOf(30));
+ Assert.AreEqual(2, array.IndexOf(50));
+ Assert.AreEqual(3, array.IndexOf(70));
+ Assert.AreEqual(-1, array.IndexOf(20));
+ Assert.AreEqual(-1, array.IndexOf(0));
+ Assert.AreEqual(-1, array.IndexOf(90));
+ Assert.AreEqual(0, array.LastIndexOf(10));
+ Assert.AreEqual(1, array.LastIndexOf(30));
+ Assert.AreEqual(2, array.LastIndexOf(50));
+ Assert.AreEqual(3, array.LastIndexOf(70));
+ Assert.AreEqual(-1, array.LastIndexOf(20));
+ Assert.AreEqual(-1, array.LastIndexOf(0));
+ Assert.AreEqual(-1, array.LastIndexOf(90));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void IndexTooLarge()
+ {
+ populate();
+ Console.WriteLine(array[4]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void IndexTooSmall()
+ {
+ populate();
+ Console.WriteLine(array[-1]);
+ }
+
+
+ [Test]
+ public void FilledTreeOutsideInput()
+ {
+ populate();
+ Assert.AreEqual(0, array.CountFrom(90));
+ Assert.AreEqual(0, array.CountFromTo(-20, 0));
+ Assert.AreEqual(0, array.CountFromTo(80, 100));
+ Assert.AreEqual(0, array.CountTo(0));
+ Assert.AreEqual(4, array.CountTo(90));
+ Assert.AreEqual(4, array.CountFromTo(-20, 90));
+ Assert.AreEqual(4, array.CountFrom(0));
+ }
+
+
+ [Test]
+ public void FilledTreeIntermediateInput()
+ {
+ populate();
+ Assert.AreEqual(3, array.CountFrom(20));
+ Assert.AreEqual(1, array.CountFromTo(20, 40));
+ Assert.AreEqual(2, array.CountTo(40));
+ }
+
+
+ [Test]
+ public void FilledTreeMatchingInput()
+ {
+ populate();
+ Assert.AreEqual(3, array.CountFrom(30));
+ Assert.AreEqual(2, array.CountFromTo(30, 70));
+ Assert.AreEqual(0, array.CountFromTo(50, 30));
+ Assert.AreEqual(0, array.CountFromTo(50, 50));
+ Assert.AreEqual(0, array.CountTo(10));
+ Assert.AreEqual(2, array.CountTo(50));
+ }
+
+
+ [Test]
+ public void CountEmptyTree()
+ {
+ Assert.AreEqual(0, array.CountFrom(20));
+ Assert.AreEqual(0, array.CountFromTo(20, 40));
+ Assert.AreEqual(0, array.CountTo(40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ array = null;
+ }
+ }
+
+
+
+
+ namespace ModificationCheck
+ {
+ [TestFixture]
+ public class Enumerator
+ {
+ private SortedArray<int> tree;
+
+ private MSG.IEnumerator<int> e;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new SortedArray<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ e = tree.GetEnumerator();
+ }
+
+
+ [Test]
+ public void CurrentAfterModification()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ Assert.AreEqual(0, e.Current);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterAdd()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ e.MoveNext();
+ }
+
+
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterRemove()
+ {
+ e.MoveNext();
+ tree.Remove(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterClear()
+ {
+ e.MoveNext();
+ tree.Clear();
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ e = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class RangeEnumerator
+ {
+ private SortedArray<int> tree;
+
+ private MSG.IEnumerator<int> e;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new SortedArray<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ e = tree.RangeFromTo(3, 7).GetEnumerator();
+ }
+
+
+ [Test]
+ public void CurrentAfterModification()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ Assert.AreEqual(3, e.Current);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterAdd()
+ {
+ tree.Add(34);
+ e.MoveNext();
+ }
+
+
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterRemove()
+ {
+ tree.Remove(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterClear()
+ {
+ tree.Clear();
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ e = null;
+ }
+ }
+ }
+
+ namespace HigherOrder
+ {
+ internal class CubeRoot: IComparable<int>
+ {
+ private int c;
+
+
+ internal CubeRoot(int c) { this.c = c; }
+
+
+ public int CompareTo(int that) { return c - that * that * that; }
+
+ public bool Equals(int that) { return c == that * that * that; }
+
+ }
+
+
+
+ class Interval: IComparable<int>
+ {
+ private int b, t;
+
+
+ internal Interval(int b, int t) { this.b = b; this.t = t; }
+
+
+ public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
+
+ public bool Equals(int that) { return that >= b && that <= t; }
+ }
+
+
+
+ [TestFixture]
+ public class Simple
+ {
+ private SortedArray<int> array;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ array = new SortedArray<int>(ic);
+ }
+
+
+ private bool never(int i) { return false; }
+
+
+ private bool always(int i) { return true; }
+
+
+ private bool even(int i) { return i % 2 == 0; }
+
+
+ private string themap(int i) { return String.Format("AA {0,4} BB", i); }
+
+
+ private string badmap(int i) { return String.Format("AA {0} BB", i); }
+
+
+ private int appfield1;
+
+ private int appfield2;
+
+
+ private void apply(int i) { appfield1++; appfield2 += i * i; }
+
+
+ [Test]
+ public void Apply()
+ {
+ Simple simple1 = new Simple();
+
+ array.Apply(new Applier<int>(simple1.apply));
+ Assert.AreEqual(0, simple1.appfield1);
+ Assert.AreEqual(0, simple1.appfield2);
+
+ Simple simple2 = new Simple();
+
+ for (int i = 0; i < 10; i++) array.Add(i);
+
+ array.Apply(new Applier<int>(simple2.apply));
+ Assert.AreEqual(10, simple2.appfield1);
+ Assert.AreEqual(285, simple2.appfield2);
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(array.All(new Filter<int>(never)));
+ Assert.IsTrue(array.All(new Filter<int>(even)));
+ Assert.IsTrue(array.All(new Filter<int>(always)));
+ for (int i = 0; i < 10; i++) array.Add(i);
+
+ Assert.IsFalse(array.All(new Filter<int>(never)));
+ Assert.IsFalse(array.All(new Filter<int>(even)));
+ Assert.IsTrue(array.All(new Filter<int>(always)));
+ array.Clear();
+ for (int i = 0; i < 10; i++) array.Add(i * 2);
+
+ Assert.IsFalse(array.All(new Filter<int>(never)));
+ Assert.IsTrue(array.All(new Filter<int>(even)));
+ Assert.IsTrue(array.All(new Filter<int>(always)));
+ array.Clear();
+ for (int i = 0; i < 10; i++) array.Add(i * 2 + 1);
+
+ Assert.IsFalse(array.All(new Filter<int>(never)));
+ Assert.IsFalse(array.All(new Filter<int>(even)));
+ Assert.IsTrue(array.All(new Filter<int>(always)));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(array.Exists(new Filter<int>(never)));
+ Assert.IsFalse(array.Exists(new Filter<int>(even)));
+ Assert.IsFalse(array.Exists(new Filter<int>(always)));
+ for (int i = 0; i < 10; i++) array.Add(i);
+
+ Assert.IsFalse(array.Exists(new Filter<int>(never)));
+ Assert.IsTrue(array.Exists(new Filter<int>(even)));
+ Assert.IsTrue(array.Exists(new Filter<int>(always)));
+ array.Clear();
+ for (int i = 0; i < 10; i++) array.Add(i * 2);
+
+ Assert.IsFalse(array.Exists(new Filter<int>(never)));
+ Assert.IsTrue(array.Exists(new Filter<int>(even)));
+ Assert.IsTrue(array.Exists(new Filter<int>(always)));
+ array.Clear();
+ for (int i = 0; i < 10; i++) array.Add(i * 2 + 1);
+
+ Assert.IsFalse(array.Exists(new Filter<int>(never)));
+ Assert.IsFalse(array.Exists(new Filter<int>(even)));
+ Assert.IsTrue(array.Exists(new Filter<int>(always)));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Assert.AreEqual(0, array.FindAll(new Filter<int>(never)).Count);
+ for (int i = 0; i < 10; i++)
+ array.Add(i);
+
+ Assert.AreEqual(0, array.FindAll(new Filter<int>(never)).Count);
+ Assert.AreEqual(10, array.FindAll(new Filter<int>(always)).Count);
+ Assert.AreEqual(5, array.FindAll(new Filter<int>(even)).Count);
+ Assert.IsTrue(((SortedArray<int>)array.FindAll(new Filter<int>(even))).Check());
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Assert.AreEqual(0, array.Map(new Mapper<int,string>(themap), new SC()).Count);
+ for (int i = 0; i < 11; i++)
+ array.Add(i * i * i);
+
+ IIndexedSorted<string> res = array.Map(new Mapper<int,string>(themap), new SC());
+
+ Assert.IsTrue(((SortedArray<string>)res).Check());
+ Assert.AreEqual(11, res.Count);
+ Assert.AreEqual("AA 0 BB", res[0]);
+ Assert.AreEqual("AA 27 BB", res[3]);
+ Assert.AreEqual("AA 125 BB", res[5]);
+ Assert.AreEqual("AA 1000 BB", res[10]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "mapper not monotonic")]
+ public void BadMap()
+ {
+ for (int i = 0; i < 11; i++)
+ array.Add(i * i * i);
+
+ ISorted<string> res = array.Map(new Mapper<int,string>(badmap), new SC());
+ }
+
+
+ [Test]
+ public void Cut()
+ {
+ for (int i = 0; i < 10; i++)
+ array.Add(i);
+
+ int low, high;
+ bool lval, hval;
+
+ Assert.IsTrue(array.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(2, low);
+ Assert.IsFalse(array.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(3, low);
+ }
+
+
+ [Test]
+ public void CutInt()
+ {
+ for (int i = 0; i < 10; i++)
+ array.Add(2 * i);
+
+ int low, high;
+ bool lval, hval;
+
+ Assert.IsFalse(array.Cut(new IC(3), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(2, low);
+ Assert.IsTrue(array.Cut(new IC(6), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(8, high);
+ Assert.AreEqual(4, low);
+ }
+
+
+ [Test]
+ public void CutInterval()
+ {
+ for (int i = 0; i < 10; i++)
+ array.Add(2 * i);
+
+ int lo, hi;
+ bool lv, hv;
+
+ Assert.IsTrue(array.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(10, hi);
+ Assert.AreEqual(4, lo);
+ Assert.IsTrue(array.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(12, hi);
+ Assert.AreEqual(4, lo);
+ for (int i = 0; i < 100; i++)
+ array.Add(2 * i);
+
+ array.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(106, hi);
+ Assert.AreEqual(76, lo);
+ array.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(8, hi);
+ Assert.AreEqual(4, lo);
+ array.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(112, hi);
+ Assert.AreEqual(78, lo);
+ }
+
+
+ [Test]
+ public void UpperCut()
+ {
+ for (int i = 0; i < 10; i++)
+ array.Add(i);
+
+ int l, h;
+ bool lv, hv;
+
+ Assert.IsFalse(array.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
+ Assert.IsTrue(lv && !hv);
+ Assert.AreEqual(9, l);
+ Assert.IsFalse(array.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
+ Assert.IsTrue(!lv && hv);
+ Assert.AreEqual(0, h);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; array = null; }
+ }
+ }
+
+
+
+
+ namespace MultiOps
+ {
+ [TestFixture]
+ public class AddAll
+ {
+ private int sqr(int i) { return i * i; }
+
+
+ SortedArray<int> array;
+
+
+ [SetUp]
+ public void Init() { array = new SortedArray<int>(new IC()); }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ array.AddAll(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(array.Check());
+ }
+
+
+ [Test]
+ public void SomeEmpty()
+ {
+ for (int i = 4; i < 9; i++) array.Add(i);
+
+ array.AddAll(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(5, array.Count);
+ Assert.IsTrue(array.Check());
+ }
+
+
+ [Test]
+ public void EmptySome()
+ {
+ array.AddAll(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(4, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.AreEqual(0, array[0]);
+ Assert.AreEqual(1, array[1]);
+ Assert.AreEqual(4, array[2]);
+ Assert.AreEqual(9, array[3]);
+ }
+
+
+ [Test]
+ public void SomeSome()
+ {
+ for (int i = 3; i < 9; i++) array.Add(i);
+
+ array.AddAll(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(9, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 0, 1, 3,4, 5, 6, 7, 8, 9));
+ }
+
+
+ [TearDown]
+ public void Dispose() { array = null; }
+ }
+
+
+
+ [TestFixture]
+ public class AddSorted
+ {
+ private int sqr(int i) { return i * i; }
+
+
+ private int bad(int i) { return i * (5 - i); }
+
+
+ SortedArray<int> array;
+
+
+ [SetUp]
+ public void Init() { array = new SortedArray<int>(new IC()); }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ array.AddSorted(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(array.Check());
+ }
+
+
+
+ [Test]
+ public void SomeEmpty()
+ {
+ for (int i = 4; i < 9; i++) array.Add(i);
+
+ array.AddSorted(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(5, array.Count);
+ Assert.IsTrue(array.Check());
+ }
+
+
+
+ [Test]
+ public void EmptySome()
+ {
+ array.AddSorted(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(4, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.AreEqual(0, array[0]);
+ Assert.AreEqual(1, array[1]);
+ Assert.AreEqual(4, array[2]);
+ Assert.AreEqual(9, array[3]);
+ }
+
+
+
+ [Test]
+ public void SomeSome()
+ {
+ for (int i = 3; i < 9; i++) array.Add(i);
+
+ array.AddSorted(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(9, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 0, 1, 3, 4, 5, 6, 7, 8, 9));
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Argument not sorted")]
+ public void EmptyBad()
+ {
+ array.AddSorted(new FunEnumerable(9, new Int2Int(bad)));
+ }
+
+
+ [TearDown]
+ public void Dispose() { array = null; }
+ }
+
+ [TestFixture]
+ public class Rest
+ {
+ SortedArray<int> array, array2;
+
+
+ [SetUp]
+ public void Init()
+ {
+ array = new SortedArray<int>(new IC());
+ array2 = new SortedArray<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ array.Add(i);
+
+ for (int i = 0; i < 10; i++)
+ array2.Add(2 * i);
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ array.RemoveAll(array2.RangeFromTo(3, 7));
+ Assert.AreEqual(8, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
+ array.RemoveAll(array2.RangeFromTo(3, 7));
+ Assert.AreEqual(8, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
+ array.RemoveAll(array2.RangeFromTo(13, 17));
+ Assert.AreEqual(8, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 8, 9));
+ array.RemoveAll(array2.RangeFromTo(3, 17));
+ Assert.AreEqual(7, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 0, 1, 2, 3, 5, 7, 9));
+ for (int i = 0; i < 10; i++) array2.Add(i);
+
+ array.RemoveAll(array2.RangeFromTo(-1, 10));
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ array.RetainAll(array2.RangeFromTo(3, 17));
+ Assert.AreEqual(3, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 4, 6, 8));
+ array.RetainAll(array2.RangeFromTo(1, 17));
+ Assert.AreEqual(3, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 4, 6, 8));
+ array.RetainAll(array2.RangeFromTo(3, 5));
+ Assert.AreEqual(1, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array, 4));
+ array.RetainAll(array2.RangeFromTo(7, 17));
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array));
+ for (int i = 0; i < 10; i++) array.Add(i);
+
+ array.RetainAll(array2.RangeFromTo(5, 5));
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array));
+ for (int i = 0; i < 10; i++) array.Add(i);
+
+ array.RetainAll(array2.RangeFromTo(15, 25));
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(array.Check());
+ Assert.IsTrue(IC.eq(array));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ Assert.IsFalse(array.ContainsAll(array2));
+ Assert.IsTrue(array.ContainsAll(array));
+ array2.Clear();
+ Assert.IsTrue(array.ContainsAll(array2));
+ array.Clear();
+ Assert.IsTrue(array.ContainsAll(array2));
+ array2.Add(8);
+ Assert.IsFalse(array.ContainsAll(array2));
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ array.RemoveInterval(3, 4);
+ Assert.IsTrue(array.Check());
+ Assert.AreEqual(6, array.Count);
+ Assert.IsTrue(IC.eq(array, 0, 1, 2, 7, 8, 9));
+ array.RemoveInterval(2, 3);
+ Assert.IsTrue(array.Check());
+ Assert.AreEqual(3, array.Count);
+ Assert.IsTrue(IC.eq(array, 0, 1, 9));
+ array.RemoveInterval(0, 3);
+ Assert.IsTrue(array.Check());
+ Assert.AreEqual(0, array.Count);
+ Assert.IsTrue(IC.eq(array));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RemoveRangeBad1()
+ {
+ array.RemoveInterval(-3, 8);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RemoveRangeBad2()
+ {
+ array.RemoveInterval(3, -8);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void RemoveRangeBad3()
+ {
+ array.RemoveInterval(3, 8);
+ }
+
+
+ [Test]
+ public void GetRange()
+ {
+ MSG.IEnumerable<int> e = array[3, 3];
+
+ Assert.IsTrue(IC.eq(e, 3, 4, 5));
+ e = array[3, 0];
+ Assert.IsTrue(IC.eq(e));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetRangeBad1()
+ {
+ object foo = array[-3, 0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetRangeBad2()
+ {
+ object foo = array[3, -1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void GetRangeBad3()
+ {
+ object foo = array[3, 8];
+ }
+
+
+ [TearDown]
+ public void Dispose() { array = null; array2 = null; }
+ }
+ }
+
+
+
+
+ namespace Sync
+ {
+ [TestFixture]
+ public class SyncRoot
+ {
+ private SortedArray<int> tree;
+
+ int sz = 5000;
+
+
+ [Test]
+ public void Safe()
+ {
+ System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(safe1));
+ System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(safe2));
+
+ t1.Start();
+ t2.Start();
+ t1.Join();
+ t2.Join();
+ Assert.AreEqual(2 * sz + 1, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ //[Test]
+ public void UnSafe()
+ {
+ bool bad = false;
+
+ for (int i = 0; i < 10; i++)
+ {
+ System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
+ System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
+
+ t1.Start();
+ t2.Start();
+ t1.Join();
+ t2.Join();
+ if (bad = 2 * sz + 1 != tree.Count)
+ {
+ Console.WriteLine("{0}::Unsafe(): bad at {1}", GetType(), i);
+ break;
+ }
+ }
+
+ Assert.IsTrue(bad, "No sync problems!");
+ }
+
+
+ [Test]
+ public void SafeUnSafe()
+ {
+ System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
+ System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
+
+ t1.Start();
+ t1.Join();
+ t2.Start();
+ t2.Join();
+ Assert.AreEqual(2 * sz + 1, tree.Count);
+ }
+
+
+ [SetUp]
+ public void Init() { tree = new SortedArray<int>(new IC()); }
+
+
+ private void unsafe1()
+ {
+ for (int i = 0; i < 2 * sz; i++)
+ tree.Add(i * 2);
+
+ for (int i = 1; i < sz; i++)
+ tree.Remove(i * 4);
+ }
+
+
+ private void safe1()
+ {
+ for (int i = 0; i < 2 * sz; i++)
+ lock (tree.SyncRoot)
+ tree.Add(i * 2);
+
+ for (int i = 1; i < sz; i++)
+ lock (tree.SyncRoot)
+ tree.Remove(i * 4);
+ }
+
+
+ private void unsafe2()
+ {
+ for (int i = 2 * sz; i > 0; i--)
+ tree.Add(i * 2 + 1);
+
+ for (int i = sz; i > 0; i--)
+ tree.Remove(i * 4 + 1);
+ }
+
+
+ private void safe2()
+ {
+ for (int i = 2 * sz; i > 0; i--)
+ lock (tree.SyncRoot)
+ tree.Add(i * 2 + 1);
+
+ for (int i = sz; i > 0; i--)
+ lock (tree.SyncRoot)
+ tree.Remove(i * 4 + 1);
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+
+
+
+ //[TestFixture]
+ public class ConcurrentQueries
+ {
+ private SortedArray<int> tree;
+
+ int sz = 500000;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new SortedArray<int>(new IC());
+ for (int i = 0; i < sz; i++)
+ {
+ tree.Add(i);
+ }
+ }
+
+
+
+ class A
+ {
+ public int count = 0;
+
+ SortedArray<int> t;
+
+
+ public A(SortedArray<int> t) { this.t = t; }
+
+
+ public void a(int i) { count++; }
+
+
+ public void traverse() { t.Apply(new Applier<int>(a)); }
+ }
+
+
+
+
+ [Test]
+ public void Safe()
+ {
+ A a = new A(tree);
+
+ a.traverse();
+ Assert.AreEqual(sz, a.count);
+ }
+
+
+ [Test]
+ public void RegrettablyUnsafe()
+ {
+ System.Threading.Thread[] t = new System.Threading.Thread[10];
+ A[] a = new A[10];
+ for (int i = 0; i < 10; i++)
+ {
+ a[i] = new A(tree);
+ t[i] = new System.Threading.Thread(new System.Threading.ThreadStart(a[i].traverse));
+ }
+
+ for (int i = 0; i < 10; i++)
+ t[i].Start();
+ for (int i = 0; i < 10; i++)
+ t[i].Join();
+ for (int i = 0; i < 10; i++)
+ Assert.AreEqual(sz,a[i].count);
+
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+ }
+
+
+
+
+ namespace Hashing
+ {
+ [TestFixture]
+ public class ISequenced
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new SortedArray<int>(new IC());
+ dat = new SortedArray<int>(new IC());
+ dut = new SortedArray<int>(new RevIC());
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new SortedArray<int>(new IC());
+ dat = new SortedArray<int>(new IC());
+ dut = new SortedArray<int>(new RevIC());
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count,params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count<<16)+retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1,3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2,3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1,3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2,7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/hashing/HashBagTests.cs b/mcs/class/Mono.C5/Test/hashing/HashBagTests.cs
new file mode 100644
index 00000000000..9fc16ff0e62
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/hashing/HashBagTests.cs
@@ -0,0 +1,464 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+namespace nunit.hashtable.bag
+{
+ [TestFixture]
+ public class Combined
+ {
+ private ICollection<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new HashBag<KeyValuePair<int,int>>();
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ q.key = 13;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(13, q.key);
+ Assert.AreEqual(79, q.value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.Update(p));
+ q.key = 3;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(3, q.key);
+ Assert.AreEqual(78, q.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ q.key = 3;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(3, q.key);
+ Assert.AreEqual(78, q.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ q.key = 13;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(13, q.key);
+ Assert.AreEqual(79, q.value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+
+ [TestFixture]
+ public class CollectionOrSink
+ {
+ private HashBag<int> hashbag;
+
+
+ [SetUp]
+ public void Init() { hashbag = new HashBag<int>(); }
+
+
+ [Test]
+ public void CountEtAl()
+ {
+ Assert.IsFalse(hashbag.IsReadOnly);
+ Assert.IsFalse(hashbag.SyncRoot == null);
+ Assert.AreEqual(0, hashbag.Count);
+ Assert.IsTrue(hashbag.IsEmpty);
+ Assert.IsTrue(hashbag.AllowsDuplicates);
+ Assert.IsTrue(hashbag.Add(0));
+ Assert.AreEqual(1, hashbag.Count);
+ Assert.IsFalse(hashbag.IsEmpty);
+ Assert.IsTrue(hashbag.Add(5));
+ Assert.AreEqual(2, hashbag.Count);
+ Assert.IsTrue(hashbag.Add(5));
+ Assert.AreEqual(3, hashbag.Count);
+ Assert.IsFalse(hashbag.IsEmpty);
+ Assert.IsTrue(hashbag.Add(8));
+ Assert.AreEqual(4, hashbag.Count);
+ Assert.AreEqual(2, hashbag.ContainsCount(5));
+ Assert.AreEqual(1, hashbag.ContainsCount(8));
+ Assert.AreEqual(1, hashbag.ContainsCount(0));
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ hashbag.Add(3);hashbag.Add(4);hashbag.Add(4);hashbag.Add(5);hashbag.Add(4);
+
+ HashBag<int> hashbag2 = new HashBag<int>();
+
+ hashbag2.AddAll(hashbag);
+ Assert.IsTrue(IC.seq(hashbag2, 3, 4, 4, 4, 5));
+ hashbag.Add(9);
+ hashbag.AddAll(hashbag2);
+ Assert.IsTrue(IC.seq(hashbag2, 3, 4, 4, 4, 5));
+ Assert.IsTrue(IC.seq(hashbag, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 9));
+ }
+
+
+ [Test]
+ public void ContainsCount()
+ {
+ Assert.AreEqual(0, hashbag.ContainsCount(5));
+ hashbag.Add(5);
+ Assert.AreEqual(1, hashbag.ContainsCount(5));
+ Assert.AreEqual(0, hashbag.ContainsCount(7));
+ hashbag.Add(8);
+ Assert.AreEqual(1, hashbag.ContainsCount(5));
+ Assert.AreEqual(0, hashbag.ContainsCount(7));
+ Assert.AreEqual(1, hashbag.ContainsCount(8));
+ hashbag.Add(5);
+ Assert.AreEqual(2, hashbag.ContainsCount(5));
+ Assert.AreEqual(0, hashbag.ContainsCount(7));
+ Assert.AreEqual(1, hashbag.ContainsCount(8));
+ }
+
+
+ [Test]
+ public void RemoveAllCopies()
+ {
+ hashbag.Add(5);hashbag.Add(7);hashbag.Add(5);
+ Assert.AreEqual(2, hashbag.ContainsCount(5));
+ Assert.AreEqual(1, hashbag.ContainsCount(7));
+ hashbag.RemoveAllCopies(5);
+ Assert.AreEqual(0, hashbag.ContainsCount(5));
+ Assert.AreEqual(1, hashbag.ContainsCount(7));
+ hashbag.Add(5);hashbag.Add(8);hashbag.Add(5);
+ hashbag.RemoveAllCopies(8);
+ Assert.IsTrue(IC.eq(hashbag, 7, 5, 5));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ HashBag<int> list2 = new HashBag<int>();
+
+ Assert.IsTrue(hashbag.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(hashbag.ContainsAll(list2));
+ hashbag.Add(4);
+ Assert.IsTrue(hashbag.ContainsAll(list2));
+ hashbag.Add(5);
+ Assert.IsTrue(hashbag.ContainsAll(list2));
+ list2.Add(20);
+ Assert.IsFalse(hashbag.ContainsAll(list2));
+ hashbag.Add(20);
+ Assert.IsTrue(hashbag.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(hashbag.ContainsAll(list2));
+ hashbag.Add(4);
+ Assert.IsTrue(hashbag.ContainsAll(list2));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ HashBag<int> list2 = new HashBag<int>();
+
+ hashbag.Add(4);hashbag.Add(5);hashbag.Add(4);hashbag.Add(6);hashbag.Add(4);
+ list2.Add(5);list2.Add(4);list2.Add(7);list2.Add(4);
+ hashbag.RetainAll(list2);
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 5));
+ hashbag.Add(6);
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ hashbag.RetainAll(list2);
+ Assert.IsTrue(IC.eq(hashbag));
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ HashBag<int> list2 = new HashBag<int>();
+
+ hashbag.Add(4);hashbag.Add(5);hashbag.Add(6);hashbag.Add(4);hashbag.Add(5);
+ list2.Add(5);list2.Add(4);list2.Add(7);list2.Add(4);
+ hashbag.RemoveAll(list2);
+ Assert.IsTrue(IC.seq(hashbag, 5, 6));
+ hashbag.Add(5);hashbag.Add(4);
+ list2.Clear();
+ list2.Add(6);list2.Add(5);
+ hashbag.RemoveAll(list2);
+ Assert.IsTrue(IC.seq(hashbag, 4, 5));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ hashbag.RemoveAll(list2);
+ Assert.IsTrue(IC.seq(hashbag, 4, 5));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ hashbag.Add(4);hashbag.Add(4);hashbag.Add(5);hashbag.Add(4);hashbag.Add(6);
+ Assert.IsFalse(hashbag.Remove(2));
+ Assert.IsTrue(hashbag.Remove(4));
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 5, 6));
+ hashbag.Add(7);
+ hashbag.Add(21);hashbag.Add(37);hashbag.Add(53);hashbag.Add(69);hashbag.Add(53);hashbag.Add(85);
+ Assert.IsTrue(hashbag.Remove(5));
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 6, 7, 21, 37, 53, 53, 69, 85));
+ Assert.IsFalse(hashbag.Remove(165));
+ Assert.IsTrue(hashbag.Check());
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 6, 7, 21, 37, 53, 53, 69, 85));
+ Assert.IsTrue(hashbag.Remove(53));
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 6, 7, 21, 37, 53, 69, 85));
+ Assert.IsTrue(hashbag.Remove(37));
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 6, 7, 21, 53, 69, 85));
+ Assert.IsTrue(hashbag.Remove(85));
+ Assert.IsTrue(IC.seq(hashbag, 4, 4, 6, 7, 21, 53, 69));
+ }
+
+
+ [TearDown]
+ public void Dispose() { hashbag = null; }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private HashBag<int> hashbag;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ hashbag = new HashBag<int>();
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { hashbag = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(hashbag.ToArray()));
+ hashbag.Add(7);
+ hashbag.Add(3);
+ hashbag.Add(10);
+ hashbag.Add(3);
+
+ int[] r = hashbag.ToArray();
+
+ Array.Sort(r);
+ Assert.AreEqual("Alles klar", aeq(r, 3, 3, 7, 10));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ //Note: for small ints the itemhasher is the identity!
+ hashbag.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ hashbag.Add(6);
+ hashbag.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ hashbag.Add(4);
+ hashbag.Add(6);
+ hashbag.Add(9);
+ hashbag.CopyTo(a, 4);
+
+ //TODO: make independent of interhasher
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 6, 9, 4, 1008, 1009));
+ hashbag.Clear();
+ hashbag.Add(7);
+ hashbag.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 6, 9, 4, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ hashbag.Add(3);
+ hashbag.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ hashbag.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ hashbag.Add(3);
+ hashbag.Add(8);
+ hashbag.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class HashingEquals
+ {
+ private ICollection<int> h1, h2;
+
+
+ [SetUp]
+ public void Init()
+ {
+ h1 = new HashBag<int>();
+ h2 = new LinkedList<int>();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ h1 = h2 = null;
+ }
+
+
+ [Test]
+ public void Hashing()
+ {
+ Assert.AreEqual(h1.GetHashCode(), h2.GetHashCode());
+ h1.Add(7);
+ h2.Add(9);
+ Assert.IsTrue(h1.GetHashCode() != h2.GetHashCode());
+ h2.Add(7);
+ h1.Add(9);
+ Assert.IsTrue(h1.GetHashCode() == h2.GetHashCode());
+ }
+
+
+ [Test]
+ public void Equals()
+ {
+ Assert.IsTrue(h1.Equals(h2));
+ h1.Add(1);
+ h1.Add(2);
+ h1.Add(1);
+ h1.Add(2);
+ h2.Add(0);
+ h2.Add(3);
+ h2.Add(0);
+ h2.Add(3);
+ Assert.IsTrue(h1.GetHashCode() == h2.GetHashCode());
+ Assert.IsTrue(!h1.Equals(h2));
+ h1.Clear();
+ h2.Clear();
+ h1.Add(1);
+ h1.Add(2);
+ h2.Add(2);
+ h2.Add(1);
+ Assert.IsTrue(h1.GetHashCode() == h2.GetHashCode());
+ Assert.IsTrue(h1.Equals(h2));
+ }
+ }
+}
diff --git a/mcs/class/Mono.C5/Test/hashing/HashDictionaryTests.cs b/mcs/class/Mono.C5/Test/hashing/HashDictionaryTests.cs
new file mode 100644
index 00000000000..3c9b434757e
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/hashing/HashDictionaryTests.cs
@@ -0,0 +1,250 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+namespace nunit.hashtable.dictionary
+{
+ [TestFixture]
+ public class HashDict
+ {
+ private HashDictionary<string,string> dict;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dict = new HashDictionary<string,string>();
+ //dict = TreeDictionary<string,string>.MakeNaturalO<string,string>();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dict = null;
+ }
+
+
+ [Test]
+ public void Initial()
+ {
+ bool res;
+
+ Assert.IsFalse(dict.IsReadOnly);
+ Assert.AreEqual(0, dict.Count, "new dict should be empty");
+ dict.Add("A", "B");
+ Assert.AreEqual(1, dict.Count, "bad count");
+ Assert.AreEqual("B", dict["A"], "Wrong value for dict[A]");
+ dict.Add("C", "D");
+ Assert.AreEqual(2, dict.Count, "bad count");
+ Assert.AreEqual("B", dict["A"], "Wrong value");
+ Assert.AreEqual("D", dict["C"], "Wrong value");
+ res = dict.Remove("A");
+ Assert.IsTrue(res, "bad return value from Remove(A)");
+ Assert.AreEqual(1, dict.Count, "bad count");
+ Assert.AreEqual("D", dict["C"], "Wrong value of dict[C]");
+ res = dict.Remove("Z");
+ Assert.IsFalse(res, "bad return value from Remove(Z)");
+ Assert.AreEqual(1, dict.Count, "bad count");
+ Assert.AreEqual("D", dict["C"], "Wrong value of dict[C] (2)");
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ dict.Add("C", "D");
+ Assert.IsTrue(dict.Contains("C"));
+ Assert.IsFalse(dict.Contains("D"));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Item has already been added. Key in dictionary: 'A' Key being added: 'A'")]
+ public void IllegalAdd()
+ {
+ dict.Add("A", "B");
+ dict.Add("A", "B");
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Key not present in Dictionary")]
+ public void GettingNonExisting()
+ {
+ Console.WriteLine(dict["R"]);
+ }
+
+
+ [Test]
+ public void Setter()
+ {
+ dict["R"] = "UYGUY";
+ Assert.AreEqual("UYGUY", dict["R"]);
+ dict["R"] = "UIII";
+ Assert.AreEqual("UIII", dict["R"]);
+ dict["S"] = "VVV";
+ Assert.AreEqual("UIII", dict["R"]);
+ Assert.AreEqual("VVV", dict["S"]);
+ //dict.dump();
+ }
+
+ [Test]
+ public void CombinedOps()
+ {
+ dict["R"] = "UIII";
+ dict["S"] = "VVV";
+ dict["T"] = "XYZ";
+
+ string s;
+
+ Assert.IsTrue(dict.Remove("S", out s));
+ Assert.AreEqual("VVV", s);
+ Assert.IsFalse(dict.Contains("S"));
+ Assert.IsFalse(dict.Remove("A", out s));
+
+ //
+ Assert.IsTrue(dict.Find("T", out s));
+ Assert.AreEqual("XYZ", s);
+ Assert.IsFalse(dict.Find("A", out s));
+
+ //
+ Assert.IsTrue(dict.Update("R", "UHU"));
+ Assert.AreEqual("UHU", dict["R"]);
+ Assert.IsFalse(dict.Update("A", "W"));
+ Assert.IsFalse(dict.Contains("A"));
+
+ //
+ s = "KKK";
+ Assert.IsFalse(dict.FindOrAdd("B", ref s));
+ Assert.AreEqual("KKK", dict["B"]);
+ Assert.IsTrue(dict.FindOrAdd("T", ref s));
+ Assert.AreEqual("XYZ", s);
+
+ //
+ s = "LLL";
+ Assert.IsTrue(dict.UpdateOrAdd("R", s));
+ Assert.AreEqual("LLL", dict["R"]);
+ s = "MMM";
+ Assert.IsFalse(dict.UpdateOrAdd("C", s));
+ Assert.AreEqual("MMM", dict["C"]);
+ }
+
+ [Test]
+ public void DeepBucket()
+ {
+ HashDictionary<int,int> dict2 = new HashDictionary<int,int>();
+
+ for (int i = 0; i < 5; i++)
+ dict2[16 * i] = 5 * i;
+
+ for (int i = 0; i < 5; i++)
+ Assert.AreEqual(5 * i, dict2[16 * i]);
+
+ for (int i = 0; i < 5; i++)
+ dict2[16 * i] = 7 * i + 1;
+
+ for (int i = 0; i < 5; i++)
+ Assert.AreEqual(7 * i + 1, dict2[16 * i]);
+ Assert.IsTrue(dict.Check());
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Enumerators
+ {
+ private HashDictionary<string,string> dict;
+
+ private MSG.IEnumerator<KeyValuePair<string,string>> dictenum;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dict = new HashDictionary<string,string>();
+ dict["S"] = "A";
+ dict["T"] = "B";
+ dict["R"] = "C";
+ dictenum = dict.GetEnumerator();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dictenum = null;
+ dict = null;
+ }
+
+
+ [Test]
+ public void Keys()
+ {
+ MSG.IEnumerator<string> keys = dict.Keys.GetEnumerator();
+
+ Assert.IsTrue(keys.MoveNext());
+ Assert.AreEqual("R", keys.Current);
+ Assert.IsTrue(keys.MoveNext());
+ Assert.AreEqual("T", keys.Current);
+ Assert.IsTrue(keys.MoveNext());
+ Assert.AreEqual("S", keys.Current);
+ Assert.IsFalse(keys.MoveNext());
+ }
+
+
+ [Test]
+ public void Values()
+ {
+ MSG.IEnumerator<string> values = dict.Values.GetEnumerator();
+
+ Assert.IsTrue(values.MoveNext());
+ Assert.AreEqual("C", values.Current);
+ Assert.IsTrue(values.MoveNext());
+ Assert.AreEqual("B", values.Current);
+ Assert.IsTrue(values.MoveNext());
+ Assert.AreEqual("A", values.Current);
+ Assert.IsFalse(values.MoveNext());
+ }
+
+
+ [Test]
+ public void NormalUse()
+ {
+ Assert.IsTrue(dictenum.MoveNext());
+ Assert.AreEqual(dictenum.Current, new KeyValuePair<string,string>("R", "C"));
+ Assert.IsTrue(dictenum.MoveNext());
+ Assert.AreEqual(dictenum.Current, new KeyValuePair<string,string>("T", "B"));
+ Assert.IsTrue(dictenum.MoveNext());
+ Assert.AreEqual(dictenum.Current, new KeyValuePair<string,string>("S", "A"));
+ Assert.IsFalse(dictenum.MoveNext());
+ }
+ }
+}
+
+
+
+
+
diff --git a/mcs/class/Mono.C5/Test/hashing/HashTableTests.cs b/mcs/class/Mono.C5/Test/hashing/HashTableTests.cs
new file mode 100644
index 00000000000..781bd2f1ad2
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/hashing/HashTableTests.cs
@@ -0,0 +1,1058 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+namespace nunit.hashtable.set
+{
+ namespace Enumerable
+ {
+ [TestFixture]
+ public class Multiops
+ {
+ private HashSet<int> list;
+
+ private Filter<int> always, never, even;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashSet<int>();
+ always = delegate{return true;};
+ never = delegate{return false;};
+ even = delegate(int i){return i%2==0;};
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(list.All(always));
+ Assert.IsTrue(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(0);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(5);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsFalse(list.All(even));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(5);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(8);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsTrue(list.Exists(even));
+ }
+
+
+ [Test]
+ public void Apply()
+ {
+ int sum = 0;
+ Applier<int> a = delegate(int i){sum=i+10*sum;};
+
+ list.Apply(a);
+ Assert.AreEqual(0, sum);
+ sum = 0;
+ list.Add(5);list.Add(8);list.Add(7);list.Add(5);
+ list.Apply(a);
+ Assert.AreEqual(758, sum);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class GetEnumerator
+ {
+ private HashSet<int> hashset;
+
+
+ [SetUp]
+ public void Init() { hashset = new HashSet<int>(); }
+
+
+ [Test]
+ public void Empty()
+ {
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+ hashset.Add(5);
+ hashset.Add(10);
+ hashset.Add(1);
+ hashset.Add(16);
+ hashset.Add(18);
+ hashset.Add(17);
+ hashset.Add(33);
+ Assert.IsTrue(IC.seq(hashset, 1, 5, 8, 10, 16, 17, 18, 33));
+ }
+
+
+#if SAFEENUMERATORS
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void TooEarlyCurrent()
+ {
+ int none = hashset.GetEnumerator().Current;
+ }
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void TooLateMoveNext()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void TooLateCurrent()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+ e.MoveNext();
+
+ int i = e.Current;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterDispose()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ e.MoveNext();
+ e.Dispose();
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void CurrentAfterDispose()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ e.MoveNext();
+ e.Dispose();
+
+ int i = e.Current;
+ }
+#endif
+
+ [Test]
+ public void DoDispose()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.Dispose();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ hashset.Add(5);
+ hashset.Add(8);
+ hashset.Add(5);
+
+ MSG.IEnumerator<int> e = hashset.GetEnumerator();
+
+ e.MoveNext();
+ hashset.Add(99);
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose() { hashset = null; }
+ }
+ }
+
+
+
+
+ namespace CollectionOrSink
+ {
+ [TestFixture]
+ public class CollectionOrSink
+ {
+ private HashSet<int> hashset;
+
+
+ [SetUp]
+ public void Init() { hashset = new HashSet<int>(); }
+
+
+ [Test]
+ public void CountEtAl()
+ {
+ Assert.AreEqual(0, hashset.Count);
+ Assert.IsTrue(hashset.IsEmpty);
+ Assert.IsFalse(hashset.AllowsDuplicates);
+ Assert.IsTrue(hashset.Add(0));
+ Assert.AreEqual(1, hashset.Count);
+ Assert.IsFalse(hashset.IsEmpty);
+ Assert.IsTrue(hashset.Add(5));
+ Assert.AreEqual(2, hashset.Count);
+ Assert.IsFalse(hashset.Add(5));
+ Assert.AreEqual(2, hashset.Count);
+ Assert.IsFalse(hashset.IsEmpty);
+ Assert.IsTrue(hashset.Add(8));
+ Assert.AreEqual(3, hashset.Count);
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ hashset.Add(3);hashset.Add(4);hashset.Add(5);
+
+ HashSet<int> hashset2 = new HashSet<int>();
+
+ hashset2.AddAll(hashset);
+ Assert.IsTrue(IC.seq(hashset2, 3, 4, 5));
+ hashset.Add(9);
+ hashset.AddAll(hashset2);
+ Assert.IsTrue(IC.seq(hashset2, 3, 4, 5));
+ Assert.IsTrue(IC.seq(hashset, 3, 4, 5, 9));
+ }
+
+
+ [TearDown]
+ public void Dispose() { hashset = null; }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private HashSet<int> hashset;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ hashset = new HashSet<int>();
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { hashset = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(hashset.ToArray()));
+ hashset.Add(7);
+ hashset.Add(3);
+ hashset.Add(10);
+
+ int[] r = hashset.ToArray();
+
+ Array.Sort(r);
+ Assert.AreEqual("Alles klar", aeq(r, 3, 7, 10));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ //Note: for small ints the itemhasher is the identity!
+ hashset.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ hashset.Add(6);
+ hashset.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ hashset.Add(4);
+ hashset.Add(9);
+ hashset.CopyTo(a, 4);
+
+ //TODO: make test independent on onterhasher
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 9, 4, 1007, 1008, 1009));
+ hashset.Clear();
+ hashset.Add(7);
+ hashset.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 9, 4, 1007, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ hashset.Add(3);
+ hashset.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ hashset.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ hashset.Add(3);
+ hashset.Add(8);
+ hashset.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sync
+ {
+ private HashSet<int> list;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashSet<int>();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ [Test]
+ public void Get()
+ {
+ Assert.IsNotNull(list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace EditableCollection
+ {
+ [TestFixture]
+ public class Collision
+ {
+ HashSet<int> hashset;
+
+
+ [SetUp]
+ public void Init()
+ {
+ hashset = new HashSet<int>();
+ }
+
+
+ [Test]
+ public void SingleCollision()
+ {
+ hashset.Add(7);
+ hashset.Add(7 - 1503427877);
+
+ //foreach (int i in hashset) Console.WriteLine("A: {0}", i);
+ hashset.Remove(7);
+ Assert.IsTrue(hashset.Contains(7 - 1503427877));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ hashset = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Searching
+ {
+ private HashSet<int> hashset;
+
+
+ [SetUp]
+ public void Init() { hashset = new HashSet<int>(); }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsFalse(hashset.Contains(5));
+ hashset.Add(5);
+ Assert.IsTrue(hashset.Contains(5));
+ Assert.IsFalse(hashset.Contains(7));
+ hashset.Add(8);
+ hashset.Add(10);
+ Assert.IsTrue(hashset.Contains(5));
+ Assert.IsFalse(hashset.Contains(7));
+ Assert.IsTrue(hashset.Contains(8));
+ Assert.IsTrue(hashset.Contains(10));
+ hashset.Remove(8);
+ Assert.IsTrue(hashset.Contains(5));
+ Assert.IsFalse(hashset.Contains(7));
+ Assert.IsFalse(hashset.Contains(8));
+ Assert.IsTrue(hashset.Contains(10));
+ hashset.Add(0);hashset.Add(16);hashset.Add(32);hashset.Add(48);hashset.Add(64);
+ Assert.IsTrue(hashset.Contains(0));
+ Assert.IsTrue(hashset.Contains(16));
+ Assert.IsTrue(hashset.Contains(32));
+ Assert.IsTrue(hashset.Contains(48));
+ Assert.IsTrue(hashset.Contains(64));
+ Assert.IsTrue(hashset.Check());
+
+ int i = 0, j = i;
+
+ Assert.IsTrue(hashset.Find(ref i));
+ Assert.AreEqual(j, i);
+ j = i = 16;
+ Assert.IsTrue(hashset.Find(ref i));
+ Assert.AreEqual(j, i);
+ j = i = 32;
+ Assert.IsTrue(hashset.Find(ref i));
+ Assert.AreEqual(j, i);
+ j = i = 48;
+ Assert.IsTrue(hashset.Find(ref i));
+ Assert.AreEqual(j, i);
+ j = i = 64;
+ Assert.IsTrue(hashset.Find(ref i));
+ Assert.AreEqual(j, i);
+ j = i = 80;
+ Assert.IsFalse(hashset.Find(ref i));
+ Assert.AreEqual(j, i);
+ }
+
+
+ [Test]
+ public void Many()
+ {
+ int j = 7373;
+ int[] a = new int[j];
+
+ for (int i = 0; i < j; i++)
+ {
+ hashset.Add(3 * i + 1);
+ a[i] = 3 * i + 1;
+ }
+
+ Assert.IsTrue(IC.seq(hashset, a));
+ }
+
+
+ [Test]
+ public void ContainsCount()
+ {
+ Assert.AreEqual(0, hashset.ContainsCount(5));
+ hashset.Add(5);
+ Assert.AreEqual(1, hashset.ContainsCount(5));
+ Assert.AreEqual(0, hashset.ContainsCount(7));
+ hashset.Add(8);
+ Assert.AreEqual(1, hashset.ContainsCount(5));
+ Assert.AreEqual(0, hashset.ContainsCount(7));
+ Assert.AreEqual(1, hashset.ContainsCount(8));
+ hashset.Add(5);
+ Assert.AreEqual(1, hashset.ContainsCount(5));
+ Assert.AreEqual(0, hashset.ContainsCount(7));
+ Assert.AreEqual(1, hashset.ContainsCount(8));
+ }
+
+
+ [Test]
+ public void RemoveAllCopies()
+ {
+ hashset.Add(5);hashset.Add(7);hashset.Add(5);
+ Assert.AreEqual(1, hashset.ContainsCount(5));
+ Assert.AreEqual(1, hashset.ContainsCount(7));
+ hashset.RemoveAllCopies(5);
+ Assert.AreEqual(0, hashset.ContainsCount(5));
+ Assert.AreEqual(1, hashset.ContainsCount(7));
+ hashset.Add(5);hashset.Add(8);hashset.Add(5);
+ hashset.RemoveAllCopies(8);
+ Assert.IsTrue(IC.eq(hashset, 7, 5));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ HashSet<int> list2 = new HashSet<int>();
+
+ Assert.IsTrue(hashset.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(hashset.ContainsAll(list2));
+ hashset.Add(4);
+ Assert.IsTrue(hashset.ContainsAll(list2));
+ hashset.Add(5);
+ Assert.IsTrue(hashset.ContainsAll(list2));
+ list2.Add(20);
+ Assert.IsFalse(hashset.ContainsAll(list2));
+ hashset.Add(20);
+ Assert.IsTrue(hashset.ContainsAll(list2));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ HashSet<int> list2 = new HashSet<int>();
+
+ hashset.Add(4);hashset.Add(5);hashset.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);
+ hashset.RetainAll(list2);
+ Assert.IsTrue(IC.seq(hashset, 4, 5));
+ hashset.Add(6);
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ hashset.RetainAll(list2);
+ Assert.IsTrue(IC.seq(hashset));
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ HashSet<int> list2 = new HashSet<int>();
+
+ hashset.Add(4);hashset.Add(5);hashset.Add(6);
+ list2.Add(5);list2.Add(7);list2.Add(4);
+ hashset.RemoveAll(list2);
+ Assert.IsTrue(IC.eq(hashset, 6));
+ hashset.Add(5);hashset.Add(4);
+ list2.Clear();
+ list2.Add(6);list2.Add(5);
+ hashset.RemoveAll(list2);
+ Assert.IsTrue(IC.eq(hashset, 4));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ hashset.RemoveAll(list2);
+ Assert.IsTrue(IC.eq(hashset, 4));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ hashset.Add(4);hashset.Add(4);hashset.Add(5);hashset.Add(4);hashset.Add(6);
+ Assert.IsFalse(hashset.Remove(2));
+ Assert.IsTrue(hashset.Remove(4));
+ Assert.IsTrue(IC.seq(hashset, 5, 6));
+ hashset.Add(7);
+ hashset.Add(21);hashset.Add(37);hashset.Add(53);hashset.Add(69);hashset.Add(85);
+ Assert.IsTrue(hashset.Remove(5));
+ Assert.IsTrue(IC.seq(hashset, 6, 7, 21, 37, 53, 69, 85));
+ Assert.IsFalse(hashset.Remove(165));
+ Assert.IsTrue(IC.seq(hashset, 6, 7, 21, 37, 53, 69, 85));
+ Assert.IsTrue(hashset.Remove(53));
+ Assert.IsTrue(IC.seq(hashset, 6, 7, 21, 37, 69, 85));
+ Assert.IsTrue(hashset.Remove(37));
+ Assert.IsTrue(IC.seq(hashset, 6, 7, 21, 69, 85));
+ Assert.IsTrue(hashset.Remove(85));
+ Assert.IsTrue(IC.seq(hashset, 6, 7, 21, 69));
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ hashset.Add(7);hashset.Add(7);
+ hashset.Clear();
+ Assert.IsTrue(hashset.IsEmpty);
+ }
+
+
+ [TearDown]
+ public void Dispose() { hashset = null; }
+ }
+
+ [TestFixture]
+ public class Combined
+ {
+ private ICollection<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new HashSet<KeyValuePair<int,int>>();
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ q.key = 13;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(13, q.key);
+ Assert.AreEqual(79, q.value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.Update(p));
+ q.key = 3;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(3, q.key);
+ Assert.AreEqual(78, q.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ q.key = 3;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(3, q.key);
+ Assert.AreEqual(78, q.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ q.key = 13;
+ Assert.IsTrue(lst.Find(ref q));
+ Assert.AreEqual(13, q.key);
+ Assert.AreEqual(79, q.value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+ KeyValuePair<int,int> q = new KeyValuePair<int,int>();
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ }
+
+
+
+
+ namespace HashingAndEquals
+ {
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashSet<int>();
+ dat = new HashSet<int>();
+ dut = new HashSet<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count, params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count << 16) + retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1, 3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2, 3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1, 3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2, 7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(2);dit.Add(1);
+ dat.Add(3);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashSet<int>();
+ dat = new HashSet<int>();
+ dut = new HashSet<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new HashSet<ICollection<int>>();
+ Dat = new HashSet<ICollection<int>>();
+ Dut = new HashSet<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashSet<int>();
+ dat = new HashSet<int>();
+ dut = new HashSet<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new LinkedList<ICollection<int>>();
+ Dat = new LinkedList<ICollection<int>>();
+ Dut = new LinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dot = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new HashSet<ISequenced<int>>();
+ Dat = new HashSet<ISequenced<int>>();
+ Dut = new HashSet<ISequenced<int>>();
+ Dot = new HashSet<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);//Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);//Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dit));
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/heaps/HeapTests.cs b/mcs/class/Mono.C5/Test/heaps/HeapTests.cs
new file mode 100644
index 00000000000..6b0fe28d2ed
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/heaps/HeapTests.cs
@@ -0,0 +1,191 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+namespace nunit.heaps
+{
+
+ [TestFixture]
+ public class IntervalHeapTests
+ {
+ IPriorityQueue<int> queue;
+
+
+ [SetUp]
+ public void Init() { queue = new IntervalHeap<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { queue = null; }
+
+
+ [Test]
+ public void Simple()
+ {
+ Assert.IsTrue(queue.AllowsDuplicates);
+ Assert.AreEqual(0, queue.Count);
+ queue.Add(8); queue.Add(18); queue.Add(8); queue.Add(3);
+ Assert.AreEqual(4, queue.Count);
+ Assert.AreEqual(18, queue.DeleteMax());
+ Assert.AreEqual(3, queue.Count);
+ Assert.AreEqual(3, queue.DeleteMin());
+ Assert.AreEqual(2, queue.Count);
+ Assert.AreEqual(8, queue.FindMax());
+ Assert.AreEqual(8, queue.DeleteMax());
+ Assert.AreEqual(8, queue.FindMax());
+ queue.Add(15);
+ Assert.AreEqual(15, queue.FindMax());
+ Assert.AreEqual(8, queue.FindMin());
+ Assert.IsNotNull(queue.SyncRoot);
+ Assert.IsTrue(queue.Comparer.Compare(2, 3) < 0);
+ Assert.IsTrue(queue.Comparer.Compare(4, 3) > 0);
+ Assert.IsTrue(queue.Comparer.Compare(3, 3) == 0);
+
+ }
+
+
+ [Test]
+ public void Enumerate()
+ {
+ int[] a = new int[4];
+ int siz = 0;
+ foreach (int i in queue)
+ siz++;
+ Assert.AreEqual(0, siz);
+
+ queue.Add(8); queue.Add(18); queue.Add(8); queue.Add(3);
+
+ foreach (int i in queue)
+ a[siz++] = i;
+ Assert.AreEqual(4, siz);
+ Array.Sort(a,0,siz);
+ Assert.AreEqual(3, a[0]);
+ Assert.AreEqual(8, a[1]);
+ Assert.AreEqual(8, a[2]);
+ Assert.AreEqual(18, a[3]);
+
+ siz = 0;
+ Assert.AreEqual(18, queue.DeleteMax());
+ foreach (int i in queue)
+ a[siz++] = i;
+ Assert.AreEqual(3, siz);
+ Array.Sort(a, 0, siz);
+ Assert.AreEqual(3, a[0]);
+ Assert.AreEqual(8, a[1]);
+ Assert.AreEqual(8, a[2]);
+
+ siz = 0;
+ Assert.AreEqual(8, queue.DeleteMax());
+ foreach (int i in queue)
+ a[siz++] = i;
+ Assert.AreEqual(2, siz);
+ Array.Sort(a, 0, siz);
+ Assert.AreEqual(3, a[0]);
+ Assert.AreEqual(8, a[1]);
+
+ siz = 0;
+ Assert.AreEqual(8, queue.DeleteMax());
+ foreach (int i in queue)
+ a[siz++] = i;
+ Assert.AreEqual(1, siz);
+ Assert.AreEqual(3, a[0]);
+ }
+
+ [Test]
+ public void Random()
+ {
+ int length = 1000;
+ int[] a = new int[length];
+ Random ran = new Random(6754);
+
+ for (int i = 0; i < length; i++)
+ queue.Add(a[i] = ran.Next());
+
+ Assert.IsTrue(queue.Check());
+ Array.Sort(a);
+ for (int i = 0; i < length / 2; i++)
+ {
+ Assert.AreEqual(a[length - i - 1], queue.DeleteMax());
+ Assert.IsTrue(queue.Check());
+ Assert.AreEqual(a[i], queue.DeleteMin());
+ Assert.IsTrue(queue.Check());
+ }
+
+ Assert.IsTrue(queue.IsEmpty);
+ }
+
+
+ [Test]
+ public void RandomDuplicates()
+ {
+ int length = 1000;
+ int s;
+ int[] a = new int[length];
+ Random ran = new Random(6754);
+
+ for (int i = 0; i < length; i++)
+ queue.Add(a[i] = ran.Next(3, 13));
+ Assert.IsTrue(queue.Check());
+
+ Array.Sort(a);
+
+ for (int i = 0; i < length/2; i++)
+ {
+ Assert.AreEqual(a[i], queue.DeleteMin());
+ Assert.IsTrue(queue.Check());
+ Assert.AreEqual(a[length - i - 1], s = queue.DeleteMax());
+ Assert.IsTrue(queue.Check());
+ }
+
+ Assert.IsTrue(queue.IsEmpty);
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ int length = 1000;
+ int[] a = new int[length];
+ Random ran = new Random(6754);
+
+ LinkedList<int> lst = new LinkedList<int>();
+ for (int i = 0; i < length; i++)
+ lst.Add(a[i] = ran.Next());
+
+ queue.AddAll(lst);
+ Assert.IsTrue(queue.Check());
+ Array.Sort(a);
+ for (int i = 0; i < length / 2; i++)
+ {
+ Assert.AreEqual(a[length - i - 1], queue.DeleteMax());
+ Assert.IsTrue(queue.Check());
+ Assert.AreEqual(a[i], queue.DeleteMin());
+ Assert.IsTrue(queue.Check());
+ }
+
+ Assert.IsTrue(queue.IsEmpty);
+ }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/linkedlists/HashedLinkedListTest.cs b/mcs/class/Mono.C5/Test/linkedlists/HashedLinkedListTest.cs
new file mode 100644
index 00000000000..e81b0065964
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/linkedlists/HashedLinkedListTest.cs
@@ -0,0 +1,2035 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+
+namespace nunit.linkedlists.hashed
+{
+ namespace Enumerable
+ {
+ [TestFixture]
+ public class Multiops
+ {
+ private HashedLinkedList<int> list;
+
+ private Filter<int> always, never, even;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedLinkedList<int>();
+ always = delegate{return true;};
+ never = delegate{return false;};
+ even = delegate(int i){return i%2==0;};
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(list.All(always));
+ Assert.IsTrue(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(8);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(5);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsFalse(list.All(even));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(5);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(8);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsTrue(list.Exists(even));
+ }
+
+
+ [Test]
+ public void Apply()
+ {
+ int sum = 0;
+ Applier<int> a = delegate(int i){sum=i+10*sum;};
+
+ list.Apply(a);
+ Assert.AreEqual(0, sum);
+ sum = 0;
+ list.Add(5);list.Add(8);list.Add(7);list.Add(5);
+ list.Apply(a);
+ Assert.AreEqual(587, sum);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class GetEnumerator
+ {
+ private HashedLinkedList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new HashedLinkedList<int>(); }
+
+
+ [Test]
+ public void Empty()
+ {
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+ list.Add(5);
+ list.Add(10);
+ list.Add(1);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(8, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(10, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(1, e.Current);
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void DoDispose()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.Dispose();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ list.Add(99);
+ e.MoveNext();
+ }
+
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace CollectionOrSink
+ {
+ [TestFixture]
+ public class CollectionOrSink
+ {
+ private HashedLinkedList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new HashedLinkedList<int>(); }
+
+
+ [Test]
+ public void CountEtAl()
+ {
+ Assert.AreEqual(0, list.Count);
+ Assert.IsTrue(list.IsEmpty);
+ Assert.IsFalse(list.AllowsDuplicates);
+ Assert.IsTrue(list.Add(5));
+ Assert.AreEqual(1, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ Assert.IsFalse(list.Add(5));
+ Assert.AreEqual(1, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ Assert.IsTrue(list.Add(8));
+ Assert.AreEqual(2, list.Count);
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ list.Add(3);list.Add(4);list.Add(5);
+
+ HashedLinkedList<int> list2 = new HashedLinkedList<int>();
+
+ list2.AddAll(list);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ list.AddAll(list2);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ Assert.IsTrue(IC.eq(list, 3, 4, 5));
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private HashedLinkedList<int> list;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedLinkedList<int>();
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(list.ToArray()));
+ list.Add(7);
+ list.Add(8);
+ Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 8));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ list.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(6);
+ list.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(4);
+ list.Add(5);
+ list.Add(9);
+ list.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 1009));
+ list.Clear();
+ list.Add(7);
+ list.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ list.Add(3);
+ list.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ list.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ list.Add(3);
+ list.Add(4);
+ list.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sync
+ {
+ private HashedLinkedList<int> list;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedLinkedList<int>();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ [Test]
+ public void Get()
+ {
+ Assert.IsNotNull(list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace EditableCollection
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private HashedLinkedList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new HashedLinkedList<int>(); }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsFalse(list.Contains(5));
+ list.Add(5);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ list.Add(8);
+ list.Add(10);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsTrue(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ list.Remove(8);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsFalse(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ }
+
+ [Test]
+ public void BadAdd()
+ {
+ Assert.IsTrue(list.Add(5));
+ Assert.IsTrue(list.Add(8));
+ Assert.IsFalse(list.Add(5));
+ }
+
+
+ [Test]
+ public void ContainsCount()
+ {
+ Assert.AreEqual(0, list.ContainsCount(5));
+ list.Add(5);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ list.Add(8);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ Assert.AreEqual(1, list.ContainsCount(8));
+ }
+
+
+ [Test]
+ public void RemoveAllCopies()
+ {
+ list.Add(5);list.Add(7);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.RemoveAllCopies(5);
+ Assert.IsTrue(list.Check());
+ Assert.AreEqual(0, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.Add(5);list.Add(8);
+ list.RemoveAllCopies(8);
+ Assert.IsTrue(IC.eq(list, 7, 5));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Filter<int> f = delegate(int i){return i%2==0;};
+
+ Assert.IsTrue(list.FindAll(f).IsEmpty);
+ list.Add(5);list.Add(8);list.Add(10);
+ Assert.IsTrue(((LinkedList<int>)list.FindAll(f)).Check());
+ Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ HashedLinkedList<int> list2 = new HashedLinkedList<int>();
+
+ Assert.IsTrue(list.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(list.ContainsAll(list2));
+ list.Add(4);
+ Assert.IsTrue(list.ContainsAll(list2));
+ list.Add(5);
+ Assert.IsTrue(list.ContainsAll(list2));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ HashedLinkedList<int> list2 = new HashedLinkedList<int>();
+
+ list.Add(4);list.Add(5);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 5));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(5);list2.Add(5);list2.Add(6);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 6));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list));
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ HashedLinkedList<int> list2 = new HashedLinkedList<int>();
+
+ list.Add(4);list.Add(5);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 6));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(6);list2.Add(5);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ list.Add(4);list.Add(5);list.Add(6);
+ Assert.IsFalse(list.Remove(2));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(list.Remove(4));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 6));
+ Assert.AreEqual(6, list.RemoveLast());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5));
+ list.Add(7);
+ Assert.AreEqual(5, list.RemoveFirst());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 7));
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ list.Add(7);list.Add(6);
+ list.Clear();
+ Assert.IsTrue(list.IsEmpty);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace IIndexed
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ }
+
+
+ [Test]
+ public void IndexOf()
+ {
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(-1, dit.LastIndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ dit.Add(5);dit.Add(7);dit.Add(8);dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ Assert.AreEqual(0, dit.LastIndexOf(7));
+ Assert.AreEqual(2, dit.IndexOf(8));
+ Assert.AreEqual(1, dit.LastIndexOf(5));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Removing
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ }
+
+
+ [Test]
+ public void RemoveAt()
+ {
+ dit.Add(5);dit.Add(7);dit.Add(9);dit.Add(1);dit.Add(2);
+ Assert.AreEqual(7, dit.RemoveAt(1));
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
+ Assert.AreEqual(5, dit.RemoveAt(0));
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1, 2));
+ Assert.AreEqual(2, dit.RemoveAt(2));
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad0()
+ {
+ dit.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBadM1()
+ {
+ dit.RemoveAt(-1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad1()
+ {
+ dit.Add(8);
+ dit.RemoveAt(1);
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ dit.RemoveInterval(0, 0);
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(3, 0);
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
+ dit.RemoveInterval(3, 1);
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
+ dit.RemoveInterval(1, 3);
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 60));
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit));
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
+ dit.RemoveInterval(2, 2);
+ Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+ }
+
+
+
+
+ namespace IList
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedLinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void FirstBad()
+ {
+ int f = lst.First;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void LastBad()
+ {
+ int f = lst.Last;
+ }
+
+
+ [Test]
+ public void FirstLast()
+ {
+ lst.Add(19);
+ Assert.AreEqual(19, lst.First);
+ Assert.AreEqual(19, lst.Last);
+ lst.Add(34);lst.InsertFirst(12);
+ Assert.AreEqual(12, lst.First);
+ Assert.AreEqual(34, lst.Last);
+ }
+
+
+ [Test]
+ public void This()
+ {
+ lst.Add(34);
+ Assert.AreEqual(34, lst[0]);
+ lst[0] = 56;
+ Assert.AreEqual(56, lst.First);
+ lst.Add(7);lst.Add(77);lst.Add(777);lst.Add(7777);
+ lst[0] = 45;lst[2] = 78;lst[4] = 101;
+ Assert.IsTrue(IC.eq(lst, 45, 7, 78, 777, 101));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptyGet()
+ {
+ int f = lst[0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowGet()
+ {
+ lst.Add(7);
+
+ int f = lst[-1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiGet()
+ {
+ lst.Add(6);
+
+ int f = lst[1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptySet()
+ {
+ lst[0] = 4;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowSet()
+ {
+ lst.Add(7);
+ lst[-1] = 9;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiSet()
+ {
+ lst.Add(6);
+ lst[1] = 11;
+ }
+ }
+
+
+ [TestFixture]
+ public class Combined
+ {
+ private IList<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new HashedLinkedList<KeyValuePair<int,int>>();
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Inserting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedLinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Insert()
+ {
+ lst.Insert(0, 5);
+ Assert.IsTrue(IC.eq(lst, 5));
+ lst.Insert(0, 7);
+ Assert.IsTrue(IC.eq(lst, 7, 5));
+ lst.Insert(1, 4);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5));
+ lst.Insert(3, 2);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertLow()
+ {
+ lst.Add(7);
+ lst.Insert(-1, 9);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertHi()
+ {
+ lst.Add(6);
+ lst.Insert(2, 11);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ for (int i = 0; i < 7; i++)
+ lst.Add(2 * i);
+
+ Assert.IsTrue(lst.FIFO);
+ Assert.AreEqual(0, lst.Remove());
+ Assert.AreEqual(2, lst.Remove());
+ lst.FIFO = false;
+ Assert.AreEqual(12, lst.Remove());
+ Assert.AreEqual(10, lst.Remove());
+ lst.FIFO = true;
+ Assert.AreEqual(4, lst.Remove());
+ Assert.AreEqual(6, lst.Remove());
+ }
+
+
+ [Test]
+ public void InsertFirstLast()
+ {
+ lst.InsertFirst(4);
+ lst.InsertLast(5);
+ lst.InsertFirst(14);
+ lst.InsertLast(15);
+ lst.InsertFirst(24);
+ lst.InsertLast(25);
+ lst.InsertFirst(34);
+ lst.InsertLast(55);
+ Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
+ }
+
+
+ [Test]
+ public void InsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ lst.Add(5);
+ lst.InsertBefore(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 3, 4, 5));
+ lst.InsertBefore(8, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 5));
+ lst.InsertBefore(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 9, 5));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertBefore(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAfter()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ lst.Add(5);
+ lst.InsertAfter(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 7, 3, 4, 5));
+ lst.InsertAfter(8, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5));
+ lst.InsertAfter(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfter()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(6);
+ lst.Add(5);
+ lst.InsertAfter(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAll()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+
+ IList<int> lst2 = new HashedLinkedList<int>();
+
+ lst2.Add(7);lst2.Add(8);lst2.Add(9);
+ lst.InsertAll(0, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
+ lst.RemoveAll(lst2);
+ lst.InsertAll(4, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 3, 4, 7, 8, 9));
+ lst.RemoveAll(lst2);
+ lst.InsertAll(2, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 7, 8, 9, 3, 4));
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Mapper<int,string> m = delegate(int i){return "<<"+i+">>";};
+ IList<string> r = lst.Map(m);
+
+ Assert.IsTrue(((LinkedList<string>)r).Check());
+ Assert.IsTrue(r.IsEmpty);
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ r = lst.Map(m);
+ Assert.IsTrue(((LinkedList<string>)r).Check());
+ Assert.AreEqual(4, r.Count);
+ for (int i = 0; i < 4; i++)
+ Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
+ }
+
+
+ [Test]
+ public void RemoveFirstLast()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ Assert.AreEqual(1, lst.RemoveFirst());
+ Assert.AreEqual(4, lst.RemoveLast());
+ Assert.AreEqual(2, lst.RemoveFirst());
+ Assert.AreEqual(3, lst.RemoveLast());
+ Assert.IsTrue(lst.IsEmpty);
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse();
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(0, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 0);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ lst.Reverse(5, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void BadReverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse(8, 3);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sorting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedLinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Sort()
+ {
+ lst.Add(5);lst.Add(6);lst.Add(55);lst.Add(7);lst.Add(3);
+ Assert.IsFalse(lst.IsSorted(new IC()));
+ lst.Sort(new IC());
+ Assert.IsTrue(lst.IsSorted(new IC()));
+ Assert.IsTrue(IC.eq(lst, 3, 5, 6, 7, 55));
+ }
+ }
+ }
+
+
+
+
+ namespace Range
+ {
+ [TestFixture]
+ public class Range
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new HashedLinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void GetRange()
+ {
+ //Assert.IsTrue(IC.eq(lst[0, 0)));
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
+ Assert.IsTrue(IC.eq(lst[3, 3], 3, 4, 5));
+ Assert.IsTrue(IC.eq(lst[6, 3], 6, 7, 8));
+ Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[0, 3].Backwards(), 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[3, 3].Backwards(), 5, 4, 3));
+ Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
+ }
+
+
+ [Test]
+ public void DirectionAndCount()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 7].Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 7].Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
+ Assert.AreEqual(4, lst[3, 4].Count);
+ Assert.AreEqual(4, lst[3, 4].Backwards().Count);
+ Assert.AreEqual(10, lst.Backwards().Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ foreach (int i in lst)
+ {
+ lst.Add(45 + i);
+ }
+ }
+ }
+ }
+
+
+
+
+ namespace View
+ {
+ [TestFixture]
+ public class Simple
+ {
+ HashedLinkedList<int> list;
+ LinkedList<int> view;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new HashedLinkedList<int>();
+ list.Add(0);list.Add(1);list.Add(2);list.Add(3);
+ view = (LinkedList<int>)list.View(1, 2);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ list = null;
+ view = null;
+ }
+
+
+ void check()
+ {
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(view.Check());
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfterLow()
+ {
+ view.InsertAfter(876, 0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfterHigh()
+ {
+ view.InsertAfter(876, 0);
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBeforeLow()
+ {
+ view.InsertBefore(876, 0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBeforeHigh()
+ {
+ view.InsertBefore(876, 0);
+ }
+
+
+ [Test]
+ public void Add()
+ {
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2));
+ view.InsertFirst(10);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 10, 1, 2));
+ view.Clear();
+ Assert.IsFalse(view.IsReadOnly);
+ Assert.IsFalse(view.AllowsDuplicates);
+ Assert.IsTrue(view.IsEmpty);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 3));
+ Assert.IsTrue(IC.eq(view));
+ view.Add(8);
+ Assert.IsFalse(view.IsEmpty);
+ Assert.IsFalse(view.AllowsDuplicates);
+ Assert.IsFalse(view.IsReadOnly);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 3));
+ Assert.IsTrue(IC.eq(view, 8));
+ view.Add(12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12));
+ view.InsertAfter(15, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12, 15));
+ view.InsertBefore(18, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
+
+ HashedLinkedList<int> lst2 = new HashedLinkedList<int>();
+
+ lst2.Add(90);lst2.Add(92);
+ view.AddAll(lst2);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
+ view.InsertLast(66);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
+ }
+
+
+ [Test]
+ public void Bxxx()
+ {
+ Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
+ Assert.AreSame(list, view.Underlying);
+ Assert.IsNull(list.Underlying);
+ Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
+ Assert.AreEqual(0,list.Offset);
+ Assert.AreEqual(1,view.Offset);
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsTrue(view.Contains(1));
+ Assert.IsFalse(view.Contains(0));
+
+ HashedLinkedList<int> lst2 = new HashedLinkedList<int>();
+
+ lst2.Add(2);
+ Assert.IsTrue(view.ContainsAll(lst2));
+ lst2.Add(3);
+ Assert.IsFalse(view.ContainsAll(lst2));
+ Assert.AreEqual(Speed.Constant, view.ContainsSpeed);
+ Assert.AreEqual(2, view.Count);
+ view.Add(1);
+ Assert.AreEqual(1, view.ContainsCount(2));
+ Assert.AreEqual(1, view.ContainsCount(1));
+ Assert.AreEqual(2, view.Count);
+ }
+
+
+ [Test]
+ public void CreateView()
+ {
+ LinkedList<int> view2 = (LinkedList<int>)view.View(1, 0);
+
+ Assert.AreSame(list, view2.Underlying);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ Assert.IsTrue(view.FIFO);
+ view.Add(23);view.Add(24);view.Add(25);
+ check();
+ Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
+ Assert.AreEqual(1, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
+ view.FIFO = false;
+ Assert.IsFalse(view.FIFO);
+ Assert.AreEqual(25, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24));
+ }
+
+
+ [Test]
+ public void MapEtc()
+ {
+ LinkedList<double> dbl = (LinkedList<double>)view.Map(new Mapper<int,double>(delegate(int i){return i/10.0;}));
+
+ Assert.IsTrue(dbl.Check());
+ Assert.AreEqual(0.1, dbl[0]);
+ Assert.AreEqual(0.2, dbl[1]);
+ for (int i = 0; i < 10; i++) view.Add(i);
+
+ LinkedList<int> list2 = (LinkedList<int>)view.FindAll(new Filter<int>(delegate(int i){return i%4==1;}));
+
+ Assert.IsTrue(list2.Check());
+ Assert.IsTrue(IC.eq(list2, 1, 5, 9));
+ }
+
+
+ [Test]
+ public void FL()
+ {
+ Assert.AreEqual(1, view.First);
+ Assert.AreEqual(2, view.Last);
+ }
+
+
+ [Test]
+ public void Indexing()
+ {
+ list.Clear();
+ for (int i = 0; i < 20; i++) list.Add(i);
+
+ view = (LinkedList<int>)list.View(5, 7);
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.IndexOf(i + 5));
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
+ }
+
+
+ [Test]
+ public void Insert()
+ {
+ view.Insert(0, 34);
+ view.Insert(1, 35);
+ view.Insert(4, 36);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
+
+ IList<int> list2 = new HashedLinkedList<int>();
+
+ list2.Add(40);list2.Add(41);
+ view.InsertAll(3, list2);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 40, 41, 2, 36));
+ }
+
+
+ [Test]
+ public void Sort()
+ {
+ view.Add(45);view.Add(47);view.Add(46);view.Add(48);
+ Assert.IsFalse(view.IsSorted(new IC()));
+ view.Sort(new IC());
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 1, 2, 5));
+ Assert.IsTrue(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ Assert.IsFalse(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ Assert.IsFalse(view.Remove(0));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ view.RemoveAllCopies(3);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5));
+ Assert.IsTrue(IC.eq(list, 0, 2, 5, 3));
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 2, 5, 1));
+
+ HashedLinkedList<int> l2 = new HashedLinkedList<int>();
+
+ l2.Add(1);l2.Add(2);l2.Add(2);l2.Add(3);l2.Add(1);
+ view.RemoveAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view, 5));
+ view.RetainAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(2);view.Add(4);view.Add(5);
+ Assert.AreEqual(2, view.RemoveAt(0));
+ Assert.AreEqual(5, view.RemoveAt(1));
+ Assert.AreEqual(4, view.RemoveAt(0));
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(8);view.Add(6);view.Add(78);
+ Assert.AreEqual(8, view.RemoveFirst());
+ Assert.AreEqual(78, view.RemoveLast());
+ view.Add(2);view.Add(5);view.Add(3);view.Add(1);
+ view.RemoveInterval(1, 2);
+ check();
+ Assert.IsTrue(IC.eq(view, 6, 1));
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ view.Clear();
+ for (int i = 0; i < 10; i++) view.Add(10 + i);
+
+ view.Reverse(3, 4);
+ check();
+ Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
+ view.Reverse();
+ Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
+ Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
+ }
+
+
+ [Test]
+ public void Slide()
+ {
+ view.Slide(1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 3));
+ view.Slide(-2);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1));
+ view.Slide(0, 3);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1, 2));
+ view.Slide(2, 1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2));
+ view.Slide(-1, 0);
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(28);
+ Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
+ }
+ [Test]
+ public void Iterate()
+ {
+ list.Clear();
+ view = null;
+ foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 10, 11 }) list.Add(i);
+
+ view = (LinkedList<int>)list.View(list.Count - 2, 2);
+ int j = 666;
+ while (true)
+ {
+ //Console.WriteLine("View: {0}: {1} --> {2}", view.Count, view.First, view.Last);
+ if ((view.Last - view.First) % 2 == 1)
+ view.Insert(1, j++);
+ check();
+ if (view.Offset == 0)
+ break;
+ else
+ view.Slide(-1,2);
+ }
+ //foreach (int i in list) Console.Write(" " + i);
+ //Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 2, 4, 8, 668, 13, 6, 1, 667, 10, 666, 11));
+ }
+
+
+ [Test]
+ public void SyncRoot()
+ {
+ Assert.AreSame(view.SyncRoot, list.SyncRoot);
+ }
+ }
+ }
+
+ namespace HashingAndEquals
+ {
+ [TestFixture]
+ public class ISequenced
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ dat = new HashedLinkedList<int>();
+ dut = new HashedLinkedList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(0);dit.Add(31);
+ dat.Add(1);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ ((HashedLinkedList<int>)dut).InsertFirst(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ dat = new HashedLinkedList<int>();
+ dut = new HashedLinkedList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count, params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count << 16) + retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1, 3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2, 3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1, 3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2, 7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(2);dit.Add(1);
+ dat.Add(3);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ dat = new HashedLinkedList<int>();
+ dut = new HashedLinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new HashedLinkedList<ICollection<int>>();
+ Dat = new HashedLinkedList<ICollection<int>>();
+ Dut = new HashedLinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ dat = new HashedLinkedList<int>();
+ dut = new HashedLinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new HashedLinkedList<ICollection<int>>();
+ Dat = new HashedLinkedList<ICollection<int>>();
+ Dut = new HashedLinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ dat = new HashedLinkedList<int>();
+ dut = new HashedLinkedList<int>();
+ dot = new HashedLinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new HashedLinkedList<ISequenced<int>>();
+ Dat = new HashedLinkedList<ISequenced<int>>();
+ Dut = new HashedLinkedList<ISequenced<int>>();
+ Dot = new HashedLinkedList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new HashedLinkedList<int>();
+ dat = new HashedLinkedList<int>();
+ dut = new HashedLinkedList<int>();
+ dot = new HashedLinkedList<int>();
+ dit.Add(2);dit.Add(1); //{2,1}
+ dat.Add(1);dat.Add(2); //{1,2}
+ dut.Add(3); //{3}
+ dot.Add(2);dot.Add(1); //{2,1}
+ Dit = new HashedLinkedList<ISequenced<int>>();
+ Dat = new HashedLinkedList<ISequenced<int>>();
+ Dut = new HashedLinkedList<ISequenced<int>>();
+ Dot = new HashedLinkedList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit); // {{2,1},{3}}
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat); // {{3},{2,1},{1,2}}
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit); // {{2,1},{3}}
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut); // {{2,1},{3}}
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/linkedlists/LinkedListTest.cs b/mcs/class/Mono.C5/Test/linkedlists/LinkedListTest.cs
new file mode 100644
index 00000000000..67ac6115682
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/linkedlists/LinkedListTest.cs
@@ -0,0 +1,2404 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+
+namespace nunit.linkedlists.plain
+{
+ namespace Enumerable
+ {
+ [TestFixture]
+ public class Multiops
+ {
+ private LinkedList<int> list;
+
+ private Filter<int> always, never, even;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new LinkedList<int>();
+ always = delegate{return true;};
+ never = delegate{return false;};
+ even = delegate(int i){return i%2==0;};
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(list.All(always));
+ Assert.IsTrue(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(8);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsTrue(list.All(even));
+ list.Add(5);
+ Assert.IsTrue(list.All(always));
+ Assert.IsFalse(list.All(never));
+ Assert.IsFalse(list.All(even));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(5);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsFalse(list.Exists(even));
+ list.Add(8);
+ Assert.IsTrue(list.Exists(always));
+ Assert.IsFalse(list.Exists(never));
+ Assert.IsTrue(list.Exists(even));
+ }
+
+
+ [Test]
+ public void Apply()
+ {
+ int sum = 0;
+ Applier<int> a = delegate(int i){sum=i+10*sum;};
+
+ list.Apply(a);
+ Assert.AreEqual(0, sum);
+ sum = 0;
+ list.Add(5);list.Add(8);list.Add(7);list.Add(5);
+ list.Apply(a);
+ Assert.AreEqual(5875, sum);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class GetEnumerator
+ {
+ private LinkedList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new LinkedList<int>(); }
+
+
+ [Test]
+ public void Empty()
+ {
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+ list.Add(5);
+ list.Add(10);
+ list.Add(1);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(8, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(5, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(10, e.Current);
+ Assert.IsTrue(e.MoveNext());
+ Assert.AreEqual(1, e.Current);
+ Assert.IsFalse(e.MoveNext());
+ }
+
+
+ [Test]
+ public void DoDispose()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ e.MoveNext();
+ e.Dispose();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ list.Add(5);
+ list.Add(8);
+ list.Add(5);
+
+ MSG.IEnumerator<int> e = list.GetEnumerator();
+
+ e.MoveNext();
+ list.Add(99);
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace CollectionOrExtensible
+ {
+ [TestFixture]
+ public class CollectionOrSink
+ {
+ private LinkedList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new LinkedList<int>(); }
+
+
+ [Test]
+ public void CountEtAl()
+ {
+ Assert.AreEqual(0, list.Count);
+ Assert.IsTrue(list.IsEmpty);
+ Assert.IsTrue(list.AllowsDuplicates);
+ list.Add(5);
+ Assert.AreEqual(1, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ list.Add(5);
+ Assert.AreEqual(2, list.Count);
+ Assert.IsFalse(list.IsEmpty);
+ list.Add(8);
+ Assert.AreEqual(3, list.Count);
+ }
+
+
+ [Test]
+ public void AddAll()
+ {
+ list.Add(3);list.Add(4);list.Add(5);
+
+ LinkedList<int> list2 = new LinkedList<int>();
+
+ list2.AddAll(list);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ list.AddAll(list2);
+ Assert.IsTrue(IC.eq(list2, 3, 4, 5));
+ Assert.IsTrue(IC.eq(list, 3, 4, 5, 3, 4, 5));
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private LinkedList<int> list;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new LinkedList<int>();
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(list.ToArray()));
+ list.Add(7);
+ list.Add(7);
+ Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 7));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ list.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(6);
+ list.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ list.Add(4);
+ list.Add(4);
+ list.Add(9);
+ list.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 4, 9, 1008, 1009));
+ list.Clear();
+ list.Add(7);
+ list.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 4, 9, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ list.Add(3);
+ list.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ list.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ list.Add(3);
+ list.Add(3);
+ list.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sync
+ {
+ private LinkedList<int> list;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new LinkedList<int>();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+
+
+ [Test]
+ public void Get()
+ {
+ Assert.IsNotNull(list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace EditableCollection
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private LinkedList<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new LinkedList<int>(); }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsFalse(list.Contains(5));
+ list.Add(5);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ list.Add(8);
+ list.Add(10);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsTrue(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ list.Remove(8);
+ Assert.IsTrue(list.Contains(5));
+ Assert.IsFalse(list.Contains(7));
+ Assert.IsFalse(list.Contains(8));
+ Assert.IsTrue(list.Contains(10));
+ }
+
+
+ [Test]
+ public void ContainsCount()
+ {
+ Assert.AreEqual(0, list.ContainsCount(5));
+ list.Add(5);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ list.Add(8);
+ Assert.AreEqual(1, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ Assert.AreEqual(1, list.ContainsCount(8));
+ list.Add(5);
+ Assert.AreEqual(2, list.ContainsCount(5));
+ Assert.AreEqual(0, list.ContainsCount(7));
+ Assert.AreEqual(1, list.ContainsCount(8));
+ }
+
+
+ [Test]
+ public void RemoveAllCopies()
+ {
+ list.Add(5);list.Add(7);list.Add(5);
+ Assert.AreEqual(2, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.RemoveAllCopies(5);
+ Assert.AreEqual(0, list.ContainsCount(5));
+ Assert.AreEqual(1, list.ContainsCount(7));
+ list.Add(5);list.Add(8);list.Add(5);
+ list.RemoveAllCopies(8);
+ Assert.IsTrue(IC.eq(list, 7, 5, 5));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Filter<int> f = delegate(int i){return i%2==0;};
+
+ Assert.IsTrue(list.FindAll(f).IsEmpty);
+ list.Add(5);list.Add(8);list.Add(5);list.Add(10);list.Add(8);
+ Assert.IsTrue(((LinkedList<int>)list.FindAll(f)).Check());
+ Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10, 8));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ LinkedList<int> list2 = new LinkedList<int>();
+
+ Assert.IsTrue(list.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(list.ContainsAll(list2));
+ list.Add(4);
+ Assert.IsTrue(list.ContainsAll(list2));
+ list.Add(5);
+ Assert.IsTrue(list.ContainsAll(list2));
+ list2.Add(4);
+ Assert.IsFalse(list.ContainsAll(list2));
+ list.Add(4);
+ Assert.IsTrue(list.ContainsAll(list2));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ LinkedList<int> list2 = new LinkedList<int>();
+
+ list.Add(4);list.Add(4);list.Add(5);list.Add(4);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);list2.Add(7);list2.Add(4);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 4, 5));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(5);list2.Add(5);list2.Add(6);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 5, 6));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RetainAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list));
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ LinkedList<int> list2 = new LinkedList<int>();
+
+ list.Add(4);list.Add(4);list.Add(5);list.Add(4);list.Add(6);
+ list2.Add(5);list2.Add(4);list2.Add(7);list2.Add(7);list2.Add(4);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 6));
+ list.Add(5);list.Add(4);list.Add(6);
+ list2.Clear();
+ list2.Add(6);list2.Add(5);list2.Add(5);list2.Add(6);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 4));
+ list2.Clear();
+ list2.Add(7);list2.Add(8);list2.Add(9);
+ list.RemoveAll(list2);
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 4));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ list.Add(4);list.Add(4);list.Add(5);list.Add(4);list.Add(6);
+ Assert.IsFalse(list.Remove(2));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(list.Remove(4));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 5, 4, 6));
+ Assert.AreEqual(6, list.RemoveLast());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 4, 5, 4));
+ list.Add(7);
+ Assert.AreEqual(4, list.RemoveFirst());
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 5, 4, 7));
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ list.Add(7);list.Add(7);
+ list.Clear();
+ Assert.IsTrue(list.IsEmpty);
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+
+
+ namespace IIndexed
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ }
+
+
+ [Test]
+ public void IndexOf()
+ {
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(-1, dit.LastIndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ dit.Add(5);dit.Add(7);dit.Add(8);dit.Add(7);
+ Assert.AreEqual(-1, dit.IndexOf(6));
+ Assert.AreEqual(0, dit.IndexOf(7));
+ Assert.AreEqual(4, dit.LastIndexOf(7));
+ Assert.AreEqual(3, dit.IndexOf(8));
+ Assert.AreEqual(1, dit.LastIndexOf(5));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Removing
+ {
+ private IIndexed<int> dit;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ }
+
+
+ [Test]
+ public void RemoveAt()
+ {
+ dit.Add(5);dit.Add(7);dit.Add(9);dit.Add(1);dit.Add(2);
+ Assert.AreEqual(7, dit.RemoveAt(1));
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
+ Assert.AreEqual(5, dit.RemoveAt(0));
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1, 2));
+ Assert.AreEqual(2, dit.RemoveAt(2));
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 9, 1));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad0()
+ {
+ dit.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBadM1()
+ {
+ dit.RemoveAt(-1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void RemoveAtBad1()
+ {
+ dit.Add(8);
+ dit.RemoveAt(1);
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ dit.RemoveInterval(0, 0);
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(3, 0);
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
+ dit.RemoveInterval(3, 1);
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
+ dit.RemoveInterval(1, 3);
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 10, 60));
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit));
+ dit.Add(10);dit.Add(20);dit.Add(30);dit.Add(40);dit.Add(50);dit.Add(60);
+ dit.RemoveInterval(0, 2);
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
+ dit.RemoveInterval(2, 2);
+ Assert.IsTrue(((LinkedList<int>)dit).Check());
+ Assert.IsTrue(IC.eq(dit, 30, 40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ }
+ }
+ }
+
+
+
+
+ namespace IList
+ {
+ [TestFixture]
+ public class Searching
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new LinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void FirstBad()
+ {
+ int f = lst.First;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "List is empty")]
+ public void LastBad()
+ {
+ int f = lst.Last;
+ }
+
+
+ [Test]
+ public void FirstLast()
+ {
+ lst.Add(19);
+ Assert.AreEqual(19, lst.First);
+ Assert.AreEqual(19, lst.Last);
+ lst.Add(34);lst.InsertFirst(12);
+ Assert.AreEqual(12, lst.First);
+ Assert.AreEqual(34, lst.Last);
+ }
+
+
+ [Test]
+ public void This()
+ {
+ lst.Add(34);
+ Assert.AreEqual(34, lst[0]);
+ lst[0] = 56;
+ Assert.AreEqual(56, lst.First);
+ lst.Add(7);lst.Add(7);lst.Add(7);lst.Add(7);
+ lst[0] = 45;lst[2] = 78;lst[4] = 101;
+ Assert.IsTrue(IC.eq(lst, 45, 7, 78, 7, 101));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptyGet()
+ {
+ int f = lst[0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowGet()
+ {
+ lst.Add(7);
+
+ int f = lst[-1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiGet()
+ {
+ lst.Add(6);
+
+ int f = lst[1];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadEmptySet()
+ {
+ lst[0] = 4;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadLowSet()
+ {
+ lst.Add(7);
+ lst[-1] = 9;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void ThisBadHiSet()
+ {
+ lst.Add(6);
+ lst[1] = 11;
+ }
+ }
+
+
+ [TestFixture]
+ public class Combined
+ {
+ private IList<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new LinkedList<KeyValuePair<int,int>>();
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Inserting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new LinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Insert()
+ {
+ lst.Insert(0, 5);
+ Assert.IsTrue(IC.eq(lst, 5));
+ lst.Insert(0, 7);
+ Assert.IsTrue(IC.eq(lst, 7, 5));
+ lst.Insert(1, 4);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5));
+ lst.Insert(3, 2);
+ Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertLow()
+ {
+ lst.Add(7);
+ lst.Insert(-1, 9);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void BadInsertHi()
+ {
+ lst.Add(6);
+ lst.Insert(2, 11);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ for (int i = 0; i < 7; i++)
+ lst.Add(2 * i);
+
+ Assert.IsTrue(lst.FIFO);
+ Assert.AreEqual(0, lst.Remove());
+ Assert.AreEqual(2, lst.Remove());
+ lst.FIFO = false;
+ Assert.AreEqual(12, lst.Remove());
+ Assert.AreEqual(10, lst.Remove());
+ lst.FIFO = true;
+ Assert.AreEqual(4, lst.Remove());
+ Assert.AreEqual(6, lst.Remove());
+ }
+
+
+ [Test]
+ public void InsertFirstLast()
+ {
+ lst.InsertFirst(4);
+ lst.InsertLast(5);
+ lst.InsertFirst(14);
+ lst.InsertLast(15);
+ lst.InsertFirst(24);
+ lst.InsertLast(25);
+ lst.InsertFirst(34);
+ lst.InsertLast(55);
+ Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
+ }
+
+
+ [Test]
+ public void InsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertBefore(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 3, 2, 5));
+ lst.InsertBefore(8, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 2, 5));
+ lst.InsertBefore(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 2, 9, 5));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBefore()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertBefore(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAfter()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertAfter(7, 2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 2, 3, 2, 7, 5));
+ lst.InsertAfter(8, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 3, 2, 7, 5));
+ lst.InsertAfter(9, 5);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 1, 8, 2, 3, 2, 7, 5, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfter()
+ {
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(2);
+ lst.Add(5);
+ lst.InsertAfter(7, 4);
+ }
+
+
+ [Test]
+ public void InsertAll()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+
+ IList<int> lst2 = new LinkedList<int>();
+
+ lst2.Add(7);lst2.Add(8);lst2.Add(9);
+ lst.InsertAll(0, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
+ lst.InsertAll(7, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4, 7, 8, 9));
+ lst.InsertAll(5, lst2);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 7, 8, 9, 3, 4, 7, 8, 9));
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Mapper<int,string> m = delegate(int i){return "<<"+i+">>";};
+ IList<string> r = lst.Map(m);
+
+ Assert.IsTrue(r.Check());
+ Assert.IsTrue(r.IsEmpty);
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ r = lst.Map(m);
+ Assert.IsTrue(r.Check());
+ Assert.AreEqual(4, r.Count);
+ for (int i = 0; i < 4; i++)
+ Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
+ }
+
+
+ [Test]
+ public void RemoveFirstLast()
+ {
+ lst.Add(1);
+ lst.Add(2);
+ lst.Add(3);
+ lst.Add(4);
+ Assert.AreEqual(1, lst.RemoveFirst());
+ Assert.AreEqual(4, lst.RemoveLast());
+ Assert.AreEqual(2, lst.RemoveFirst());
+ Assert.AreEqual(3, lst.RemoveLast());
+ Assert.IsTrue(lst.IsEmpty);
+ }
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void RemoveFirstEmpty()
+ {
+ lst.RemoveFirst();
+ }
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void RemoveLastEmpty()
+ {
+ lst.RemoveLast();
+ }
+
+ [Test]
+ public void Reverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse();
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(0, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 0);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
+ lst.Reverse(7, 3);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ lst.Reverse(5, 1);
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void BadReverse()
+ {
+ for (int i = 0; i < 10; i++)
+ lst.Add(i);
+
+ lst.Reverse(8, 3);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Sorting
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new LinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Sort()
+ {
+ lst.Add(5);lst.Add(6);lst.Add(5);lst.Add(7);lst.Add(3);
+ Assert.IsFalse(lst.IsSorted(new IC()));
+ lst.Sort(new IC());
+ Assert.IsTrue(lst.Check());
+ Assert.IsTrue(lst.IsSorted(new IC()));
+ Assert.IsTrue(IC.eq(lst, 3, 5, 5, 6, 7));
+ }
+
+
+ [Test]
+ public void Stability()
+ {
+ IList<KeyValuePair<int,string>> lst2 = new LinkedList<KeyValuePair<int,string>>();
+ IComparer<KeyValuePair<int,string>> c = new KeyValuePairComparer<int,string>(new IC());
+
+ lst2.Add(new KeyValuePair<int,string>(5, "a"));
+ lst2.Add(new KeyValuePair<int,string>(5, "b"));
+ lst2.Add(new KeyValuePair<int,string>(6, "c"));
+ lst2.Add(new KeyValuePair<int,string>(4, "d"));
+ lst2.Add(new KeyValuePair<int,string>(3, "e"));
+ lst2.Add(new KeyValuePair<int,string>(4, "f"));
+ lst2.Add(new KeyValuePair<int,string>(5, "h"));
+ Assert.IsFalse(lst2.IsSorted(c));
+ lst2.Sort(c);
+ Assert.IsTrue(lst2.IsSorted(c));
+
+ KeyValuePair<int,string> p = lst2.RemoveFirst();
+
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual("e", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(4, p.key);
+ Assert.AreEqual("d", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(4, p.key);
+ Assert.AreEqual("f", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(5, p.key);
+ Assert.AreEqual("a", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(5, p.key);
+ Assert.AreEqual("b", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(5, p.key);
+ Assert.AreEqual("h", p.value);
+ p = lst2.RemoveFirst();
+ Assert.AreEqual(6, p.key);
+ Assert.AreEqual("c", p.value);
+ Assert.IsTrue(lst2.IsEmpty);
+ }
+ }
+ }
+
+
+ namespace IStackQueue
+ {
+ [TestFixture]
+ public class Stack
+ {
+ private IStack<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new LinkedList<int>(); }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.Push(7);
+ list.Push(5);
+ list.Push(7);
+ list.Push(8);
+ list.Push(9);
+ Assert.AreEqual(9, list.Pop());
+ Assert.AreEqual(8, list.Pop());
+ Assert.AreEqual(7, list.Pop());
+ Assert.AreEqual(5, list.Pop());
+ Assert.AreEqual(7, list.Pop());
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void PopEmpty()
+ {
+ list.Push(5);
+ Assert.AreEqual(5, list.Pop());
+ list.Pop();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ [TestFixture]
+ public class Queue
+ {
+ private IQueue<int> list;
+
+
+ [SetUp]
+ public void Init() { list = new LinkedList<int>(); }
+
+
+ [Test]
+ public void Normal()
+ {
+ list.EnQueue(7);
+ list.EnQueue(5);
+ list.EnQueue(7);
+ list.EnQueue(8);
+ list.EnQueue(9);
+ Assert.AreEqual(7, list.DeQueue());
+ Assert.AreEqual(5, list.DeQueue());
+ Assert.AreEqual(7, list.DeQueue());
+ Assert.AreEqual(8, list.DeQueue());
+ Assert.AreEqual(9, list.DeQueue());
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void DeQueueEmpty()
+ {
+ list.EnQueue(5);
+ Assert.AreEqual(5, list.DeQueue());
+ list.DeQueue();
+ }
+
+
+ [TearDown]
+ public void Dispose() { list = null; }
+ }
+ }
+
+
+ namespace Range
+ {
+ [TestFixture]
+ public class Range
+ {
+ private IList<int> lst;
+
+
+ [SetUp]
+ public void Init() { lst = new LinkedList<int>(); }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void GetRange()
+ {
+ //Assert.IsTrue(IC.eq(lst[0, 0)));
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
+ Assert.IsTrue(IC.eq(lst[3, 4], 3, 4, 5, 6));
+ Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void BadGetRange()
+ {
+ object foo= lst[0, 11];
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[0, 4].Backwards(), 3, 2, 1, 0));
+ Assert.IsTrue(IC.eq(lst[3, 4].Backwards(), 6, 5, 4, 3));
+ Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
+ }
+
+
+ [Test]
+ public void DirectionAndCount()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 7].Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 7].Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
+ Assert.AreEqual(4, lst[3, 4].Count);
+ Assert.AreEqual(4, lst[3, 4].Backwards().Count);
+ Assert.AreEqual(10, lst.Backwards().Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextAfterUpdate()
+ {
+ for (int i = 0; i < 10; i++) lst.Add(i);
+
+ foreach (int i in lst)
+ {
+ lst.Add(45 + i);
+ }
+ }
+ }
+ }
+
+
+
+
+ namespace View
+ {
+ [TestFixture]
+ public class Simple
+ {
+ LinkedList<int> list, view;
+
+
+ [SetUp]
+ public void Init()
+ {
+ list = new LinkedList<int>();
+ list.Add(0);list.Add(1);list.Add(2);list.Add(3);
+ view = (LinkedList<int>)list.View(1, 2);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ list = view = null;
+ }
+
+
+ void check()
+ {
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(view.Check());
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfterLow()
+ {
+ view.InsertAfter(876, 0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertAfterHigh()
+ {
+ view.InsertAfter(876, 0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBeforeLow()
+ {
+ view.InsertBefore(876, 0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Target item not found")]
+ public void BadInsertBeforeHigh()
+ {
+ view.InsertBefore(876, 0);
+ }
+
+
+
+ [Test]
+ public void Add()
+ {
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2));
+ view.InsertFirst(10);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
+ Assert.IsTrue(IC.eq(view, 10, 1, 2));
+ view.Clear();
+ Assert.IsFalse(view.IsReadOnly);
+ Assert.IsTrue(view.AllowsDuplicates);
+ Assert.IsTrue(view.IsEmpty);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 3));
+ Assert.IsTrue(IC.eq(view));
+ view.Add(8);
+ Assert.IsFalse(view.IsEmpty);
+ Assert.IsTrue(view.AllowsDuplicates);
+ Assert.IsFalse(view.IsReadOnly);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 3));
+ Assert.IsTrue(IC.eq(view, 8));
+ view.Add(12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12));
+ view.InsertAfter(15, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 12, 15));
+ view.InsertBefore(18, 12);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
+
+ LinkedList<int> lst2 = new LinkedList<int>();
+
+ lst2.Add(90);lst2.Add(92);
+ view.AddAll(lst2);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
+ view.InsertLast(66);
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
+ Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
+ }
+
+
+ [Test]
+ public void Bxxx()
+ {
+ Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
+ Assert.AreSame(list, view.Underlying);
+ Assert.IsNull(list.Underlying);
+ Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
+ Assert.AreEqual(0,list.Offset);
+ Assert.AreEqual(1,view.Offset);
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsTrue(view.Contains(1));
+ Assert.IsFalse(view.Contains(0));
+
+ LinkedList<int> lst2 = new LinkedList<int>();
+
+ lst2.Add(2);
+ Assert.IsTrue(view.ContainsAll(lst2));
+ lst2.Add(3);
+ Assert.IsFalse(view.ContainsAll(lst2));
+ Assert.AreEqual(Speed.Linear, view.ContainsSpeed);
+ Assert.AreEqual(2, view.Count);
+ view.Add(1);
+ Assert.AreEqual(1, view.ContainsCount(2));
+ Assert.AreEqual(2, view.ContainsCount(1));
+ Assert.AreEqual(3, view.Count);
+ }
+
+
+ [Test]
+ public void CreateView()
+ {
+ LinkedList<int> view2 = (LinkedList<int>)view.View(1, 0);
+
+ Assert.AreSame(list, view2.Underlying);
+ }
+
+
+ [Test]
+ public void FIFO()
+ {
+ Assert.IsTrue(view.FIFO);
+ view.Add(23);view.Add(24);view.Add(25);
+ check();
+ Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
+ Assert.AreEqual(1, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
+ view.FIFO = false;
+ Assert.IsFalse(view.FIFO);
+ Assert.AreEqual(25, view.Remove());
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 23, 24));
+ }
+
+
+ [Test]
+ public void MapEtc()
+ {
+ LinkedList<double> dbl = (LinkedList<double>)view.Map(new Mapper<int,double>(delegate(int i){return i/10.0;}));
+
+ Assert.IsTrue(dbl.Check());
+ Assert.AreEqual(0.1, dbl[0]);
+ Assert.AreEqual(0.2, dbl[1]);
+ for (int i = 0; i < 10; i++) view.Add(i);
+
+ list = (LinkedList<int>)view.FindAll(new Filter<int>(delegate(int i){return i%4==1;}));
+ Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 1, 1, 5, 9));
+ }
+
+
+ [Test]
+ public void FL()
+ {
+ Assert.AreEqual(1, view.First);
+ Assert.AreEqual(2, view.Last);
+ }
+
+
+ [Test]
+ public void Indexing()
+ {
+ list.Clear();
+ for (int i = 0; i < 20; i++) list.Add(i);
+
+ view = (LinkedList<int>)list.View(5, 7);
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.IndexOf(i + 5));
+
+ for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
+ }
+
+
+ [Test]
+ public void INsert()
+ {
+ view.Insert(0, 34);
+ view.Insert(1, 35);
+ view.Insert(4, 36);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
+
+ IList<int> list2 = new LinkedList<int>();
+
+ list2.AddAll(view);
+ view.InsertAll(3, list2);
+ Assert.IsTrue(view.Check());
+ Assert.IsTrue(IC.eq(view, 34, 35, 1, 34, 35, 1, 2, 36, 2, 36));
+ }
+
+
+ [Test]
+ public void Sort()
+ {
+ view.Add(45);view.Add(47);view.Add(46);view.Add(48);
+ Assert.IsFalse(view.IsSorted(new IC()));
+ view.Sort(new IC());
+ check();
+ Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
+ Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 1, 2, 1, 5, 3, 1, 3, 0));
+ Assert.IsTrue(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 1, 5, 3, 1, 3, 0));
+ Assert.IsTrue(view.Remove(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5, 3, 1, 3, 0));
+ Assert.IsTrue(view.Remove(0));
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5, 3, 1, 3));
+ view.RemoveAllCopies(3);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 5, 1));
+ Assert.IsTrue(IC.eq(list, 0, 2, 5, 1, 3));
+ view.Add(1);view.Add(5);view.Add(3);view.Add(1);view.Add(3);view.Add(0);
+ Assert.IsTrue(IC.eq(view, 2, 5, 1, 1, 5, 3, 1, 3, 0));
+
+ LinkedList<int> l2 = new LinkedList<int>();
+
+ l2.Add(1);l2.Add(2);l2.Add(2);l2.Add(3);l2.Add(1);
+ view.RemoveAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view, 5, 5, 1, 3, 0));
+ view.RetainAll(l2);
+ check();
+ Assert.IsTrue(IC.eq(view, 1, 3));
+ view.Add(2);view.Add(4);view.Add(5);
+ Assert.AreEqual(1, view.RemoveAt(0));
+ Assert.AreEqual(5, view.RemoveAt(3));
+ Assert.AreEqual(2, view.RemoveAt(1));
+ check();
+ Assert.IsTrue(IC.eq(view, 3, 4));
+ view.Add(8);
+ Assert.AreEqual(3, view.RemoveFirst());
+ Assert.AreEqual(8, view.RemoveLast());
+ view.Add(2);view.Add(5);view.Add(3);view.Add(1);
+ view.RemoveInterval(1, 2);
+ check();
+ Assert.IsTrue(IC.eq(view, 4, 3, 1));
+ }
+
+
+ [Test]
+ public void Reverse()
+ {
+ view.Clear();
+ for (int i = 0; i < 10; i++) view.Add(10 + i);
+
+ view.Reverse(3, 4);
+ check();
+ Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
+ view.Reverse();
+ Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
+ Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
+ }
+
+
+ [Test]
+ public void Slide()
+ {
+ view.Slide(1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2, 3));
+ view.Slide(-2);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1));
+ view.Slide(0, 3);
+ check();
+ Assert.IsTrue(IC.eq(view, 0, 1, 2));
+ view.Slide(2, 1);
+ check();
+ Assert.IsTrue(IC.eq(view, 2));
+ view.Slide(-1, 0);
+ check();
+ Assert.IsTrue(IC.eq(view));
+ view.Add(28);
+ Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
+ }
+ [Test]
+ public void Iterate()
+ {
+ list.Clear();
+ view = null;
+ foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 2, 7 }) list.Add(i);
+
+ view = (LinkedList<int>)list.View(list.Count - 2, 2);
+ while (true)
+ {
+ //Console.WriteLine("View: {0}: {1} --> {2}", view.Count, view.First, view.Last);
+ if ((view.Last - view.First) % 2 == 1)
+ view.Insert(1, 666);
+ check();
+ if (view.Offset == 0)
+ break;
+ else
+ view.Slide(-1,2);
+ }
+ //foreach (int i in list) Console.Write(" " + i);
+ //Assert.IsTrue(list.Check());
+ Assert.IsTrue(IC.eq(list, 2, 4, 8, 666, 13, 6, 1, 666, 2, 666, 7));
+ }
+
+
+ [Test]
+ public void SyncRoot()
+ {
+ Assert.AreSame(view.SyncRoot, list.SyncRoot);
+ }
+ }
+ }
+
+
+
+
+ namespace LinkedListOfTreesORLists
+ {
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new TreeSet<int>(new IC());
+ dut = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new LinkedList<ICollection<int>>();
+ Dat = new LinkedList<ICollection<int>>();
+ Dut = new LinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new TreeSet<int>(new IC());
+ dut = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new LinkedList<ICollection<int>>();
+ Dat = new LinkedList<ICollection<int>>();
+ Dut = new LinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeSet<int>(new IC());
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dot = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(2);dat.Add(1);
+ dut.Add(3);
+ dot.Add(1);dot.Add(2);
+ Dit = new LinkedList<ISequenced<int>>();
+ Dat = new LinkedList<ISequenced<int>>();
+ Dut = new LinkedList<ISequenced<int>>();
+ Dot = new LinkedList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeSet<int>(new IC());
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dot = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(2);dat.Add(1);
+ dut.Add(3);
+ dot.Add(1);dot.Add(2);
+ Dit = new LinkedList<ISequenced<int>>();
+ Dat = new LinkedList<ISequenced<int>>();
+ Dut = new LinkedList<ISequenced<int>>();
+ Dot = new LinkedList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+
+
+
+
+ namespace HashingAndEquals
+ {
+ [TestFixture]
+ public class ISequenced
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(0);dit.Add(31);
+ dat.Add(1);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ ((LinkedList<int>)dut).InsertFirst(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count, params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count << 16) + retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1, 3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2, 3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1, 3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2, 7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void EqualHashButDifferent()
+ {
+ dit.Add(2);dit.Add(1);
+ dat.Add(3);dat.Add(0);
+ Assert.AreEqual(dit.GetHashCode(), dat.GetHashCode());
+ Assert.IsFalse(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnorderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ICollection<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new LinkedList<ICollection<int>>();
+ Dat = new LinkedList<ICollection<int>>();
+ Dut = new LinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Assert.IsTrue(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfUnOrdered
+ {
+ private ICollection<int> dit, dat, dut;
+
+ private ISequenced<ICollection<int>> Dit, Dat, Dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ Dit = new LinkedList<ICollection<int>>();
+ Dat = new LinkedList<ICollection<int>>();
+ Dut = new LinkedList<ICollection<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dit);Dut.Add(dut);Dut.Add(dat);
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dut));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = null;
+ Dit = Dat = Dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelUnOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dot = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new LinkedList<ISequenced<int>>();
+ Dat = new LinkedList<ISequenced<int>>();
+ Dut = new LinkedList<ISequenced<int>>();
+ Dot = new LinkedList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsTrue(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class MultiLevelOrderedOfOrdered
+ {
+ private ISequenced<int> dit, dat, dut, dot;
+
+ private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new LinkedList<int>();
+ dat = new LinkedList<int>();
+ dut = new LinkedList<int>();
+ dot = new LinkedList<int>();
+ dit.Add(2);dit.Add(1);
+ dat.Add(1);dat.Add(2);
+ dut.Add(3);
+ dot.Add(2);dot.Add(1);
+ Dit = new LinkedList<ISequenced<int>>();
+ Dat = new LinkedList<ISequenced<int>>();
+ Dut = new LinkedList<ISequenced<int>>();
+ Dot = new LinkedList<ISequenced<int>>();
+ }
+
+
+ [Test]
+ public void Check()
+ {
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsTrue(dit.Equals(dot));
+ Assert.IsFalse(dit.Equals(dut));
+ }
+
+
+ [Test]
+ public void Multi()
+ {
+ Dit.Add(dit);Dit.Add(dut);Dit.Add(dit);
+ Dat.Add(dut);Dat.Add(dit);Dat.Add(dat);
+ Dut.Add(dot);Dut.Add(dut);Dut.Add(dit);
+ Dot.Add(dit);Dot.Add(dit);Dot.Add(dut);
+ Assert.IsTrue(Dit.Equals(Dut));
+ Assert.IsFalse(Dit.Equals(Dat));
+ Assert.IsFalse(Dit.Equals(Dot));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = dat = dut = dot = null;
+ Dit = Dat = Dut = Dot = null;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/nunit.csproj b/mcs/class/Mono.C5/Test/nunit.csproj
new file mode 100644
index 00000000000..8263becbea6
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/nunit.csproj
@@ -0,0 +1,127 @@
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <ProductVersion>8.0.40426</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{08CBFDEB-A2E2-4F0E-A4E1-B996B05569DE}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <StartupObject>
+ </StartupObject>
+ <RootNamespace>nunit</RootNamespace>
+ <NoStandardLibraries>false</NoStandardLibraries>
+ <AssemblyName>nunit</AssemblyName>
+ <FileUpgradeFlags>
+ </FileUpgradeFlags>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
+ <DebugSymbols>true</DebugSymbols>
+ <Optimize>false</Optimize>
+ <OutputPath>.\bin\Debug\</OutputPath>
+ <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <WarningLevel>4</WarningLevel>
+ <IncrementalBuild>false</IncrementalBuild>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
+ <DebugSymbols>false</DebugSymbols>
+ <Optimize>true</Optimize>
+ <OutputPath>.\bin\Release\</OutputPath>
+ <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
+ <DefineConstants>TRACE</DefineConstants>
+ <WarningLevel>4</WarningLevel>
+ <IncrementalBuild>false</IncrementalBuild>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.framework, Version=2.1.4.0, Culture=neutral, processorArchitecture=MSIL">
+ <HintPath>..\..\NUnit\nunit.framework\bin\Debug\nunit.framework.dll</HintPath>
+ <Name>nunit.framework</Name>
+ <SpecificVersion>False</SpecificVersion>
+ <Aliases>global</Aliases>
+ <ExecutableExtension>.dll</ExecutableExtension>
+ </Reference>
+ <Reference Include="System">
+ <HintPath>..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.2.30703\System.dll</HintPath>
+ <Name>System</Name>
+ </Reference>
+ <Reference Include="System.Data">
+ <HintPath>..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.2.30703\System.Data.dll</HintPath>
+ <Name>System.Data</Name>
+ </Reference>
+ <Reference Include="System.Xml">
+ <HintPath>..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.2.30703\System.XML.dll</HintPath>
+ <Name>System.XML</Name>
+ </Reference>
+ <ProjectReference Include="..\C5\C5.csproj">
+ <Project>{D70489CD-ABDA-48FF-BD1E-BE3F7495BE71}</Project>
+ <Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
+ <Name>C5</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="AssemblyInfo.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="BasesTest.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="Sorting.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="SupportClasses.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="arrays\ArrayListTest.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="arrays\HashedArrayListTest.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="arrays\SortedArrayTests.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="hashing\HashBagTests.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="hashing\HashDictionaryTests.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="hashing\HashTableTests.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="heaps\HeapTests.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="linkedlists\HashedLinkedListTest.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="linkedlists\LinkedListTest.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="trees\Bag.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="trees\Dictionary.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="trees\RedBlackTreeSetTests.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ </ItemGroup>
+ <ItemGroup>
+ <AppDesigner Include="Project\" />
+ </ItemGroup>
+ <ItemGroup>
+ <Folder Include="Project\" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
+ <PropertyGroup>
+ <PreBuildEvent>
+ </PreBuildEvent>
+ <PostBuildEvent>
+ </PostBuildEvent>
+ </PropertyGroup>
+ <ProjectExtensions>
+ <VisualStudio>
+ </VisualStudio>
+ </ProjectExtensions>
+</Project> \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/nunit.csproj.user b/mcs/class/Mono.C5/Test/nunit.csproj.user
new file mode 100644
index 00000000000..9d081a79080
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/nunit.csproj.user
@@ -0,0 +1,58 @@
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <LastOpenVersion>8.0.40426</LastOpenVersion>
+ <ReferencePath>C:\Programmer\NUnit V2.1\bin\</ReferencePath>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
+ <DebugSecurityZoneURL>
+ </DebugSecurityZoneURL>
+ <DebugSecurityZone>NoZone</DebugSecurityZone>
+ <EnableASPDebugging>false</EnableASPDebugging>
+ <EnableASPXDebugging>false</EnableASPXDebugging>
+ <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
+ <EnableSQLServerDebugging>false</EnableSQLServerDebugging>
+ <RemoteDebugEnabled>false</RemoteDebugEnabled>
+ <RemoteDebugMachine>
+ </RemoteDebugMachine>
+ <StartAction>Project</StartAction>
+ <StartArguments>
+ </StartArguments>
+ <StartPage>
+ </StartPage>
+ <StartProgram>
+ </StartProgram>
+ <StartURL>
+ </StartURL>
+ <StartWorkingDirectory>
+ </StartWorkingDirectory>
+ <StartWithIE>false</StartWithIE>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
+ <DebugSecurityZoneURL>
+ </DebugSecurityZoneURL>
+ <DebugSecurityZone>NoZone</DebugSecurityZone>
+ <EnableASPDebugging>false</EnableASPDebugging>
+ <EnableASPXDebugging>false</EnableASPXDebugging>
+ <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
+ <EnableSQLServerDebugging>false</EnableSQLServerDebugging>
+ <RemoteDebugEnabled>false</RemoteDebugEnabled>
+ <RemoteDebugMachine>
+ </RemoteDebugMachine>
+ <StartAction>Project</StartAction>
+ <StartArguments>
+ </StartArguments>
+ <StartPage>
+ </StartPage>
+ <StartProgram>
+ </StartProgram>
+ <StartURL>
+ </StartURL>
+ <StartWorkingDirectory>
+ </StartWorkingDirectory>
+ <StartWithIE>true</StartWithIE>
+ </PropertyGroup>
+ <PropertyGroup>
+ <ProjectView>ProjectFiles</ProjectView>
+ <ProjectTrust>0</ProjectTrust>
+ </PropertyGroup>
+</Project> \ No newline at end of file
diff --git a/mcs/class/Mono.C5/Test/trees/Bag.cs b/mcs/class/Mono.C5/Test/trees/Bag.cs
new file mode 100644
index 00000000000..4b1eaa3e53f
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/trees/Bag.cs
@@ -0,0 +1,2785 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG=System.Collections.Generic;
+
+
+namespace nunit.trees.TreeBag
+{
+ [TestFixture]
+ public class Combined
+ {
+ private IIndexedSorted<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new TreeBag<KeyValuePair<int,int>>(new KeyValuePairComparer<int,int>(new IC()));
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[11].key);
+ Assert.AreEqual(79, lst[11].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[11].key);
+ Assert.AreEqual(79, lst[11].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Simple
+ {
+ private TreeBag<string> bag;
+
+
+ [SetUp]
+ public void Init()
+ {
+ bag = new TreeBag<string>(new SC());
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ bag = null;
+ }
+
+ [Test]
+ public void Initial()
+ {
+ bool res;
+
+ Assert.IsFalse(bag.IsReadOnly);
+ Assert.AreEqual(0, bag.Count, "new bag should be empty");
+ Assert.AreEqual(0, bag.ContainsCount("A"));
+ Assert.AreEqual(0, bag.ContainsCount("B"));
+ Assert.AreEqual(0, bag.ContainsCount("C"));
+ Assert.IsFalse(bag.Contains("A"));
+ Assert.IsFalse(bag.Contains("B"));
+ Assert.IsFalse(bag.Contains("C"));
+ bag.Add("A");
+ Assert.AreEqual(1, bag.Count);
+ Assert.AreEqual(1, bag.ContainsCount("A"));
+ Assert.AreEqual(0, bag.ContainsCount("B"));
+ Assert.AreEqual(0, bag.ContainsCount("C"));
+ Assert.IsTrue(bag.Contains("A"));
+ Assert.IsFalse(bag.Contains("B"));
+ Assert.IsFalse(bag.Contains("C"));
+ bag.Add("C");
+ Assert.AreEqual(2, bag.Count);
+ Assert.AreEqual(1, bag.ContainsCount("A"));
+ Assert.AreEqual(0, bag.ContainsCount("B"));
+ Assert.AreEqual(1, bag.ContainsCount("C"));
+ Assert.IsTrue(bag.Contains("A"));
+ Assert.IsFalse(bag.Contains("B"));
+ Assert.IsTrue(bag.Contains("C"));
+ bag.Add("C");
+ Assert.AreEqual(3, bag.Count);
+ Assert.AreEqual(1, bag.ContainsCount("A"));
+ Assert.AreEqual(0, bag.ContainsCount("B"));
+ Assert.AreEqual(2, bag.ContainsCount("C"));
+ Assert.IsTrue(bag.Contains("A"));
+ Assert.IsFalse(bag.Contains("B"));
+ Assert.IsTrue(bag.Contains("C"));
+ bag.Add("B");
+ bag.Add("C");
+ Assert.AreEqual(5, bag.Count);
+ Assert.AreEqual(1, bag.ContainsCount("A"));
+ Assert.AreEqual(1, bag.ContainsCount("B"));
+ Assert.AreEqual(3, bag.ContainsCount("C"));
+ Assert.IsTrue(bag.Contains("A"));
+ Assert.IsTrue(bag.Contains("B"));
+ Assert.IsTrue(bag.Contains("C"));
+ res = bag.Remove("C");
+ Assert.AreEqual(4, bag.Count);
+ Assert.AreEqual(1, bag.ContainsCount("A"));
+ Assert.AreEqual(1, bag.ContainsCount("B"));
+ Assert.AreEqual(2, bag.ContainsCount("C"));
+ Assert.IsTrue(bag.Contains("A"));
+ Assert.IsTrue(bag.Contains("B"));
+ Assert.IsTrue(bag.Contains("C"));
+ res = bag.Remove("A");
+ Assert.AreEqual(3, bag.Count);
+ Assert.AreEqual(0, bag.ContainsCount("A"));
+ Assert.AreEqual(1, bag.ContainsCount("B"));
+ Assert.AreEqual(2, bag.ContainsCount("C"));
+ Assert.IsFalse(bag.Contains("A"));
+ Assert.IsTrue(bag.Contains("B"));
+ Assert.IsTrue(bag.Contains("C"));
+ bag.RemoveAllCopies("C");
+ Assert.AreEqual(1, bag.Count);
+ Assert.AreEqual(0, bag.ContainsCount("A"));
+ Assert.AreEqual(1, bag.ContainsCount("B"));
+ Assert.AreEqual(0, bag.ContainsCount("C"));
+ Assert.IsFalse(bag.Contains("A"));
+ Assert.IsTrue(bag.Contains("B"));
+ Assert.IsFalse(bag.Contains("C"));
+ Assert.IsFalse(bag.Contains("Z"));
+ Assert.IsFalse(bag.Remove("Z"));
+ bag.RemoveAllCopies("Z");
+ Assert.AreEqual(1, bag.Count);
+ Assert.AreEqual(0, bag.ContainsCount("A"));
+ Assert.AreEqual(1, bag.ContainsCount("B"));
+ Assert.AreEqual(0, bag.ContainsCount("C"));
+ Assert.IsFalse(bag.Contains("A"));
+ Assert.IsTrue(bag.Contains("B"));
+ Assert.IsFalse(bag.Contains("C"));
+ Assert.IsFalse(bag.Contains("Z"));
+ }
+ }
+
+ [TestFixture]
+ public class FindOrAdd
+ {
+ private TreeBag<KeyValuePair<int,string>> bag;
+
+
+ [SetUp]
+ public void Init()
+ {
+ bag = new TreeBag<KeyValuePair<int,string>>(new KeyValuePairComparer<int,string>(new IC()));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ bag = null;
+ }
+
+
+ [Test]
+ public void Test()
+ {
+ KeyValuePair<int,string> p = new KeyValuePair<int,string>(3,"tre");
+ Assert.IsFalse(bag.FindOrAdd(ref p));
+ p.value = "drei";
+ Assert.IsTrue(bag.FindOrAdd(ref p));
+ Assert.AreEqual("tre", p.value);
+ p.value = "three";
+ Assert.AreEqual(2, bag.ContainsCount(p));
+ Assert.AreEqual("tre", bag[0].value);
+ }
+ }
+
+
+ [TestFixture]
+ public class Enumerators
+ {
+ private TreeBag<string> bag;
+
+ private MSG.IEnumerator<string> bagenum;
+
+
+ [SetUp]
+ public void Init()
+ {
+ bag = new TreeBag<string>(new SC());
+ foreach (string s in new string[] { "A", "B", "A", "A", "B", "C", "D", "B" })
+ bag.Add(s);
+
+ bagenum = bag.GetEnumerator();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ bagenum = null;
+ bag = null;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void MoveNextOnModified()
+ {
+ //TODO: also problem before first MoveNext!!!!!!!!!!
+ bagenum.MoveNext();
+ bag.Add("T");
+ bagenum.MoveNext();
+ }
+
+
+ [Test]
+ public void NormalUse()
+ {
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "A");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "A");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "A");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "B");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "B");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "B");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "C");
+ Assert.IsTrue(bagenum.MoveNext());
+ Assert.AreEqual(bagenum.Current, "D");
+ Assert.IsFalse(bagenum.MoveNext());
+ }
+ }
+
+ [TestFixture]
+ public class Ranges
+ {
+ private TreeBag<int> tree;
+
+ private IComparer<int> c;
+
+
+ [SetUp]
+ public void Init()
+ {
+ c = new IC();
+ tree = new TreeBag<int>(c);
+ for (int i = 1; i <= 10; i++)
+ {
+ tree.Add(i * 2);tree.Add(i);
+ }
+ }
+
+
+ [Test]
+ public void Enumerator()
+ {
+ MSG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
+ int i = 0;
+ int[] all = new int[] { 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16 };
+ while (e.MoveNext())
+ {
+ Assert.AreEqual(all[i++], e.Current);
+ }
+
+ Assert.AreEqual(12, i);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void Enumerator2()
+ {
+ MSG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
+ int i = e.Current;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void Enumerator3()
+ {
+ MSG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
+
+ while (e.MoveNext());
+
+ int i = e.Current;
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ int[] all = new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 };
+
+ tree.RemoveRangeFrom(18);
+ Assert.IsTrue(IC.eq(tree, new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16 }));
+ tree.RemoveRangeFrom(28);
+ Assert.IsTrue(IC.eq(tree, new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16 }));
+ tree.RemoveRangeFrom(1);
+ Assert.IsTrue(IC.eq(tree));
+ foreach (int i in all) tree.Add(i);
+
+ tree.RemoveRangeTo(10);
+ Assert.IsTrue(IC.eq(tree, new int[] { 10, 10, 12, 14, 16, 18, 20 }));
+ tree.RemoveRangeTo(2);
+ Assert.IsTrue(IC.eq(tree, new int[] { 10, 10, 12, 14, 16, 18, 20 }));
+ tree.RemoveRangeTo(21);
+ Assert.IsTrue(IC.eq(tree));
+ foreach (int i in all) tree.Add(i);
+
+ tree.RemoveRangeFromTo(4, 8);
+ Assert.IsTrue(IC.eq(tree, 1, 2, 2, 3, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20));
+ tree.RemoveRangeFromTo(14, 28);
+ Assert.IsTrue(IC.eq(tree, 1, 2, 2, 3, 8, 8, 9, 10, 10, 12));
+ tree.RemoveRangeFromTo(0, 9);
+ Assert.IsTrue(IC.eq(tree, 9, 10, 10, 12));
+ tree.RemoveRangeFromTo(0, 81);
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ int[] all = new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 };
+
+ Assert.IsTrue(IC.eq(tree, all));
+ Assert.IsTrue(IC.eq(tree.RangeAll(), all));
+ Assert.AreEqual(20, tree.RangeAll().Count);
+ Assert.IsTrue(IC.eq(tree.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
+ Assert.AreEqual(5, tree.RangeFrom(11).Count);
+ Assert.IsTrue(IC.eq(tree.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(1), all));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(0), all));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(21), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(20), new int[] { 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(8), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(7), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6 }));
+ Assert.AreEqual(9, tree.RangeTo(7).Count);
+ Assert.IsTrue(IC.eq(tree.RangeTo(1), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(0), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(3), new int[] { 1, 2,2 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(20), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(21), all));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12), new int[] { 7, 8, 8, 9, 10, 10 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11), new int[] { 6, 6, 7, 8, 8, 9, 10, 10 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12), new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10 }));
+ Assert.AreEqual(15, tree.RangeFromTo(1, 12).Count);
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(2, 12), new int[] { 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21), new int[] { 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20), new int[] { 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18 }));
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ int[] all = new int[] { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 12, 14, 16, 18, 20 };
+ int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 };
+
+ Assert.IsTrue(IC.eq(tree, all));
+ Assert.IsTrue(IC.eq(tree.RangeAll().Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(1).Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(0).Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(21).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(20).Backwards(), new int[] { 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(8).Backwards(), new int[] { 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(7).Backwards(), new int[] { 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(1).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(0).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(3).Backwards(), new int[] { 2, 2, 1 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(21).Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12).Backwards(), new int[] { 10, 10, 9, 8, 8, 7 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11).Backwards(), new int[] { 10, 10, 9, 8, 8, 7, 6, 6 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(0, 12).Backwards(), new int[] { 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12).Backwards(), new int[] { 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 10, 9, 8, 8, 7, 6, 6 }));
+ }
+
+
+ [Test]
+ public void Direction()
+ {
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFrom(20).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeTo(7).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFromTo(1, 12).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeAll().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFrom(20).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeTo(7).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFromTo(1, 12).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeAll().Backwards().Direction);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ c = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class BagItf
+ {
+ private TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 5);
+ }
+ }
+
+
+ [Test]
+ public void Both()
+ {
+ Assert.AreEqual(0, tree.ContainsCount(7));
+ Assert.AreEqual(1, tree.ContainsCount(10));
+ Assert.AreEqual(2, tree.ContainsCount(17));
+ tree.RemoveAllCopies(17);
+ Assert.AreEqual(0, tree.ContainsCount(17));
+ tree.RemoveAllCopies(7);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Div
+ {
+ private TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 5);
+ }
+ }
+
+
+ [Test]
+ public void NoDuplicates()
+ {
+ Assert.IsTrue(tree.AllowsDuplicates);
+ loadup();
+ Assert.IsTrue(tree.AllowsDuplicates);
+ }
+
+
+ [Test]
+ public void Add()
+ {
+ Assert.IsTrue(tree.Add(17));
+ Assert.IsTrue(tree.Add(17));
+ Assert.IsTrue(tree.Add(18));
+ Assert.IsTrue(tree.Add(18));
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(IC.eq(tree, 17, 17, 18, 18));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private TreeBag<int> tree;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
+ tree.Add(4);
+ tree.Add(7);
+ tree.Add(4);
+ Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 4, 7));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ tree.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ tree.Add(6);
+ tree.Add(6);
+ tree.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 6, 1004, 1005, 1006, 1007, 1008, 1009));
+ tree.Add(4);
+ tree.Add(9);
+ tree.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 6, 4, 6, 6, 9, 1008, 1009));
+ tree.Clear();
+ tree.Add(7);
+ tree.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 6, 4, 6, 6, 9, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ tree.Add(3);
+ tree.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ tree.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ tree.Add(3);
+ tree.Add(4);
+ tree.CopyTo(a, 9);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Remove
+ {
+ private TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 5);
+ }
+ //10,11,12,13,14,15,15,16,16,17,17,18,18,19,19,20,21,22,23,24
+ }
+
+
+ [Test]
+ public void SmallTrees()
+ {
+ tree.Clear();
+ tree.Add(9);
+ tree.Add(7);
+ tree.Add(9);
+ Assert.IsTrue(tree.Remove(7));
+ Assert.IsTrue(tree.Check(""));
+ }
+
+
+ [Test]
+ public void ByIndex()
+ {
+ //Remove root!
+ int n = tree.Count;
+ int i = tree[10];
+
+ Assert.AreEqual(17,tree.RemoveAt(10));
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsTrue(tree.Contains(i));
+ Assert.AreEqual(n - 1, tree.Count);
+ Assert.AreEqual(17, tree.RemoveAt(9));
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 2, tree.Count);
+
+ //Low end
+ i = tree.FindMin();
+ tree.RemoveAt(0);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 3, tree.Count);
+
+ //high end
+ i = tree.FindMax();
+ tree.RemoveAt(tree.Count - 1);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 4, tree.Count);
+
+ //Some leaf
+ //tree.dump();
+ i = 18;
+ Assert.AreEqual(i, tree.RemoveAt(9));
+ Assert.IsTrue(tree.Check(""));
+ Assert.AreEqual(i, tree.RemoveAt(8));
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 6, tree.Count);
+ }
+
+
+ [Test]
+ public void AlmostEmpty()
+ {
+ //Almost empty
+ tree.Clear();
+ tree.Add(3);
+ tree.RemoveAt(0);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(3));
+ Assert.AreEqual(0, tree.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void Empty()
+ {
+ tree.Clear();
+ tree.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void HighIndex()
+ {
+ tree.RemoveAt(tree.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void LowIndex()
+ {
+ tree.RemoveAt(-1);
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ //Note: ids does not match for bag
+ Assert.IsFalse(tree.Remove(-20));
+
+ //1b
+ Assert.IsTrue(tree.Remove(20));
+ Assert.IsTrue(tree.Check("T1"));
+ Assert.IsFalse(tree.Remove(20));
+
+ //1b
+ Assert.IsTrue(tree.Remove(10));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //case 1c
+ Assert.IsTrue(tree.Remove(24));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //1a (terminating)
+ Assert.IsTrue(tree.Remove(16));
+ Assert.IsTrue(tree.Remove(16));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //2
+ Assert.IsTrue(tree.Remove(18));
+ Assert.IsTrue(tree.Remove(17));
+ Assert.IsTrue(tree.Remove(18));
+ Assert.IsTrue(tree.Remove(17));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //2+1b
+ Assert.IsTrue(tree.Remove(15));
+ Assert.IsTrue(tree.Remove(15));
+ for (int i = 0; i < 5; i++) tree.Add(17 + i);
+ Assert.IsTrue(tree.Remove(23));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //1a+1b
+ Assert.IsTrue(tree.Remove(11));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //2+1c
+ for (int i = 0; i < 10; i++)
+ tree.Add(50 - 2 * i);
+
+ Assert.IsTrue(tree.Remove(42));
+ Assert.IsTrue(tree.Remove(38));
+ Assert.IsTrue(tree.Remove(22));
+ Assert.IsTrue(tree.Remove(40));
+
+ //
+ for (int i = 0; i < 48; i++)
+ tree.Remove(i);
+
+ //Almost empty tree:*
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(48));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //Empty tree:*
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class PredecessorStructure
+ {
+ private TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ for (int i = 0; i < 20; i++)
+ tree.Add(2 * i);
+ for (int i = 0; i < 10; i++)
+ tree.Add(4 * i);
+ }
+
+
+ [Test]
+ public void Predecessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.Predecessor(7));
+ Assert.AreEqual(6, tree.Predecessor(8));
+
+ //The bottom
+ Assert.AreEqual(0, tree.Predecessor(1));
+
+ //The top
+ Assert.AreEqual(38, tree.Predecessor(39));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was -2.")]
+ public void PredecessorTooLow1()
+ {
+ tree.Predecessor(-2);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was 0.")]
+ public void PredecessorTooLow2()
+ {
+ tree.Predecessor(0);
+ }
+
+
+ [Test]
+ public void WeakPredecessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.WeakPredecessor(7));
+ Assert.AreEqual(8, tree.WeakPredecessor(8));
+
+ //The bottom
+ Assert.AreEqual(0, tree.WeakPredecessor(1));
+ Assert.AreEqual(0, tree.WeakPredecessor(0));
+
+ //The top
+ Assert.AreEqual(38, tree.WeakPredecessor(39));
+ Assert.AreEqual(38, tree.WeakPredecessor(38));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was -2.")]
+ public void WeakPredecessorTooLow1()
+ {
+ tree.WeakPredecessor(-2);
+ }
+
+
+ [Test]
+ public void Successor()
+ {
+ loadup();
+ Assert.AreEqual(8, tree.Successor(7));
+ Assert.AreEqual(10, tree.Successor(8));
+
+ //The bottom
+ Assert.AreEqual(2, tree.Successor(0));
+ Assert.AreEqual(0, tree.Successor(-1));
+
+ //The top
+ Assert.AreEqual(38, tree.Successor(37));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 38.")]
+ public void SuccessorTooHigh1()
+ {
+ tree.Successor(38);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 39.")]
+ public void SuccessorTooHigh2()
+ {
+ tree.Successor(39);
+ }
+
+
+ [Test]
+ public void WeakSuccessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.WeakSuccessor(6));
+ Assert.AreEqual(8, tree.WeakSuccessor(7));
+
+ //The bottom
+ Assert.AreEqual(0, tree.WeakSuccessor(-1));
+ Assert.AreEqual(0, tree.WeakSuccessor(0));
+
+ //The top
+ Assert.AreEqual(38, tree.WeakSuccessor(37));
+ Assert.AreEqual(38, tree.WeakSuccessor(38));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 39.")]
+ public void WeakSuccessorTooHigh1()
+ {
+ tree.WeakSuccessor(39);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class PriorityQueue
+ {
+ private TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ foreach (int i in new int[] { 1, 2, 3, 4 })
+ tree.Add(i);
+ tree.Add(1);
+ tree.Add(3);
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ loadup();
+ Assert.AreEqual(1, tree.FindMin());
+ Assert.AreEqual(4, tree.FindMax());
+ Assert.AreEqual(1, tree.DeleteMin());
+ Assert.AreEqual(4, tree.DeleteMax());
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.AreEqual(1, tree.FindMin());
+ Assert.AreEqual(3, tree.FindMax());
+ Assert.AreEqual(1, tree.DeleteMin());
+ Assert.AreEqual(3, tree.DeleteMax());
+ Assert.IsTrue(tree.Check("Normal test 2"), "Bad tree");
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty1()
+ {
+ tree.FindMin();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty2()
+ {
+ tree.FindMax();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty3()
+ {
+ tree.DeleteMin();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty4()
+ {
+ tree.DeleteMax();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IndexingAndCounting
+ {
+ private TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ }
+
+
+ private void populate()
+ {
+ tree.Add(30);
+ tree.Add(30);
+ tree.Add(50);
+ tree.Add(10);
+ tree.Add(70);
+ tree.Add(70);
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ populate();
+
+ int[] a = tree.ToArray();
+
+ Assert.AreEqual(6, a.Length);
+ Assert.AreEqual(10, a[0]);
+ Assert.AreEqual(30, a[1]);
+ Assert.AreEqual(30, a[2]);
+ Assert.AreEqual(50, a[3]);
+ Assert.AreEqual(70, a[4]);
+ Assert.AreEqual(70, a[5]);
+ }
+
+
+ [Test]
+ public void GoodIndex()
+ {
+ Assert.AreEqual(-1, tree.IndexOf(20));
+ Assert.AreEqual(-1, tree.LastIndexOf(20));
+ populate();
+ Assert.AreEqual(10, tree[0]);
+ Assert.AreEqual(30, tree[1]);
+ Assert.AreEqual(30, tree[2]);
+ Assert.AreEqual(50, tree[3]);
+ Assert.AreEqual(70, tree[4]);
+ Assert.AreEqual(70, tree[5]);
+ Assert.AreEqual(0, tree.IndexOf(10));
+ Assert.AreEqual(1, tree.IndexOf(30));
+ Assert.AreEqual(3, tree.IndexOf(50));
+ Assert.AreEqual(4, tree.IndexOf(70));
+ Assert.AreEqual(-1, tree.IndexOf(20));
+ Assert.AreEqual(-1, tree.IndexOf(0));
+ Assert.AreEqual(-1, tree.IndexOf(90));
+ Assert.AreEqual(0, tree.LastIndexOf(10));
+ Assert.AreEqual(2, tree.LastIndexOf(30));
+ Assert.AreEqual(3, tree.LastIndexOf(50));
+ Assert.AreEqual(5, tree.LastIndexOf(70));
+ Assert.AreEqual(-1, tree.LastIndexOf(20));
+ Assert.AreEqual(-1, tree.LastIndexOf(0));
+ Assert.AreEqual(-1, tree.LastIndexOf(90));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void IndexTooLarge()
+ {
+ populate();
+ Console.WriteLine(tree[6]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void IndexTooSmall()
+ {
+ populate();
+ Console.WriteLine(tree[-1]);
+ }
+
+
+ [Test]
+ public void FilledTreeOutsideInput()
+ {
+ populate();
+ Assert.AreEqual(0, tree.CountFrom(90));
+ Assert.AreEqual(0, tree.CountFromTo(-20, 0));
+ Assert.AreEqual(0, tree.CountFromTo(80, 100));
+ Assert.AreEqual(0, tree.CountTo(0));
+ Assert.AreEqual(6, tree.CountTo(90));
+ Assert.AreEqual(6, tree.CountFromTo(-20, 90));
+ Assert.AreEqual(6, tree.CountFrom(0));
+ }
+
+
+ [Test]
+ public void FilledTreeIntermediateInput()
+ {
+ populate();
+ Assert.AreEqual(5, tree.CountFrom(20));
+ Assert.AreEqual(2, tree.CountFromTo(20, 40));
+ Assert.AreEqual(3, tree.CountTo(40));
+ }
+
+
+ [Test]
+ public void FilledTreeMatchingInput()
+ {
+ populate();
+ Assert.AreEqual(5, tree.CountFrom(30));
+ Assert.AreEqual(3, tree.CountFromTo(30, 70));
+ Assert.AreEqual(0, tree.CountFromTo(50, 30));
+ Assert.AreEqual(0, tree.CountFromTo(50, 50));
+ Assert.AreEqual(0, tree.CountTo(10));
+ Assert.AreEqual(3, tree.CountTo(50));
+ }
+
+
+ [Test]
+ public void CountEmptyTree()
+ {
+ Assert.AreEqual(0, tree.CountFrom(20));
+ Assert.AreEqual(0, tree.CountFromTo(20, 40));
+ Assert.AreEqual(0, tree.CountTo(40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+
+ namespace ModificationCheck
+ {
+ [TestFixture]
+ public class Enumerator
+ {
+ private TreeBag<int> tree;
+
+ private MSG.IEnumerator<int> e;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+ tree.Add(3);
+ tree.Add(7);
+ e = tree.GetEnumerator();
+ }
+
+
+ [Test]
+ public void CurrentAfterModification()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ Assert.AreEqual(0, e.Current);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterAdd()
+ {
+ tree.Add(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterRemove()
+ {
+ tree.Remove(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterClear()
+ {
+ tree.Clear();
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ e = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class RangeEnumerator
+ {
+ private TreeBag<int> tree;
+
+ private MSG.IEnumerator<int> e;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ e = tree.RangeFromTo(3, 7).GetEnumerator();
+ }
+
+
+ [Test]
+ public void CurrentAfterModification()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ Assert.AreEqual(3, e.Current);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterAdd()
+ {
+ tree.Add(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterRemove()
+ {
+ tree.Remove(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterClear()
+ {
+ tree.Clear();
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ e = null;
+ }
+ }
+ }
+
+
+
+
+ namespace PathcopyPersistence
+ {
+ [TestFixture]
+ public class Navigation
+ {
+ private TreeBag<int> tree, snap;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeBag<int>(ic);
+ for (int i = 0; i <= 20; i++)
+ tree.Add(2 * i + 1);
+ tree.Add(13);
+ snap = (TreeBag<int>)tree.Snapshot();
+ for (int i = 0; i <= 10; i++)
+ tree.Remove(4 * i + 1);
+ }
+
+
+ private bool twomodeleven(int i)
+ {
+ return i % 11 == 2;
+ }
+
+
+ [Test]
+ public void InternalEnum()
+ {
+ Assert.IsTrue(IC.eq(snap.FindAll(new Filter<int>(twomodeleven)), 13, 13, 35));
+ }
+
+
+ public void MoreCut()
+ {
+ //TODO: Assert.Fail("more tests of Cut needed");
+ }
+
+
+ [Test]
+ public void Cut()
+ {
+ int lo, hi;
+ bool lv, hv;
+
+ Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(64), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(5, hi);
+ Assert.AreEqual(3, lo);
+ Assert.IsTrue(snap.Cut(new HigherOrder.CubeRoot(125), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(7, hi);
+ Assert.AreEqual(3, lo);
+ Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(125000), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && !hv);
+ Assert.AreEqual(41, lo);
+ Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(-27), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(!lv && hv);
+ Assert.AreEqual(1, hi);
+ }
+
+
+ [Test]
+ public void Range()
+ {
+ Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 16), 5, 7, 9, 11,13, 13, 15));
+ Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 17), 5, 7, 9, 11,13, 13, 15));
+ Assert.IsTrue(IC.eq(snap.RangeFromTo(6, 16), 7, 9, 11,13, 13, 15));
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsTrue(snap.Contains(5));
+ Assert.IsTrue(snap.Contains(13));
+ Assert.AreEqual(1, snap.ContainsCount(5));
+ Assert.AreEqual(2, snap.ContainsCount(13));
+ }
+
+
+ [Test]
+ public void FindMin()
+ {
+ Assert.AreEqual(1, snap.FindMin());
+ }
+
+
+ [Test]
+ public void FindMax()
+ {
+ Assert.AreEqual(41, snap.FindMax());
+ }
+
+
+ [Test]
+ public void Predecessor()
+ {
+ Assert.AreEqual(13, snap.Predecessor(15));
+ Assert.AreEqual(15, snap.Predecessor(16));
+ Assert.AreEqual(15, snap.Predecessor(17));
+ Assert.AreEqual(17, snap.Predecessor(18));
+ }
+
+
+ [Test]
+ public void Successor()
+ {
+ Assert.AreEqual(17, snap.Successor(15));
+ Assert.AreEqual(17, snap.Successor(16));
+ Assert.AreEqual(19, snap.Successor(17));
+ Assert.AreEqual(19, snap.Successor(18));
+ }
+
+
+ [Test]
+ public void WeakPredecessor()
+ {
+ Assert.AreEqual(15, snap.WeakPredecessor(15));
+ Assert.AreEqual(15, snap.WeakPredecessor(16));
+ Assert.AreEqual(17, snap.WeakPredecessor(17));
+ Assert.AreEqual(17, snap.WeakPredecessor(18));
+ }
+
+
+ [Test]
+ public void WeakSuccessor()
+ {
+ Assert.AreEqual(15, snap.WeakSuccessor(15));
+ Assert.AreEqual(17, snap.WeakSuccessor(16));
+ Assert.AreEqual(17, snap.WeakSuccessor(17));
+ Assert.AreEqual(19, snap.WeakSuccessor(18));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
+ public void CountTo()
+ {
+ int j = snap.CountTo(15);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
+ public void Indexing()
+ {
+ int j = snap[4];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
+ public void Indexing2()
+ {
+ int j = snap.IndexOf(5);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ ic = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Single
+ {
+ private TreeBag<int> tree;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeBag<int>(ic);
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i + 1);
+ }
+
+
+ [Test]
+ public void EnumerationWithAdd()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ int i = 0;
+ TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
+
+ foreach (int j in snap)
+ {
+ Assert.AreEqual(1 + 2 * i++, j);
+ tree.Add(21 - j);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ }
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
+
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ tree.Remove(19);
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ }
+
+
+ [Test]
+ public void RemoveNormal()
+ {
+ tree.Clear();
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 10);
+ }
+ tree.Add(15);
+
+ int[] orig = new int[] { 10, 11, 12, 13, 14, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 };
+ TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
+
+ Assert.IsFalse(tree.Remove(-20));
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+
+ //decrease items case
+ Assert.IsTrue(tree.Remove(15));
+ Assert.IsTrue(tree.Check("T1"));
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ //snap.dump();
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //No demote case, with move_item
+ Assert.IsTrue(tree.Remove(20));
+ Assert.IsTrue(tree.Check("T1"));
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsFalse(tree.Remove(20));
+
+ //plain case 2
+ tree.Snapshot();
+ Assert.IsTrue(tree.Remove(14));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //case 1b
+ Assert.IsTrue(tree.Remove(25));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //case 1c
+ Assert.IsTrue(tree.Remove(29));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //1a (terminating)
+ Assert.IsTrue(tree.Remove(10));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //2+1b
+ Assert.IsTrue(tree.Remove(12));
+ tree.Snapshot();
+ Assert.IsTrue(tree.Remove(11));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //1a+1b
+ Assert.IsTrue(tree.Remove(18));
+ Assert.IsTrue(tree.Remove(13));
+ Assert.IsTrue(tree.Remove(15));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //2+1c
+ for (int i = 0; i < 10; i++)
+ tree.Add(50 - 2 * i);
+
+ Assert.IsTrue(tree.Remove(42));
+ Assert.IsTrue(tree.Remove(38));
+ Assert.IsTrue(tree.Remove(28));
+ Assert.IsTrue(tree.Remove(40));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //
+ Assert.IsTrue(tree.Remove(16));
+ Assert.IsTrue(tree.Remove(23));
+ Assert.IsTrue(tree.Remove(17));
+ Assert.IsTrue(tree.Remove(19));
+ Assert.IsTrue(tree.Remove(50));
+ Assert.IsTrue(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(21));
+ Assert.IsTrue(tree.Remove(22));
+ Assert.IsTrue(tree.Remove(24));
+ for (int i = 0; i < 48; i++)
+ tree.Remove(i);
+
+ //Almost empty tree:
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(48));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //Empty tree:
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ }
+
+
+ [Test]
+ public void Add()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
+
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ tree.Add(10);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ tree.Add(16);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+
+ tree.Add(9);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //Promote+zigzig
+ tree.Add(40);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ for (int i = 1; i < 4; i++)
+ tree.Add(40 - 2 * i);
+
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+
+ //Zigzag:
+ tree.Add(32);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
+
+ tree.Clear();
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.AreEqual(0, tree.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Cannot snapshot a snapshot")]
+ public void SnapSnap()
+ {
+ TreeBag<int> snap = (TreeBag<int>)tree.Snapshot();
+
+ snap.Snapshot();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ ic = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Multiple
+ {
+ private TreeBag<int> tree;
+
+ private IComparer<int> ic;
+
+
+ private bool eq(MSG.IEnumerable<int> me, int[] that)
+ {
+ int i = 0, maxind = that.Length - 1;
+
+ foreach (int item in me)
+ if (i > maxind || ic.Compare(item, that[i++]) != 0)
+ return false;
+
+ return true;
+ }
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeBag<int>(ic);
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i + 1);
+ }
+
+
+ [Test]
+ public void First()
+ {
+ TreeBag<int>[] snaps = new TreeBag<int>[10];
+
+ for (int i = 0; i < 10; i++)
+ {
+ snaps[i] = (TreeBag<int>)(tree.Snapshot());
+ tree.Add(2 * i);
+ }
+
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.AreEqual(i + 10, snaps[i].Count);
+ }
+
+ snaps[5] = null;
+ snaps[9] = null;
+ GC.Collect();
+ snaps[8].Dispose();
+ tree.Remove(14);
+
+ int[] res = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19 };
+ int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
+ int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
+
+ Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
+ Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
+ Assert.IsTrue(IC.eq(tree, res));
+ Assert.IsTrue(tree.Check("B"));
+ Assert.IsTrue(snaps[3].Check("B"));
+ Assert.IsTrue(snaps[7].Check("B"));
+ }
+
+
+ [Test]
+ public void CollectingTheMaster()
+ {
+ TreeBag<int>[] snaps = new TreeBag<int>[10];
+
+ for (int i = 0; i < 10; i++)
+ {
+ snaps[i] = (TreeBag<int>)(tree.Snapshot());
+ tree.Add(2 * i);
+ }
+
+ tree = null;
+ GC.Collect();
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.AreEqual(i + 10, snaps[i].Count);
+ }
+
+ snaps[5] = null;
+ snaps[9] = null;
+ GC.Collect();
+ snaps[8].Dispose();
+
+ int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
+ int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
+
+ Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
+ Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
+ Assert.IsTrue(snaps[3].Check("B"));
+ Assert.IsTrue(snaps[7].Check("B"));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ ic = null;
+ }
+ }
+ }
+
+
+
+
+ namespace HigherOrder
+ {
+ internal class CubeRoot: IComparable<int>
+ {
+ private int c;
+
+
+ internal CubeRoot(int c) { this.c = c; }
+
+
+ public int CompareTo(int that) { return c - that * that * that; }
+ public bool Equals(int that) { return c == that * that * that; }
+ }
+
+
+
+ class Interval: IComparable<int>
+ {
+ private int b, t;
+
+
+ internal Interval(int b, int t) { this.b = b; this.t = t; }
+
+
+ public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
+ public bool Equals(int that) { return that >= b && that <= t; }
+ }
+
+
+
+ [TestFixture]
+ public class Simple
+ {
+ private TreeBag<int> tree;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeBag<int>(ic);
+ }
+
+
+ private bool never(int i) { return false; }
+
+
+ private bool always(int i) { return true; }
+
+
+ private bool even(int i) { return i % 2 == 0; }
+
+
+ private string themap(int i) { return String.Format("AA {0,4} BB", i); }
+
+
+ private string badmap(int i) { return String.Format("AA {0} BB", i); }
+
+
+ private int appfield1;
+
+ private int appfield2;
+
+
+ private void apply(int i) { appfield1++; appfield2 += i * i; }
+
+
+ [Test]
+ public void Apply()
+ {
+ Simple simple1 = new Simple();
+
+ tree.Apply(new Applier<int>(simple1.apply));
+ Assert.AreEqual(0, simple1.appfield1);
+ Assert.AreEqual(0, simple1.appfield2);
+
+ Simple simple2 = new Simple();
+
+ for (int i = 0; i < 10; i++) tree.Add(i);
+ tree.Add(2);
+
+ tree.Apply(new Applier<int>(simple2.apply));
+ Assert.AreEqual(11, simple2.appfield1);
+ Assert.AreEqual(289, simple2.appfield2);
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(tree.All(new Filter<int>(never)));
+ Assert.IsTrue(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ Assert.IsFalse(tree.All(new Filter<int>(never)));
+ Assert.IsFalse(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2);
+
+ Assert.IsFalse(tree.All(new Filter<int>(never)));
+ Assert.IsTrue(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1);
+
+ Assert.IsFalse(tree.All(new Filter<int>(never)));
+ Assert.IsFalse(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsFalse(tree.Exists(new Filter<int>(even)));
+ Assert.IsFalse(tree.Exists(new Filter<int>(always)));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(even)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2);
+
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(even)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1);
+
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsFalse(tree.Exists(new Filter<int>(even)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(always)));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Assert.AreEqual(0, tree.FindAll(new Filter<int>(never)).Count);
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+ tree.Add(2);
+
+ Assert.AreEqual(0, tree.FindAll(new Filter<int>(never)).Count);
+ Assert.AreEqual(11, tree.FindAll(new Filter<int>(always)).Count);
+ Assert.AreEqual(6, tree.FindAll(new Filter<int>(even)).Count);
+ Assert.IsTrue(((TreeBag<int>)tree.FindAll(new Filter<int>(even))).Check("R"));
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Assert.AreEqual(0, tree.Map(new Mapper<int,string>(themap), new SC()).Count);
+ for (int i = 0; i < 14; i++)
+ tree.Add(i * i * i);
+ tree.Add(1);
+
+ IIndexedSorted<string> res = tree.Map(new Mapper<int,string>(themap), new SC());
+
+ Assert.IsTrue(((TreeBag<string>)res).Check("R"));
+ Assert.AreEqual(15, res.Count);
+ Assert.AreEqual("AA 0 BB", res[0]);
+ Assert.AreEqual("AA 1 BB", res[1]);
+ Assert.AreEqual("AA 1 BB", res[2]);
+ Assert.AreEqual("AA 8 BB", res[3]);
+ Assert.AreEqual("AA 27 BB", res[4]);
+ Assert.AreEqual("AA 125 BB", res[6]);
+ Assert.AreEqual("AA 1000 BB", res[11]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "mapper not monotonic")]
+ public void BadMap()
+ {
+ for (int i = 0; i < 11; i++)
+ tree.Add(i * i * i);
+
+ ISorted<string> res = tree.Map(new Mapper<int,string>(badmap), new SC());
+ }
+
+
+ [Test]
+ public void Cut()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+ tree.Add(3);
+
+ int low, high;
+ bool lval, hval;
+
+ Assert.IsTrue(tree.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(2, low);
+ Assert.IsFalse(tree.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(3, low);
+ }
+
+
+ [Test]
+ public void CutInt()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i);
+
+ int low, high;
+ bool lval, hval;
+
+ Assert.IsFalse(tree.Cut(new IC(3), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(2, low);
+ Assert.IsTrue(tree.Cut(new IC(6), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(8, high);
+ Assert.AreEqual(4, low);
+ }
+
+
+ [Test]
+ public void CutInterval()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i);
+
+ int lo, hi;
+ bool lv, hv;
+
+ Assert.IsTrue(tree.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(10, hi);
+ Assert.AreEqual(4, lo);
+ Assert.IsTrue(tree.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(12, hi);
+ Assert.AreEqual(4, lo);
+ for (int i = 0; i < 100; i++)
+ tree.Add(2 * i);
+
+ tree.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(106, hi);
+ Assert.AreEqual(76, lo);
+ tree.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(8, hi);
+ Assert.AreEqual(4, lo);
+ tree.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(112, hi);
+ Assert.AreEqual(78, lo);
+ }
+
+
+ [Test]
+ public void UpperCut()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ int l, h;
+ bool lv, hv;
+
+ Assert.IsFalse(tree.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
+ Assert.IsTrue(lv && !hv);
+ Assert.AreEqual(9, l);
+ Assert.IsFalse(tree.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
+ Assert.IsTrue(!lv && hv);
+ Assert.AreEqual(0, h);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; tree = null; }
+ }
+ }
+
+
+
+
+ namespace MultiOps
+ {
+ [TestFixture]
+ public class AddAll
+ {
+ private int sqr(int i) { return i * i; }
+
+
+ TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init() { tree = new TreeBag<int>(new IC()); }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ tree.AddAll(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ [Test]
+ public void SomeEmpty()
+ {
+ for (int i = 4; i < 9; i++) tree.Add(i);
+
+ tree.AddAll(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(5, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ [Test]
+ public void EmptySome()
+ {
+ tree.AddAll(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree[0]);
+ Assert.AreEqual(1, tree[1]);
+ Assert.AreEqual(4, tree[2]);
+ Assert.AreEqual(9, tree[3]);
+ }
+
+
+ [Test]
+ public void SomeSome()
+ {
+ for (int i = 5; i < 9; i++) tree.Add(i);
+ tree.Add(1);
+
+ tree.AddAll(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(9, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 1, 4, 5, 6, 7, 8, 9));
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+
+
+
+ [TestFixture]
+ public class AddSorted
+ {
+ private int sqr(int i) { return i * i; }
+
+ private int step(int i) { return i/3; }
+
+
+ private int bad(int i) { return i * (5 - i); }
+
+
+ TreeBag<int> tree;
+
+
+ [SetUp]
+ public void Init() { tree = new TreeBag<int>(new IC()); }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ tree.AddSorted(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ [Test]
+ public void SomeEmpty()
+ {
+ for (int i = 4; i < 9; i++) tree.Add(i);
+
+ tree.AddSorted(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(5, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ [Test]
+ public void EmptySome()
+ {
+ tree.AddSorted(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree[0]);
+ Assert.AreEqual(1, tree[1]);
+ Assert.AreEqual(4, tree[2]);
+ Assert.AreEqual(9, tree[3]);
+ }
+
+ [Test]
+ public void EmptySome2()
+ {
+ tree.AddSorted(new FunEnumerable(4, new Int2Int(step)));
+ //tree.dump();
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree[0]);
+ Assert.AreEqual(0, tree[1]);
+ Assert.AreEqual(0, tree[2]);
+ Assert.AreEqual(1, tree[3]);
+ }
+
+
+ [Test]
+ public void SomeSome()
+ {
+ for (int i = 5; i < 9; i++) tree.Add(i);
+ tree.Add(1);
+
+ tree.AddSorted(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(9, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1,1, 4, 5, 6, 7, 8, 9));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Argument not sorted")]
+ public void EmptyBad()
+ {
+ tree.AddSorted(new FunEnumerable(9, new Int2Int(bad)));
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+
+
+
+ [TestFixture]
+ public class Rest
+ {
+ TreeBag<int> tree, tree2;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeBag<int>(new IC());
+ tree2 = new TreeBag<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+ tree.Add(4);
+
+ for (int i = 0; i < 10; i++)
+ tree2.Add(2 * i);
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ tree.RemoveAll(tree2.RangeFromTo(3, 7));
+ Assert.AreEqual(9, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3,4, 5, 7, 8, 9));
+ tree.RemoveAll(tree2.RangeFromTo(3, 7));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
+ tree.RemoveAll(tree2.RangeFromTo(13, 17));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
+ tree.RemoveAll(tree2.RangeFromTo(3, 17));
+ Assert.AreEqual(7, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 9));
+ for (int i = 0; i < 10; i++) tree2.Add(i);
+
+ tree.RemoveAll(tree2.RangeFromTo(-1, 10));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+ private void pint<T>(MSG.IEnumerable<T> e)
+ {
+ foreach (T i in e)
+ Console.Write("{0} ", i);
+
+ Console.WriteLine();
+ }
+
+ [Test]
+ public void RetainAll()
+ {
+ tree.Add(8);tree2.Add(6);
+ //pint<int>(tree);
+ //pint<int>(tree2);
+ tree.RetainAll(tree2.RangeFromTo(3, 17));
+ Assert.AreEqual(3, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 4, 6, 8));
+ tree.RetainAll(tree2.RangeFromTo(1, 17));
+ Assert.AreEqual(3, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 4, 6 , 8));
+ tree.RetainAll(tree2.RangeFromTo(3, 5));
+ Assert.AreEqual(1, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 4));
+ tree.RetainAll(tree2.RangeFromTo(7, 17));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ tree.RetainAll(tree2.RangeFromTo(5, 5));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ tree.RetainAll(tree2.RangeFromTo(15, 25));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ Assert.IsFalse(tree.ContainsAll(tree2));
+ Assert.IsTrue(tree.ContainsAll(tree));
+ tree2.Clear();
+ Assert.IsTrue(tree.ContainsAll(tree2));
+ tree.Clear();
+ Assert.IsTrue(tree.ContainsAll(tree2));
+ tree2.Add(8);
+ Assert.IsFalse(tree.ContainsAll(tree2));
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ tree.RemoveInterval(3, 4);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(7, tree.Count);
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 6, 7, 8, 9));
+ tree.RemoveInterval(2, 3);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(IC.eq(tree, 0, 1, 8,9));
+ tree.RemoveInterval(0, 4);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RemoveRangeBad1()
+ {
+ tree.RemoveInterval(-3, 8);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RemoveRangeBad2()
+ {
+ tree.RemoveInterval(3, -8);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void RemoveRangeBad3()
+ {
+ tree.RemoveInterval(3, 9);
+ }
+
+
+ [Test]
+ public void GetRange()
+ {
+ Assert.IsTrue(IC.eq(tree[3, 3]));
+ Assert.IsTrue(IC.eq(tree[3, 4], 3));
+ Assert.IsTrue(IC.eq(tree[3, 5], 3, 4));
+ Assert.IsTrue(IC.eq(tree[3, 6], 3, 4, 4));
+ Assert.IsTrue(IC.eq(tree[3, 7], 3, 4, 4, 5));
+ Assert.IsTrue(IC.eq(tree[4, 4]));
+ Assert.IsTrue(IC.eq(tree[4, 5], 4));
+ Assert.IsTrue(IC.eq(tree[4, 6], 4, 4));
+ Assert.IsTrue(IC.eq(tree[4, 7], 4, 4, 5));
+ Assert.IsTrue(IC.eq(tree[4, 8], 4, 4, 5, 6));
+ Assert.IsTrue(IC.eq(tree[5, 5]));
+ Assert.IsTrue(IC.eq(tree[5, 6], 4));
+ Assert.IsTrue(IC.eq(tree[5, 7], 4, 5));
+ Assert.IsTrue(IC.eq(tree[5, 8], 4, 5, 6));
+ Assert.IsTrue(IC.eq(tree[5, 9], 4, 5, 6, 7));
+ Assert.IsTrue(IC.eq(tree[5, 11], 4, 5, 6, 7, 8, 9));
+ }
+
+
+ [Test]
+ public void GetRangeBackwards()
+ {
+ Assert.IsTrue(IC.eq(tree[3, 3].Backwards()));
+ Assert.IsTrue(IC.eq(tree[3, 4].Backwards(), 3));
+ Assert.IsTrue(IC.eq(tree[3, 5].Backwards(), 4, 3));
+ Assert.IsTrue(IC.eq(tree[3, 6].Backwards(), 4, 4, 3));
+ Assert.IsTrue(IC.eq(tree[3, 7].Backwards(), 5, 4, 4, 3));
+ Assert.IsTrue(IC.eq(tree[4, 4].Backwards()));
+ Assert.IsTrue(IC.eq(tree[4, 5].Backwards(), 4));
+ Assert.IsTrue(IC.eq(tree[4, 6].Backwards(), 4, 4));
+ Assert.IsTrue(IC.eq(tree[4, 7].Backwards(), 5, 4, 4));
+ Assert.IsTrue(IC.eq(tree[4, 8].Backwards(), 6, 5, 4, 4));
+ Assert.IsTrue(IC.eq(tree[5, 5].Backwards()));
+ Assert.IsTrue(IC.eq(tree[5, 6].Backwards(), 4));
+ Assert.IsTrue(IC.eq(tree[5, 7].Backwards(), 5, 4));
+ Assert.IsTrue(IC.eq(tree[5, 8].Backwards(), 6, 5, 4));
+ Assert.IsTrue(IC.eq(tree[5, 9].Backwards(), 7, 6, 5, 4));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetRangeBad1()
+ {
+ object foo = tree[-3, 0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetRangeBad2()
+ {
+ object foo = tree[3, 2];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void GetRangeBad3()
+ {
+ object foo = tree[3, 12];
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; tree2 = null; }
+ }
+ }
+
+
+ namespace Hashing
+ {
+ [TestFixture]
+ public class ISequenced
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeBag<int>(new IC());
+ dat = new TreeBag<int>(new IC());
+ dut = new TreeBag<int>(new RevIC());
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeBag<int>(new IC());
+ dat = new TreeBag<int>(new IC());
+ dut = new TreeBag<int>(new RevIC());
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count, params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count << 16) + retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1, 3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2, 3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1, 3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2, 7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+ }
+
+}
diff --git a/mcs/class/Mono.C5/Test/trees/Dictionary.cs b/mcs/class/Mono.C5/Test/trees/Dictionary.cs
new file mode 100644
index 00000000000..e58dd5b15bf
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/trees/Dictionary.cs
@@ -0,0 +1,264 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG=System.Collections.Generic;
+
+
+namespace nunit.trees.RBDictionary
+{
+ [TestFixture]
+ public class RBDict
+ {
+ private TreeDictionary<string,string> dict;
+
+
+ [SetUp]
+ public void Init() { dict = new TreeDictionary<string,string>(new SC()); }
+
+
+ [TearDown]
+ public void Dispose() { dict = null; }
+
+ [Test]
+ public void SyncRoot()
+ {
+ Assert.IsFalse(dict.SyncRoot == null);
+ }
+
+ [Test]
+ public void Pred()
+ {
+ dict.Add("A", "1");
+ dict.Add("C", "2");
+ dict.Add("E", "3");
+ Assert.AreEqual("1", dict.Predecessor("B").value);
+ Assert.AreEqual("1", dict.Predecessor("C").value);
+ Assert.AreEqual("1", dict.WeakPredecessor("B").value);
+ Assert.AreEqual("2", dict.WeakPredecessor("C").value);
+ Assert.AreEqual("2", dict.Successor("B").value);
+ Assert.AreEqual("3", dict.Successor("C").value);
+ Assert.AreEqual("2", dict.WeakSuccessor("B").value);
+ Assert.AreEqual("2", dict.WeakSuccessor("C").value);
+ }
+
+
+ [Test]
+ public void Initial()
+ {
+ bool res;
+ Assert.IsFalse(dict.IsReadOnly);
+
+ Assert.AreEqual(dict.Count, 0, "new dict should be empty");
+ dict.Add("A", "B");
+ Assert.AreEqual(dict.Count, 1, "bad count");
+ Assert.AreEqual(dict["A"], "B", "Wrong value for dict[A]");
+ dict.Add("C", "D");
+ Assert.AreEqual(dict.Count, 2, "bad count");
+ Assert.AreEqual(dict["A"], "B", "Wrong value");
+ Assert.AreEqual(dict["C"], "D", "Wrong value");
+ res = dict.Remove("A");
+ Assert.IsTrue(res, "bad return value from Remove(A)");
+ Assert.IsTrue(dict.Check());
+ Assert.AreEqual(dict.Count, 1, "bad count");
+ Assert.AreEqual(dict["C"], "D", "Wrong value of dict[C]");
+ res = dict.Remove("Z");
+ Assert.IsFalse(res, "bad return value from Remove(Z)");
+ Assert.AreEqual(dict.Count, 1, "bad count");
+ Assert.AreEqual(dict["C"], "D", "Wrong value of dict[C] (2)");
+ dict.Clear();
+ Assert.AreEqual(dict.Count, 0, "dict should be empty");
+ }
+ [Test]
+ public void Contains()
+ {
+ dict.Add("C", "D");
+ Assert.IsTrue(dict.Contains("C"));
+ Assert.IsFalse(dict.Contains("D"));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Item has already been added. Key in dictionary: 'A' Key being added: 'A'")]
+ public void IllegalAdd()
+ {
+ dict.Add("A", "B");
+ dict.Add("A", "B");
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Key not present in Dictionary")]
+ public void GettingNonExisting()
+ {
+ Console.WriteLine(dict["R"]);
+ }
+
+
+ [Test]
+ public void Setter()
+ {
+ dict["R"] = "UYGUY";
+ Assert.AreEqual(dict["R"], "UYGUY");
+ dict["R"] = "UIII";
+ Assert.AreEqual(dict["R"], "UIII");
+ dict["S"] = "VVV";
+ Assert.AreEqual(dict["R"], "UIII");
+ Assert.AreEqual(dict["S"], "VVV");
+ //dict.dump();
+ }
+ }
+
+ [TestFixture]
+ public class Enumerators
+ {
+ private TreeDictionary<string,string> dict;
+
+ private MSG.IEnumerator<KeyValuePair<string,string>> dictenum;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dict = new TreeDictionary<string,string>(new SC());
+ dict["S"] = "A";
+ dict["T"] = "B";
+ dict["R"] = "C";
+ dictenum = dict.GetEnumerator();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dictenum = null;
+ dict = null;
+ }
+
+
+
+ [Test]
+ public void Keys()
+ {
+ MSG.IEnumerator<string> keys = dict.Keys.GetEnumerator();
+ Assert.AreEqual(3, dict.Keys.Count);
+ Assert.IsTrue(keys.MoveNext());
+ Assert.AreEqual("R",keys.Current);
+ Assert.IsTrue(keys.MoveNext());
+ Assert.AreEqual("S",keys.Current);
+ Assert.IsTrue(keys.MoveNext());
+ Assert.AreEqual("T",keys.Current);
+ Assert.IsFalse(keys.MoveNext());
+ }
+
+ [Test]
+ public void Values()
+ {
+ MSG.IEnumerator<string> values = dict.Values.GetEnumerator();
+ Assert.AreEqual(3, dict.Values.Count);
+ Assert.IsTrue(values.MoveNext());
+ Assert.AreEqual("C",values.Current);
+ Assert.IsTrue(values.MoveNext());
+ Assert.AreEqual("A",values.Current);
+ Assert.IsTrue(values.MoveNext());
+ Assert.AreEqual("B",values.Current);
+ Assert.IsFalse(values.MoveNext());
+ }
+
+
+
+ [Test]
+ public void NormalUse()
+ {
+ Assert.IsTrue(dictenum.MoveNext());
+ Assert.AreEqual(dictenum.Current, new KeyValuePair<string,string>("R", "C"));
+ Assert.IsTrue(dictenum.MoveNext());
+ Assert.AreEqual(dictenum.Current, new KeyValuePair<string,string>("S", "A"));
+ Assert.IsTrue(dictenum.MoveNext());
+ Assert.AreEqual(dictenum.Current, new KeyValuePair<string,string>("T", "B"));
+ Assert.IsFalse(dictenum.MoveNext());
+ }
+ }
+
+
+
+
+ namespace PathCopyPersistence
+ {
+ [TestFixture]
+ public class Simple
+ {
+ private TreeDictionary<string,string> dict;
+
+ private TreeDictionary<string,string> snap;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dict = new TreeDictionary<string,string>(new SC());
+ dict["S"] = "A";
+ dict["T"] = "B";
+ dict["R"] = "C";
+ dict["V"] = "G";
+ snap = (TreeDictionary<string,string>)dict.Snapshot();
+ }
+
+
+ [Test]
+ public void Test()
+ {
+ dict["SS"] = "D";
+ Assert.AreEqual(5, dict.Count);
+ Assert.AreEqual(4, snap.Count);
+ dict["T"] = "bb";
+ Assert.AreEqual(5, dict.Count);
+ Assert.AreEqual(4, snap.Count);
+ Assert.AreEqual("B", snap["T"]);
+ Assert.AreEqual("bb", dict["T"]);
+ Assert.IsFalse(dict.IsReadOnly);
+ Assert.IsTrue(snap.IsReadOnly);
+ //Finally, update of root node:
+ TreeDictionary<string,string> snap2 = (TreeDictionary<string,string>)dict.Snapshot();
+ dict["S"] = "abe";
+ Assert.AreEqual("abe", dict["S"]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void UpdateSnap()
+ {
+ snap["Y"] = "J";
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dict = null;
+ snap = null;
+ }
+ }
+ }
+}
diff --git a/mcs/class/Mono.C5/Test/trees/RedBlackTreeSetTests.cs b/mcs/class/Mono.C5/Test/trees/RedBlackTreeSetTests.cs
new file mode 100644
index 00000000000..6f2ef951b99
--- /dev/null
+++ b/mcs/class/Mono.C5/Test/trees/RedBlackTreeSetTests.cs
@@ -0,0 +1,2709 @@
+/*
+ Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using System;
+using C5;
+using NUnit.Framework;
+using MSG = System.Collections.Generic;
+
+namespace nunit.trees.TreeSet
+{
+ [TestFixture]
+ public class Combined
+ {
+ private IIndexedSorted<KeyValuePair<int,int>> lst;
+
+
+ [SetUp]
+ public void Init()
+ {
+ lst = new TreeSet<KeyValuePair<int,int>>(new KeyValuePairComparer<int,int>(new IC()));
+ for (int i = 0; i < 10; i++)
+ lst.Add(new KeyValuePair<int,int>(i, i + 30));
+ }
+
+
+ [TearDown]
+ public void Dispose() { lst = null; }
+
+
+ [Test]
+ public void Find()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Find(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Find(ref p));
+ }
+
+
+ [Test]
+ public void FindOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.FindOrAdd(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.FindOrAdd(ref p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void Update()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.Update(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.Update(p));
+ }
+
+
+ [Test]
+ public void UpdateOrAdd()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.UpdateOrAdd(p));
+ Assert.AreEqual(3, lst[3].key);
+ Assert.AreEqual(78, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 79);
+ Assert.IsFalse(lst.UpdateOrAdd(p));
+ Assert.AreEqual(13, lst[10].key);
+ Assert.AreEqual(79, lst[10].value);
+ }
+
+
+ [Test]
+ public void RemoveWithReturn()
+ {
+ KeyValuePair<int,int> p = new KeyValuePair<int,int>(3, 78);
+
+ Assert.IsTrue(lst.RemoveWithReturn(ref p));
+ Assert.AreEqual(3, p.key);
+ Assert.AreEqual(33, p.value);
+ Assert.AreEqual(4, lst[3].key);
+ Assert.AreEqual(34, lst[3].value);
+ p = new KeyValuePair<int,int>(13, 78);
+ Assert.IsFalse(lst.RemoveWithReturn(ref p));
+ }
+ }
+
+
+ [TestFixture]
+ public class Ranges
+ {
+ private TreeSet<int> tree;
+
+ private IComparer<int> c;
+
+
+ [SetUp]
+ public void Init()
+ {
+ c = new IC();
+ tree = new TreeSet<int>(c);
+ for (int i = 1; i <= 10; i++)
+ {
+ tree.Add(i * 2);
+ }
+ }
+
+
+ [Test]
+ public void Enumerator()
+ {
+ MSG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
+ int i = 3;
+
+ while (e.MoveNext())
+ {
+ Assert.AreEqual(2 * i++, e.Current);
+ }
+
+ Assert.AreEqual(9, i);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void Enumerator2()
+ {
+ MSG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
+ int i = e.Current;
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException))]
+ public void Enumerator3()
+ {
+ MSG.IEnumerator<int> e = tree.RangeFromTo(5, 17).GetEnumerator();
+
+ while (e.MoveNext());
+
+ int i = e.Current;
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
+
+ tree.RemoveRangeFrom(18);
+ Assert.IsTrue(IC.eq(tree, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
+ tree.RemoveRangeFrom(28);
+ Assert.IsTrue(IC.eq(tree, new int[] { 2, 4, 6, 8, 10, 12, 14, 16 }));
+ tree.RemoveRangeFrom(2);
+ Assert.IsTrue(IC.eq(tree));
+ foreach (int i in all) tree.Add(i);
+
+ tree.RemoveRangeTo(10);
+ Assert.IsTrue(IC.eq(tree, new int[] { 10, 12, 14, 16, 18, 20 }));
+ tree.RemoveRangeTo(2);
+ Assert.IsTrue(IC.eq(tree, new int[] { 10, 12, 14, 16, 18, 20 }));
+ tree.RemoveRangeTo(21);
+ Assert.IsTrue(IC.eq(tree));
+ foreach (int i in all) tree.Add(i);
+
+ tree.RemoveRangeFromTo(4, 8);
+ Assert.IsTrue(IC.eq(tree, 2, 8, 10, 12, 14, 16, 18, 20));
+ tree.RemoveRangeFromTo(14, 28);
+ Assert.IsTrue(IC.eq(tree, 2, 8, 10, 12));
+ tree.RemoveRangeFromTo(0, 9);
+ Assert.IsTrue(IC.eq(tree, 10, 12));
+ tree.RemoveRangeFromTo(0, 81);
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+ [Test]
+ public void Normal()
+ {
+ int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
+
+ Assert.IsTrue(IC.eq(tree, all));
+ Assert.IsTrue(IC.eq(tree.RangeAll(), all));
+ Assert.AreEqual(10, tree.RangeAll().Count);
+ Assert.IsTrue(IC.eq(tree.RangeFrom(11), new int[] { 12, 14, 16, 18, 20 }));
+ Assert.AreEqual(5, tree.RangeFrom(11).Count);
+ Assert.IsTrue(IC.eq(tree.RangeFrom(12), new int[] { 12, 14, 16, 18, 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(2), all));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(1), all));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(21), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(20), new int[] { 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(8), new int[] { 2, 4, 6 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(7), new int[] { 2, 4, 6 }));
+ Assert.AreEqual(3, tree.RangeTo(7).Count);
+ Assert.IsTrue(IC.eq(tree.RangeTo(2), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(1), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(3), new int[] { 2 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(20), new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(21), all));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12), new int[] { 8, 10 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11), new int[] { 6, 8, 10 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12), new int[] { 2, 4, 6, 8, 10 }));
+ Assert.AreEqual(5, tree.RangeFromTo(1, 12).Count);
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(2, 12), new int[] { 2, 4, 6, 8, 10 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21), new int[] { 6, 8, 10, 12, 14, 16, 18, 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20), new int[] { 6, 8, 10, 12, 14, 16, 18 }));
+ }
+
+
+ [Test]
+ public void Backwards()
+ {
+ int[] all = new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
+ int[] lla = new int[] { 20, 18, 16, 14, 12, 10, 8, 6, 4, 2 };
+
+ Assert.IsTrue(IC.eq(tree, all));
+ Assert.IsTrue(IC.eq(tree.RangeAll().Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(11).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(12).Backwards(), new int[] { 20, 18, 16, 14, 12 }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(2).Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(1).Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(21).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeFrom(20).Backwards(), new int[] { 20 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(8).Backwards(), new int[] { 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(7).Backwards(), new int[] { 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(2).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(1).Backwards(), new int[] { }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(3).Backwards(), new int[] { 2 }));
+ Assert.IsTrue(IC.eq(tree.RangeTo(20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6, 4, 2}));
+ Assert.IsTrue(IC.eq(tree.RangeTo(21).Backwards(), lla));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(7, 12).Backwards(), new int[] { 10, 8 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 11).Backwards(), new int[] { 10, 8, 6 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(1, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(2, 12).Backwards(), new int[] { 10, 8, 6, 4, 2 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 21).Backwards(), new int[] { 20, 18, 16, 14, 12, 10, 8, 6 }));
+ Assert.IsTrue(IC.eq(tree.RangeFromTo(6, 20).Backwards(), new int[] { 18, 16, 14, 12, 10, 8, 6 }));
+ }
+
+ [Test]
+ public void Direction()
+ {
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFrom(20).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeTo(7).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeFromTo(1, 12).Direction);
+ Assert.AreEqual(EnumerationDirection.Forwards, tree.RangeAll().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFrom(20).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeTo(7).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeFromTo(1, 12).Backwards().Direction);
+ Assert.AreEqual(EnumerationDirection.Backwards, tree.RangeAll().Backwards().Direction);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ c = null;
+ }
+ }
+
+ [TestFixture]
+ public class BagItf
+ {
+ private TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 10);
+ }
+ }
+
+
+ [Test]
+ public void Both()
+ {
+ Assert.AreEqual(0, tree.ContainsCount(7));
+ Assert.AreEqual(1, tree.ContainsCount(10));
+ tree.RemoveAllCopies(10);
+ Assert.AreEqual(0, tree.ContainsCount(10));
+ tree.RemoveAllCopies(7);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+ [TestFixture]
+ public class Div
+ {
+ private TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 10);
+ }
+ }
+
+
+ [Test]
+ public void NoDuplicates()
+ {
+ Assert.IsFalse(tree.AllowsDuplicates);
+ loadup();
+ Assert.IsFalse(tree.AllowsDuplicates);
+ }
+
+ [Test]
+ public void Add()
+ {
+ Assert.IsTrue(tree.Add(17));
+ Assert.IsFalse(tree.Add(17));
+ Assert.IsTrue(tree.Add(18));
+ Assert.IsFalse(tree.Add(18));
+ Assert.AreEqual(2, tree.Count);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+ [TestFixture]
+ public class FindOrAdd
+ {
+ private TreeSet<KeyValuePair<int,string>> bag;
+
+
+ [SetUp]
+ public void Init()
+ {
+ bag = new TreeSet<KeyValuePair<int,string>>(new KeyValuePairComparer<int,string>(new IC()));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ bag = null;
+ }
+
+
+ [Test]
+ public void Test()
+ {
+ KeyValuePair<int,string> p = new KeyValuePair<int,string>(3, "tre");
+
+ Assert.IsFalse(bag.FindOrAdd(ref p));
+ p.value = "drei";
+ Assert.IsTrue(bag.FindOrAdd(ref p));
+ Assert.AreEqual("tre", p.value);
+ p.value = "three";
+ Assert.AreEqual(1, bag.ContainsCount(p));
+ Assert.AreEqual("tre", bag[0].value);
+ }
+ }
+
+
+
+ [TestFixture]
+ public class ArrayTest
+ {
+ private TreeSet<int> tree;
+
+ int[] a;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ a = new int[10];
+ for (int i = 0; i < 10; i++)
+ a[i] = 1000 + i;
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+
+
+ private string aeq(int[] a, params int[] b)
+ {
+ if (a.Length != b.Length)
+ return "Lengths differ: " + a.Length + " != " + b.Length;
+
+ for (int i = 0; i < a.Length; i++)
+ if (a[i] != b[i])
+ return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
+
+ return "Alles klar";
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ Assert.AreEqual("Alles klar", aeq(tree.ToArray()));
+ tree.Add(7);
+ tree.Add(4);
+ Assert.AreEqual("Alles klar", aeq(tree.ToArray(), 4, 7));
+ }
+
+
+ [Test]
+ public void CopyTo()
+ {
+ tree.CopyTo(a, 1);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ tree.Add(6);
+ tree.CopyTo(a, 2);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
+ tree.Add(4);
+ tree.Add(9);
+ tree.CopyTo(a, 4);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 1009));
+ tree.Clear();
+ tree.Add(7);
+ tree.CopyTo(a, 9);
+ Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 4, 6, 9, 1007, 1008, 7));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToBad()
+ {
+ tree.Add(3);
+ tree.CopyTo(a, 10);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void CopyToBad2()
+ {
+ tree.CopyTo(a, -1);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CopyToTooFar()
+ {
+ tree.Add(3);
+ tree.Add(4);
+ tree.CopyTo(a, 9);
+ }
+ }
+
+
+
+
+ [TestFixture]
+ public class Remove
+ {
+ private TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 10);
+ }
+ }
+
+
+ [Test]
+ public void SmallTrees()
+ {
+ tree.Clear();
+ tree.Add(7);
+ tree.Add(9);
+ Assert.IsTrue(tree.Remove(7));
+ Assert.IsTrue(tree.Check(""));
+ }
+
+
+ [Test]
+ public void ByIndex()
+ {
+ //Remove root!
+ int n = tree.Count;
+ int i = tree[10];
+
+ tree.RemoveAt(10);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 1, tree.Count);
+
+ //Low end
+ i = tree.FindMin();
+ tree.RemoveAt(0);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 2, tree.Count);
+
+ //high end
+ i = tree.FindMax();
+ tree.RemoveAt(tree.Count - 1);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 3, tree.Count);
+
+ //Some leaf
+ i = 18;
+ tree.RemoveAt(7);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(i));
+ Assert.AreEqual(n - 4, tree.Count);
+ }
+
+
+ [Test]
+ public void AlmostEmpty()
+ {
+ //Almost empty
+ tree.Clear();
+ tree.Add(3);
+ tree.RemoveAt(0);
+ Assert.IsTrue(tree.Check(""));
+ Assert.IsFalse(tree.Contains(3));
+ Assert.AreEqual(0, tree.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void Empty()
+ {
+ tree.Clear();
+ tree.RemoveAt(0);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void HighIndex()
+ {
+ tree.RemoveAt(tree.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException), "Index out of range for sequenced collection")]
+ public void LowIndex()
+ {
+ tree.RemoveAt(-1);
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ Assert.IsFalse(tree.Remove(-20));
+
+ //No demote case, with move_item
+ Assert.IsTrue(tree.Remove(20));
+ Assert.IsTrue(tree.Check("T1"));
+ Assert.IsFalse(tree.Remove(20));
+
+ //plain case 2
+ Assert.IsTrue(tree.Remove(14));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //case 1b
+ Assert.IsTrue(tree.Remove(25));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //case 1c
+ Assert.IsTrue(tree.Remove(29));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //1a (terminating)
+ Assert.IsTrue(tree.Remove(10));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //2+1b
+ Assert.IsTrue(tree.Remove(12));
+ Assert.IsTrue(tree.Remove(11));
+
+ //1a+1b
+ Assert.IsTrue(tree.Remove(18));
+ Assert.IsTrue(tree.Remove(13));
+ Assert.IsTrue(tree.Remove(15));
+
+ //2+1c
+ for (int i = 0; i < 10; i++)
+ tree.Add(50 - 2 * i);
+
+ Assert.IsTrue(tree.Remove(42));
+ Assert.IsTrue(tree.Remove(38));
+ Assert.IsTrue(tree.Remove(28));
+ Assert.IsTrue(tree.Remove(40));
+
+ //
+ Assert.IsTrue(tree.Remove(16));
+ Assert.IsTrue(tree.Remove(23));
+ Assert.IsTrue(tree.Remove(17));
+ Assert.IsTrue(tree.Remove(19));
+ Assert.IsTrue(tree.Remove(50));
+ Assert.IsTrue(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(21));
+ Assert.IsTrue(tree.Remove(22));
+ Assert.IsTrue(tree.Remove(24));
+ for (int i = 0; i < 48; i++)
+ tree.Remove(i);
+
+ //Almost empty tree:
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(48));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+
+ //Empty tree:
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class PredecessorStructure
+ {
+ private TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ for (int i = 0; i < 20; i++)
+ tree.Add(2 * i);
+ }
+
+
+ [Test]
+ public void Predecessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.Predecessor(7));
+ Assert.AreEqual(6, tree.Predecessor(8));
+
+ //The bottom
+ Assert.AreEqual(0, tree.Predecessor(1));
+
+ //The top
+ Assert.AreEqual(38, tree.Predecessor(39));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was -2.")]
+ public void PredecessorTooLow1()
+ {
+ tree.Predecessor(-2);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was 0.")]
+ public void PredecessorTooLow2()
+ {
+ tree.Predecessor(0);
+ }
+
+
+ [Test]
+ public void WeakPredecessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.WeakPredecessor(7));
+ Assert.AreEqual(8, tree.WeakPredecessor(8));
+
+ //The bottom
+ Assert.AreEqual(0, tree.WeakPredecessor(1));
+ Assert.AreEqual(0, tree.WeakPredecessor(0));
+
+ //The top
+ Assert.AreEqual(38, tree.WeakPredecessor(39));
+ Assert.AreEqual(38, tree.WeakPredecessor(38));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Below minimum of set\r\nParameter name: item\r\nActual value was -2.")]
+ public void WeakPredecessorTooLow1()
+ {
+ tree.WeakPredecessor(-2);
+ }
+
+
+ [Test]
+ public void Successor()
+ {
+ loadup();
+ Assert.AreEqual(8, tree.Successor(7));
+ Assert.AreEqual(10, tree.Successor(8));
+
+ //The bottom
+ Assert.AreEqual(2, tree.Successor(0));
+ Assert.AreEqual(0, tree.Successor(-1));
+
+ //The top
+ Assert.AreEqual(38, tree.Successor(37));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 38.")]
+ public void SuccessorTooHigh1()
+ {
+ tree.Successor(38);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 39.")]
+ public void SuccessorTooHigh2()
+ {
+ tree.Successor(39);
+ }
+
+
+ [Test]
+ public void WeakSuccessor()
+ {
+ loadup();
+ Assert.AreEqual(6, tree.WeakSuccessor(6));
+ Assert.AreEqual(8, tree.WeakSuccessor(7));
+
+ //The bottom
+ Assert.AreEqual(0, tree.WeakSuccessor(-1));
+ Assert.AreEqual(0, tree.WeakSuccessor(0));
+
+ //The top
+ Assert.AreEqual(38, tree.WeakSuccessor(37));
+ Assert.AreEqual(38, tree.WeakSuccessor(38));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException), "Above maximum of set\r\nParameter name: item\r\nActual value was 39.")]
+ public void WeakSuccessorTooHigh1()
+ {
+ tree.WeakSuccessor(39);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class PriorityQueue
+ {
+ private TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ }
+
+
+ private void loadup()
+ {
+ foreach (int i in new int[] { 1, 2, 3, 4 })
+ tree.Add(i);
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ loadup();
+ Assert.AreEqual(1, tree.FindMin());
+ Assert.AreEqual(4, tree.FindMax());
+ Assert.AreEqual(1, tree.DeleteMin());
+ Assert.AreEqual(4, tree.DeleteMax());
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.AreEqual(2, tree.FindMin());
+ Assert.AreEqual(3, tree.FindMax());
+ Assert.AreEqual(2, tree.DeleteMin());
+ Assert.AreEqual(3, tree.DeleteMax());
+ Assert.IsTrue(tree.Check("Normal test 2"), "Bad tree");
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty1()
+ {
+ tree.FindMin();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty2()
+ {
+ tree.FindMax();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty3()
+ {
+ tree.DeleteMin();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Priority queue is empty")]
+ public void Empty4()
+ {
+ tree.DeleteMax();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IndexingAndCounting
+ {
+ private TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ }
+
+
+ private void populate()
+ {
+ tree.Add(30);
+ tree.Add(50);
+ tree.Add(10);
+ tree.Add(70);
+ }
+
+
+ [Test]
+ public void ToArray()
+ {
+ populate();
+
+ int[] a = tree.ToArray();
+
+ Assert.AreEqual(4, a.Length);
+ Assert.AreEqual(10, a[0]);
+ Assert.AreEqual(30, a[1]);
+ Assert.AreEqual(50, a[2]);
+ Assert.AreEqual(70, a[3]);
+ }
+
+
+ [Test]
+ public void GoodIndex()
+ {
+ Assert.AreEqual(-1, tree.IndexOf(20));
+ Assert.AreEqual(-1, tree.LastIndexOf(20));
+ populate();
+ Assert.AreEqual(10, tree[0]);
+ Assert.AreEqual(30, tree[1]);
+ Assert.AreEqual(50, tree[2]);
+ Assert.AreEqual(70, tree[3]);
+ Assert.AreEqual(0, tree.IndexOf(10));
+ Assert.AreEqual(1, tree.IndexOf(30));
+ Assert.AreEqual(2, tree.IndexOf(50));
+ Assert.AreEqual(3, tree.IndexOf(70));
+ Assert.AreEqual(-1, tree.IndexOf(20));
+ Assert.AreEqual(-1, tree.IndexOf(0));
+ Assert.AreEqual(-1, tree.IndexOf(90));
+ Assert.AreEqual(0, tree.LastIndexOf(10));
+ Assert.AreEqual(1, tree.LastIndexOf(30));
+ Assert.AreEqual(2, tree.LastIndexOf(50));
+ Assert.AreEqual(3, tree.LastIndexOf(70));
+ Assert.AreEqual(-1, tree.LastIndexOf(20));
+ Assert.AreEqual(-1, tree.LastIndexOf(0));
+ Assert.AreEqual(-1, tree.LastIndexOf(90));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void IndexTooLarge()
+ {
+ populate();
+ Console.WriteLine(tree[4]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(IndexOutOfRangeException))]
+ public void IndexTooSmall()
+ {
+ populate();
+ Console.WriteLine(tree[-1]);
+ }
+
+
+ [Test]
+ public void FilledTreeOutsideInput()
+ {
+ populate();
+ Assert.AreEqual(0, tree.CountFrom(90));
+ Assert.AreEqual(0, tree.CountFromTo(-20, 0));
+ Assert.AreEqual(0, tree.CountFromTo(80, 100));
+ Assert.AreEqual(0, tree.CountTo(0));
+ Assert.AreEqual(4, tree.CountTo(90));
+ Assert.AreEqual(4, tree.CountFromTo(-20, 90));
+ Assert.AreEqual(4, tree.CountFrom(0));
+ }
+
+
+ [Test]
+ public void FilledTreeIntermediateInput()
+ {
+ populate();
+ Assert.AreEqual(3, tree.CountFrom(20));
+ Assert.AreEqual(1, tree.CountFromTo(20, 40));
+ Assert.AreEqual(2, tree.CountTo(40));
+ }
+
+
+ [Test]
+ public void FilledTreeMatchingInput()
+ {
+ populate();
+ Assert.AreEqual(3, tree.CountFrom(30));
+ Assert.AreEqual(2, tree.CountFromTo(30, 70));
+ Assert.AreEqual(0, tree.CountFromTo(50, 30));
+ Assert.AreEqual(0, tree.CountFromTo(50, 50));
+ Assert.AreEqual(0, tree.CountTo(10));
+ Assert.AreEqual(2, tree.CountTo(50));
+ }
+
+
+ [Test]
+ public void CountEmptyTree()
+ {
+ Assert.AreEqual(0, tree.CountFrom(20));
+ Assert.AreEqual(0, tree.CountFromTo(20, 40));
+ Assert.AreEqual(0, tree.CountTo(40));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ }
+ }
+
+
+
+
+ namespace ModificationCheck
+ {
+ [TestFixture]
+ public class Enumerator
+ {
+ private TreeSet<int> tree;
+
+ private MSG.IEnumerator<int> e;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ e = tree.GetEnumerator();
+ }
+
+
+ [Test]
+ public void CurrentAfterModification()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ Assert.AreEqual(0, e.Current);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterAdd()
+ {
+ tree.Add(34);
+ e.MoveNext();
+ }
+
+
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterRemove()
+ {
+ tree.Remove(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterClear()
+ {
+ tree.Clear();
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ e = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class RangeEnumerator
+ {
+ private TreeSet<int> tree;
+
+ private MSG.IEnumerator<int> e;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ e = tree.RangeFromTo(3, 7).GetEnumerator();
+ }
+
+
+ [Test]
+ public void CurrentAfterModification()
+ {
+ e.MoveNext();
+ tree.Add(34);
+ Assert.AreEqual(3, e.Current);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterAdd()
+ {
+ tree.Add(34);
+ e.MoveNext();
+ }
+
+
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterRemove()
+ {
+ tree.Remove(34);
+ e.MoveNext();
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Collection was modified")]
+ public void MoveNextAfterClear()
+ {
+ tree.Clear();
+ e.MoveNext();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ e = null;
+ }
+ }
+ }
+
+
+
+
+ namespace PathcopyPersistence
+ {
+ [TestFixture]
+ public class Navigation
+ {
+ private TreeSet<int> tree, snap;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeSet<int>(ic);
+ for (int i = 0; i <= 20; i++)
+ tree.Add(2 * i + 1);
+
+ snap = (TreeSet<int>)tree.Snapshot();
+ for (int i = 0; i <= 10; i++)
+ tree.Remove(4 * i + 1);
+ }
+
+
+ private bool twomodeleven(int i)
+ {
+ return i % 11 == 2;
+ }
+
+
+ [Test]
+ public void InternalEnum()
+ {
+ Assert.IsTrue(IC.eq(snap.FindAll(new Filter<int>(twomodeleven)), 13, 35));
+ }
+
+
+ public void MoreCut() { }
+
+ [Test]
+ public void Cut()
+ {
+ int lo, hi;
+ bool lv, hv;
+
+ Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(64), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(5, hi);
+ Assert.AreEqual(3, lo);
+ Assert.IsTrue(snap.Cut(new HigherOrder.CubeRoot(125), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(7, hi);
+ Assert.AreEqual(3, lo);
+ Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(125000), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && !hv);
+ Assert.AreEqual(41, lo);
+ Assert.IsFalse(snap.Cut(new HigherOrder.CubeRoot(-27), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(!lv && hv);
+ Assert.AreEqual(1, hi);
+ }
+
+
+ [Test]
+ public void Range()
+ {
+ Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 16), 5, 7, 9, 11, 13, 15));
+ Assert.IsTrue(IC.eq(snap.RangeFromTo(5, 17), 5, 7, 9, 11, 13, 15));
+ Assert.IsTrue(IC.eq(snap.RangeFromTo(6, 16), 7, 9, 11, 13, 15));
+ //Assert.AreEqual(snap.RangeFromTo(6, 16).Count, 5);
+ }
+
+
+ [Test]
+ public void Contains()
+ {
+ Assert.IsTrue(snap.Contains(5));
+ }
+
+
+ [Test]
+ public void FindMin()
+ {
+ Assert.AreEqual(1, snap.FindMin());
+ }
+
+
+ [Test]
+ public void FindMax()
+ {
+ Assert.AreEqual(41, snap.FindMax());
+ }
+
+
+ [Test]
+ public void Predecessor()
+ {
+ Assert.AreEqual(13, snap.Predecessor(15));
+ Assert.AreEqual(15, snap.Predecessor(16));
+ Assert.AreEqual(15, snap.Predecessor(17));
+ Assert.AreEqual(17, snap.Predecessor(18));
+ }
+
+
+ [Test]
+ public void Successor()
+ {
+ Assert.AreEqual(17, snap.Successor(15));
+ Assert.AreEqual(17, snap.Successor(16));
+ Assert.AreEqual(19, snap.Successor(17));
+ Assert.AreEqual(19, snap.Successor(18));
+ }
+
+
+ [Test]
+ public void WeakPredecessor()
+ {
+ Assert.AreEqual(15, snap.WeakPredecessor(15));
+ Assert.AreEqual(15, snap.WeakPredecessor(16));
+ Assert.AreEqual(17, snap.WeakPredecessor(17));
+ Assert.AreEqual(17, snap.WeakPredecessor(18));
+ }
+
+
+ [Test]
+ public void WeakSuccessor()
+ {
+ Assert.AreEqual(15, snap.WeakSuccessor(15));
+ Assert.AreEqual(17, snap.WeakSuccessor(16));
+ Assert.AreEqual(17, snap.WeakSuccessor(17));
+ Assert.AreEqual(19, snap.WeakSuccessor(18));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
+ public void CountTo()
+ {
+ int j = snap.CountTo(15);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
+ public void Indexing()
+ {
+ int j = snap[4];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(NotSupportedException), "Indexing not supported for snapshots")]
+ public void Indexing2()
+ {
+ int j = snap.IndexOf(5);
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ ic = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Single
+ {
+ private TreeSet<int> tree;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeSet<int>(ic);
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i + 1);
+ }
+
+
+ [Test]
+ public void EnumerationWithAdd()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ int i = 0;
+ TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
+
+ foreach (int j in snap)
+ {
+ Assert.AreEqual(1 + 2 * i++, j);
+ tree.Add(21 - j);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ }
+ }
+
+
+ [Test]
+ public void Remove()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
+
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ tree.Remove(19);
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ }
+
+
+ [Test]
+ public void RemoveNormal()
+ {
+ tree.Clear();
+ for (int i = 10; i < 20; i++)
+ {
+ tree.Add(i);
+ tree.Add(i + 10);
+ }
+
+ int[] orig = new int[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 };
+ TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
+
+ Assert.IsFalse(tree.Remove(-20));
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //No demote case, with move_item
+ Assert.IsTrue(tree.Remove(20));
+ Assert.IsTrue(tree.Check("T1"));
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsFalse(tree.Remove(20));
+
+ //plain case 2
+ tree.Snapshot();
+ Assert.IsTrue(tree.Remove(14));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //case 1b
+ Assert.IsTrue(tree.Remove(25));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //case 1c
+ Assert.IsTrue(tree.Remove(29));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //1a (terminating)
+ Assert.IsTrue(tree.Remove(10));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //2+1b
+ Assert.IsTrue(tree.Remove(12));
+ tree.Snapshot();
+ Assert.IsTrue(tree.Remove(11));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //1a+1b
+ Assert.IsTrue(tree.Remove(18));
+ Assert.IsTrue(tree.Remove(13));
+ Assert.IsTrue(tree.Remove(15));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //2+1c
+ for (int i = 0; i < 10; i++)
+ tree.Add(50 - 2 * i);
+
+ Assert.IsTrue(tree.Remove(42));
+ Assert.IsTrue(tree.Remove(38));
+ Assert.IsTrue(tree.Remove(28));
+ Assert.IsTrue(tree.Remove(40));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //
+ Assert.IsTrue(tree.Remove(16));
+ Assert.IsTrue(tree.Remove(23));
+ Assert.IsTrue(tree.Remove(17));
+ Assert.IsTrue(tree.Remove(19));
+ Assert.IsTrue(tree.Remove(50));
+ Assert.IsTrue(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(21));
+ Assert.IsTrue(tree.Remove(22));
+ Assert.IsTrue(tree.Remove(24));
+ for (int i = 0; i < 48; i++)
+ tree.Remove(i);
+
+ //Almost empty tree:
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Remove(48));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+
+ //Empty tree:
+ Assert.IsFalse(tree.Remove(26));
+ Assert.IsTrue(tree.Check("Normal test 1"), "Bad tree");
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ }
+
+
+ [Test]
+ public void Add()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
+
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ tree.Add(10);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ tree.Add(16);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+
+ //Promote+zigzig
+ tree.Add(40);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ for (int i = 1; i < 4; i++)
+ tree.Add(40 - 2 * i);
+
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+
+ //Zigzag:
+ tree.Add(32);
+ Assert.IsTrue(snap.Check("M"), "Bad snap!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ }
+
+
+ [Test]
+ public void Clear()
+ {
+ int[] orig = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
+ TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
+
+ tree.Clear();
+ Assert.IsTrue(snap.Check("Snap"), "Bad snap!");
+ Assert.IsTrue(tree.Check("Tree"), "Bad tree!");
+ Assert.IsTrue(IC.eq(snap, orig), "Snap was changed!");
+ Assert.AreEqual(0, tree.Count);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(InvalidOperationException), "Cannot snapshot a snapshot")]
+ public void SnapSnap()
+ {
+ TreeSet<int> snap = (TreeSet<int>)tree.Snapshot();
+
+ snap.Snapshot();
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ ic = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class Multiple
+ {
+ private TreeSet<int> tree;
+
+ private IComparer<int> ic;
+
+
+ private bool eq(MSG.IEnumerable<int> me, int[] that)
+ {
+ int i = 0, maxind = that.Length - 1;
+
+ foreach (int item in me)
+ if (i > maxind || ic.Compare(item, that[i++]) != 0)
+ return false;
+
+ return true;
+ }
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeSet<int>(ic);
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i + 1);
+ }
+
+
+ [Test]
+ public void First()
+ {
+ TreeSet<int>[] snaps = new TreeSet<int>[10];
+
+ for (int i = 0; i < 10; i++)
+ {
+ snaps[i] = (TreeSet<int>)(tree.Snapshot());
+ tree.Add(2 * i);
+ }
+
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.AreEqual(i + 10, snaps[i].Count);
+ }
+
+ snaps[5] = null;
+ snaps[9] = null;
+ GC.Collect();
+ snaps[8].Dispose();
+ tree.Remove(14);
+
+ int[] res = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19 };
+ int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
+ int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
+
+ Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
+ Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
+ Assert.IsTrue(IC.eq(tree, res));
+ Assert.IsTrue(tree.Check("B"));
+ Assert.IsTrue(snaps[3].Check("B"));
+ Assert.IsTrue(snaps[7].Check("B"));
+ }
+
+
+ [Test]
+ public void CollectingTheMaster()
+ {
+ TreeSet<int>[] snaps = new TreeSet<int>[10];
+
+ for (int i = 0; i < 10; i++)
+ {
+ snaps[i] = (TreeSet<int>)(tree.Snapshot());
+ tree.Add(2 * i);
+ }
+
+ tree = null;
+ GC.Collect();
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.AreEqual(i + 10, snaps[i].Count);
+ }
+
+ snaps[5] = null;
+ snaps[9] = null;
+ GC.Collect();
+ snaps[8].Dispose();
+
+ int[] snap7 = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 19 };
+ int[] snap3 = new int[] { 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19 };
+
+ Assert.IsTrue(IC.eq(snaps[3], snap3), "Snap 3 was changed!");
+ Assert.IsTrue(IC.eq(snaps[7], snap7), "Snap 7 was changed!");
+ Assert.IsTrue(snaps[3].Check("B"));
+ Assert.IsTrue(snaps[7].Check("B"));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ tree = null;
+ ic = null;
+ }
+ }
+ }
+
+
+
+
+ namespace HigherOrder
+ {
+ internal class CubeRoot: IComparable<int>
+ {
+ private int c;
+
+
+ internal CubeRoot(int c) { this.c = c; }
+
+
+ public int CompareTo(int that) { return c - that * that * that; }
+ public bool Equals(int that) { return c == that * that * that; }
+ }
+
+
+
+ class Interval: IComparable<int>
+ {
+ private int b, t;
+
+
+ internal Interval(int b, int t) { this.b = b; this.t = t; }
+
+
+ public int CompareTo(int that) { return that < b ? 1 : that > t ? -1 : 0; }
+ public bool Equals(int that) { return that >= b && that <= t; }
+ }
+
+
+
+ [TestFixture]
+ public class Simple
+ {
+ private TreeSet<int> tree;
+
+ private IComparer<int> ic;
+
+
+ [SetUp]
+ public void Init()
+ {
+ ic = new IC();
+ tree = new TreeSet<int>(ic);
+ }
+
+
+ private bool never(int i) { return false; }
+
+
+ private bool always(int i) { return true; }
+
+
+ private bool even(int i) { return i % 2 == 0; }
+
+
+ private string themap(int i) { return String.Format("AA {0,4} BB", i); }
+
+
+ private string badmap(int i) { return String.Format("AA {0} BB", i); }
+
+
+ private int appfield1;
+
+ private int appfield2;
+
+
+ private void apply(int i) { appfield1++; appfield2 += i * i; }
+
+
+ [Test]
+ public void Apply()
+ {
+ Simple simple1 = new Simple();
+
+ tree.Apply(new Applier<int>(simple1.apply));
+ Assert.AreEqual(0, simple1.appfield1);
+ Assert.AreEqual(0, simple1.appfield2);
+
+ Simple simple2 = new Simple();
+
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ tree.Apply(new Applier<int>(simple2.apply));
+ Assert.AreEqual(10, simple2.appfield1);
+ Assert.AreEqual(285, simple2.appfield2);
+ }
+
+
+ [Test]
+ public void All()
+ {
+ Assert.IsTrue(tree.All(new Filter<int>(never)));
+ Assert.IsTrue(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ Assert.IsFalse(tree.All(new Filter<int>(never)));
+ Assert.IsFalse(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2);
+
+ Assert.IsFalse(tree.All(new Filter<int>(never)));
+ Assert.IsTrue(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1);
+
+ Assert.IsFalse(tree.All(new Filter<int>(never)));
+ Assert.IsFalse(tree.All(new Filter<int>(even)));
+ Assert.IsTrue(tree.All(new Filter<int>(always)));
+ }
+
+
+ [Test]
+ public void Exists()
+ {
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsFalse(tree.Exists(new Filter<int>(even)));
+ Assert.IsFalse(tree.Exists(new Filter<int>(always)));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(even)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2);
+
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(even)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(always)));
+ tree.Clear();
+ for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1);
+
+ Assert.IsFalse(tree.Exists(new Filter<int>(never)));
+ Assert.IsFalse(tree.Exists(new Filter<int>(even)));
+ Assert.IsTrue(tree.Exists(new Filter<int>(always)));
+ }
+
+
+ [Test]
+ public void FindAll()
+ {
+ Assert.AreEqual(0, tree.FindAll(new Filter<int>(never)).Count);
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ Assert.AreEqual(0, tree.FindAll(new Filter<int>(never)).Count);
+ Assert.AreEqual(10, tree.FindAll(new Filter<int>(always)).Count);
+ Assert.AreEqual(5, tree.FindAll(new Filter<int>(even)).Count);
+ Assert.IsTrue(((TreeSet<int>)tree.FindAll(new Filter<int>(even))).Check("R"));
+ }
+
+
+ [Test]
+ public void Map()
+ {
+ Assert.AreEqual(0, tree.Map(new Mapper<int,string>(themap), new SC()).Count);
+ for (int i = 0; i < 11; i++)
+ tree.Add(i * i * i);
+
+ IIndexedSorted<string> res = tree.Map(new Mapper<int,string>(themap), new SC());
+
+ Assert.IsTrue(((TreeSet<string>)res).Check("R"));
+ Assert.AreEqual(11, res.Count);
+ Assert.AreEqual("AA 0 BB", res[0]);
+ Assert.AreEqual("AA 27 BB", res[3]);
+ Assert.AreEqual("AA 125 BB", res[5]);
+ Assert.AreEqual("AA 1000 BB", res[10]);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "mapper not monotonic")]
+ public void BadMap()
+ {
+ for (int i = 0; i < 11; i++)
+ tree.Add(i * i * i);
+
+ ISorted<string> res = tree.Map(new Mapper<int,string>(badmap), new SC());
+ }
+
+
+ [Test]
+ public void Cut()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ int low, high;
+ bool lval, hval;
+
+ Assert.IsTrue(tree.Cut(new CubeRoot(27), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(2, low);
+ Assert.IsFalse(tree.Cut(new CubeRoot(30), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(3, low);
+ }
+
+
+ [Test]
+ public void CutInt()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i);
+
+ int low, high;
+ bool lval, hval;
+
+ Assert.IsFalse(tree.Cut(new IC(3), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(4, high);
+ Assert.AreEqual(2, low);
+ Assert.IsTrue(tree.Cut(new IC(6), out low, out lval, out high, out hval));
+ Assert.IsTrue(lval && hval);
+ Assert.AreEqual(8, high);
+ Assert.AreEqual(4, low);
+ }
+
+
+ [Test]
+ public void CutInterval()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(2 * i);
+
+ int lo, hi;
+ bool lv, hv;
+
+ Assert.IsTrue(tree.Cut(new Interval(5, 9), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(10, hi);
+ Assert.AreEqual(4, lo);
+ Assert.IsTrue(tree.Cut(new Interval(6, 10), out lo, out lv, out hi, out hv));
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(12, hi);
+ Assert.AreEqual(4, lo);
+ for (int i = 0; i < 100; i++)
+ tree.Add(2 * i);
+
+ tree.Cut(new Interval(77, 105), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(106, hi);
+ Assert.AreEqual(76, lo);
+ tree.Cut(new Interval(5, 7), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(8, hi);
+ Assert.AreEqual(4, lo);
+ tree.Cut(new Interval(80, 110), out lo, out lv, out hi, out hv);
+ Assert.IsTrue(lv && hv);
+ Assert.AreEqual(112, hi);
+ Assert.AreEqual(78, lo);
+ }
+
+
+ [Test]
+ public void UpperCut()
+ {
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ int l, h;
+ bool lv, hv;
+
+ Assert.IsFalse(tree.Cut(new CubeRoot(1000), out l, out lv, out h, out hv));
+ Assert.IsTrue(lv && !hv);
+ Assert.AreEqual(9, l);
+ Assert.IsFalse(tree.Cut(new CubeRoot(-50), out l, out lv, out h, out hv));
+ Assert.IsTrue(!lv && hv);
+ Assert.AreEqual(0, h);
+ }
+
+
+ [TearDown]
+ public void Dispose() { ic = null; tree = null; }
+ }
+ }
+
+
+
+
+ namespace MultiOps
+ {
+ [TestFixture]
+ public class AddAll
+ {
+ private int sqr(int i) { return i * i; }
+
+
+ TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init() { tree = new TreeSet<int>(new IC()); }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ tree.AddAll(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ [Test]
+ public void SomeEmpty()
+ {
+ for (int i = 4; i < 9; i++) tree.Add(i);
+
+ tree.AddAll(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(5, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ [Test]
+ public void EmptySome()
+ {
+ tree.AddAll(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree[0]);
+ Assert.AreEqual(1, tree[1]);
+ Assert.AreEqual(4, tree[2]);
+ Assert.AreEqual(9, tree[3]);
+ }
+
+
+ [Test]
+ public void SomeSome()
+ {
+ for (int i = 5; i < 9; i++) tree.Add(i);
+
+ tree.AddAll(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 4, 5, 6, 7, 8, 9));
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+
+
+
+ [TestFixture]
+ public class AddSorted
+ {
+ private int sqr(int i) { return i * i; }
+
+
+ private int bad(int i) { return i * (5 - i); }
+
+
+ TreeSet<int> tree;
+
+
+ [SetUp]
+ public void Init() { tree = new TreeSet<int>(new IC()); }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ tree.AddSorted(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+
+ [Test]
+ public void SomeEmpty()
+ {
+ for (int i = 4; i < 9; i++) tree.Add(i);
+
+ tree.AddSorted(new FunEnumerable(0, new Int2Int(sqr)));
+ Assert.AreEqual(5, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+
+ [Test]
+ public void EmptySome()
+ {
+ tree.AddSorted(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(4, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree[0]);
+ Assert.AreEqual(1, tree[1]);
+ Assert.AreEqual(4, tree[2]);
+ Assert.AreEqual(9, tree[3]);
+ }
+
+
+
+ [Test]
+ public void SomeSome()
+ {
+ for (int i = 5; i < 9; i++) tree.Add(i);
+
+ tree.AddSorted(new FunEnumerable(4, new Int2Int(sqr)));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 4, 5, 6, 7, 8, 9));
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException), "Argument not sorted")]
+ public void EmptyBad()
+ {
+ tree.AddSorted(new FunEnumerable(9, new Int2Int(bad)));
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+
+ [TestFixture]
+ public class Rest
+ {
+ TreeSet<int> tree, tree2;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ tree2 = new TreeSet<int>(new IC());
+ for (int i = 0; i < 10; i++)
+ tree.Add(i);
+
+ for (int i = 0; i < 10; i++)
+ tree2.Add(2 * i);
+ }
+
+
+ [Test]
+ public void RemoveAll()
+ {
+ tree.RemoveAll(tree2.RangeFromTo(3, 7));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
+ tree.RemoveAll(tree2.RangeFromTo(3, 7));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
+ tree.RemoveAll(tree2.RangeFromTo(13, 17));
+ Assert.AreEqual(8, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 8, 9));
+ tree.RemoveAll(tree2.RangeFromTo(3, 17));
+ Assert.AreEqual(7, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 3, 5, 7, 9));
+ for (int i = 0; i < 10; i++) tree2.Add(i);
+
+ tree.RemoveAll(tree2.RangeFromTo(-1, 10));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+
+ [Test]
+ public void RetainAll()
+ {
+ tree.RetainAll(tree2.RangeFromTo(3, 17));
+ Assert.AreEqual(3, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 4, 6, 8));
+ tree.RetainAll(tree2.RangeFromTo(1, 17));
+ Assert.AreEqual(3, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 4, 6, 8));
+ tree.RetainAll(tree2.RangeFromTo(3, 5));
+ Assert.AreEqual(1, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree, 4));
+ tree.RetainAll(tree2.RangeFromTo(7, 17));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ tree.RetainAll(tree2.RangeFromTo(5, 5));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ for (int i = 0; i < 10; i++) tree.Add(i);
+
+ tree.RetainAll(tree2.RangeFromTo(15, 25));
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(tree.Check());
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+
+ [Test]
+ public void ContainsAll()
+ {
+ Assert.IsFalse(tree.ContainsAll(tree2));
+ Assert.IsTrue(tree.ContainsAll(tree));
+ tree2.Clear();
+ Assert.IsTrue(tree.ContainsAll(tree2));
+ tree.Clear();
+ Assert.IsTrue(tree.ContainsAll(tree2));
+ tree2.Add(8);
+ Assert.IsFalse(tree.ContainsAll(tree2));
+ }
+
+
+ [Test]
+ public void RemoveInterval()
+ {
+ tree.RemoveInterval(3, 4);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(6, tree.Count);
+ Assert.IsTrue(IC.eq(tree, 0, 1, 2, 7, 8, 9));
+ tree.RemoveInterval(2, 3);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(3, tree.Count);
+ Assert.IsTrue(IC.eq(tree, 0, 1, 9));
+ tree.RemoveInterval(0, 3);
+ Assert.IsTrue(tree.Check());
+ Assert.AreEqual(0, tree.Count);
+ Assert.IsTrue(IC.eq(tree));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RemoveRangeBad1()
+ {
+ tree.RemoveInterval(-3, 8);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void RemoveRangeBad2()
+ {
+ tree.RemoveInterval(3, -8);
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void RemoveRangeBad3()
+ {
+ tree.RemoveInterval(3, 8);
+ }
+
+
+ [Test]
+ public void GetRange()
+ {
+ MSG.IEnumerable<int> e = tree[3, 6];
+
+ Assert.IsTrue(IC.eq(e, 3, 4, 5));
+ e = tree[3, 3];
+ Assert.IsTrue(IC.eq(e));
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetRangeBad1()
+ {
+ object foo = tree[-3, 0];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetRangeBad2()
+ {
+ object foo = tree[3, 2];
+ }
+
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void GetRangeBad3()
+ {
+ object foo = tree[3, 11];
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; tree2 = null; }
+ }
+ }
+
+
+
+
+ namespace Sync
+ {
+ [TestFixture]
+ public class SyncRoot
+ {
+ private TreeSet<int> tree;
+
+ int sz = 5000;
+
+
+ [Test]
+ public void Safe()
+ {
+ System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(safe1));
+ System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(safe2));
+
+ t1.Start();
+ t2.Start();
+ t1.Join();
+ t2.Join();
+ Assert.AreEqual(2 * sz + 1, tree.Count);
+ Assert.IsTrue(tree.Check());
+ }
+
+
+ //[Test]
+ public void UnSafe()
+ {
+ bool bad = false;
+
+ for (int i = 0; i < 10; i++)
+ {
+ System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
+ System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
+
+ t1.Start();
+ t2.Start();
+ t1.Join();
+ t2.Join();
+ if (bad = 2 * sz + 1 != tree.Count)
+ {
+ Console.WriteLine("{0}::Unsafe(): bad at {1}", GetType(), i);
+ break;
+ }
+ }
+
+ Assert.IsTrue(bad, "No sync problems!");
+ }
+
+
+ [Test]
+ public void SafeUnSafe()
+ {
+ System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe1));
+ System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ThreadStart(unsafe2));
+
+ t1.Start();
+ t1.Join();
+ t2.Start();
+ t2.Join();
+ Assert.AreEqual(2 * sz + 1, tree.Count);
+ }
+
+
+ [SetUp]
+ public void Init() { tree = new TreeSet<int>(new IC()); }
+
+
+ private void unsafe1()
+ {
+ for (int i = 0; i < 2 * sz; i++)
+ tree.Add(i * 2);
+
+ for (int i = 1; i < sz; i++)
+ tree.Remove(i * 4);
+ }
+
+
+ private void safe1()
+ {
+ for (int i = 0; i < 2 * sz; i++)
+ lock (tree.SyncRoot)
+ tree.Add(i * 2);
+
+ for (int i = 1; i < sz; i++)
+ lock (tree.SyncRoot)
+ tree.Remove(i * 4);
+ }
+
+
+ private void unsafe2()
+ {
+ for (int i = 2 * sz; i > 0; i--)
+ tree.Add(i * 2 + 1);
+
+ for (int i = sz; i > 0; i--)
+ tree.Remove(i * 4 + 1);
+ }
+
+
+ private void safe2()
+ {
+ for (int i = 2 * sz; i > 0; i--)
+ lock (tree.SyncRoot)
+ tree.Add(i * 2 + 1);
+
+ for (int i = sz; i > 0; i--)
+ lock (tree.SyncRoot)
+ tree.Remove(i * 4 + 1);
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+
+
+
+ //[TestFixture]
+ public class ConcurrentQueries
+ {
+ private TreeSet<int> tree;
+
+ int sz = 500000;
+
+
+ [SetUp]
+ public void Init()
+ {
+ tree = new TreeSet<int>(new IC());
+ for (int i = 0; i < sz; i++)
+ {
+ tree.Add(i);
+ }
+ }
+
+
+
+ class A
+ {
+ public int count = 0;
+
+ TreeSet<int> t;
+
+
+ public A(TreeSet<int> t) { this.t = t; }
+
+
+ public void a(int i) { count++; }
+
+
+ public void traverse() { t.Apply(new Applier<int>(a)); }
+ }
+
+
+
+
+ [Test]
+ public void Safe()
+ {
+ A a = new A(tree);
+
+ a.traverse();
+ Assert.AreEqual(sz, a.count);
+ }
+
+
+ [Test]
+ public void RegrettablyUnsafe()
+ {
+ System.Threading.Thread[] t = new System.Threading.Thread[10];
+ A[] a = new A[10];
+ for (int i = 0; i < 10; i++)
+ {
+ a[i] = new A(tree);
+ t[i] = new System.Threading.Thread(new System.Threading.ThreadStart(a[i].traverse));
+ }
+
+ for (int i = 0; i < 10; i++)
+ t[i].Start();
+ for (int i = 0; i < 10; i++)
+ t[i].Join();
+ for (int i = 0; i < 10; i++)
+ Assert.AreEqual(sz,a[i].count);
+
+ }
+
+
+ [TearDown]
+ public void Dispose() { tree = null; }
+ }
+ }
+
+
+
+
+ namespace Hashing
+ {
+ [TestFixture]
+ public class ISequenced
+ {
+ private ISequenced<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeSet<int>(new IC());
+ dat = new TreeSet<int>(new IC());
+ dut = new TreeSet<int>(new RevIC());
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval = retval * 31 + i;
+
+ return retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsFalse(dit.Equals(dut));
+ Assert.IsFalse(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+
+
+ [TestFixture]
+ public class IEditableCollection
+ {
+ private ICollection<int> dit, dat, dut;
+
+
+ [SetUp]
+ public void Init()
+ {
+ dit = new TreeSet<int>(new IC());
+ dat = new TreeSet<int>(new IC());
+ dut = new TreeSet<int>(new RevIC());
+ }
+
+
+ [Test]
+ public void EmptyEmpty()
+ {
+ Assert.IsTrue(dit.Equals(dat));
+ }
+
+
+ [Test]
+ public void EmptyNonEmpty()
+ {
+ dit.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ }
+
+
+ public int hasher(int count,params int[] items)
+ {
+ int retval = 0;
+
+ foreach (int i in items)
+ retval ^= i;
+
+ return (count<<16)+retval;
+ }
+
+
+ [Test]
+ public void HashVal()
+ {
+ Assert.AreEqual(hasher(0), dit.GetHashCode());
+ dit.Add(3);
+ Assert.AreEqual(hasher(1,3), dit.GetHashCode());
+ dit.Add(7);
+ Assert.AreEqual(hasher(2,3, 7), dit.GetHashCode());
+ Assert.AreEqual(hasher(0), dut.GetHashCode());
+ dut.Add(3);
+ Assert.AreEqual(hasher(1,3), dut.GetHashCode());
+ dut.Add(7);
+ Assert.AreEqual(hasher(2,7, 3), dut.GetHashCode());
+ }
+
+
+ [Test]
+ public void Normal()
+ {
+ dit.Add(3);
+ dit.Add(7);
+ dat.Add(3);
+ Assert.IsFalse(dit.Equals(dat));
+ Assert.IsFalse(dat.Equals(dit));
+ dat.Add(7);
+ Assert.IsTrue(dit.Equals(dat));
+ Assert.IsTrue(dat.Equals(dit));
+ }
+
+
+ [Test]
+ public void WrongOrder()
+ {
+ dit.Add(3);
+ dut.Add(3);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ dit.Add(7);
+ dut.Add(7);
+ Assert.IsTrue(dit.Equals(dut));
+ Assert.IsTrue(dut.Equals(dit));
+ }
+
+
+ [Test]
+ public void Reflexive()
+ {
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(3);
+ Assert.IsTrue(dit.Equals(dit));
+ dit.Add(7);
+ Assert.IsTrue(dit.Equals(dit));
+ }
+
+
+ [TearDown]
+ public void Dispose()
+ {
+ dit = null;
+ dat = null;
+ dut = null;
+ }
+ }
+
+ }
+} \ No newline at end of file