From d91cda984ce03930ee4ed38fdd8a5407592dafd8 Mon Sep 17 00:00:00 2001 From: Martin Baulig Date: Mon, 15 Nov 2004 00:12:52 +0000 Subject: 2004-11-15 Martin Baulig Importing version 0.5 of C5's test suite, http://www.itu.dk/research/c5/. svn path=/trunk/mcs/; revision=36110 --- mcs/class/Mono.C5/Test/.cvsignore | 3 + mcs/class/Mono.C5/Test/AssemblyInfo.cs | 79 + mcs/class/Mono.C5/Test/BasesTest.cs | 256 ++ mcs/class/Mono.C5/Test/ChangeLog | 4 + mcs/class/Mono.C5/Test/Sorting.cs | 268 ++ mcs/class/Mono.C5/Test/SupportClasses.cs | 156 ++ mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs | 2453 +++++++++++++++++ .../Mono.C5/Test/arrays/HashedArrayListTest.cs | 2004 ++++++++++++++ mcs/class/Mono.C5/Test/arrays/SortedArrayTests.cs | 2202 ++++++++++++++++ mcs/class/Mono.C5/Test/hashing/HashBagTests.cs | 464 ++++ .../Mono.C5/Test/hashing/HashDictionaryTests.cs | 250 ++ mcs/class/Mono.C5/Test/hashing/HashTableTests.cs | 1058 ++++++++ mcs/class/Mono.C5/Test/heaps/HeapTests.cs | 191 ++ .../Test/linkedlists/HashedLinkedListTest.cs | 2035 ++++++++++++++ .../Mono.C5/Test/linkedlists/LinkedListTest.cs | 2404 +++++++++++++++++ mcs/class/Mono.C5/Test/nunit.csproj | 127 + mcs/class/Mono.C5/Test/nunit.csproj.user | 58 + mcs/class/Mono.C5/Test/trees/Bag.cs | 2785 ++++++++++++++++++++ mcs/class/Mono.C5/Test/trees/Dictionary.cs | 264 ++ .../Mono.C5/Test/trees/RedBlackTreeSetTests.cs | 2709 +++++++++++++++++++ 20 files changed, 19770 insertions(+) create mode 100644 mcs/class/Mono.C5/Test/.cvsignore create mode 100644 mcs/class/Mono.C5/Test/AssemblyInfo.cs create mode 100644 mcs/class/Mono.C5/Test/BasesTest.cs create mode 100644 mcs/class/Mono.C5/Test/ChangeLog create mode 100644 mcs/class/Mono.C5/Test/Sorting.cs create mode 100644 mcs/class/Mono.C5/Test/SupportClasses.cs create mode 100644 mcs/class/Mono.C5/Test/arrays/ArrayListTest.cs create mode 100644 mcs/class/Mono.C5/Test/arrays/HashedArrayListTest.cs create mode 100644 mcs/class/Mono.C5/Test/arrays/SortedArrayTests.cs create mode 100644 mcs/class/Mono.C5/Test/hashing/HashBagTests.cs create mode 100644 mcs/class/Mono.C5/Test/hashing/HashDictionaryTests.cs create mode 100644 mcs/class/Mono.C5/Test/hashing/HashTableTests.cs create mode 100644 mcs/class/Mono.C5/Test/heaps/HeapTests.cs create mode 100644 mcs/class/Mono.C5/Test/linkedlists/HashedLinkedListTest.cs create mode 100644 mcs/class/Mono.C5/Test/linkedlists/LinkedListTest.cs create mode 100644 mcs/class/Mono.C5/Test/nunit.csproj create mode 100644 mcs/class/Mono.C5/Test/nunit.csproj.user create mode 100644 mcs/class/Mono.C5/Test/trees/Bag.cs create mode 100644 mcs/class/Mono.C5/Test/trees/Dictionary.cs create mode 100644 mcs/class/Mono.C5/Test/trees/RedBlackTreeSetTests.cs (limited to 'mcs/class/Mono.C5') 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 and Peter Sestoft + 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\. 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 and Peter Sestoft + 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 + { + public ABT() : base(8,new DefaultReferenceTypeHasher()) { } + + + 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 + { + 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 h = new NaturalComparer(); + 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 h = new NaturalComparerO(); + 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 h = C5.ComparerBuilder.FromComparable.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 h = C5.ComparerBuilder.FromComparable.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 h = C5.ComparerBuilder.FromComparable.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 h = new DefaultReferenceTypeHasher(); + 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 h = new DefaultValueTypeHasher(); + 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 h = C5.HasherBuilder.ByPrototype.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 h = C5.HasherBuilder.ByPrototype.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 h = C5.HasherBuilder.ByPrototype.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> h = C5.HasherBuilder.ByPrototype>.Examine(); + ICollection s = new LinkedList(); + ICollection t = new LinkedList(); + ICollection u = new LinkedList(); + 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> h = C5.HasherBuilder.ByPrototype>.Examine(); + ISequenced s = new LinkedList(); + ISequenced t = new LinkedList(); + ISequenced u = new LinkedList(); + 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 + + 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 and Peter Sestoft + 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(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(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(a, 0, length, ic); + for (int i = 1; i < length; i++) + Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i); + + Sorting.InsertionSort(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(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(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(a, 0, length, ic); + for (int i = 1; i < length; i++) + Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i); + + Sorting.InsertionSort(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(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(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(a, 0, length, ic); + for (int i = 1; i < length; i++) + Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i); + + Sorting.InsertionSort(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(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(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(a, 0, length, ic); + for (int i = 1; i < length; i++) + Assert.IsTrue(a[i - 1] <= a[i], "Inversion at " + i); + + Sorting.InsertionSort(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 and Peter Sestoft + 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 + { + public int Compare(string a, string b) + { + return a.CompareTo(b); + } + + + public void appl(String s) + { + System.Console.WriteLine("--{0}", s); + } + } + + + + class IC: IComparer, IComparable, IComparer, IComparable + { + 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 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 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 + { + 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 size; + + Int2Int f; + + + public FunEnumerable(int size, Int2Int f) + { + this.size = size; this.f = f; + } + + + public bool Exists(Filter filter) { return false; } + + + public bool All(Filter filter) { return false; } + + + public MSG.IEnumerator GetEnumerator() + { + for (int i = 0; i < size; i++) + yield return f(i); + } + + public void Apply(Applier 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 and Peter Sestoft + 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 list; + + private Filter always, never, even; + + + [SetUp] + public void Init() + { + list = new ArrayList(); + 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 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 list; + + + [SetUp] + public void Init() { list = new ArrayList(); } + + + [Test] + public void Empty() + { + MSG.IEnumerator 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 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 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 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 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 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 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 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 list; + + + [SetUp] + public void Init() { list = new ArrayList(); } + + + [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 list2 = new ArrayList(); + + 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 list; + + int[] a; + + + [SetUp] + public void Init() + { + list = new ArrayList(); + 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 list; + + + [SetUp] + public void Init() + { + list = new ArrayList(); + } + + + [TearDown] + public void Dispose() { list = null; } + + + [Test] + public void Get() + { + Assert.IsNotNull(list.SyncRoot); + } + } + } + + + + + namespace EditableCollection + { + [TestFixture] + public class Searching + { + private ArrayList list; + + + [SetUp] + public void Init() { list = new ArrayList(); } + + + [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 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)list.FindAll(f)).Check()); + Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10, 8)); + } + + + [Test] + public void ContainsAll() + { + ArrayList list2 = new ArrayList(); + + 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 list2 = new ArrayList(); + + 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 list2 = new ArrayList(); + + 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 dit; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + } + + + [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 dit; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + } + + + [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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2)); + Assert.AreEqual(5, dit.RemoveAt(0)); + Assert.IsTrue(((ArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 9, 1, 2)); + Assert.AreEqual(2, dit.RemoveAt(2)); + Assert.IsTrue(((ArrayList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60)); + dit.RemoveInterval(3, 1); + Assert.IsTrue(((ArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60)); + dit.RemoveInterval(1, 3); + Assert.IsTrue(((ArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 60)); + dit.RemoveInterval(0, 2); + Assert.IsTrue(((ArrayList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60)); + dit.RemoveInterval(2, 2); + Assert.IsTrue(((ArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40)); + } + + + [TearDown] + public void Dispose() + { + dit = null; + } + } + } + + + + + namespace List + { + [TestFixture] + public class Searching + { + private IList lst; + + + [SetUp] + public void Init() { lst = new ArrayList(); } + + + [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 lst; + + + [SetUp] + public void Init() { lst = new ArrayList(); } + + + [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 lst2 = new ArrayList(); + + 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 m = delegate(int i){return "<<"+i+">>";}; + IList 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> lst; + + + [SetUp] + public void Init() + { + lst = new ArrayList>(); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i,i+30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[10].key); + Assert.AreEqual(79, lst[10].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Sorting + { + private IList lst; + + + [SetUp] + public void Init() { lst = new ArrayList(); } + + + [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> lst2 = new ArrayList>(); + IComparer> c = new KeyValuePairComparer(new IC()); + + lst2.Add(new KeyValuePair(5, "a")); + lst2.Add(new KeyValuePair(5, "b")); + lst2.Add(new KeyValuePair(6, "c")); + lst2.Add(new KeyValuePair(4, "d")); + lst2.Add(new KeyValuePair(3, "e")); + lst2.Add(new KeyValuePair(4, "f")); + lst2.Add(new KeyValuePair(5, "h")); + Assert.IsFalse(lst2.IsSorted(c)); + lst2.Sort(c); + Assert.IsTrue(lst2.IsSorted(c)); + + KeyValuePair 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 list; + + + [SetUp] + public void Init() { list = new ArrayList(); } + + + [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 list; + + + [SetUp] + public void Init() { list = new ArrayList(); } + + + [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 lst; + + + [SetUp] + public void Init() { lst = new ArrayList(); } + + + [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 list, view; + + + [SetUp] + public void Init() + { + list = new ArrayList(); + list.Add(0);list.Add(1);list.Add(2);list.Add(3); + view = (ArrayList)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 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 lst2 = new ArrayList(); + + 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 lst2 = new ArrayList(); + + 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 view2 = (ArrayList)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 dbl = (ArrayList)view.Map(new Mapper(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)view.FindAll(new Filter(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)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 list2 = new ArrayList(); + + 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 lst = new ArrayList(); + lst.Add(2); + lst = lst.View(1, 1); + } + + [Test] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void RangeCheck2() + { + IList lst = new ArrayList(); + 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 l2 = new ArrayList(); + + 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)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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new TreeSet(new IC()); + dut = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new TreeSet(new IC()); + dut = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new TreeSet(new IC()); + dat = new ArrayList(); + dut = new ArrayList(); + dot = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(2);dat.Add(1); + dut.Add(3); + dot.Add(1);dot.Add(2); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + Dot = new ArrayList>(); + } + + + [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 dit, dat, dut, dot; + + private ISequenced> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new TreeSet(new IC()); + dat = new ArrayList(); + dut = new ArrayList(); + dot = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(2);dat.Add(1); + dut.Add(3); + dot.Add(1);dot.Add(2); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + Dot = new ArrayList>(); + } + + + [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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new ArrayList(); + dut = new ArrayList(); + } + + + [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)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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new ArrayList(); + dut = new ArrayList(); + } + + + [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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new ArrayList(); + dut = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new ArrayList(); + dut = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new ArrayList(); + dut = new ArrayList(); + dot = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + Dot = new ArrayList>(); + } + + + [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 dit, dat, dut, dot; + + private ISequenced> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new ArrayList(); + dat = new ArrayList(); + dut = new ArrayList(); + dot = new ArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new ArrayList>(); + Dat = new ArrayList>(); + Dut = new ArrayList>(); + Dot = new ArrayList>(); + } + + + [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 and Peter Sestoft + 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 list; + + private Filter always, never, even; + + + [SetUp] + public void Init() + { + list = new HashedArrayList(); + 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 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 list; + + + [SetUp] + public void Init() { list = new HashedArrayList(); } + + + [Test] + public void Empty() + { + MSG.IEnumerator 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 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 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 e = list.GetEnumerator(); + + e.MoveNext(); + list.Add(99); + e.MoveNext(); + } + + + + [TearDown] + public void Dispose() { list = null; } + } + } + + + + + namespace CollectionOrSink + { + [TestFixture] + public class CollectionOrSink + { + private HashedArrayList list; + + + [SetUp] + public void Init() { list = new HashedArrayList(); } + + + [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 list2 = new HashedArrayList(); + + 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 list; + + int[] a; + + + [SetUp] + public void Init() + { + list = new HashedArrayList(); + 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 list; + + + [SetUp] + public void Init() + { + list = new HashedArrayList(); + } + + + [TearDown] + public void Dispose() { list = null; } + + + [Test] + public void Get() + { + Assert.IsNotNull(list.SyncRoot); + } + } + } + + + + + namespace EditableCollection + { + [TestFixture] + public class Searching + { + private HashedArrayList list; + + + [SetUp] + public void Init() { list = new HashedArrayList(); } + + + [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 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)list.FindAll(f)).Check()); + Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10)); + } + + + [Test] + public void ContainsAll() + { + HashedArrayList list2 = new HashedArrayList(); + + 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 list2 = new HashedArrayList(); + + 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 list2 = new HashedArrayList(); + + 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 dit; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + } + + + [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 dit; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + } + + + [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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2)); + Assert.AreEqual(5, dit.RemoveAt(0)); + Assert.IsTrue(((HashedArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 9, 1, 2)); + Assert.AreEqual(2, dit.RemoveAt(2)); + Assert.IsTrue(((HashedArrayList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60)); + dit.RemoveInterval(3, 1); + Assert.IsTrue(((HashedArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60)); + dit.RemoveInterval(1, 3); + Assert.IsTrue(((HashedArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 60)); + dit.RemoveInterval(0, 2); + Assert.IsTrue(((HashedArrayList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60)); + dit.RemoveInterval(2, 2); + Assert.IsTrue(((HashedArrayList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40)); + } + + + [TearDown] + public void Dispose() + { + dit = null; + } + } + } + + + + + namespace IList + { + [TestFixture] + public class Searching + { + private IList lst; + + + [SetUp] + public void Init() { lst = new HashedArrayList(); } + + + [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 lst; + + + [SetUp] + public void Init() { lst = new HashedArrayList(); } + + + [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 lst2 = new HashedArrayList(); + + 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 m = delegate(int i){return "<<"+i+">>";}; + IList r = lst.Map(m); + + Assert.IsTrue(((ArrayList)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)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> lst; + + + [SetUp] + public void Init() + { + lst = new HashedArrayList>(); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[10].key); + Assert.AreEqual(79, lst[10].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Sorting + { + private IList lst; + + + [SetUp] + public void Init() { lst = new HashedArrayList(); } + + + [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 lst; + + + [SetUp] + public void Init() { lst = new HashedArrayList(); } + + + [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 list; + HashedArrayList view; + + + [SetUp] + public void Init() + { + list = new HashedArrayList(); + list.Add(0);list.Add(1);list.Add(2);list.Add(3); + view = (HashedArrayList)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 lst2 = new HashedArrayList(); + + 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 lst2 = new HashedArrayList(); + + 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 view2 = (HashedArrayList)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 dbl = (ArrayList)view.Map(new Mapper(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 list2 = (ArrayList)view.FindAll(new Filter(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)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 list2 = new HashedArrayList(); + + 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 l2 = new HashedArrayList(); + + 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)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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + dat = new HashedArrayList(); + dut = new HashedArrayList(); + } + + + [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)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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + dat = new HashedArrayList(); + dut = new HashedArrayList(); + } + + + [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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + dat = new HashedArrayList(); + dut = new HashedArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new HashedArrayList>(); + Dat = new HashedArrayList>(); + Dut = new HashedArrayList>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + dat = new HashedArrayList(); + dut = new HashedArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new HashedArrayList>(); + Dat = new HashedArrayList>(); + Dut = new HashedArrayList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + dat = new HashedArrayList(); + dut = new HashedArrayList(); + dot = new HashedArrayList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new HashedArrayList>(); + Dat = new HashedArrayList>(); + Dut = new HashedArrayList>(); + Dot = new HashedArrayList>(); + } + + + [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 dit, dat, dut, dot; + + private ISequenced> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new HashedArrayList(); + dat = new HashedArrayList(); + dut = new HashedArrayList(); + dot = new HashedArrayList(); + 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>(); + Dat = new HashedArrayList>(); + Dut = new HashedArrayList>(); + Dot = new HashedArrayList>(); + } + + + [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 and Peter Sestoft + 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 array; + + private IComparer c; + + + [SetUp] + public void Init() + { + c = new IC(); + array = new SortedArray(c); + for (int i = 1; i <= 10; i++) + { + array.Add(i * 2); + } + } + + + [Test] + public void Enumerator() + { + MSG.IEnumerator 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 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 array; + + + [SetUp] + public void Init() + { + array = new SortedArray(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 array; + + + [SetUp] + public void Init() + { + array = new SortedArray(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> bag; + + + [SetUp] + public void Init() + { + bag = new SortedArray>(new KeyValuePairComparer(new IC())); + } + + + [TearDown] + public void Dispose() + { + bag = null; + } + + + [Test] + public void Test() + { + KeyValuePair p = new KeyValuePair(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 tree; + + int[] a; + + + [SetUp] + public void Init() + { + tree = new SortedArray(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> lst; + + + [SetUp] + public void Init() + { + lst = new SortedArray>(new KeyValuePairComparer(new IC())); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[10].key); + Assert.AreEqual(79, lst[10].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Remove + { + private SortedArray array; + + + [SetUp] + public void Init() + { + array = new SortedArray(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 tree; + + + [SetUp] + public void Init() + { + tree = new SortedArray(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 tree; + + + [SetUp] + public void Init() + { + tree = new SortedArray(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 array; + + + [SetUp] + public void Init() + { + array = new SortedArray(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 tree; + + private MSG.IEnumerator e; + + + [SetUp] + public void Init() + { + tree = new SortedArray(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 tree; + + private MSG.IEnumerator e; + + + [SetUp] + public void Init() + { + tree = new SortedArray(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 + { + 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 + { + 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 array; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + array = new SortedArray(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(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(simple2.apply)); + Assert.AreEqual(10, simple2.appfield1); + Assert.AreEqual(285, simple2.appfield2); + } + + + [Test] + public void All() + { + Assert.IsTrue(array.All(new Filter(never))); + Assert.IsTrue(array.All(new Filter(even))); + Assert.IsTrue(array.All(new Filter(always))); + for (int i = 0; i < 10; i++) array.Add(i); + + Assert.IsFalse(array.All(new Filter(never))); + Assert.IsFalse(array.All(new Filter(even))); + Assert.IsTrue(array.All(new Filter(always))); + array.Clear(); + for (int i = 0; i < 10; i++) array.Add(i * 2); + + Assert.IsFalse(array.All(new Filter(never))); + Assert.IsTrue(array.All(new Filter(even))); + Assert.IsTrue(array.All(new Filter(always))); + array.Clear(); + for (int i = 0; i < 10; i++) array.Add(i * 2 + 1); + + Assert.IsFalse(array.All(new Filter(never))); + Assert.IsFalse(array.All(new Filter(even))); + Assert.IsTrue(array.All(new Filter(always))); + } + + + [Test] + public void Exists() + { + Assert.IsFalse(array.Exists(new Filter(never))); + Assert.IsFalse(array.Exists(new Filter(even))); + Assert.IsFalse(array.Exists(new Filter(always))); + for (int i = 0; i < 10; i++) array.Add(i); + + Assert.IsFalse(array.Exists(new Filter(never))); + Assert.IsTrue(array.Exists(new Filter(even))); + Assert.IsTrue(array.Exists(new Filter(always))); + array.Clear(); + for (int i = 0; i < 10; i++) array.Add(i * 2); + + Assert.IsFalse(array.Exists(new Filter(never))); + Assert.IsTrue(array.Exists(new Filter(even))); + Assert.IsTrue(array.Exists(new Filter(always))); + array.Clear(); + for (int i = 0; i < 10; i++) array.Add(i * 2 + 1); + + Assert.IsFalse(array.Exists(new Filter(never))); + Assert.IsFalse(array.Exists(new Filter(even))); + Assert.IsTrue(array.Exists(new Filter(always))); + } + + + [Test] + public void FindAll() + { + Assert.AreEqual(0, array.FindAll(new Filter(never)).Count); + for (int i = 0; i < 10; i++) + array.Add(i); + + Assert.AreEqual(0, array.FindAll(new Filter(never)).Count); + Assert.AreEqual(10, array.FindAll(new Filter(always)).Count); + Assert.AreEqual(5, array.FindAll(new Filter(even)).Count); + Assert.IsTrue(((SortedArray)array.FindAll(new Filter(even))).Check()); + } + + + [Test] + public void Map() + { + Assert.AreEqual(0, array.Map(new Mapper(themap), new SC()).Count); + for (int i = 0; i < 11; i++) + array.Add(i * i * i); + + IIndexedSorted res = array.Map(new Mapper(themap), new SC()); + + Assert.IsTrue(((SortedArray)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 res = array.Map(new Mapper(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 array; + + + [SetUp] + public void Init() { array = new SortedArray(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 array; + + + [SetUp] + public void Init() { array = new SortedArray(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 array, array2; + + + [SetUp] + public void Init() + { + array = new SortedArray(new IC()); + array2 = new SortedArray(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 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 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(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 tree; + + int sz = 500000; + + + [SetUp] + public void Init() + { + tree = new SortedArray(new IC()); + for (int i = 0; i < sz; i++) + { + tree.Add(i); + } + } + + + + class A + { + public int count = 0; + + SortedArray t; + + + public A(SortedArray t) { this.t = t; } + + + public void a(int i) { count++; } + + + public void traverse() { t.Apply(new Applier(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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new SortedArray(new IC()); + dat = new SortedArray(new IC()); + dut = new SortedArray(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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new SortedArray(new IC()); + dat = new SortedArray(new IC()); + dut = new SortedArray(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 and Peter Sestoft + 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> lst; + + + [SetUp] + public void Init() + { + lst = new HashBag>(); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + 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(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + 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(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 p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + Assert.IsTrue(lst.RemoveWithReturn(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + + [TestFixture] + public class CollectionOrSink + { + private HashBag hashbag; + + + [SetUp] + public void Init() { hashbag = new HashBag(); } + + + [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 hashbag2 = new HashBag(); + + 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 list2 = new HashBag(); + + 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 list2 = new HashBag(); + + 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 list2 = new HashBag(); + + 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 hashbag; + + int[] a; + + + [SetUp] + public void Init() + { + hashbag = new HashBag(); + 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 h1, h2; + + + [SetUp] + public void Init() + { + h1 = new HashBag(); + h2 = new LinkedList(); + } + + + [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 and Peter Sestoft + 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 dict; + + + [SetUp] + public void Init() + { + dict = new HashDictionary(); + //dict = TreeDictionary.MakeNaturalO(); + } + + + [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 dict2 = new HashDictionary(); + + 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 dict; + + private MSG.IEnumerator> dictenum; + + + [SetUp] + public void Init() + { + dict = new HashDictionary(); + 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 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 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("R", "C")); + Assert.IsTrue(dictenum.MoveNext()); + Assert.AreEqual(dictenum.Current, new KeyValuePair("T", "B")); + Assert.IsTrue(dictenum.MoveNext()); + Assert.AreEqual(dictenum.Current, new KeyValuePair("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 and Peter Sestoft + 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 list; + + private Filter always, never, even; + + + [SetUp] + public void Init() + { + list = new HashSet(); + 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 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 hashset; + + + [SetUp] + public void Init() { hashset = new HashSet(); } + + + [Test] + public void Empty() + { + MSG.IEnumerator 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 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 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 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 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 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 e = hashset.GetEnumerator(); + + e.MoveNext(); + hashset.Add(99); + e.MoveNext(); + } + + + [TearDown] + public void Dispose() { hashset = null; } + } + } + + + + + namespace CollectionOrSink + { + [TestFixture] + public class CollectionOrSink + { + private HashSet hashset; + + + [SetUp] + public void Init() { hashset = new HashSet(); } + + + [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 hashset2 = new HashSet(); + + 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 hashset; + + int[] a; + + + [SetUp] + public void Init() + { + hashset = new HashSet(); + 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 list; + + + [SetUp] + public void Init() + { + list = new HashSet(); + } + + + [TearDown] + public void Dispose() { list = null; } + + + [Test] + public void Get() + { + Assert.IsNotNull(list.SyncRoot); + } + } + } + + + + + namespace EditableCollection + { + [TestFixture] + public class Collision + { + HashSet hashset; + + + [SetUp] + public void Init() + { + hashset = new HashSet(); + } + + + [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 hashset; + + + [SetUp] + public void Init() { hashset = new HashSet(); } + + + [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 list2 = new HashSet(); + + 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 list2 = new HashSet(); + + 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 list2 = new HashSet(); + + 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> lst; + + + [SetUp] + public void Init() + { + lst = new HashSet>(); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + 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(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + 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(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 p = new KeyValuePair(3, 78); + KeyValuePair q = new KeyValuePair(); + + Assert.IsTrue(lst.RemoveWithReturn(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + } + + + + + namespace HashingAndEquals + { + [TestFixture] + public class IEditableCollection + { + private ICollection dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new HashSet(); + dat = new HashSet(); + dut = new HashSet(); + } + + + [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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new HashSet(); + dat = new HashSet(); + dut = new HashSet(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new HashSet>(); + Dat = new HashSet>(); + Dut = new HashSet>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new HashSet(); + dat = new HashSet(); + dut = new HashSet(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + dot = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new HashSet>(); + Dat = new HashSet>(); + Dut = new HashSet>(); + Dot = new HashSet>(); + } + + + [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 and Peter Sestoft + 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 queue; + + + [SetUp] + public void Init() { queue = new IntervalHeap(); } + + + [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 lst = new LinkedList(); + 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 and Peter Sestoft + 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 list; + + private Filter always, never, even; + + + [SetUp] + public void Init() + { + list = new HashedLinkedList(); + 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 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 list; + + + [SetUp] + public void Init() { list = new HashedLinkedList(); } + + + [Test] + public void Empty() + { + MSG.IEnumerator 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 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 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 e = list.GetEnumerator(); + + e.MoveNext(); + list.Add(99); + e.MoveNext(); + } + + + + [TearDown] + public void Dispose() { list = null; } + } + } + + + + + namespace CollectionOrSink + { + [TestFixture] + public class CollectionOrSink + { + private HashedLinkedList list; + + + [SetUp] + public void Init() { list = new HashedLinkedList(); } + + + [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 list2 = new HashedLinkedList(); + + 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 list; + + int[] a; + + + [SetUp] + public void Init() + { + list = new HashedLinkedList(); + 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 list; + + + [SetUp] + public void Init() + { + list = new HashedLinkedList(); + } + + + [TearDown] + public void Dispose() { list = null; } + + + [Test] + public void Get() + { + Assert.IsNotNull(list.SyncRoot); + } + } + } + + + + + namespace EditableCollection + { + [TestFixture] + public class Searching + { + private HashedLinkedList list; + + + [SetUp] + public void Init() { list = new HashedLinkedList(); } + + + [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 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)list.FindAll(f)).Check()); + Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10)); + } + + + [Test] + public void ContainsAll() + { + HashedLinkedList list2 = new HashedLinkedList(); + + 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 list2 = new HashedLinkedList(); + + 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 list2 = new HashedLinkedList(); + + 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 dit; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + } + + + [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 dit; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + } + + + [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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2)); + Assert.AreEqual(5, dit.RemoveAt(0)); + Assert.IsTrue(((HashedLinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 9, 1, 2)); + Assert.AreEqual(2, dit.RemoveAt(2)); + Assert.IsTrue(((HashedLinkedList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60)); + dit.RemoveInterval(3, 1); + Assert.IsTrue(((HashedLinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60)); + dit.RemoveInterval(1, 3); + Assert.IsTrue(((HashedLinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 60)); + dit.RemoveInterval(0, 2); + Assert.IsTrue(((HashedLinkedList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60)); + dit.RemoveInterval(2, 2); + Assert.IsTrue(((HashedLinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40)); + } + + + [TearDown] + public void Dispose() + { + dit = null; + } + } + } + + + + + namespace IList + { + [TestFixture] + public class Searching + { + private IList lst; + + + [SetUp] + public void Init() { lst = new HashedLinkedList(); } + + + [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> lst; + + + [SetUp] + public void Init() + { + lst = new HashedLinkedList>(); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[10].key); + Assert.AreEqual(79, lst[10].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Inserting + { + private IList lst; + + + [SetUp] + public void Init() { lst = new HashedLinkedList(); } + + + [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 lst2 = new HashedLinkedList(); + + 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 m = delegate(int i){return "<<"+i+">>";}; + IList r = lst.Map(m); + + Assert.IsTrue(((LinkedList)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)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 lst; + + + [SetUp] + public void Init() { lst = new HashedLinkedList(); } + + + [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 lst; + + + [SetUp] + public void Init() { lst = new HashedLinkedList(); } + + + [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 list; + LinkedList view; + + + [SetUp] + public void Init() + { + list = new HashedLinkedList(); + list.Add(0);list.Add(1);list.Add(2);list.Add(3); + view = (LinkedList)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 lst2 = new HashedLinkedList(); + + 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 lst2 = new HashedLinkedList(); + + 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 view2 = (LinkedList)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 dbl = (LinkedList)view.Map(new Mapper(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 list2 = (LinkedList)view.FindAll(new Filter(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)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 list2 = new HashedLinkedList(); + + 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 l2 = new HashedLinkedList(); + + 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)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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + dat = new HashedLinkedList(); + dut = new HashedLinkedList(); + } + + + [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)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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + dat = new HashedLinkedList(); + dut = new HashedLinkedList(); + } + + + [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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + dat = new HashedLinkedList(); + dut = new HashedLinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new HashedLinkedList>(); + Dat = new HashedLinkedList>(); + Dut = new HashedLinkedList>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + dat = new HashedLinkedList(); + dut = new HashedLinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new HashedLinkedList>(); + Dat = new HashedLinkedList>(); + Dut = new HashedLinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + dat = new HashedLinkedList(); + dut = new HashedLinkedList(); + dot = new HashedLinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new HashedLinkedList>(); + Dat = new HashedLinkedList>(); + Dut = new HashedLinkedList>(); + Dot = new HashedLinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ISequenced> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new HashedLinkedList(); + dat = new HashedLinkedList(); + dut = new HashedLinkedList(); + dot = new HashedLinkedList(); + 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>(); + Dat = new HashedLinkedList>(); + Dut = new HashedLinkedList>(); + Dot = new HashedLinkedList>(); + } + + + [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 and Peter Sestoft + 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 list; + + private Filter always, never, even; + + + [SetUp] + public void Init() + { + list = new LinkedList(); + 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 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 list; + + + [SetUp] + public void Init() { list = new LinkedList(); } + + + [Test] + public void Empty() + { + MSG.IEnumerator 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 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 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 e = list.GetEnumerator(); + + e.MoveNext(); + list.Add(99); + e.MoveNext(); + } + + + [TearDown] + public void Dispose() { list = null; } + } + } + + + + + namespace CollectionOrExtensible + { + [TestFixture] + public class CollectionOrSink + { + private LinkedList list; + + + [SetUp] + public void Init() { list = new LinkedList(); } + + + [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 list2 = new LinkedList(); + + 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 list; + + int[] a; + + + [SetUp] + public void Init() + { + list = new LinkedList(); + 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 list; + + + [SetUp] + public void Init() + { + list = new LinkedList(); + } + + + [TearDown] + public void Dispose() { list = null; } + + + [Test] + public void Get() + { + Assert.IsNotNull(list.SyncRoot); + } + } + } + + + + + namespace EditableCollection + { + [TestFixture] + public class Searching + { + private LinkedList list; + + + [SetUp] + public void Init() { list = new LinkedList(); } + + + [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 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)list.FindAll(f)).Check()); + Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10, 8)); + } + + + [Test] + public void ContainsAll() + { + LinkedList list2 = new LinkedList(); + + 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 list2 = new LinkedList(); + + 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 list2 = new LinkedList(); + + 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 dit; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + } + + + [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 dit; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + } + + + [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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2)); + Assert.AreEqual(5, dit.RemoveAt(0)); + Assert.IsTrue(((LinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 9, 1, 2)); + Assert.AreEqual(2, dit.RemoveAt(2)); + Assert.IsTrue(((LinkedList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60)); + dit.RemoveInterval(3, 1); + Assert.IsTrue(((LinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60)); + dit.RemoveInterval(1, 3); + Assert.IsTrue(((LinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 10, 60)); + dit.RemoveInterval(0, 2); + Assert.IsTrue(((LinkedList)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)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60)); + dit.RemoveInterval(2, 2); + Assert.IsTrue(((LinkedList)dit).Check()); + Assert.IsTrue(IC.eq(dit, 30, 40)); + } + + + [TearDown] + public void Dispose() + { + dit = null; + } + } + } + + + + + namespace IList + { + [TestFixture] + public class Searching + { + private IList lst; + + + [SetUp] + public void Init() { lst = new LinkedList(); } + + + [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> lst; + + + [SetUp] + public void Init() + { + lst = new LinkedList>(); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[10].key); + Assert.AreEqual(79, lst[10].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Inserting + { + private IList lst; + + + [SetUp] + public void Init() { lst = new LinkedList(); } + + + [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 lst2 = new LinkedList(); + + 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 m = delegate(int i){return "<<"+i+">>";}; + IList 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 lst; + + + [SetUp] + public void Init() { lst = new LinkedList(); } + + + [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> lst2 = new LinkedList>(); + IComparer> c = new KeyValuePairComparer(new IC()); + + lst2.Add(new KeyValuePair(5, "a")); + lst2.Add(new KeyValuePair(5, "b")); + lst2.Add(new KeyValuePair(6, "c")); + lst2.Add(new KeyValuePair(4, "d")); + lst2.Add(new KeyValuePair(3, "e")); + lst2.Add(new KeyValuePair(4, "f")); + lst2.Add(new KeyValuePair(5, "h")); + Assert.IsFalse(lst2.IsSorted(c)); + lst2.Sort(c); + Assert.IsTrue(lst2.IsSorted(c)); + + KeyValuePair 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 list; + + + [SetUp] + public void Init() { list = new LinkedList(); } + + + [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 list; + + + [SetUp] + public void Init() { list = new LinkedList(); } + + + [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 lst; + + + [SetUp] + public void Init() { lst = new LinkedList(); } + + + [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 list, view; + + + [SetUp] + public void Init() + { + list = new LinkedList(); + list.Add(0);list.Add(1);list.Add(2);list.Add(3); + view = (LinkedList)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 lst2 = new LinkedList(); + + 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 lst2 = new LinkedList(); + + 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 view2 = (LinkedList)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 dbl = (LinkedList)view.Map(new Mapper(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)view.FindAll(new Filter(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)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 list2 = new LinkedList(); + + 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 l2 = new LinkedList(); + + 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)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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new TreeSet(new IC()); + dut = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new TreeSet(new IC()); + dut = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new TreeSet(new IC()); + dat = new LinkedList(); + dut = new LinkedList(); + dot = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(2);dat.Add(1); + dut.Add(3); + dot.Add(1);dot.Add(2); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + Dot = new LinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ISequenced> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new TreeSet(new IC()); + dat = new LinkedList(); + dut = new LinkedList(); + dot = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(2);dat.Add(1); + dut.Add(3); + dot.Add(1);dot.Add(2); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + Dot = new LinkedList>(); + } + + + [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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + } + + + [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)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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + } + + + [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 dit, dat, dut; + + private ICollection> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + } + + + [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 dit, dat, dut; + + private ISequenced> Dit, Dat, Dut; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ICollection> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + dot = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + Dot = new LinkedList>(); + } + + + [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 dit, dat, dut, dot; + + private ISequenced> Dit, Dat, Dut, Dot; + + + [SetUp] + public void Init() + { + dit = new LinkedList(); + dat = new LinkedList(); + dut = new LinkedList(); + dot = new LinkedList(); + dit.Add(2);dit.Add(1); + dat.Add(1);dat.Add(2); + dut.Add(3); + dot.Add(2);dot.Add(1); + Dit = new LinkedList>(); + Dat = new LinkedList>(); + Dut = new LinkedList>(); + Dot = new LinkedList>(); + } + + + [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 @@ + + + Debug + 8.0.40426 + 2.0 + {08CBFDEB-A2E2-4F0E-A4E1-B996B05569DE} + Library + + + nunit + false + nunit + + + + + true + false + .\bin\Debug\ + false + DEBUG;TRACE + 4 + false + + + false + true + .\bin\Release\ + false + TRACE + 4 + false + + + + ..\..\NUnit\nunit.framework\bin\Debug\nunit.framework.dll + nunit.framework + False + global + .dll + + + ..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.2.30703\System.dll + System + + + ..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.2.30703\System.Data.dll + System.Data + + + ..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.2.30703\System.XML.dll + System.XML + + + {D70489CD-ABDA-48FF-BD1E-BE3F7495BE71} + {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + C5 + + + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + Code + + + + + + + + + + + + + + + + + + + + \ 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 @@ + + + 8.0.40426 + C:\Programmer\NUnit V2.1\bin\ + + + + + NoZone + false + false + false + false + false + + + Project + + + + + + + + + + + false + + + + + NoZone + false + false + false + false + false + + + Project + + + + + + + + + + + true + + + ProjectFiles + 0 + + \ 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 and Peter Sestoft + 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> lst; + + + [SetUp] + public void Init() + { + lst = new TreeBag>(new KeyValuePairComparer(new IC())); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[11].key); + Assert.AreEqual(79, lst[11].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Simple + { + private TreeBag bag; + + + [SetUp] + public void Init() + { + bag = new TreeBag(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> bag; + + + [SetUp] + public void Init() + { + bag = new TreeBag>(new KeyValuePairComparer(new IC())); + } + + + [TearDown] + public void Dispose() + { + bag = null; + } + + + [Test] + public void Test() + { + KeyValuePair p = new KeyValuePair(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 bag; + + private MSG.IEnumerator bagenum; + + + [SetUp] + public void Init() + { + bag = new TreeBag(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 tree; + + private IComparer c; + + + [SetUp] + public void Init() + { + c = new IC(); + tree = new TreeBag(c); + for (int i = 1; i <= 10; i++) + { + tree.Add(i * 2);tree.Add(i); + } + } + + + [Test] + public void Enumerator() + { + MSG.IEnumerator 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 e = tree.RangeFromTo(5, 17).GetEnumerator(); + int i = e.Current; + } + + + [Test] + [ExpectedException(typeof(InvalidOperationException))] + public void Enumerator3() + { + MSG.IEnumerator 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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + int[] a; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + private MSG.IEnumerator e; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree; + + private MSG.IEnumerator e; + + + [SetUp] + public void Init() + { + tree = new TreeBag(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 tree, snap; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + tree = new TreeBag(ic); + for (int i = 0; i <= 20; i++) + tree.Add(2 * i + 1); + tree.Add(13); + snap = (TreeBag)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(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 tree; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + tree = new TreeBag(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 snap = (TreeBag)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 snap = (TreeBag)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 snap = (TreeBag)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 snap = (TreeBag)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 snap = (TreeBag)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 snap = (TreeBag)tree.Snapshot(); + + snap.Snapshot(); + } + + + [TearDown] + public void Dispose() + { + tree = null; + ic = null; + } + } + + + + [TestFixture] + public class Multiple + { + private TreeBag tree; + + private IComparer ic; + + + private bool eq(MSG.IEnumerable 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(ic); + for (int i = 0; i < 10; i++) + tree.Add(2 * i + 1); + } + + + [Test] + public void First() + { + TreeBag[] snaps = new TreeBag[10]; + + for (int i = 0; i < 10; i++) + { + snaps[i] = (TreeBag)(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[] snaps = new TreeBag[10]; + + for (int i = 0; i < 10; i++) + { + snaps[i] = (TreeBag)(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 + { + 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 + { + 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 tree; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + tree = new TreeBag(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(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(simple2.apply)); + Assert.AreEqual(11, simple2.appfield1); + Assert.AreEqual(289, simple2.appfield2); + } + + + [Test] + public void All() + { + Assert.IsTrue(tree.All(new Filter(never))); + Assert.IsTrue(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + for (int i = 0; i < 10; i++) tree.Add(i); + + Assert.IsFalse(tree.All(new Filter(never))); + Assert.IsFalse(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2); + + Assert.IsFalse(tree.All(new Filter(never))); + Assert.IsTrue(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1); + + Assert.IsFalse(tree.All(new Filter(never))); + Assert.IsFalse(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + } + + + [Test] + public void Exists() + { + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsFalse(tree.Exists(new Filter(even))); + Assert.IsFalse(tree.Exists(new Filter(always))); + for (int i = 0; i < 10; i++) tree.Add(i); + + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsTrue(tree.Exists(new Filter(even))); + Assert.IsTrue(tree.Exists(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2); + + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsTrue(tree.Exists(new Filter(even))); + Assert.IsTrue(tree.Exists(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1); + + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsFalse(tree.Exists(new Filter(even))); + Assert.IsTrue(tree.Exists(new Filter(always))); + } + + + [Test] + public void FindAll() + { + Assert.AreEqual(0, tree.FindAll(new Filter(never)).Count); + for (int i = 0; i < 10; i++) + tree.Add(i); + tree.Add(2); + + Assert.AreEqual(0, tree.FindAll(new Filter(never)).Count); + Assert.AreEqual(11, tree.FindAll(new Filter(always)).Count); + Assert.AreEqual(6, tree.FindAll(new Filter(even)).Count); + Assert.IsTrue(((TreeBag)tree.FindAll(new Filter(even))).Check("R")); + } + + + [Test] + public void Map() + { + Assert.AreEqual(0, tree.Map(new Mapper(themap), new SC()).Count); + for (int i = 0; i < 14; i++) + tree.Add(i * i * i); + tree.Add(1); + + IIndexedSorted res = tree.Map(new Mapper(themap), new SC()); + + Assert.IsTrue(((TreeBag)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 res = tree.Map(new Mapper(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 tree; + + + [SetUp] + public void Init() { tree = new TreeBag(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 tree; + + + [SetUp] + public void Init() { tree = new TreeBag(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 tree, tree2; + + + [SetUp] + public void Init() + { + tree = new TreeBag(new IC()); + tree2 = new TreeBag(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(MSG.IEnumerable e) + { + foreach (T i in e) + Console.Write("{0} ", i); + + Console.WriteLine(); + } + + [Test] + public void RetainAll() + { + tree.Add(8);tree2.Add(6); + //pint(tree); + //pint(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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new TreeBag(new IC()); + dat = new TreeBag(new IC()); + dut = new TreeBag(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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new TreeBag(new IC()); + dat = new TreeBag(new IC()); + dut = new TreeBag(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 and Peter Sestoft + 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 dict; + + + [SetUp] + public void Init() { dict = new TreeDictionary(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 dict; + + private MSG.IEnumerator> dictenum; + + + [SetUp] + public void Init() + { + dict = new TreeDictionary(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 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 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("R", "C")); + Assert.IsTrue(dictenum.MoveNext()); + Assert.AreEqual(dictenum.Current, new KeyValuePair("S", "A")); + Assert.IsTrue(dictenum.MoveNext()); + Assert.AreEqual(dictenum.Current, new KeyValuePair("T", "B")); + Assert.IsFalse(dictenum.MoveNext()); + } + } + + + + + namespace PathCopyPersistence + { + [TestFixture] + public class Simple + { + private TreeDictionary dict; + + private TreeDictionary snap; + + + [SetUp] + public void Init() + { + dict = new TreeDictionary(new SC()); + dict["S"] = "A"; + dict["T"] = "B"; + dict["R"] = "C"; + dict["V"] = "G"; + snap = (TreeDictionary)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 snap2 = (TreeDictionary)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 and Peter Sestoft + 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> lst; + + + [SetUp] + public void Init() + { + lst = new TreeSet>(new KeyValuePairComparer(new IC())); + for (int i = 0; i < 10; i++) + lst.Add(new KeyValuePair(i, i + 30)); + } + + + [TearDown] + public void Dispose() { lst = null; } + + + [Test] + public void Find() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Find(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Find(ref p)); + } + + + [Test] + public void FindOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.FindOrAdd(ref p)); + Assert.AreEqual(3, p.key); + Assert.AreEqual(33, p.value); + p = new KeyValuePair(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 p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.Update(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 78); + Assert.IsFalse(lst.Update(p)); + } + + + [Test] + public void UpdateOrAdd() + { + KeyValuePair p = new KeyValuePair(3, 78); + + Assert.IsTrue(lst.UpdateOrAdd(p)); + Assert.AreEqual(3, lst[3].key); + Assert.AreEqual(78, lst[3].value); + p = new KeyValuePair(13, 79); + Assert.IsFalse(lst.UpdateOrAdd(p)); + Assert.AreEqual(13, lst[10].key); + Assert.AreEqual(79, lst[10].value); + } + + + [Test] + public void RemoveWithReturn() + { + KeyValuePair p = new KeyValuePair(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(13, 78); + Assert.IsFalse(lst.RemoveWithReturn(ref p)); + } + } + + + [TestFixture] + public class Ranges + { + private TreeSet tree; + + private IComparer c; + + + [SetUp] + public void Init() + { + c = new IC(); + tree = new TreeSet(c); + for (int i = 1; i <= 10; i++) + { + tree.Add(i * 2); + } + } + + + [Test] + public void Enumerator() + { + MSG.IEnumerator 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 e = tree.RangeFromTo(5, 17).GetEnumerator(); + int i = e.Current; + } + + + [Test] + [ExpectedException(typeof(InvalidOperationException))] + public void Enumerator3() + { + MSG.IEnumerator 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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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> bag; + + + [SetUp] + public void Init() + { + bag = new TreeSet>(new KeyValuePairComparer(new IC())); + } + + + [TearDown] + public void Dispose() + { + bag = null; + } + + + [Test] + public void Test() + { + KeyValuePair p = new KeyValuePair(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 tree; + + int[] a; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + private MSG.IEnumerator e; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree; + + private MSG.IEnumerator e; + + + [SetUp] + public void Init() + { + tree = new TreeSet(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 tree, snap; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + tree = new TreeSet(ic); + for (int i = 0; i <= 20; i++) + tree.Add(2 * i + 1); + + snap = (TreeSet)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(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 tree; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + tree = new TreeSet(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 snap = (TreeSet)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 snap = (TreeSet)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 snap = (TreeSet)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 snap = (TreeSet)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 snap = (TreeSet)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 snap = (TreeSet)tree.Snapshot(); + + snap.Snapshot(); + } + + + [TearDown] + public void Dispose() + { + tree = null; + ic = null; + } + } + + + + [TestFixture] + public class Multiple + { + private TreeSet tree; + + private IComparer ic; + + + private bool eq(MSG.IEnumerable 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(ic); + for (int i = 0; i < 10; i++) + tree.Add(2 * i + 1); + } + + + [Test] + public void First() + { + TreeSet[] snaps = new TreeSet[10]; + + for (int i = 0; i < 10; i++) + { + snaps[i] = (TreeSet)(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[] snaps = new TreeSet[10]; + + for (int i = 0; i < 10; i++) + { + snaps[i] = (TreeSet)(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 + { + 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 + { + 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 tree; + + private IComparer ic; + + + [SetUp] + public void Init() + { + ic = new IC(); + tree = new TreeSet(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(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(simple2.apply)); + Assert.AreEqual(10, simple2.appfield1); + Assert.AreEqual(285, simple2.appfield2); + } + + + [Test] + public void All() + { + Assert.IsTrue(tree.All(new Filter(never))); + Assert.IsTrue(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + for (int i = 0; i < 10; i++) tree.Add(i); + + Assert.IsFalse(tree.All(new Filter(never))); + Assert.IsFalse(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2); + + Assert.IsFalse(tree.All(new Filter(never))); + Assert.IsTrue(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1); + + Assert.IsFalse(tree.All(new Filter(never))); + Assert.IsFalse(tree.All(new Filter(even))); + Assert.IsTrue(tree.All(new Filter(always))); + } + + + [Test] + public void Exists() + { + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsFalse(tree.Exists(new Filter(even))); + Assert.IsFalse(tree.Exists(new Filter(always))); + for (int i = 0; i < 10; i++) tree.Add(i); + + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsTrue(tree.Exists(new Filter(even))); + Assert.IsTrue(tree.Exists(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2); + + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsTrue(tree.Exists(new Filter(even))); + Assert.IsTrue(tree.Exists(new Filter(always))); + tree.Clear(); + for (int i = 0; i < 10; i++) tree.Add(i * 2 + 1); + + Assert.IsFalse(tree.Exists(new Filter(never))); + Assert.IsFalse(tree.Exists(new Filter(even))); + Assert.IsTrue(tree.Exists(new Filter(always))); + } + + + [Test] + public void FindAll() + { + Assert.AreEqual(0, tree.FindAll(new Filter(never)).Count); + for (int i = 0; i < 10; i++) + tree.Add(i); + + Assert.AreEqual(0, tree.FindAll(new Filter(never)).Count); + Assert.AreEqual(10, tree.FindAll(new Filter(always)).Count); + Assert.AreEqual(5, tree.FindAll(new Filter(even)).Count); + Assert.IsTrue(((TreeSet)tree.FindAll(new Filter(even))).Check("R")); + } + + + [Test] + public void Map() + { + Assert.AreEqual(0, tree.Map(new Mapper(themap), new SC()).Count); + for (int i = 0; i < 11; i++) + tree.Add(i * i * i); + + IIndexedSorted res = tree.Map(new Mapper(themap), new SC()); + + Assert.IsTrue(((TreeSet)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 res = tree.Map(new Mapper(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 tree; + + + [SetUp] + public void Init() { tree = new TreeSet(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 tree; + + + [SetUp] + public void Init() { tree = new TreeSet(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 tree, tree2; + + + [SetUp] + public void Init() + { + tree = new TreeSet(new IC()); + tree2 = new TreeSet(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 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 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(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 tree; + + int sz = 500000; + + + [SetUp] + public void Init() + { + tree = new TreeSet(new IC()); + for (int i = 0; i < sz; i++) + { + tree.Add(i); + } + } + + + + class A + { + public int count = 0; + + TreeSet t; + + + public A(TreeSet t) { this.t = t; } + + + public void a(int i) { count++; } + + + public void traverse() { t.Apply(new Applier(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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new TreeSet(new IC()); + dat = new TreeSet(new IC()); + dut = new TreeSet(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 dit, dat, dut; + + + [SetUp] + public void Init() + { + dit = new TreeSet(new IC()); + dat = new TreeSet(new IC()); + dut = new TreeSet(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 -- cgit v1.2.3