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 import java.util.Arrays; 25 import java.util.List; 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for {@code GeneralRange}. 30 * 31 * @author Louis Wasserman 32 */ 33 @GwtCompatible(emulated = true) 34 public class GeneralRangeTest extends TestCase { 35 private static final Ordering<Integer> ORDERING = Ordering.natural().nullsFirst(); 36 37 private static final List<Integer> IN_ORDER_VALUES = Arrays.asList(null, 1, 2, 3, 4, 5); 38 testCreateEmptyRangeFails()39 public void testCreateEmptyRangeFails() { 40 for (BoundType lboundType : BoundType.values()) { 41 for (BoundType uboundType : BoundType.values()) { 42 try { 43 GeneralRange.range(ORDERING, 4, lboundType, 2, uboundType); 44 fail("Expected IAE"); 45 } catch (IllegalArgumentException expected) { 46 } 47 } 48 } 49 } 50 testCreateEmptyRangeOpenOpenFails()51 public void testCreateEmptyRangeOpenOpenFails() { 52 for (Integer i : IN_ORDER_VALUES) { 53 try { 54 GeneralRange.range(ORDERING, i, OPEN, i, OPEN); 55 fail("Expected IAE"); 56 } catch (IllegalArgumentException expected) { 57 } 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( 100 ORDERING.compare(i, 3) > 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), 101 range.contains(i)); 102 assertEquals( 103 ORDERING.compare(i, 3) < 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), 104 range.tooLow(i)); 105 assertFalse(range.tooHigh(i)); 106 } 107 } 108 } 109 110 public void testUpperRange() { 111 for (BoundType lBoundType : BoundType.values()) { 112 GeneralRange<Integer> range = GeneralRange.upTo(ORDERING, 3, lBoundType); 113 for (Integer i : IN_ORDER_VALUES) { 114 assertEquals( 115 ORDERING.compare(i, 3) < 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), 116 range.contains(i)); 117 assertEquals( 118 ORDERING.compare(i, 3) > 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), 119 range.tooHigh(i)); 120 assertFalse(range.tooLow(i)); 121 } 122 } 123 } 124 125 public void testDoublyBoundedAgainstRange() { 126 for (BoundType lboundType : BoundType.values()) { 127 for (BoundType uboundType : BoundType.values()) { 128 Range<Integer> range = Range.range(2, lboundType, 4, uboundType); 129 GeneralRange<Integer> gRange = GeneralRange.range(ORDERING, 2, lboundType, 4, uboundType); 130 for (Integer i : IN_ORDER_VALUES) { 131 assertEquals(i != null && range.contains(i), gRange.contains(i)); 132 } 133 } 134 } 135 } 136 137 public void testIntersectAgainstMatchingEndpointsRange() { 138 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN); 139 assertEquals( 140 GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN), 141 range.intersect(GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED))); 142 } 143 144 public void testIntersectAgainstBiggerRange() { 145 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN); 146 147 assertEquals( 148 GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN), 149 range.intersect(GeneralRange.range(ORDERING, null, OPEN, 5, CLOSED))); 150 151 assertEquals( 152 GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN), 153 range.intersect(GeneralRange.range(ORDERING, 2, OPEN, 5, CLOSED))); 154 155 assertEquals( 156 GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN), 157 range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 4, OPEN))); 158 } 159 160 public void testIntersectAgainstSmallerRange() { 161 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN); 162 assertEquals( 163 GeneralRange.range(ORDERING, 3, CLOSED, 4, OPEN), 164 range.intersect(GeneralRange.range(ORDERING, 3, CLOSED, 4, CLOSED))); 165 } 166 167 public void testIntersectOverlappingRange() { 168 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED); 169 assertEquals( 170 GeneralRange.range(ORDERING, 3, CLOSED, 4, CLOSED), 171 range.intersect(GeneralRange.range(ORDERING, 3, CLOSED, 5, CLOSED))); 172 assertEquals( 173 GeneralRange.range(ORDERING, 2, OPEN, 3, OPEN), 174 range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 3, OPEN))); 175 } 176 177 public void testIntersectNonOverlappingRange() { 178 GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED); 179 assertTrue(range.intersect(GeneralRange.range(ORDERING, 5, CLOSED, 6, CLOSED)).isEmpty()); 180 assertTrue(range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 2, OPEN)).isEmpty()); 181 } 182 183 public void testFromRangeAll() { 184 assertEquals(GeneralRange.all(Ordering.natural()), GeneralRange.from(Range.all())); 185 } 186 187 public void testFromRangeOneEnd() { 188 for (BoundType endpointType : BoundType.values()) { 189 assertEquals( 190 GeneralRange.upTo(Ordering.natural(), 3, endpointType), 191 GeneralRange.from(Range.upTo(3, endpointType))); 192 193 assertEquals( 194 GeneralRange.downTo(Ordering.natural(), 3, endpointType), 195 GeneralRange.from(Range.downTo(3, endpointType))); 196 } 197 } 198 199 public void testFromRangeTwoEnds() { 200 for (BoundType lowerType : BoundType.values()) { 201 for (BoundType upperType : BoundType.values()) { 202 assertEquals( 203 GeneralRange.range(Ordering.natural(), 3, lowerType, 4, upperType), 204 GeneralRange.from(Range.range(3, lowerType, 4, upperType))); 205 } 206 } 207 } 208 209 public void testReverse() { 210 assertEquals(GeneralRange.all(ORDERING.reverse()), GeneralRange.all(ORDERING).reverse()); 211 assertEquals( 212 GeneralRange.downTo(ORDERING.reverse(), 3, CLOSED), 213 GeneralRange.upTo(ORDERING, 3, CLOSED).reverse()); 214 assertEquals( 215 GeneralRange.upTo(ORDERING.reverse(), 3, OPEN), 216 GeneralRange.downTo(ORDERING, 3, OPEN).reverse()); 217 assertEquals( 218 GeneralRange.range(ORDERING.reverse(), 5, OPEN, 3, CLOSED), 219 GeneralRange.range(ORDERING, 3, CLOSED, 5, OPEN).reverse()); 220 } 221 222 @GwtIncompatible // NullPointerTester 223 public void testNullPointers() { 224 new NullPointerTester().testAllPublicStaticMethods(GeneralRange.class); 225 } 226 } 227