#region CPL License /* Nuclex Framework Copyright (C) 2002-2009 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Generic; #if UNITTEST using NUnit.Framework; using NMock2; namespace Nuclex.Support.Collections { /// Unit Test for the ReverseComparer helper class [TestFixture] public class ReverseComparerTest { #region class FortyTwoComparer /// Special comparer in which 42 is larger than everything private class FortyTwoComparer : IComparer { /// Compares the left value to the right value /// Value on the left side /// Value on the right side /// The relationship of the two values public int Compare(int left, int right) { // Is there a 42 in the arguments? if(left == 42) { if(right == 42) { return 0; // both are equal } else { return +1; // left is larger } } else if(right == 42) { return -1; // right is larger } // No 42 encountered, proceed as normal return Math.Sign(left - right); } } #endregion // class FortyTwoComparer /// /// Tests whether the default constructor of the reverse comparer works /// [Test] public void TestDefaultConstructor() { new ReverseComparer(); } /// /// Tests whether the full constructor of the reverse comparer works /// [Test] public void TestFullConstructor() { new ReverseComparer(new FortyTwoComparer()); } /// /// Tests whether the full constructor of the reverse comparer works /// [Test] public void TestReversedDefaultComparer() { Comparer comparer = Comparer.Default; ReverseComparer reverseComparer = new ReverseComparer(comparer); Assert.Greater(0, comparer.Compare(10, 20)); Assert.Less(0, comparer.Compare(20, 10)); Assert.Less(0, reverseComparer.Compare(10, 20)); Assert.Greater(0, reverseComparer.Compare(20, 10)); } /// /// Tests whether the full constructor of the reverse comparer works /// [Test] public void TestReversedCustomComparer() { FortyTwoComparer fortyTwoComparer = new FortyTwoComparer(); ReverseComparer reverseFortyTwoComparer = new ReverseComparer( fortyTwoComparer ); Assert.Less(0, fortyTwoComparer.Compare(42, 84)); Assert.Greater(0, fortyTwoComparer.Compare(84, 42)); Assert.Greater(0, reverseFortyTwoComparer.Compare(42, 84)); Assert.Less(0, reverseFortyTwoComparer.Compare(84, 42)); } } } // namespace Nuclex.Support.Collections #endif // UNITTEST