/* Copyright (c) 2003-2006 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 SCG = System.Collections.Generic; namespace C5UnitTests.support { namespace bases { [TestFixture] public class ArrayBaseTest { class ABT : ArrayBase { public ABT() : base(8,NaturalEqualityComparer.Default) { } public override string Choose() { if (size > 0) return array[0]; throw new NoSuchItemException(); } 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] [ExpectedException(typeof(NotComparableException))] public void NotComparable() { SCG.IComparer foo = Comparer.Default; } [Test] public void GenericC() { SCG.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() { SCG.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() { SCG.IComparer h = Comparer.Default; 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); Assert.AreSame(h, Comparer.Default); } [Test] public void OrdinaryCViaBuilder() { SCG.IComparer h = Comparer.Default; string s = "bamse"; string t = "bamse"; string u = "bimse"; Assert.AreEqual(0, h.Compare(s, t)); Assert.IsTrue(h.Compare(s, u) < 0); Assert.AreSame(h, Comparer.Default); } public void ComparerViaBuilderTest(T item1, T item2) where T : IComparable { SCG.IComparer h = Comparer.Default; Assert.AreSame(h, Comparer.Default); Assert.AreEqual(0, h.Compare(item1, item1)); Assert.AreEqual(0, h.Compare(item2, item2)); Assert.IsTrue(h.Compare(item1, item2) < 0); Assert.IsTrue(h.Compare(item2, item1) > 0); Assert.AreEqual(Math.Sign(item1.CompareTo(item2)), Math.Sign(h.Compare(item1, item2))); Assert.AreEqual(Math.Sign(item2.CompareTo(item1)), Math.Sign(h.Compare(item2, item1))); } [Test] public void PrimitiveComparersViaBuilder() { ComparerViaBuilderTest('A', 'a'); ComparerViaBuilderTest(-122, 126); ComparerViaBuilderTest(122, 126); ComparerViaBuilderTest(-30000, 3); ComparerViaBuilderTest(3, 50000); ComparerViaBuilderTest(-10000000, 10000); ComparerViaBuilderTest(10000000, 3000000000); ComparerViaBuilderTest(-1000000000000, 10000000); ComparerViaBuilderTest(10000000000000UL, 10000000000004UL); ComparerViaBuilderTest(-0.001F, 0.00001F); ComparerViaBuilderTest(-0.001, 0.00001E-200); ComparerViaBuilderTest(-20.001M, 19.999M); } // This test is obsoleted by the one above, but we keep it for good measure [Test] public void IntComparerViaBuilder() { SCG.IComparer h = Comparer.Default; int s = 4; int t = 4; int u = 5; Assert.AreEqual(0, h.Compare(s, t)); Assert.IsTrue(h.Compare(s, u) < 0); Assert.AreSame(h, Comparer.Default); } [Test] public void Nulls() { Assert.IsTrue(Comparer.Default.Compare(null, "abe") < 0); Assert.IsTrue(Comparer.Default.Compare(null, null) == 0); Assert.IsTrue(Comparer.Default.Compare("abe", null) > 0); } } [TestFixture] public class EqualityComparers { [Test] public void ReftypeequalityComparer() { SCG.IEqualityComparer h = NaturalEqualityComparer.Default; 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 ValuetypeequalityComparer() { SCG.IEqualityComparer h = NaturalEqualityComparer.Default; 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)); } internal class REHTest { public override int GetHashCode() { return 37; } } [Test] public void ReferenceEqualityEqualityComparerTest() { REHTest rehtest = new REHTest(); SCG.IEqualityComparer equalityComparer = ReferenceEqualityComparer.Default; Assert.AreEqual(37, rehtest.GetHashCode()); Assert.IsFalse(equalityComparer.GetHashCode(rehtest) == 37); } [Test] public void ReftypeequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; 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)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void ValuetypeequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; 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)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void CharequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; char s = 'å'; char t = 'å'; char u = 'r'; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void SbyteequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; sbyte s = 3; sbyte t = 3; sbyte u = -5; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void ByteequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; byte s = 3; byte t = 3; byte u = 5; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void ShortequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; short s = 3; short t = 3; short u = -5; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void UshortequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; ushort s = 3; ushort t = 3; ushort u = 60000; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void IntequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; 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)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void UintequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; uint s = 3; uint t = 3; uint u = 3000000000; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void LongequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; long s = 3; long t = 3; long u = -500000000000000L; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void UlongequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; ulong s = 3; ulong t = 3; ulong u = 500000000000000UL; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void FloatequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; float s = 3.1F; float t = 3.1F; float u = -5.2F; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void DoubleequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; double s = 3.12345; double t = 3.12345; double u = -5.2; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void DecimalequalityComparerViaBuilder() { SCG.IEqualityComparer h = EqualityComparer.Default; decimal s = 3.0001M; decimal t = 3.0001M; decimal u = -500000000000000M; Assert.AreEqual(s.GetHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer.Default); } [Test] public void UnseqequalityComparerViaBuilder() { SCG.IEqualityComparer> h = EqualityComparer>.Default; 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.GetUnsequencedHashCode(), h.GetHashCode(s)); Assert.IsTrue(h.Equals(s, t)); Assert.IsFalse(h.Equals(s, u)); Assert.AreSame(h, EqualityComparer>.Default); } [Test] public void SeqequalityComparerViaBuilder2() { SCG.IEqualityComparer> h = EqualityComparer>.Default; LinkedList s = new LinkedList(); s.Add(1); s.Add(2); s.Add(3); Assert.AreEqual(CHC.sequencedhashcode(1, 2, 3), h.GetHashCode(s)); } [Test] public void UnseqequalityComparerViaBuilder2() { SCG.IEqualityComparer> h = EqualityComparer>.Default; HashSet s = new HashSet(); s.Add(1); s.Add(2); s.Add(3); Assert.AreEqual(CHC.unsequencedhashcode(1, 2, 3), h.GetHashCode(s)); } //generic types implementing collection interfaces [Test] public void SeqequalityComparerViaBuilder3() { SCG.IEqualityComparer> h = EqualityComparer>.Default; IList s = new LinkedList(); s.Add(1); s.Add(2); s.Add(3); Assert.AreEqual(CHC.sequencedhashcode(1, 2, 3), h.GetHashCode(s)); } interface IFoo : ICollection { void Bamse(); } class Foo : HashSet, IFoo { internal Foo() : base() { } public void Bamse() { } } [Test] public void UnseqequalityComparerViaBuilder3() { SCG.IEqualityComparer> h = EqualityComparer>.Default; IFoo s = new Foo(); s.Add(1); s.Add(2); s.Add(3); Assert.AreEqual(CHC.unsequencedhashcode(1, 2, 3), h.GetHashCode(s)); } //Nongeneric types implementing collection types: interface IBaz : ISequenced { void Bamse(); } class Baz : LinkedList, IBaz { internal Baz() : base() { } public void Bamse() { } //int ISequenced.GetHashCode() { return sequencedhashcode(); } //bool ISequenced.Equals(ISequenced that) { return sequencedequals(that); } } [Test] public void SeqequalityComparerViaBuilder4() { SCG.IEqualityComparer h = EqualityComparer.Default; IBaz s = new Baz(); s.Add(1); s.Add(2); s.Add(3); Assert.AreEqual(CHC.sequencedhashcode(1, 2, 3), h.GetHashCode(s)); } interface IBar : ICollection { void Bamse(); } class Bar : HashSet, IBar { internal Bar() : base() { } public void Bamse() { } //TODO: remove all this workaround stuff: bool ICollection.ContainsAll(System.Collections.Generic.IEnumerable items) { throw new NotImplementedException(); } void ICollection.RemoveAll(System.Collections.Generic.IEnumerable items) { throw new NotImplementedException(); } void ICollection.RetainAll(System.Collections.Generic.IEnumerable items) { throw new NotImplementedException(); } void IExtensible.AddAll(System.Collections.Generic.IEnumerable items) { throw new NotImplementedException(); } } [Test] public void UnseqequalityComparerViaBuilder4() { SCG.IEqualityComparer h = EqualityComparer.Default; IBar s = new Bar(); s.Add(1); s.Add(2); s.Add(3); Assert.AreEqual(CHC.unsequencedhashcode(1, 2, 3), h.GetHashCode(s)); } [Test] public void StaticEqualityComparerWithNull() { ArrayList arr = new ArrayList(); SCG.IEqualityComparer eqc = EqualityComparer.Default; Assert.IsTrue(CollectionBase.StaticEquals(arr, arr, eqc)); Assert.IsTrue(CollectionBase.StaticEquals(null, null, eqc)); Assert.IsFalse(CollectionBase.StaticEquals(arr, null, eqc)); Assert.IsFalse(CollectionBase.StaticEquals(null, arr, eqc)); } } } }