1 /* 2 * Copyright (C) 2016 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 import static java.util.Arrays.asList; 21 import static java.util.Comparator.comparing; 22 import static java.util.Comparator.naturalOrder; 23 24 import com.google.common.annotations.GwtCompatible; 25 import com.google.common.collect.testing.Helpers; 26 import com.google.common.testing.CollectorTester; 27 import com.google.common.testing.EqualsTester; 28 import java.util.Arrays; 29 import java.util.Collections; 30 import java.util.Comparator; 31 import java.util.Optional; 32 import junit.framework.TestCase; 33 34 /** 35 * Tests for {@code Comparators}. 36 * 37 * @author Louis Wasserman 38 */ 39 @GwtCompatible 40 public class ComparatorsTest extends TestCase { 41 @SuppressWarnings("unchecked") // dang varargs testLexicographical()42 public void testLexicographical() { 43 Comparator<String> comparator = Ordering.natural(); 44 Comparator<Iterable<String>> lexy = Comparators.lexicographical(comparator); 45 46 ImmutableList<String> empty = ImmutableList.of(); 47 ImmutableList<String> a = ImmutableList.of("a"); 48 ImmutableList<String> aa = ImmutableList.of("a", "a"); 49 ImmutableList<String> ab = ImmutableList.of("a", "b"); 50 ImmutableList<String> b = ImmutableList.of("b"); 51 52 Helpers.testComparator(lexy, empty, a, aa, ab, b); 53 54 new EqualsTester() 55 .addEqualityGroup(lexy, Comparators.lexicographical(comparator)) 56 .addEqualityGroup(Comparators.lexicographical(String.CASE_INSENSITIVE_ORDER)) 57 .addEqualityGroup(Ordering.natural()) 58 .testEquals(); 59 } 60 testIsInOrder()61 public void testIsInOrder() { 62 assertFalse(Comparators.isInOrder(asList(5, 3, 0, 9), Ordering.natural())); 63 assertFalse(Comparators.isInOrder(asList(0, 5, 3, 9), Ordering.natural())); 64 assertTrue(Comparators.isInOrder(asList(0, 3, 5, 9), Ordering.natural())); 65 assertTrue(Comparators.isInOrder(asList(0, 0, 3, 3), Ordering.natural())); 66 assertTrue(Comparators.isInOrder(asList(0, 3), Ordering.natural())); 67 assertTrue(Comparators.isInOrder(Collections.singleton(1), Ordering.natural())); 68 assertTrue(Comparators.isInOrder(Collections.<Integer>emptyList(), Ordering.natural())); 69 } 70 testIsInStrictOrder()71 public void testIsInStrictOrder() { 72 assertFalse(Comparators.isInStrictOrder(asList(5, 3, 0, 9), Ordering.natural())); 73 assertFalse(Comparators.isInStrictOrder(asList(0, 5, 3, 9), Ordering.natural())); 74 assertTrue(Comparators.isInStrictOrder(asList(0, 3, 5, 9), Ordering.natural())); 75 assertFalse(Comparators.isInStrictOrder(asList(0, 0, 3, 3), Ordering.natural())); 76 assertTrue(Comparators.isInStrictOrder(asList(0, 3), Ordering.natural())); 77 assertTrue(Comparators.isInStrictOrder(Collections.singleton(1), Ordering.natural())); 78 assertTrue(Comparators.isInStrictOrder(Collections.<Integer>emptyList(), Ordering.natural())); 79 } 80 testLeastCollector()81 public void testLeastCollector() { 82 CollectorTester.of(Comparators.<Integer>least(2, Comparator.naturalOrder())) 83 .expectCollects(Arrays.asList(1, 2), 1, 2, 3, 4, 5, 6) 84 .expectCollects(Arrays.asList(1), 1) 85 .expectCollects(Collections.emptyList()); 86 } 87 testGreatestCollector()88 public void testGreatestCollector() { 89 CollectorTester.of(Comparators.<Integer>greatest(2, Comparator.naturalOrder())) 90 .expectCollects(Arrays.asList(6, 5), 1, 2, 3, 4, 5, 6) 91 .expectCollects(Arrays.asList(1), 1) 92 .expectCollects(Collections.emptyList()); 93 } 94 testEmptiesFirst()95 public void testEmptiesFirst() { 96 Optional<String> empty = Optional.empty(); 97 Optional<String> abc = Optional.of("abc"); 98 Optional<String> z = Optional.of("z"); 99 100 Comparator<Optional<String>> comparator = Comparators.emptiesFirst(comparing(String::length)); 101 Helpers.testComparator(comparator, empty, z, abc); 102 103 // Just demonstrate that no explicit type parameter is required 104 comparator = Comparators.emptiesFirst(naturalOrder()); 105 } 106 testEmptiesLast()107 public void testEmptiesLast() { 108 Optional<String> empty = Optional.empty(); 109 Optional<String> abc = Optional.of("abc"); 110 Optional<String> z = Optional.of("z"); 111 112 Comparator<Optional<String>> comparator = Comparators.emptiesLast(comparing(String::length)); 113 Helpers.testComparator(comparator, z, abc, empty); 114 115 // Just demonstrate that no explicit type parameter is required 116 comparator = Comparators.emptiesLast(naturalOrder()); 117 } 118 testMinMaxNatural()119 public void testMinMaxNatural() { 120 assertThat(Comparators.min(1, 2)).isEqualTo(1); 121 assertThat(Comparators.min(2, 1)).isEqualTo(1); 122 assertThat(Comparators.max(1, 2)).isEqualTo(2); 123 assertThat(Comparators.max(2, 1)).isEqualTo(2); 124 } 125 testMinMaxNatural_equalInstances()126 public void testMinMaxNatural_equalInstances() { 127 Foo a = new Foo(1); 128 Foo b = new Foo(1); 129 assertThat(Comparators.min(a, b)).isSameInstanceAs(a); 130 assertThat(Comparators.max(a, b)).isSameInstanceAs(a); 131 } 132 testMinMaxComparator()133 public void testMinMaxComparator() { 134 Comparator<Integer> natural = Ordering.natural(); 135 Comparator<Integer> reverse = Collections.reverseOrder(natural); 136 assertThat(Comparators.min(1, 2, reverse)).isEqualTo(2); 137 assertThat(Comparators.min(2, 1, reverse)).isEqualTo(2); 138 assertThat(Comparators.max(1, 2, reverse)).isEqualTo(1); 139 assertThat(Comparators.max(2, 1, reverse)).isEqualTo(1); 140 } 141 testMinMaxComparator_equalInstances()142 public void testMinMaxComparator_equalInstances() { 143 Comparator<Foo> natural = Ordering.natural(); 144 Comparator<Foo> reverse = Collections.reverseOrder(natural); 145 Foo a = new Foo(1); 146 Foo b = new Foo(1); 147 assertThat(Comparators.min(a, b, reverse)).isSameInstanceAs(a); 148 assertThat(Comparators.max(a, b, reverse)).isSameInstanceAs(a); 149 } 150 151 private static class Foo implements Comparable<Foo> { 152 final Integer value; 153 Foo(int value)154 Foo(int value) { 155 this.value = value; 156 } 157 158 @Override hashCode()159 public int hashCode() { 160 return value.hashCode(); 161 } 162 163 @Override equals(Object o)164 public boolean equals(Object o) { 165 return (o instanceof Foo) && ((Foo) o).value.equals(value); 166 } 167 168 @Override compareTo(Foo other)169 public int compareTo(Foo other) { 170 return value.compareTo(other.value); 171 } 172 } 173 } 174