• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.base.Preconditions.checkArgument;
20 import static com.google.common.collect.DiscreteDomains.integers;
21 
22 import com.google.common.collect.testing.SampleElements;
23 import com.google.common.collect.testing.SetTestSuiteBuilder;
24 import com.google.common.collect.testing.TestSetGenerator;
25 import com.google.common.collect.testing.features.CollectionFeature;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.testers.SetHashCodeTester;
28 
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32 
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Set;
36 import java.util.SortedSet;
37 import java.util.TreeSet;
38 
39 /**
40  * @author Gregory Kick
41  */
42 public class ContiguousSetNonGwtTest extends TestCase {
43   public static class BuiltTests extends TestCase {
suite()44     public static Test suite() {
45       TestSuite suite = new TestSuite();
46 
47       suite.addTest(
48           SetTestSuiteBuilder
49               .using(
50                   new TestIntegerSetGenerator() {
51                     @Override
52                     protected Set<Integer> create(Integer[] elements) {
53                       // reject duplicates at creation, just so that I can use
54                       // that SetFeature below, which stops a test from running
55                       // that doesn't work. hack!
56                       SortedSet<Integer> set = new TreeSet<Integer>();
57                       Collections.addAll(set, elements);
58                       checkArgument(set.size() == elements.length);
59                       return Ranges.closed(set.first(), set.last()).asSet(integers());
60                     }
61                   })
62               .withFeatures(
63                   CollectionSize.ONE,
64                   CollectionSize.SEVERAL,
65                   CollectionFeature.KNOWN_ORDER,
66                   CollectionFeature.ALLOWS_NULL_QUERIES,
67                   CollectionFeature.NON_STANDARD_TOSTRING,
68                   CollectionFeature.RESTRICTS_ELEMENTS,
69                   CollectionFeature.REJECTS_DUPLICATES_AT_CREATION)
70               .suppressing(SetHashCodeTester.getHashCodeMethods())
71               .named("DiscreteRange.asSet, closed")
72               .createTestSuite());
73 
74       return suite;
75     }
76   }
77 
78   abstract static class TestIntegerSetGenerator implements TestSetGenerator<Integer> {
samples()79     @Override public SampleElements<Integer> samples() {
80       return new SampleElements<Integer>(1, 2, 3, 4, 5);
81     }
82 
create(Object... elements)83     @Override public Set<Integer> create(Object... elements) {
84       Integer[] array = new Integer[elements.length];
85       int i = 0;
86       for (Object e : elements) {
87         array[i++] = (Integer) e;
88       }
89       return create(array);
90     }
91 
create(Integer[] elements)92     protected abstract Set<Integer> create(Integer[] elements);
93 
createArray(int length)94     @Override public Integer[] createArray(int length) {
95       return new Integer[length];
96     }
97 
order(List<Integer> insertionOrder)98     @Override public List<Integer> order(List<Integer> insertionOrder) {
99       return Ordering.natural().sortedCopy(insertionOrder);
100     }
101   }
102 
testNothing()103   public void testNothing() {
104     /*
105      * It's a warning if a TestCase subclass contains no tests, so we add one.
106      * Alternatively, we could stop extending TestCase, but I worry that someone
107      * will add a test in the future and not realize that it's being ignored.
108      */
109   }
110 }
111