Home | History | Annotate | Download | only in collect
      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 java.util.Arrays;
     21 import java.util.Comparator;
     22 import java.util.List;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import com.google.common.annotations.GwtCompatible;
     27 import com.google.common.annotations.GwtIncompatible;
     28 import com.google.common.base.Objects;
     29 import com.google.common.testing.NullPointerTester;
     30 
     31 /**
     32  * Tests for {@code GeneralRange}.
     33  *
     34  * @author Louis Wasserman
     35  */
     36 @GwtCompatible(emulated = true)
     37 public class GeneralRangeTest extends TestCase {
     38   private static final Ordering<Integer> ORDERING = Ordering.natural().nullsFirst();
     39 
     40   private static final List<Integer> IN_ORDER_VALUES = Arrays.asList(null, 1, 2, 3, 4, 5);
     41 
     42   public void testCreateEmptyRangeFails() {
     43     for (BoundType lboundType : BoundType.values()) {
     44       for (BoundType uboundType : BoundType.values()) {
     45         try {
     46           GeneralRange<Integer> range = GeneralRange.range(ORDERING, 4, lboundType, 2, uboundType);
     47           fail("Expected IAE");
     48         } catch (IllegalArgumentException expected) {}
     49       }
     50     }
     51   }
     52 
     53   public void testCreateEmptyRangeOpenOpenFails() {
     54     for (Integer i : IN_ORDER_VALUES) {
     55       try {
     56         GeneralRange.range(ORDERING, i, OPEN, i, OPEN);
     57         fail("Expected IAE");
     58       } catch (IllegalArgumentException expected) {}
     59     }
     60   }
     61 
     62   public void testCreateEmptyRangeClosedOpenSucceeds() {
     63     for (Integer i : IN_ORDER_VALUES) {
     64       GeneralRange<Integer> range = GeneralRange.range(ORDERING, i, CLOSED, i, OPEN);
     65       for (Integer j : IN_ORDER_VALUES) {
     66         assertFalse(range.contains(j));
     67       }
     68     }
     69   }
     70 
     71   public void testCreateEmptyRangeOpenClosedSucceeds() {
     72     for (Integer i : IN_ORDER_VALUES) {
     73       GeneralRange<Integer> range = GeneralRange.range(ORDERING, i, OPEN, i, CLOSED);
     74       for (Integer j : IN_ORDER_VALUES) {
     75         assertFalse(range.contains(j));
     76       }
     77     }
     78   }
     79 
     80   public void testCreateSingletonRangeSucceeds() {
     81     for (Integer i : IN_ORDER_VALUES) {
     82       GeneralRange<Integer> range = GeneralRange.range(ORDERING, i, CLOSED, i, CLOSED);
     83       for (Integer j : IN_ORDER_VALUES) {
     84         assertEquals(Objects.equal(i, j), range.contains(j));
     85       }
     86     }
     87   }
     88 
     89   public void testSingletonRange() {
     90     GeneralRange<Integer> range = GeneralRange.range(ORDERING, 3, CLOSED, 3, CLOSED);
     91     for (Integer i : IN_ORDER_VALUES) {
     92       assertEquals(ORDERING.compare(i, 3) == 0, range.contains(i));
     93     }
     94   }
     95 
     96   public void testLowerRange() {
     97     for (BoundType lBoundType : BoundType.values()) {
     98       GeneralRange<Integer> range = GeneralRange.downTo(ORDERING, 3, lBoundType);
     99       for (Integer i : IN_ORDER_VALUES) {
    100         assertEquals(ORDERING.compare(i, 3) > 0
    101             || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), range.contains(i));
    102         assertEquals(ORDERING.compare(i, 3) < 0
    103             || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), range.tooLow(i));
    104         assertFalse(range.tooHigh(i));
    105       }
    106     }
    107   }
    108 
    109   public void testUpperRange() {
    110     for (BoundType lBoundType : BoundType.values()) {
    111       GeneralRange<Integer> range = GeneralRange.upTo(ORDERING, 3, lBoundType);
    112       for (Integer i : IN_ORDER_VALUES) {
    113         assertEquals(ORDERING.compare(i, 3) < 0
    114             || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), range.contains(i));
    115         assertEquals(ORDERING.compare(i, 3) > 0
    116             || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), range.tooHigh(i));
    117         assertFalse(range.tooLow(i));
    118       }
    119     }
    120   }
    121 
    122   public void testDoublyBoundedAgainstRange() {
    123     for (BoundType lboundType : BoundType.values()) {
    124       for (BoundType uboundType : BoundType.values()) {
    125         Range<Integer> range = Ranges.range(2, lboundType, 4, uboundType);
    126         GeneralRange<Integer> gRange = GeneralRange.range(ORDERING, 2, lboundType, 4, uboundType);
    127         for (Integer i : IN_ORDER_VALUES) {
    128           assertEquals(i != null && range.contains(i), gRange.contains(i));
    129         }
    130       }
    131     }
    132   }
    133 
    134   public void testIntersectAgainstMatchingEndpointsRange() {
    135     GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN);
    136     assertEquals(GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN),
    137         range.intersect(GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED)));
    138   }
    139 
    140   public void testIntersectAgainstBiggerRange() {
    141     GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN);
    142 
    143     assertEquals(GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN),
    144         range.intersect(GeneralRange.range(ORDERING, null, OPEN, 5, CLOSED)));
    145 
    146     assertEquals(GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN),
    147         range.intersect(GeneralRange.range(ORDERING, 2, OPEN, 5, CLOSED)));
    148 
    149     assertEquals(GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN),
    150         range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 4, OPEN)));
    151   }
    152 
    153   public void testIntersectAgainstSmallerRange() {
    154     GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN);
    155     assertEquals(GeneralRange.range(ORDERING, 3, CLOSED, 4, OPEN),
    156         range.intersect(GeneralRange.range(ORDERING, 3, CLOSED, 4, CLOSED)));
    157   }
    158 
    159   public void testIntersectOverlappingRange() {
    160     GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED);
    161     assertEquals(GeneralRange.range(ORDERING, 3, CLOSED, 4, CLOSED),
    162         range.intersect(GeneralRange.range(ORDERING, 3, CLOSED, 5, CLOSED)));
    163     assertEquals(GeneralRange.range(ORDERING, 2, OPEN, 3, OPEN),
    164         range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 3, OPEN)));
    165   }
    166 
    167   public void testIntersectNonOverlappingRange() {
    168     GeneralRange<Integer> range = GeneralRange.range(ORDERING, 2, OPEN, 4, CLOSED);
    169     assertTrue(range.intersect(GeneralRange.range(ORDERING, 5, CLOSED, 6, CLOSED)).isEmpty());
    170     assertTrue(range.intersect(GeneralRange.range(ORDERING, 1, OPEN, 2, OPEN)).isEmpty());
    171   }
    172 
    173   public void testFromRangeAll() {
    174     assertEquals(GeneralRange.all(Ordering.natural()), GeneralRange.from(Ranges.all()));
    175   }
    176 
    177   public void testFromRangeOneEnd() {
    178     for (BoundType endpointType : BoundType.values()) {
    179       assertEquals(GeneralRange.upTo(Ordering.natural(), 3, endpointType),
    180           GeneralRange.from(Ranges.upTo(3, endpointType)));
    181 
    182       assertEquals(GeneralRange.downTo(Ordering.natural(), 3, endpointType),
    183           GeneralRange.from(Ranges.downTo(3, endpointType)));
    184     }
    185   }
    186 
    187   public void testFromRangeTwoEnds() {
    188     for (BoundType lowerType : BoundType.values()) {
    189       for (BoundType upperType : BoundType.values()) {
    190         assertEquals(GeneralRange.range(Ordering.natural(), 3, lowerType, 4, upperType),
    191             GeneralRange.from(Ranges.range(3, lowerType, 4, upperType)));
    192       }
    193     }
    194   }
    195 
    196   public void testReverse() {
    197     assertEquals(GeneralRange.all(ORDERING.reverse()), GeneralRange.all(ORDERING).reverse());
    198     assertEquals(GeneralRange.downTo(ORDERING.reverse(), 3, CLOSED),
    199         GeneralRange.upTo(ORDERING, 3, CLOSED).reverse());
    200     assertEquals(GeneralRange.upTo(ORDERING.reverse(), 3, OPEN),
    201         GeneralRange.downTo(ORDERING, 3, OPEN).reverse());
    202     assertEquals(GeneralRange.range(ORDERING.reverse(), 5, OPEN, 3, CLOSED),
    203         GeneralRange.range(ORDERING, 3, CLOSED, 5, OPEN).reverse());
    204   }
    205 
    206   @GwtIncompatible("NullPointerTester")
    207   public void testNullPointers() throws Exception {
    208     NullPointerTester tester = new NullPointerTester();
    209     tester.setDefault(Comparator.class, Ordering.arbitrary());
    210     tester.setDefault(BoundType.class, BoundType.CLOSED);
    211     tester.testAllPublicStaticMethods(GeneralRange.class);
    212   }
    213 }
    214