1 /* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.annotations.GwtCompatible; 20 import junit.framework.AssertionFailedError; 21 import junit.framework.TestCase; 22 23 /** 24 * Unit test for {@link ComparisonChain}. 25 * 26 * @author Kevin Bourrillion 27 */ 28 @GwtCompatible 29 public class ComparisonChainTest extends TestCase { 30 private static final DontCompareMe DONT_COMPARE_ME = new DontCompareMe(); 31 32 private static class DontCompareMe implements Comparable<DontCompareMe> { 33 @Override compareTo(DontCompareMe o)34 public int compareTo(DontCompareMe o) { 35 throw new AssertionFailedError(); 36 } 37 } 38 testCompareBooleans()39 public void testCompareBooleans() { 40 assertEquals( 41 0, 42 ComparisonChain.start() 43 .compare(true, true) 44 .compare(true, Boolean.TRUE) 45 .compare(Boolean.TRUE, true) 46 .compare(Boolean.TRUE, Boolean.TRUE) 47 .result()); 48 } 49 testDegenerate()50 public void testDegenerate() { 51 // kinda bogus, but who cares? 52 assertEquals(0, ComparisonChain.start().result()); 53 } 54 testOneEqual()55 public void testOneEqual() { 56 assertEquals(0, ComparisonChain.start().compare("a", "a").result()); 57 } 58 testOneEqualUsingComparator()59 public void testOneEqualUsingComparator() { 60 assertEquals( 61 0, ComparisonChain.start().compare("a", "A", String.CASE_INSENSITIVE_ORDER).result()); 62 } 63 testManyEqual()64 public void testManyEqual() { 65 assertEquals( 66 0, 67 ComparisonChain.start() 68 .compare(1, 1) 69 .compare(1L, 1L) 70 .compareFalseFirst(true, true) 71 .compare(1.0, 1.0) 72 .compare(1.0f, 1.0f) 73 .compare("a", "a", Ordering.usingToString()) 74 .result()); 75 } 76 testShortCircuitLess()77 public void testShortCircuitLess() { 78 assertTrue( 79 ComparisonChain.start().compare("a", "b").compare(DONT_COMPARE_ME, DONT_COMPARE_ME).result() 80 < 0); 81 } 82 83 public void testShortCircuitGreater() { 84 assertTrue( 85 ComparisonChain.start().compare("b", "a").compare(DONT_COMPARE_ME, DONT_COMPARE_ME).result() 86 > 0); 87 } 88 testShortCircuitSecondStep()89 public void testShortCircuitSecondStep() { 90 assertTrue( 91 ComparisonChain.start() 92 .compare("a", "a") 93 .compare("a", "b") 94 .compare(DONT_COMPARE_ME, DONT_COMPARE_ME) 95 .result() 96 < 0); 97 } 98 99 public void testCompareFalseFirst() { 100 assertTrue(ComparisonChain.start().compareFalseFirst(true, true).result() == 0); 101 assertTrue(ComparisonChain.start().compareFalseFirst(true, false).result() > 0); 102 assertTrue(ComparisonChain.start().compareFalseFirst(false, true).result() < 0); 103 assertTrue(ComparisonChain.start().compareFalseFirst(false, false).result() == 0); 104 } 105 106 public void testCompareTrueFirst() { 107 assertTrue(ComparisonChain.start().compareTrueFirst(true, true).result() == 0); 108 assertTrue(ComparisonChain.start().compareTrueFirst(true, false).result() < 0); 109 assertTrue(ComparisonChain.start().compareTrueFirst(false, true).result() > 0); 110 assertTrue(ComparisonChain.start().compareTrueFirst(false, false).result() == 0); 111 } 112 } 113