• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import junit.framework.AssertionFailedError;
23 import junit.framework.TestCase;
24 
25 /**
26  * Unit test for {@link ComparisonChain}.
27  *
28  * @author Kevin Bourrillion
29  */
30 @GwtCompatible
31 public class ComparisonChainTest extends TestCase {
32   private static final DontCompareMe DONT_COMPARE_ME = new DontCompareMe();
33 
34   private static class DontCompareMe implements Comparable<DontCompareMe> {
35     @Override
compareTo(DontCompareMe o)36     public int compareTo(DontCompareMe o) {
37       throw new AssertionFailedError();
38     }
39   }
40 
41   @SuppressWarnings("deprecation")
testCompareBooleans()42   public void testCompareBooleans() {
43     assertThat(
44             ComparisonChain.start()
45                 .compare(true, true)
46                 .compare(true, Boolean.TRUE)
47                 .compare(Boolean.TRUE, true)
48                 .compare(Boolean.TRUE, Boolean.TRUE)
49                 .result())
50         .isEqualTo(0);
51   }
52 
testDegenerate()53   public void testDegenerate() {
54     // kinda bogus, but who cares?
55     assertThat(ComparisonChain.start().result()).isEqualTo(0);
56   }
57 
testOneEqual()58   public void testOneEqual() {
59     assertThat(ComparisonChain.start().compare("a", "a").result()).isEqualTo(0);
60   }
61 
testOneEqualUsingComparator()62   public void testOneEqualUsingComparator() {
63     assertThat(ComparisonChain.start().compare("a", "A", String.CASE_INSENSITIVE_ORDER).result())
64         .isEqualTo(0);
65   }
66 
testManyEqual()67   public void testManyEqual() {
68     assertThat(
69             ComparisonChain.start()
70                 .compare(1, 1)
71                 .compare(1L, 1L)
72                 .compareFalseFirst(true, true)
73                 .compare(1.0, 1.0)
74                 .compare(1.0f, 1.0f)
75                 .compare("a", "a", Ordering.usingToString())
76                 .result())
77         .isEqualTo(0);
78   }
79 
testShortCircuitLess()80   public void testShortCircuitLess() {
81     assertThat(
82             ComparisonChain.start()
83                 .compare("a", "b")
84                 .compare(DONT_COMPARE_ME, DONT_COMPARE_ME)
85                 .result())
86         .isLessThan(0);
87   }
88 
testShortCircuitGreater()89   public void testShortCircuitGreater() {
90     assertThat(
91             ComparisonChain.start()
92                 .compare("b", "a")
93                 .compare(DONT_COMPARE_ME, DONT_COMPARE_ME)
94                 .result())
95         .isGreaterThan(0);
96   }
97 
testShortCircuitSecondStep()98   public void testShortCircuitSecondStep() {
99     assertThat(
100             ComparisonChain.start()
101                 .compare("a", "a")
102                 .compare("a", "b")
103                 .compare(DONT_COMPARE_ME, DONT_COMPARE_ME)
104                 .result())
105         .isLessThan(0);
106   }
107 
testCompareFalseFirst()108   public void testCompareFalseFirst() {
109     assertThat(ComparisonChain.start().compareFalseFirst(true, true).result()).isEqualTo(0);
110     assertThat(ComparisonChain.start().compareFalseFirst(true, false).result()).isGreaterThan(0);
111     assertThat(ComparisonChain.start().compareFalseFirst(false, true).result()).isLessThan(0);
112     assertThat(ComparisonChain.start().compareFalseFirst(false, false).result()).isEqualTo(0);
113   }
114 
testCompareTrueFirst()115   public void testCompareTrueFirst() {
116     assertThat(ComparisonChain.start().compareTrueFirst(true, true).result()).isEqualTo(0);
117     assertThat(ComparisonChain.start().compareTrueFirst(true, false).result()).isLessThan(0);
118     assertThat(ComparisonChain.start().compareTrueFirst(false, true).result()).isGreaterThan(0);
119     assertThat(ComparisonChain.start().compareTrueFirst(false, false).result()).isEqualTo(0);
120   }
121 }
122