Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2008 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 com.google.common.annotations.GwtIncompatible;
     20 import com.google.common.collect.testing.ListTestSuiteBuilder;
     21 import com.google.common.collect.testing.SetTestSuiteBuilder;
     22 import com.google.common.collect.testing.TestStringSetGenerator;
     23 import com.google.common.collect.testing.TestStringSortedSetGenerator;
     24 import com.google.common.collect.testing.features.CollectionFeature;
     25 import com.google.common.collect.testing.features.CollectionSize;
     26 import com.google.common.collect.testing.google.SetGenerators.DegeneratedImmutableSetGenerator;
     27 import com.google.common.collect.testing.google.SetGenerators.ImmutableSetAsListGenerator;
     28 import com.google.common.collect.testing.google.SetGenerators.ImmutableSetCopyOfGenerator;
     29 import com.google.common.collect.testing.google.SetGenerators.ImmutableSetWithBadHashesGenerator;
     30 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetAsListGenerator;
     31 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetAsListSubListGenerator;
     32 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetCopyOfGenerator;
     33 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetExplicitComparator;
     34 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetExplicitSuperclassComparatorGenerator;
     35 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetHeadsetGenerator;
     36 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetReversedOrderGenerator;
     37 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetSubsetAsListGenerator;
     38 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetSubsetGenerator;
     39 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetTailsetGenerator;
     40 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedSetUnhashableGenerator;
     41 import com.google.common.collect.testing.google.SetGenerators.ImmutableSortedsetSubsetAsListSubListGenerator;
     42 import com.google.common.collect.testing.testers.SetHashCodeTester;
     43 import com.google.common.testing.SerializableTester;
     44 
     45 import junit.framework.Test;
     46 import junit.framework.TestCase;
     47 import junit.framework.TestSuite;
     48 
     49 import java.util.List;
     50 import java.util.Set;
     51 import java.util.SortedSet;
     52 
     53 /**
     54  * Collection tests for {@link ImmutableSet} and {@link ImmutableSortedSet}.
     55  *
     56  * @author Kevin Bourrillion
     57  * @author Jared Levy
     58  */
     59 @GwtIncompatible("suite") // handled by collect/gwt/suites
     60 public class ImmutableSetCollectionTest extends TestCase {
     61   public static Test suite() {
     62     TestSuite suite = new TestSuite();
     63 
     64     suite.addTest(SetTestSuiteBuilder.using(new ImmutableSetCopyOfGenerator())
     65         .named(ImmutableSetTest.class.getName())
     66         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
     67             CollectionFeature.ALLOWS_NULL_QUERIES)
     68         .createTestSuite());
     69 
     70     suite.addTest(SetTestSuiteBuilder.using(new TestStringSetGenerator() {
     71           @Override protected Set<String> create(String[] elements) {
     72             Set<String> set = ImmutableSet.copyOf(elements);
     73             return SerializableTester.reserialize(set);
     74           }
     75         })
     76         .named(ImmutableSetTest.class.getName() + ", reserialized")
     77         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
     78             CollectionFeature.ALLOWS_NULL_QUERIES)
     79         .createTestSuite());
     80 
     81     suite.addTest(SetTestSuiteBuilder.using(
     82         new ImmutableSetWithBadHashesGenerator())
     83         .named(ImmutableSetTest.class.getName() + ", with bad hashes")
     84         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
     85             CollectionFeature.ALLOWS_NULL_QUERIES)
     86         .createTestSuite());
     87 
     88     suite.addTest(SetTestSuiteBuilder.using(
     89         new DegeneratedImmutableSetGenerator())
     90         .named(ImmutableSetTest.class.getName() + ", degenerate")
     91         .withFeatures(CollectionSize.ONE, CollectionFeature.KNOWN_ORDER,
     92             CollectionFeature.ALLOWS_NULL_QUERIES)
     93         .createTestSuite());
     94 
     95     suite.addTest(SetTestSuiteBuilder.using(
     96         new ImmutableSortedSetCopyOfGenerator())
     97         .named(ImmutableSortedSetTest.class.getName())
     98         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
     99             CollectionFeature.ALLOWS_NULL_QUERIES)
    100         .createTestSuite());
    101 
    102     suite.addTest(SetTestSuiteBuilder.using(new TestStringSortedSetGenerator() {
    103           @Override protected SortedSet<String> create(String[] elements) {
    104             SortedSet<String> set = ImmutableSortedSet.copyOf(elements);
    105             return SerializableTester.reserialize(set);
    106           }
    107         })
    108         .named(ImmutableSortedSetTest.class.getName() + ", reserialized")
    109         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    110             CollectionFeature.ALLOWS_NULL_QUERIES)
    111         .createTestSuite());
    112 
    113     suite.addTest(SetTestSuiteBuilder.using(
    114         new ImmutableSortedSetHeadsetGenerator())
    115         .named(ImmutableSortedSetTest.class.getName() + ", headset")
    116         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    117             CollectionFeature.ALLOWS_NULL_QUERIES)
    118         .createTestSuite());
    119 
    120     suite.addTest(SetTestSuiteBuilder.using(
    121         new ImmutableSortedSetTailsetGenerator())
    122         .named(ImmutableSortedSetTest.class.getName() + ", tailset")
    123         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    124             CollectionFeature.ALLOWS_NULL_QUERIES)
    125         .createTestSuite());
    126 
    127     suite.addTest(SetTestSuiteBuilder.using(
    128         new ImmutableSortedSetSubsetGenerator())
    129         .named(ImmutableSortedSetTest.class.getName() + ", subset")
    130         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    131             CollectionFeature.ALLOWS_NULL_QUERIES)
    132         .createTestSuite());
    133 
    134     suite.addTest(SetTestSuiteBuilder.using(
    135         new TestStringSortedSetGenerator() {
    136           @Override protected SortedSet<String> create(String[] elements) {
    137             List<String> list = Lists.newArrayList(elements);
    138             list.add("zzz");
    139             return SerializableTester.reserialize(
    140                 ImmutableSortedSet.copyOf(list).headSet("zzy"));
    141           }
    142         })
    143         .named(
    144             ImmutableSortedSetTest.class.getName() + ", headset, reserialized")
    145         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    146             CollectionFeature.ALLOWS_NULL_QUERIES)
    147         .createTestSuite());
    148 
    149     suite.addTest(SetTestSuiteBuilder.using(
    150         new TestStringSortedSetGenerator() {
    151           @Override protected SortedSet<String> create(String[] elements) {
    152             List<String> list = Lists.newArrayList(elements);
    153             list.add("\0");
    154             return SerializableTester.reserialize(
    155                 ImmutableSortedSet.copyOf(list).tailSet("\0\0"));
    156           }
    157         })
    158         .named(
    159             ImmutableSortedSetTest.class.getName() + ", tailset, reserialized")
    160         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    161             CollectionFeature.ALLOWS_NULL_QUERIES)
    162         .createTestSuite());
    163 
    164     suite.addTest(SetTestSuiteBuilder.using(
    165         new TestStringSortedSetGenerator() {
    166           @Override protected SortedSet<String> create(String[] elements) {
    167             List<String> list = Lists.newArrayList(elements);
    168             list.add("\0");
    169             list.add("zzz");
    170             return SerializableTester.reserialize(
    171                 ImmutableSortedSet.copyOf(list).subSet("\0\0", "zzy"));
    172           }
    173         })
    174         .named(
    175             ImmutableSortedSetTest.class.getName() + ", subset, reserialized")
    176         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    177             CollectionFeature.ALLOWS_NULL_QUERIES)
    178         .createTestSuite());
    179 
    180     suite.addTest(SetTestSuiteBuilder.using(
    181         new ImmutableSortedSetExplicitComparator())
    182         .named(ImmutableSortedSetTest.class.getName()
    183             + ", explicit comparator, vararg")
    184         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    185             CollectionFeature.ALLOWS_NULL_QUERIES)
    186         .createTestSuite());
    187 
    188     suite.addTest(SetTestSuiteBuilder.using(
    189         new ImmutableSortedSetExplicitSuperclassComparatorGenerator())
    190         .named(ImmutableSortedSetTest.class.getName()
    191             + ", explicit superclass comparator, iterable")
    192         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    193             CollectionFeature.ALLOWS_NULL_QUERIES)
    194         .createTestSuite());
    195 
    196     suite.addTest(SetTestSuiteBuilder.using(
    197         new ImmutableSortedSetReversedOrderGenerator())
    198         .named(ImmutableSortedSetTest.class.getName()
    199             + ", reverseOrder, iterator")
    200         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    201             CollectionFeature.ALLOWS_NULL_QUERIES)
    202         .createTestSuite());
    203 
    204     suite.addTest(SetTestSuiteBuilder.using(
    205         new ImmutableSortedSetUnhashableGenerator())
    206         .suppressing(SetHashCodeTester.getHashCodeMethods())
    207         .named(ImmutableSortedSetTest.class.getName() + ", unhashable")
    208         .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    209             CollectionFeature.ALLOWS_NULL_QUERIES)
    210         .createTestSuite());
    211 
    212     suite.addTest(ListTestSuiteBuilder.using(new ImmutableSetAsListGenerator())
    213         .named("ImmutableSet.asList")
    214         .withFeatures(CollectionSize.ANY,
    215             CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
    216             CollectionFeature.ALLOWS_NULL_QUERIES)
    217         .createTestSuite());
    218 
    219     suite.addTest(ListTestSuiteBuilder.using(
    220         new ImmutableSortedSetAsListGenerator())
    221         .named("ImmutableSortedSet.asList")
    222         .withFeatures(CollectionSize.ANY,
    223             CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
    224             CollectionFeature.ALLOWS_NULL_QUERIES)
    225         .createTestSuite());
    226 
    227     suite.addTest(ListTestSuiteBuilder.using(
    228         new ImmutableSortedSetSubsetAsListGenerator())
    229         .named("ImmutableSortedSet.subSet.asList")
    230         .withFeatures(CollectionSize.ANY,
    231             CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
    232             CollectionFeature.ALLOWS_NULL_QUERIES)
    233         .createTestSuite());
    234 
    235     suite.addTest(ListTestSuiteBuilder.using(
    236         new ImmutableSortedSetAsListSubListGenerator())
    237         .named("ImmutableSortedSet.asList.subList")
    238         .withFeatures(CollectionSize.ANY,
    239             CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
    240             CollectionFeature.ALLOWS_NULL_QUERIES)
    241         .createTestSuite());
    242 
    243     suite.addTest(ListTestSuiteBuilder.using(
    244         new ImmutableSortedsetSubsetAsListSubListGenerator())
    245         .named("ImmutableSortedSet.subSet.asList.subList")
    246         .withFeatures(CollectionSize.ANY,
    247             CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
    248             CollectionFeature.ALLOWS_NULL_QUERIES)
    249         .createTestSuite());
    250 
    251     return suite;
    252   }
    253 }
    254