1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.google.common.collect; 16 17 import static com.google.common.collect.BoundType.CLOSED; 18 import static com.google.common.collect.BoundType.OPEN; 19 20 import com.google.common.annotations.GwtCompatible; 21 import com.google.common.annotations.GwtIncompatible; 22 import com.google.common.base.Objects; 23 import com.google.common.testing.NullPointerTester; 24 25 import junit.framework.TestCase; 26 27 import java.util.Arrays; 28 import java.util.List; 29 30 /** 31 * Tests for {@code GeneralRange}. 32 * 33 * @author Louis Wasserman 34 */ 35 @GwtCompatible(emulated = true) 36 public class GeneralRangeTest extends TestCase { 37 private static final Ordering<Integer> ORDERING = Ordering.natural().nullsFirst(); 38 39 private static final List<Integer> IN_ORDER_VALUES = Arrays.asList(null, 1, 2, 3, 4, 5); 40 testCreateEmptyRangeFails()41 public void testCreateEmptyRangeFails() { 42 for (BoundType lboundType : BoundType.values()) { 43 for (BoundType uboundType : BoundType.values()) { 44 try { 45 GeneralRange.range(ORDERING, 4, lboundType, 2, uboundType); 46 fail("Expected IAE"); 47 } catch (IllegalArgumentException expected) {} 48 } 49 } 50 } 51 testCreateEmptyRangeOpenOpenFails()52 public void testCreateEmptyRangeOpenOpenFails() { 53 for (Integer i : IN_ORDER_VALUES) { 54 try { 55 GeneralRange.range(ORDERING, i, OPEN, i, OPEN); 56 fail("Expected IAE"); 57 } catch (IllegalArgumentException expected) {} 58 } 59 } 60 testCreateEmptyRangeClosedOpenSucceeds()61 public void testCreateEmptyRangeClosedOpenSucceeds() { 62 for (Integer i : IN_ORDER_VALUES) { 63 GeneralRange<Integer> range = GeneralRange.range(ORDERING, i, CLOSED, i, OPEN); 64 for (Integer j : IN_ORDER_VALUES) { 65 assertFalse(range.contains(j)); 66 } 67 } 68 } 69 testCreateEmptyRangeOpenClosedSucceeds()70 public void testCreateEmptyRangeOpenClosedSucceeds() { 71 for (Integer i : IN_ORDER_VALUES) { 72 GeneralRange<Integer> range = GeneralRange.range(ORDERING, i, OPEN, i, CLOSED); 73 for (Integer j : IN_ORDER_VALUES) { 74 assertFalse(range.contains(j)); 75 } 76 } 77 } 78 testCreateSingletonRangeSucceeds()79 public void testCreateSingletonRangeSucceeds() { 80 for (Integer i : IN_ORDER_VALUES) { 81 GeneralRange<Integer> range = GeneralRange.range(ORDERING, i, CLOSED, i, CLOSED); 82 for (Integer j : IN_ORDER_VALUES) { 83 assertEquals(Objects.equal(i, j), range.contains(j)); 84 } 85 } 86 } 87 testSingletonRange()88 public void testSingletonRange() { 89 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 3, CLOSED, 3, CLOSED); 90 for (Integer i : IN_ORDER_VALUES) { 91 assertEquals(ORDERING.compare(i, 3) == 0, range.contains(i)); 92 } 93 } 94 testLowerRange()95 public void testLowerRange() { 96 for (BoundType lBoundType : BoundType.values()) { 97 GeneralRange<Integer> range = GeneralRange.downTo(ORDERING, 3, lBoundType); 98 for (Integer i : IN_ORDER_VALUES) { 99 assertEquals(ORDERING.compare(i, 3) > 0 100 || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), range.contains(i)); 101 assertEquals(ORDERING.compare(i, 3) < 0 102 || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), range.tooLow(i)); 103 assertFalse(range.tooHigh(i)); 104 } 105 } 106 } 107 108 public void testUpperRange() { 109 for (BoundType lBoundType : BoundType.values()) { 110 GeneralRange<Integer> range = GeneralRange.upTo(ORDERING, 3, lBoundType); 111 for (Integer i : IN_ORDER_VALUES) { 112 assertEquals(ORDERING.compare(i, 3) < 0 113 || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), range.contains(i)); 114 assertEquals(ORDERING.compare(i, 3) > 0 115 || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), range.tooHigh(i)); 116 assertFalse(range.tooLow(i)); 117 } 118 } 119 } 120 121 public void testDoublyBoundedAgainstRange() { 122 for (BoundType lboundType : BoundType.values()) { 123 for (BoundType uboundType : BoundType.values()) { 124 Range<Integer> range = Range.range(2, lboundType, 4, uboundType); 125 GeneralRange<Integer> gRange = GeneralRange.range(ORDERING, 2, lboundType, 4, uboundType); 126 for (Integer i : IN_ORDER_VALUES) { 127 assertEquals(i != null && range.contains(i), gRange.contains(i)); 128 } 129 } 130 } 131 } 132 133 public void testIntersectAgainstMatchingEndpointsRange() { 134 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN); 135 assertEquals(GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN), 136 range.intersect(GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED))); 137 } 138 139 public void testIntersectAgainstBiggerRange() { 140 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN); 141 142 assertEquals(GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN), 143 range.intersect(GeneralRange.range(ORDERING, null, OPEN, 5, CLOSED))); 144 145 assertEquals(GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN), 146 range.intersect(GeneralRange.range(ORDERING, 2, OPEN, 5, CLOSED))); 147 148 assertEquals(GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN), 149 range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 4, OPEN))); 150 } 151 152 public void testIntersectAgainstSmallerRange() { 153 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN); 154 assertEquals(GeneralRange.range(ORDERING, 3, CLOSED, 4, OPEN), 155 range.intersect(GeneralRange.range(ORDERING, 3, CLOSED, 4, CLOSED))); 156 } 157 158 public void testIntersectOverlappingRange() { 159 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED); 160 assertEquals(GeneralRange.range(ORDERING, 3, CLOSED, 4, CLOSED), 161 range.intersect(GeneralRange.range(ORDERING, 3, CLOSED, 5, CLOSED))); 162 assertEquals(GeneralRange.range(ORDERING, 2, OPEN, 3, OPEN), 163 range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 3, OPEN))); 164 } 165 166 public void testIntersectNonOverlappingRange() { 167 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED); 168 assertTrue(range.intersect(GeneralRange.range(ORDERING, 5, CLOSED, 6, CLOSED)).isEmpty()); 169 assertTrue(range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 2, OPEN)).isEmpty()); 170 } 171 172 public void testFromRangeAll() { 173 assertEquals(GeneralRange.all(Ordering.natural()), GeneralRange.from(Range.all())); 174 } 175 176 public void testFromRangeOneEnd() { 177 for (BoundType endpointType : BoundType.values()) { 178 assertEquals(GeneralRange.upTo(Ordering.natural(), 3, endpointType), 179 GeneralRange.from(Range.upTo(3, endpointType))); 180 181 assertEquals(GeneralRange.downTo(Ordering.natural(), 3, endpointType), 182 GeneralRange.from(Range.downTo(3, endpointType))); 183 } 184 } 185 186 public void testFromRangeTwoEnds() { 187 for (BoundType lowerType : BoundType.values()) { 188 for (BoundType upperType : BoundType.values()) { 189 assertEquals(GeneralRange.range(Ordering.natural(), 3, lowerType, 4, upperType), 190 GeneralRange.from(Range.range(3, lowerType, 4, upperType))); 191 } 192 } 193 } 194 195 public void testReverse() { 196 assertEquals(GeneralRange.all(ORDERING.reverse()), GeneralRange.all(ORDERING).reverse()); 197 assertEquals(GeneralRange.downTo(ORDERING.reverse(), 3, CLOSED), 198 GeneralRange.upTo(ORDERING, 3, CLOSED).reverse()); 199 assertEquals(GeneralRange.upTo(ORDERING.reverse(), 3, OPEN), 200 GeneralRange.downTo(ORDERING, 3, OPEN).reverse()); 201 assertEquals(GeneralRange.range(ORDERING.reverse(), 5, OPEN, 3, CLOSED), 202 GeneralRange.range(ORDERING, 3, CLOSED, 5, OPEN).reverse()); 203 } 204 205 @GwtIncompatible("NullPointerTester") 206 public void testNullPointers() { 207 new NullPointerTester().testAllPublicStaticMethods(GeneralRange.class); 208 } 209 } 210