Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect.testing;
 
 
 
 import java.util.Set;
Generates a test suite covering the java.util.Set implementations in the java.util package. Can be subclassed to specify tests that should be suppressed.

Author(s):
Kevin Bourrillion
 
 public class TestsForSetsInJavaUtil {
   public static Test suite() {
     return new TestsForSetsInJavaUtil().allTests();
   }
 
   public Test allTests() {
     TestSuite suite = new TestSuite("java.util Sets");
     suite.addTest(testsForEmptySet());
     suite.addTest(testsForSingletonSet());
     suite.addTest(testsForHashSet());
     suite.addTest(testsForLinkedHashSet());
     suite.addTest(testsForEnumSet());
     suite.addTest(testsForTreeSetNatural());
     suite.addTest(testsForCheckedSet());
     suite.addTest(testsForAbstractSet());
 
     return suite;
   }
 
   protected Collection<MethodsuppressForEmptySet() {
     return Collections.emptySet();
   }
     return Collections.emptySet();
   }
   protected Collection<MethodsuppressForHashSet() {
     return Collections.emptySet();
   }
     return Collections.emptySet();
   }
   protected Collection<MethodsuppressForEnumSet() {
     return Collections.emptySet();
   }
     return Collections.emptySet();
   }
     return Collections.emptySet();
   }
     return Collections.emptySet();
   }
     return Collections.emptySet();
   }
   protected Collection<MethodsuppressForCheckedSet() {
     return Collections.emptySet();
  }
    return Collections.emptySet();
  }
    return Collections.emptySet();
  }
    return Collections.emptySet();
  }
  public Test testsForEmptySet() {
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              return Collections.emptySet();
            }
          })
        .named("emptySet")
        .withFeatures(
            .,
            .)
        .createTestSuite();
  }
  public Test testsForSingletonSet() {
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              return Collections.singleton(elements[0]);
            }
          })
        .named("singleton")
        .withFeatures(
            .,
            .,
            .)
        .createTestSuite();
  }
  public Test testsForHashSet() {
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              return new HashSet<String>(MinimalCollection.of(elements));
            }
          })
        .named("HashSet")
        .withFeatures(
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
  public Test testsForLinkedHashSet() {
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              return new LinkedHashSet<String>(MinimalCollection.of(elements));
            }
          })
        .named("LinkedHashSet")
        .withFeatures(
            .,
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
  public Test testsForEnumSet() {
    return SetTestSuiteBuilder
        .using(new TestEnumSetGenerator() {
            @Override public Set<AnEnumcreate(AnEnum[] elements) {
              return (elements.length == 0)
                  ? EnumSet.noneOf(AnEnum.class)
                  : EnumSet.copyOf(MinimalCollection.of(elements));
            }
          })
        .named("EnumSet")
        .withFeatures(
            .,
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
    return SortedSetTestSuiteBuilder
        .using(new TestStringSortedSetGenerator() {
            @Override public SortedSet<Stringcreate(String[] elements) {
              return new TreeSet<String>(MinimalCollection.of(elements));
            }
          })
        .named("TreeSet, natural")
        .withFeatures(
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
    return SortedSetTestSuiteBuilder
        .using(new TestStringSortedSetGenerator() {
            @Override public SortedSet<Stringcreate(String[] elements) {
              SortedSet<Stringset
                  = new TreeSet<String>(arbitraryNullFriendlyComparator());
              Collections.addAll(setelements);
              return set;
            }
          })
        .named("TreeSet, with comparator")
        .withFeatures(
            .,
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              return new CopyOnWriteArraySet<String>(
                  MinimalCollection.of(elements));
            }
          })
        .named("CopyOnWriteArraySet")
        .withFeatures(
            .,
            .,
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              Set<StringinnerSet = new HashSet<String>();
              Collections.addAll(innerSetelements);
              return Collections.unmodifiableSet(innerSet);
            }
          })
        .named("unmodifiableSet/HashSet")
        .withFeatures(
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
  public Test testsForCheckedSet() {
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator() {
            @Override public Set<Stringcreate(String[] elements) {
              Set<StringinnerSet = new HashSet<String>();
              Collections.addAll(innerSetelements);
              return Collections.checkedSet(innerSetString.class);
            }
          })
        .named("checkedSet/HashSet")
        .withFeatures(
            .,
            .,
            .,
            .,
            .)
        .createTestSuite();
  }
  public Test testsForAbstractSet() {
    return SetTestSuiteBuilder
        .using(new TestStringSetGenerator () {
            @Override protected Set<Stringcreate(String[] elements) {
              final String[] deduped = dedupe(elements);
              return new AbstractSet<String>() {
                @Override public int size() {
                  return deduped.length;
                }
                @Override public Iterator<Stringiterator() {
                  return MinimalCollection.of(deduped).iterator();
                }
              };
            }
          })
        .named("AbstractSet")
        .withFeatures(
            .,
            .,
            .// in this case, anyway
            .)
        .createTestSuite();
  }
    return SetTestSuiteBuilder
        .using(new TestCollidingSetGenerator() {
            @Override
            public Set<Objectcreate(Object... elements) {
              return new HashSet<Object>(MinimalCollection.of(elements));
            }
          })
        .named("badly colliding HashSet")
        .withFeatures(
            .,
            .,
            .)
        .createTestSuite();
  }
  private static String[] dedupe(String[] elements) {
    Set<Stringtmp = new LinkedHashSet<String>();
    Collections.addAll(tmpelements);
    return tmp.toArray(new String[0]);
  }
    return new NullFriendlyComparator<T>();
  }
  private static final class NullFriendlyComparator<T>
      implements Comparator<T>, Serializable {
    @Override
    public int compare(T left, T right) {
      return String.valueOf(left).compareTo(String.valueOf(right));
    }
  }
New to GrepCode? Check out our FAQ X