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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20
21import junit.framework.AssertionFailedError;
22import junit.framework.TestCase;
23
24/**
25 * Unit test for {@link ComparisonChain}.
26 *
27 * @author Kevin Bourrillion
28 */
29@GwtCompatible
30public class ComparisonChainTest extends TestCase {
31  private static final DontCompareMe DONT_COMPARE_ME = new DontCompareMe();
32
33  private static class DontCompareMe implements Comparable<DontCompareMe> {
34    @Override
35    public int compareTo(DontCompareMe o) {
36      throw new AssertionFailedError();
37    }
38  }
39
40  public void testDegenerate() {
41    // kinda bogus, but who cares?
42    assertEquals(0, ComparisonChain.start().result());
43  }
44
45  public void testOneEqual() {
46    assertEquals(0, ComparisonChain.start()
47        .compare("a", "a")
48        .result());
49  }
50
51  public void testOneEqualUsingComparator() {
52    assertEquals(0, ComparisonChain.start()
53        .compare("a", "A", String.CASE_INSENSITIVE_ORDER)
54        .result());
55  }
56
57  public void testManyEqual() {
58    assertEquals(0, ComparisonChain.start()
59        .compare(1, 1)
60        .compare(1L, 1L)
61        .compare(true, true)
62        .compare(1.0, 1.0)
63        .compare(1.0f, 1.0f)
64        .compare("a", "a", Ordering.usingToString())
65        .result());
66  }
67
68  public void testShortCircuitLess() {
69    assertTrue(ComparisonChain.start()
70        .compare("a", "b")
71        .compare(DONT_COMPARE_ME, DONT_COMPARE_ME)
72        .result() < 0);
73  }
74
75  public void testShortCircuitGreater() {
76    assertTrue(ComparisonChain.start()
77        .compare("b", "a")
78        .compare(DONT_COMPARE_ME, DONT_COMPARE_ME)
79        .result() > 0);
80  }
81
82  public void testShortCircuitSecondStep() {
83    assertTrue(ComparisonChain.start()
84        .compare("a", "a")
85        .compare("a", "b")
86        .compare(DONT_COMPARE_ME, DONT_COMPARE_ME)
87        .result() < 0);
88  }
89}
90