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");
      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.collect.BoundType.CLOSED;
     20 import static com.google.common.collect.BoundType.OPEN;
     21 
     22 import com.google.common.annotations.GwtCompatible;
     23 import com.google.common.testing.EqualsTester;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import java.util.Arrays;
     28 import java.util.List;
     29 import java.util.NoSuchElementException;
     30 
     31 /**
     32  * @author Gregory Kick
     33  */
     34 @GwtCompatible
     35 public class RangesTest extends TestCase {
     36   public void testSingleton() {
     37     assertEquals(Ranges.closed(0, 0), Ranges.singleton(0));
     38     assertEquals(Ranges.closed(9, 9), Ranges.singleton(9));
     39   }
     40 
     41   public void testEncloseAll() {
     42     assertEquals(Ranges.closed(0, 0), Ranges.encloseAll(Arrays.asList(0)));
     43     assertEquals(Ranges.closed(-3, 5), Ranges.encloseAll(Arrays.asList(5, -3)));
     44     assertEquals(Ranges.closed(-3, 5), Ranges.encloseAll(Arrays.asList(1, 2, 2, 2, 5, -3, 0, -1)));
     45   }
     46 
     47   public void testEncloseAll_empty() {
     48     try {
     49       Ranges.encloseAll(ImmutableSet.<Integer>of());
     50       fail();
     51     } catch (NoSuchElementException expected) {}
     52   }
     53 
     54   public void testEncloseAll_nullValue() {
     55     List<Integer> nullFirst = Lists.newArrayList(null, 0);
     56     try {
     57       Ranges.encloseAll(nullFirst);
     58       fail();
     59     } catch (NullPointerException expected) {}
     60     List<Integer> nullNotFirst = Lists.newArrayList(0, null);
     61     try {
     62       Ranges.encloseAll(nullNotFirst);
     63       fail();
     64     } catch (NullPointerException expected) {}
     65   }
     66 
     67   public void testEquivalentFactories() {
     68     new EqualsTester()
     69         .addEqualityGroup(Ranges.all())
     70         .addEqualityGroup(
     71             Ranges.atLeast(1),
     72             Ranges.downTo(1, CLOSED))
     73         .addEqualityGroup(
     74             Ranges.greaterThan(1),
     75             Ranges.downTo(1, OPEN))
     76         .addEqualityGroup(
     77             Ranges.atMost(7),
     78             Ranges.upTo(7, CLOSED))
     79         .addEqualityGroup(
     80             Ranges.lessThan(7),
     81             Ranges.upTo(7, OPEN))
     82         .addEqualityGroup(
     83             Ranges.open(1, 7),
     84             Ranges.range(1, OPEN, 7, OPEN))
     85         .addEqualityGroup(
     86             Ranges.openClosed(1, 7),
     87             Ranges.range(1, OPEN, 7, CLOSED))
     88         .addEqualityGroup(
     89             Ranges.closed(1, 7),
     90             Ranges.range(1, CLOSED, 7, CLOSED))
     91         .addEqualityGroup(
     92             Ranges.closedOpen(1, 7),
     93             Ranges.range(1, CLOSED, 7, OPEN))
     94         .testEquals();
     95   }
     96 }
     97