#region Apache License 2.0 /* Nuclex .NET Framework Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #endregion // Apache License 2.0 using System; using System.Collections.Generic; #if UNITTEST using NUnit.Framework; namespace Nuclex.Support.Collections { /// Unit Test for the ReverseComparer helper class [TestFixture] internal 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