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