text:
package com.google.common.collect;

import static com.google.common.collect.Iterables.unmodifiableIterable;
import static com.google.common.collect.Sets.newEnumSet;
import static com.google.common.collect.Sets.newHashSet;
import static com.google.common.collect.Sets.newLinkedHashSet;
import static com.google.common.collect.Sets.powerSet;
import static com.google.common.collect.Sets.unmodifiableNavigableSet;
import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static java.io.ObjectStreamConstants.TC_REFERENCE;
import static java.io.ObjectStreamConstants.baseWireHandle;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Predicate;
import com.google.common.collect.testing.AnEnum;
import com.google.common.collect.testing.IteratorTester;
import com.google.common.collect.testing.MinimalIterable;
import com.google.common.collect.testing.NavigableSetTestSuiteBuilder;
import com.google.common.collect.testing.SafeTreeSet;
import com.google.common.collect.testing.SetTestSuiteBuilder;
import com.google.common.collect.testing.TestEnumSetGenerator;
import com.google.common.collect.testing.TestStringSetGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.SetFeature;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArraySet;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
 * Unit test for {@code Sets}.
 *
 * @author Kevin Bourrillion
 * @author Jared Levy
 */
@GwtCompatible(emulated = true)
public class SetsTest extends TestCase {

  private static final IteratorTester.KnownOrder KNOWN_ORDER =
      IteratorTester.KnownOrder.KNOWN_ORDER;

  private static final Collection<Integer> EMPTY_COLLECTION = Arrays.<Integer>asList();

  private static final Collection<Integer> SOME_COLLECTION = Arrays.asList(0, 1, 1);

  private static final Iterable<Integer> SOME_ITERABLE =
      new Iterable<Integer>() {
        @Override
        public Iterator<Integer> iterator() {
          return SOME_COLLECTION.iterator();
        }
      };

  private static final List<Integer> LONGER_LIST = Arrays.asList(8, 6, 7, 5, 3, 0, 9);

  private static final Comparator<Integer> SOME_COMPARATOR = Collections.reverseOrder();

  @GwtIncompatible // suite
  public static Test suite() {
    TestSuite suite = new TestSuite();
    suite.addTestSuite(SetsTest.class);

    suite.addTest(
        SetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  protected Set<String> create(String[] elements) {
                    return Sets.newConcurrentHashSet(Arrays.asList(elements));
                  }
                })
            .named("Sets.newConcurrentHashSet")
            .withFeatures(CollectionSize.ANY, SetFeature.GENERAL_PURPOSE)
            .createTestSuite());

    suite.addTest(
        SetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  protected Set<String> create(String[] elements) {
                    int size = elements.length;
                    // Remove last element, if size > 1
                    Set<String> set1 =
                        (size > 1)
                            ? Sets.newHashSet(Arrays.asList(elements).subList(0, size - 1))
                            : Sets.newHashSet(elements);
                    // Remove first element, if size > 0
                    Set<String> set2 =
                        (size > 0)
                            ? Sets.newHashSet(Arrays.asList(elements).subList(1, size))
                            : Sets.<String>newHashSet();
                    return Sets.union(set1, set2);
                  }
                })
            .named("Sets.union")
            .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES)
            .createTestSuite());

    suite.addTest(
        SetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  protected Set<String> create(String[] elements) {
                    Set<String> set1 = Sets.newHashSet(elements);
                    set1.add(samples().e3());
                    Set<String> set2 = Sets.newHashSet(elements);
                    set2.add(samples().e4());
                    return Sets.intersection(set1, set2);
                  }
                })
            .named("Sets.intersection")
            .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES)
            .createTestSuite());

    suite.addTest(
        SetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  protected Set<String> create(String[] elements) {
                    Set<String> set1 = Sets.newHashSet(elements);
                    set1.add(samples().e3());
                    Set<String> set2 = Sets.newHashSet(samples().e3());
                    return Sets.difference(set1, set2);
                  }
                })
            .named("Sets.difference")
            .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES)
            .createTestSuite());

    suite.addTest(
        SetTestSuiteBuilder.using(
                new TestEnumSetGenerator() {
                  @Override
                  protected Set<AnEnum> create(AnEnum[] elements) {
                    AnEnum[] otherElements = new AnEnum[elements.length - 1];
                    System.arraycopy(elements, 1, otherElements, 0, otherElements.length);
                    return Sets.immutableEnumSet(elements[0], otherElements);
                  }
                })
            .named("Sets.immutableEnumSet")
            .withFeatures(
                CollectionSize.ONE, CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_QUERIES)
            .createTestSuite());

    suite.addTest(
        NavigableSetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  protected Set<String> create(String[] elements) {
                    SafeTreeSet<String> set = new SafeTreeSet<>(Arrays.asList(elements));
                    return Sets.unmodifiableNavigableSet(set);
                  }

                  @Override
                  public List<String> order(List<String> insertionOrder) {
                    return Ordering.natural().sortedCopy(insertionOrder);
                  }
                })
            .named("Sets.unmodifiableNavigableSet[TreeSet]")
            .withFeatures(
                CollectionSize.ANY, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE)
            .createTestSuite());

    suite.addTest(testsForFilter());
    suite.addTest(testsForFilterNoNulls());
    suite.addTest(testsForFilterFiltered());

    return suite;
  }

  @GwtIncompatible // suite
  private static Test testsForFilter() {
    return SetTestSuiteBuilder.using(
            new TestStringSetGenerator() {
              @Override
              public Set<String> create(String[] elements) {
                Set<String> unfiltered = Sets.newLinkedHashSet();
                unfiltered.add("yyy");
                Collections.addAll(unfiltered, elements);
                unfiltered.add("zzz");
                return Sets.filter(unfiltered, Collections2Test.NOT_YYY_ZZZ);
              }
            })
        .named("Sets.filter")
        .withFeatures(
            CollectionFeature.SUPPORTS_ADD,
            CollectionFeature.SUPPORTS_REMOVE,
            CollectionFeature.ALLOWS_NULL_VALUES,
            CollectionFeature.KNOWN_ORDER,
            CollectionSize.ANY)
        .createTestSuite();
  }

  @GwtIncompatible // suite
  private static Test testsForFilterNoNulls() {
    TestSuite suite = new TestSuite();
    suite.addTest(
        SetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  public Set<String> create(String[] elements) {
                    Set<String> unfiltered = Sets.newLinkedHashSet();
                    unfiltered.add("yyy");
                    unfiltered.addAll(ImmutableList.copyOf(elements));
                    unfiltered.add("zzz");
                    return Sets.filter(unfiltered, Collections2Test.LENGTH_1);
                  }
                })
            .named("Sets.filter, no nulls")
            .withFeatures(
                CollectionFeature.SUPPORTS_ADD,
                CollectionFeature.SUPPORTS_REMOVE,
                CollectionFeature.KNOWN_ORDER,
                CollectionSize.ANY,
                CollectionFeature.ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        NavigableSetTestSuiteBuilder.using(
                new TestStringSetGenerator() {
                  @Override
                  public NavigableSet<String> create(String[] elements) {
                    NavigableSet<String> unfiltered = Sets.newTreeSet();
                    unfiltered.add("yyy");
                    unfiltered.addAll(ImmutableList.copyOf(elements));
                    unfiltered.add("zzz");
                    return Sets.filter(unfiltered, Collections2Test.LENGTH_1);
                  }

                  @Override
                  public List<String> order(List<String> insertionOrder) {
                    return Ordering.natural().sortedCopy(insertionOrder);
                  }
                })
            .named("Sets.filter[NavigableSet]")
            .withFeatures(
                CollectionFeature.SUPPORTS_ADD,
                CollectionFeature.SUPPORTS_REMOVE,
                CollectionFeature.KNOWN_ORDER,
                CollectionSize.ANY,
                CollectionFeature.ALLOWS_NULL_QUERIES)
            .createTestSuite());
    return suite;
  }

  @GwtIncompatible // suite
  private static Test testsForFilterFiltered() {
    return SetTestSuiteBuilder.using(
            new TestStringSetGenerator() {
              @Override
              public Set<String> create(String[] elements) {
                Set<String> unfiltered = Sets.newLinkedHashSet();
                unfiltered.add("yyy");
                unfiltered.addAll(ImmutableList.copyOf(elements));
                unfiltered.add("zzz");
                unfiltered.add("abc");
                return Sets.filter(
                    Sets.filter(unfiltered, Collections2Test.LENGTH_1),
                    Collections2Test.NOT_YYY_ZZZ);
              }
            })
        .named("Sets.filter, filtered input")
        .withFeatures(
            CollectionFeature.SUPPORTS_ADD,
            CollectionFeature.SUPPORTS_REMOVE,
            CollectionFeature.KNOWN_ORDER,
            CollectionSize.ANY,
            CollectionFeature.ALLOWS_NULL_QUERIES)
        .createTestSuite();
  }

  private enum SomeEnum {
    A,
    B,
    C,
    D
  }

  public void testImmutableEnumSet() {
    Set<SomeEnum> units = Sets.immutableEnumSet(SomeEnum.D, SomeEnum.B);

    assertThat(units).containsExactly(SomeEnum.B, SomeEnum.D).inOrder();
    try {
      units.remove(SomeEnum.B);
      fail("ImmutableEnumSet should throw an exception on remove()");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      units.add(SomeEnum.C);
      fail("ImmutableEnumSet should throw an exception on add()");
    } catch (UnsupportedOperationException expected) {
    }
  }

  @GwtIncompatible // SerializableTester
  public void testImmutableEnumSet_serialized() {
    Set<SomeEnum> units = Sets.immutableEnumSet(SomeEnum.D, SomeEnum.B);

    assertThat(units).containsExactly(SomeEnum.B, SomeEnum.D).inOrder();

    Set<SomeEnum> copy = SerializableTester.reserializeAndAssert(units);
    assertTrue(copy instanceof ImmutableEnumSet);
  }

  public void testImmutableEnumSet_fromIterable() {
    ImmutableSet<SomeEnum> none = Sets.immutableEnumSet(MinimalIterable.<SomeEnum>of());
    assertThat(none).isEmpty();

    ImmutableSet<SomeEnum> one = Sets.immutableEnumSet(MinimalIterable.of(SomeEnum.B));
    assertThat(one).contains(SomeEnum.B);

    ImmutableSet<SomeEnum> two = Sets.immutableEnumSet(MinimalIterable.of(SomeEnum.D, SomeEnum.B));
    assertThat(two).containsExactly(SomeEnum.B, SomeEnum.D).inOrder();
  }

  @GwtIncompatible // java serialization not supported in GWT.
  public void testImmutableEnumSet_deserializationMakesDefensiveCopy() throws Exception {
    ImmutableSet<SomeEnum> original = Sets.immutableEnumSet(SomeEnum.A, SomeEnum.B);
    int handleOffset = 6;
    byte[] serializedForm = serializeWithBackReference(original, handleOffset);
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedForm));

    ImmutableSet<?> deserialized = (ImmutableSet<?>) in.readObject();
    EnumSet<?> delegate = (EnumSet<?>) in.readObject();

    assertEquals(original, deserialized);
    assertTrue(delegate.remove(SomeEnum.A));
    assertTrue(deserialized.contains(SomeEnum.A));
  }

  @GwtIncompatible // java serialization not supported in GWT.
  private static byte[] serializeWithBackReference(Object original, int handleOffset)
      throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);

    out.writeObject(original);

    byte[] handle = toByteArray(baseWireHandle + handleOffset);
    byte[] ref = prepended(TC_REFERENCE, handle);
    bos.write(ref);

    return bos.toByteArray();
  }

  private static byte[] prepended(byte b, byte[] array) {
    byte[] out = new byte[array.length + 1];
    out[0] = b;
    System.arraycopy(array, 0, out, 1, array.length);
    return out;
  }

  @GwtIncompatible // java.nio.ByteBuffer
  private static byte[] toByteArray(int h) {
    return ByteBuffer.allocate(4).putInt(h).array();
  }

  public void testNewEnumSet_empty() {
    EnumSet<SomeEnum> copy = newEnumSet(Collections.<SomeEnum>emptySet(), SomeEnum.class);
    assertEquals(EnumSet.noneOf(SomeEnum.class), copy);
  }

  public void testNewEnumSet_enumSet() {
    EnumSet<SomeEnum> set = EnumSet.of(SomeEnum.A, SomeEnum.D);
    assertEquals(set, newEnumSet(set, SomeEnum.class));
  }

  public void testNewEnumSet_collection() {
    Set<SomeEnum> set = ImmutableSet.of(SomeEnum.B, SomeEnum.C);
    assertEquals(set, newEnumSet(set, SomeEnum.class));
  }

  public void testNewEnumSet_iterable() {
    Set<SomeEnum> set = ImmutableSet.of(SomeEnum.A, SomeEnum.B, SomeEnum.C);
    assertEquals(set, newEnumSet(unmodifiableIterable(set), SomeEnum.class));
  }

  public void testNewHashSetEmpty() {
    HashSet<Integer> set = Sets.newHashSet();
    verifySetContents(set, EMPTY_COLLECTION);
  }

  public void testNewHashSetVarArgs() {
    HashSet<Integer> set = Sets.newHashSet(0, 1, 1);
    verifySetContents(set, Arrays.asList(0, 1));
  }

  public void testNewHashSetFromCollection() {
    HashSet<Integer> set = Sets.newHashSet(SOME_COLLECTION);
    verifySetContents(set, SOME_COLLECTION);
  }

  public void testNewHashSetFromIterable() {
    HashSet<Integer> set = Sets.newHashSet(SOME_ITERABLE);
    verifySetContents(set, SOME_ITERABLE);
  }

  public void testNewHashSetWithExpectedSizeSmall() {
    HashSet<Integer> set = Sets.newHashSetWithExpectedSize(0);
    verifySetContents(set, EMPTY_COLLECTION);
  }

  public void testNewHashSetWithExpectedSizeLarge() {
    HashSet<Integer> set = Sets.newHashSetWithExpectedSize(1000);
    verifySetContents(set, EMPTY_COLLECTION);
  }

  public void testNewHashSetFromIterator() {
    HashSet<Integer> set = Sets.newHashSet(SOME_COLLECTION.iterator());
    verifySetContents(set, SOME_COLLECTION);
  }

  public void testNewConcurrentHashSetEmpty() {
    Set<Integer> set = Sets.newConcurrentHashSet();
    verifySetContents(set, EMPTY_COLLECTION);
  }

  public void testNewConcurrentHashSetFromCollection() {
    Set<Integer> set = Sets.newConcurrentHashSet(SOME_COLLECTION);
    verifySetContents(set, SOME_COLLECTION);
  }

  public void testNewLinkedHashSetEmpty() {
    LinkedHashSet<Integer> set = Sets.newLinkedHashSet();
    verifyLinkedHashSetContents(set, EMPTY_COLLECTION);
  }

  public void testNewLinkedHashSetFromCollection() {
    LinkedHashSet<Integer> set = Sets.newLinkedHashSet(LONGER_LIST);
    verifyLinkedHashSetContents(set, LONGER_LIST);
  }

  public void testNewLinkedHashSetFromIterable() {
    LinkedHashSet<Integer> set =
        Sets.newLinkedHashSet(
            new Iterable<Integer>() {
              @Override
              public Iterator<Integer> iterator() {
                return LONGER_LIST.iterator();
              }
            });
    verifyLinkedHashSetContents(set, LONGER_LIST);
  }

  public void testNewLinkedHashSetWithExpectedSizeSmall() {
    LinkedHashSet<Integer> set = Sets.newLinkedHashSetWithExpectedSize(0);
    verifySetContents(set, EMPTY_COLLECTION);
  }

  public void testNewLinkedHashSetWithExpectedSizeLarge() {
    LinkedHashSet<Integer> set = Sets.newLinkedHashSetWithExpectedSize(1000);
    verifySetContents(set, EMPTY_COLLECTION);
  }

  public void testNewTreeSetEmpty() {
    TreeSet<Integer> set = Sets.newTreeSet();
    verifySortedSetContents(set, EMPTY_COLLECTION, null);
  }

  public void testNewTreeSetEmptyDerived() {
    TreeSet<Derived> set = Sets.newTreeSet();
    assertTrue(set.isEmpty());
    set.add(new Derived("foo"));
    set.add(new Derived("bar"));
    assertThat(set).containsExactly(new Derived("bar"), new Derived("foo")).inOrder();
  }

  public void testNewTreeSetEmptyNonGeneric() {
    TreeSet<LegacyComparable> set = Sets.newTreeSet();
    assertTrue(set.isEmpty());
    set.add(new LegacyComparable("foo"));
    set.add(new LegacyComparable("bar"));
    assertThat(set)
        .containsExactly(new LegacyComparable("bar"), new LegacyComparable("foo"))
        .inOrder();
  }

  public void testNewTreeSetFromCollection() {
    TreeSet<Integer> set = Sets.newTreeSet(SOME_COLLECTION);
    verifySortedSetContents(set, SOME_COLLECTION, null);
  }

  public void testNewTreeSetFromIterable() {
    TreeSet<Integer> set = Sets.newTreeSet(SOME_ITERABLE);
    verifySortedSetContents(set, SOME_ITERABLE, null);
  }

  public void testNewTreeSetFromIterableDerived() {
    Iterable<Derived> iterable = Arrays.asList(new Derived("foo"), new Derived("bar"));
    TreeSet<Derived> set = Sets.newTreeSet(iterable);
    assertThat(set).containsExactly(new Derived("bar"), new Derived("foo")).inOrder();
  }

  public void testNewTreeSetFromIterableNonGeneric() {
    Iterable<LegacyComparable> iterable =
        Arrays.asList(new LegacyComparable("foo"), new LegacyComparable("bar"));
    TreeSet<LegacyComparable> set = Sets.newTreeSet(iterable);
    assertThat(set)
        .containsExactly(new LegacyComparable("bar"), new LegacyComparable("foo"))
        .inOrder();
  }

  public void testNewTreeSetEmptyWithComparator() {
    TreeSet<Integer> set = Sets.newTreeSet(SOME_COMPARATOR);
    verifySortedSetContents(set, EMPTY_COLLECTION, SOME_COMPARATOR);
  }

  public void testNewIdentityHashSet() {
    Set<Integer> set = Sets.newIdentityHashSet();
    Integer value1 = new Integer(12357);
    Integer value2 = new Integer(12357);
    assertTrue(set.add(value1));
    assertFalse(set.contains(value2));
    assertTrue(set.contains(value1));
    assertTrue(set.add(value2));
    assertEquals(2, set.size());
  }

  @GwtIncompatible // CopyOnWriteArraySet
  public void testNewCOWASEmpty() {
    CopyOnWriteArraySet<Integer> set = Sets.newCopyOnWriteArraySet();
    verifySetContents(set, EMPTY_COLLECTION);
  }

  @GwtIncompatible // CopyOnWriteArraySet
  public void testNewCOWASFromIterable() {
    CopyOnWriteArraySet<Integer> set = Sets.newCopyOnWriteArraySet(SOME_ITERABLE);
    verifySetContents(set, SOME_COLLECTION);
  }

  public void testComplementOfEnumSet() {
    Set<SomeEnum> units = EnumSet.of(SomeEnum.B, SomeEnum.D);
    EnumSet<SomeEnum> otherUnits = Sets.complementOf(units);
    verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
  }

  public void testComplementOfEnumSetWithType() {
    Set<SomeEnum> units = EnumSet.of(SomeEnum.B, SomeEnum.D);
    EnumSet<SomeEnum> otherUnits = Sets.complementOf(units, SomeEnum.class);
    verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
  }

  public void testComplementOfRegularSet() {
    Set<SomeEnum> units = Sets.newHashSet(SomeEnum.B, SomeEnum.D);
    EnumSet<SomeEnum> otherUnits = Sets.complementOf(units);
    verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
  }

  public void testComplementOfRegularSetWithType() {
    Set<SomeEnum> units = Sets.newHashSet(SomeEnum.B, SomeEnum.D);
    EnumSet<SomeEnum> otherUnits = Sets.complementOf(units, SomeEnum.class);
    verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
  }

  public void testComplementOfEmptySet() {
    Set<SomeEnum> noUnits = Collections.emptySet();
    EnumSet<SomeEnum> allUnits = Sets.complementOf(noUnits, SomeEnum.class);
    verifySetContents(EnumSet.allOf(SomeEnum.class), allUnits);
  }

  public void testComplementOfFullSet() {
    Set<SomeEnum> allUnits = Sets.newHashSet(SomeEnum.values());
    EnumSet<SomeEnum> noUnits = Sets.complementOf(allUnits, SomeEnum.class);
    verifySetContents(noUnits, EnumSet.noneOf(SomeEnum.class));
  }

  public void testComplementOfEmptyEnumSetWithoutType() {
    Set<SomeEnum> noUnits = EnumSet.noneOf(SomeEnum.class);
    EnumSet<SomeEnum> allUnits = Sets.complementOf(noUnits);
    verifySetContents(allUnits, EnumSet.allOf(SomeEnum.class));
  }

  public void testComplementOfEmptySetWithoutTypeDoesntWork() {
    Set<SomeEnum> set = Collections.emptySet();
    try {
      Sets.complementOf(set);
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }

  @GwtIncompatible // NullPointerTester
  public void testNullPointerExceptions() {
    new NullPointerTester()
        .setDefault(Enum.class, SomeEnum.A)
        .setDefault(Class.class, SomeEnum.class) // for newEnumSet
        .testAllPublicStaticMethods(Sets.class);
  }

  public void testNewSetFromMap() {
    Set<Integer> set = Sets.newSetFromMap(new HashMap<Integer, Boolean>());
    set.addAll(SOME_COLLECTION);
    verifySetContents(set, SOME_COLLECTION);
  }

  @GwtIncompatible // SerializableTester
  public void testNewSetFromMapSerialization() {
    Set<Integer> set = Sets.newSetFromMap(new LinkedHashMap<Integer, Boolean>());
    set.addAll(SOME_COLLECTION);
    Set<Integer> copy = SerializableTester.reserializeAndAssert(set);
    assertThat(copy).containsExactly(0, 1).inOrder();
  }

  public void testNewSetFromMapIllegal() {
    Map<Integer, Boolean> map = new LinkedHashMap<>();
    map.put(2, true);
    try {
      Sets.newSetFromMap(map);
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }

  // TODO: the overwhelming number of suppressions below suggests that maybe
  // it's not worth having a varargs form of this method at all...

  /** The 0-ary cartesian product is a single empty list. */
  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_zeroary() {
    assertThat(Sets.cartesianProduct()).containsExactly(list());
  }

  /** A unary cartesian product is one list of size 1 for each element in the input set. */
  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_unary() {
    assertThat(Sets.cartesianProduct(set(1, 2))).containsExactly(list(1), list(2));
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_binary0x0() {
    Set<Integer> mt = emptySet();
    assertEmpty(Sets.cartesianProduct(mt, mt));
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_binary0x1() {
    Set<Integer> mt = emptySet();
    assertEmpty(Sets.cartesianProduct(mt, set(1)));
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_binary1x0() {
    Set<Integer> mt = emptySet();
    assertEmpty(Sets.cartesianProduct(set(1), mt));
  }

  private static void assertEmpty(Set<? extends List<?>> set) {
    assertTrue(set.isEmpty());
    assertEquals(0, set.size());
    assertFalse(set.iterator().hasNext());
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_binary1x1() {
    assertThat(Sets.cartesianProduct(set(1), set(2))).contains(list(1, 2));
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_binary1x2() {
    assertThat(Sets.cartesianProduct(set(1), set(2, 3)))
        .containsExactly(list(1, 2), list(1, 3))
        .inOrder();
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_binary2x2() {
    assertThat(Sets.cartesianProduct(set(1, 2), set(3, 4)))
        .containsExactly(list(1, 3), list(1, 4), list(2, 3), list(2, 4))
        .inOrder();
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_2x2x2() {
    assertThat(Sets.cartesianProduct(set(0, 1), set(0, 1), set(0, 1)))
        .containsExactly(
            list(0, 0, 0),
            list(0, 0, 1),
            list(0, 1, 0),
            list(0, 1, 1),
            list(1, 0, 0),
            list(1, 0, 1),
            list(1, 1, 0),
            list(1, 1, 1))
        .inOrder();
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_contains() {
    Set<List<Integer>> actual = Sets.cartesianProduct(set(1, 2), set(3, 4));
    assertTrue(actual.contains(list(1, 3)));
    assertTrue(actual.contains(list(1, 4)));
    assertTrue(actual.contains(list(2, 3)));
    assertTrue(actual.contains(list(2, 4)));
    assertFalse(actual.contains(list(3, 1)));
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_unrelatedTypes() {
    Set<Integer> x = set(1, 2);
    Set<String> y = set("3", "4");

    List<Object> exp1 = list((Object) 1, "3");
    List<Object> exp2 = list((Object) 1, "4");
    List<Object> exp3 = list((Object) 2, "3");
    List<Object> exp4 = list((Object) 2, "4");

    assertThat(Sets.<Object>cartesianProduct(x, y))
        .containsExactly(exp1, exp2, exp3, exp4)
        .inOrder();
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProductTooBig() {
    Set<Integer> set = ContiguousSet.create(Range.closed(0, 10000), DiscreteDomain.integers());
    try {
      Sets.cartesianProduct(set, set, set, set, set);
      fail("Expected IAE");
    } catch (IllegalArgumentException expected) {
    }
  }

  @SuppressWarnings("unchecked") // varargs!
  public void testCartesianProduct_hashCode() {
    // Run through the same cartesian products we tested above

    Set<List<Integer>> degenerate = Sets.cartesianProduct();
    checkHashCode(degenerate);

    checkHashCode(Sets.cartesianProduct(set(1, 2)));

    int num = Integer.MAX_VALUE / 3 * 2; // tickle overflow-related problems
    checkHashCode(Sets.cartesianProduct(set(1, 2, num)));

    Set<Integer> mt = emptySet();
    checkHashCode(Sets.cartesianProduct(mt, mt));
    checkHashCode(Sets.cartesianProduct(mt, set(num)));
    checkHashCode(Sets.cartesianProduct(set(num), mt));
    checkHashCode(Sets.cartesianProduct(set(num), set(1)));
    checkHashCode(Sets.cartesianProduct(set(1), set(2, num)));
    checkHashCode(Sets.cartesianProduct(set(1, num), set(2, num - 1)));
    checkHashCode(Sets.cartesianProduct(set(1, num), set(2, num - 1), set(3, num + 1)));

    // a bigger one
    checkHashCode(
        Sets.cartesianProduct(set(1, num, num + 1), set(2), set(3, num + 2), set(4, 5, 6, 7, 8)));
  }

  public void testPowerSetEmpty() {
    ImmutableSet<Integer> elements = ImmutableSet.of();
    Set<Set<Integer>> powerSet = powerSet(elements);
    assertEquals(1, powerSet.size());
    assertEquals(ImmutableSet.of(ImmutableSet.of()), powerSet);
    assertEquals(0, powerSet.hashCode());
  }

  public void testPowerSetContents() {
    ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3);
    Set<Set<Integer>> powerSet = powerSet(elements);
    assertEquals(8, powerSet.size());
    assertEquals(4 * 1 + 4 * 2 + 4 * 3, powerSet.hashCode());

    Set<Set<Integer>> expected = newHashSet();
    expected.add(ImmutableSet.<Integer>of());
    expected.add(ImmutableSet.of(1));
    expected.add(ImmutableSet.of(2));
    expected.add(ImmutableSet.of(3));
    expected.add(ImmutableSet.of(1, 2));
    expected.add(ImmutableSet.of(1, 3));
    expected.add(ImmutableSet.of(2, 3));
    expected.add(ImmutableSet.of(1, 2, 3));

    Set<Set<Integer>> almostPowerSet = newHashSet(expected);
    almostPowerSet.remove(ImmutableSet.of(1, 2, 3));
    almostPowerSet.add(ImmutableSet.of(1, 2, 4));

    new EqualsTester()
        .addEqualityGroup(expected, powerSet)
        .addEqualityGroup(ImmutableSet.of(1, 2, 3))
        .addEqualityGroup(almostPowerSet)
        .testEquals();

    for (Set<Integer> subset : expected) {
      assertTrue(powerSet.contains(subset));
    }
    assertFalse(powerSet.contains(ImmutableSet.of(1, 2, 4)));
    assertFalse(powerSet.contains(singleton(null)));
    assertFalse(powerSet.contains(null));
    assertFalse(powerSet.contains((Object) "notASet"));
  }

  public void testPowerSetIteration_manual() {
    ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3);
    Set<Set<Integer>> powerSet = powerSet(elements);
    // The API doesn't promise this iteration order, but it's convenient here.
    Iterator<Set<Integer>> i = powerSet.iterator();
    assertEquals(ImmutableSet.of(), i.next());
    assertEquals(ImmutableSet.of(1), i.next());
    assertEquals(ImmutableSet.of(2), i.next());
    assertEquals(ImmutableSet.of(2, 1), i.next());
    assertEquals(ImmutableSet.of(3), i.next());
    assertEquals(ImmutableSet.of(3, 1), i.next());
    assertEquals(ImmutableSet.of(3, 2), i.next());
    assertEquals(ImmutableSet.of(3, 2, 1), i.next());
    assertFalse(i.hasNext());
    try {
      i.next();
      fail();
    } catch (NoSuchElementException expected) {
    }
  }

  @GwtIncompatible // too slow for GWT
  public void testPowerSetIteration_iteratorTester() {
    ImmutableSet<Integer> elements = ImmutableSet.of(1, 2);

    Set<Set<Integer>> expected = newLinkedHashSet();
    expected.add(ImmutableSet.<Integer>of());
    expected.add(ImmutableSet.of(1));
    expected.add(ImmutableSet.of(2));
    expected.add(ImmutableSet.of(1, 2));

    final Set<Set<Integer>> powerSet = powerSet(elements);
    new IteratorTester<Set<Integer>>(6, UNMODIFIABLE, expected, KNOWN_ORDER) {
      @Override
      protected Iterator<Set<Integer>> newTargetIterator() {
        return powerSet.iterator();
      }
    }.test();
  }

  public void testPowerSetIteration_iteratorTester_fast() {
    ImmutableSet<Integer> elements = ImmutableSet.of(1, 2);

    Set<Set<Integer>> expected = newLinkedHashSet();
    expected.add(ImmutableSet.<Integer>of());
    expected.add(ImmutableSet.of(1));
    expected.add(ImmutableSet.of(2));
    expected.add(ImmutableSet.of(1, 2));

    final Set<Set<Integer>> powerSet = powerSet(elements);
    new IteratorTester<Set<Integer>>(4, UNMODIFIABLE, expected, KNOWN_ORDER) {
      @Override
      protected Iterator<Set<Integer>> newTargetIterator() {
        return powerSet.iterator();
      }
    }.test();
  }

  public void testPowerSetSize() {
    assertPowerSetSize(1);
    assertPowerSetSize(2, 'a');
    assertPowerSetSize(4, 'a', 'b');
    assertPowerSetSize(8, 'a', 'b', 'c');
    assertPowerSetSize(16, 'a', 'b', 'd', 'e');
    assertPowerSetSize(
        1 << 30, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
        'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4');
  }

  public void testPowerSetCreationErrors() {
    try {
      Set<Set<Character>> unused =
          powerSet(
              newHashSet(
                  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                  'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5'));
      fail();
    } catch (IllegalArgumentException expected) {
    }

    try {
      Set<Set<Integer>> unused = powerSet(ContiguousSet.closed(0, Integer.MAX_VALUE / 2));
      fail();
    } catch (IllegalArgumentException expected) {
    }

    try {
      powerSet(singleton(null));
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() {
    ImmutableList<Integer> allElements =
        ImmutableList.of(
            4233352, 3284593, 3794208, 3849533, 4013967, 2902658, 1886275, 2131109, 985872,
            1843868);
    for (int i = 0; i < allElements.size(); i++) {
      Set<Integer> elements = newHashSet(allElements.subList(0, i));
      Set<Set<Integer>> powerSet1 = powerSet(elements);
      Set<Set<Integer>> powerSet2 = powerSet(elements);
      new EqualsTester()
          .addEqualityGroup(powerSet1, powerSet2, toHashSets(powerSet1))
          .addEqualityGroup(ImmutableSet.of())
          .addEqualityGroup(ImmutableSet.of(9999999))
          .addEqualityGroup("notASet")
          .testEquals();
      assertEquals(toHashSets(powerSet1).hashCode(), powerSet1.hashCode());
    }
  }

  public void testPowerSetEquals_independentOfOrder() {
    ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3, 4);
    Set<Set<Integer>> forward = powerSet(elements);
    Set<Set<Integer>> reverse = powerSet(ImmutableSet.copyOf(elements.asList().reverse()));
    new EqualsTester().addEqualityGroup(forward, reverse).testEquals();
  }

  /**
   * Test that a hash code miscomputed by "input.hashCode() * tooFarValue / 2" is correct under our
   * {@code hashCode} implementation.
   */
  public void testPowerSetHashCode_inputHashCodeTimesTooFarValueIsZero() {
    Set<Object> sumToEighthMaxIntElements =
        newHashSet(objectWithHashCode(1 << 29), objectWithHashCode(0));
    assertPowerSetHashCode(1 << 30, sumToEighthMaxIntElements);

    Set<Object> sumToQuarterMaxIntElements =
        newHashSet(objectWithHashCode(1 << 30), objectWithHashCode(0));
    assertPowerSetHashCode(1 << 31, sumToQuarterMaxIntElements);
  }

  public void testPowerSetShowOff() {
    Set<Object> zero = ImmutableSet.of();
    Set<Set<Object>> one = powerSet(zero);
    Set<Set<Set<Object>>> two = powerSet(one);
    Set<Set<Set<Set<Object>>>> four = powerSet(two);
    Set<Set<Set<Set<Set<Object>>>>> sixteen = powerSet(four);
    Set<Set<Set<Set<Set<Set<Object>>>>>> sixtyFiveThousandish = powerSet(sixteen);
    assertEquals(1 << 16, sixtyFiveThousandish.size());

    assertTrue(powerSet(makeSetOfZeroToTwentyNine()).contains(makeSetOfZeroToTwentyNine()));
    assertFalse(powerSet(makeSetOfZeroToTwentyNine()).contains(ImmutableSet.of(30)));
  }

  private static Set<Integer> makeSetOfZeroToTwentyNine() {
    // TODO: use Range once it's publicly available
    Set<Integer> zeroToTwentyNine = newHashSet();
    for (int i = 0; i < 30; i++) {
      zeroToTwentyNine.add(i);
    }
    return zeroToTwentyNine;
  }

  private static <E> Set<Set<E>> toHashSets(Set<Set<E>> powerSet) {
    Set<Set<E>> result = newHashSet();
    for (Set<E> subset : powerSet) {
      result.add(new HashSet<E>(subset));
    }
    return result;
  }

  private static Object objectWithHashCode(final int hashCode) {
    return new Object() {
      @Override
      public int hashCode() {
        return hashCode;
      }
    };
  }

  private static void assertPowerSetHashCode(int expected, Set<?> elements) {
    assertEquals(expected, powerSet(elements).hashCode());
  }

  private static void assertPowerSetSize(int i, Object... elements) {
    assertEquals(i, powerSet(newHashSet(elements)).size());
  }

  private static void checkHashCode(Set<?> set) {
    assertEquals(Sets.newHashSet(set).hashCode(), set.hashCode());
  }

  public void testCombinations() {
    ImmutableList<Set<Integer>> sampleSets =
        ImmutableList.<Set<Integer>>of(
            ImmutableSet.<Integer>of(),
            ImmutableSet.of(1, 2),
            ImmutableSet.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    for (Set<Integer> sampleSet : sampleSets) {
      for (int k = 0; k <= sampleSet.size(); k++) {
        final int size = k;
        Set<Set<Integer>> expected =
            Sets.filter(
                Sets.powerSet(sampleSet),
                new Predicate<Set<Integer>>() {

                  @Override
                  public boolean apply(Set<Integer> input) {
                    return input.size() == size;
                  }
                });
        assertWithMessage("Sets.combinations(%s, %s)", sampleSet, k)
            .that(Sets.combinations(sampleSet, k))
            .containsExactlyElementsIn(expected)
            .inOrder();
      }
    }
  }

  private static <E> Set<E> set(E... elements) {
    return ImmutableSet.copyOf(elements);
  }

  private static <E> List<E> list(E... elements) {
    return ImmutableList.copyOf(elements);
  }

  /**
   * Utility method to verify that the given LinkedHashSet is equal to and hashes identically to a
   * set constructed with the elements in the given collection. Also verifies that the ordering in
   * the set is the same as the ordering of the given contents.
   */
  private static <E> void verifyLinkedHashSetContents(
      LinkedHashSet<E> set, Collection<E> contents) {
    assertEquals(
        "LinkedHashSet should have preserved order for iteration",
        new ArrayList<E>(set),
        new ArrayList<E>(contents));
    verifySetContents(set, contents);
  }

  /**
   * Utility method to verify that the given SortedSet is equal to and hashes identically to a set
   * constructed with the elements in the given iterable. Also verifies that the comparator is the
   * same as the given comparator.
   */
  private static <E> void verifySortedSetContents(
      SortedSet<E> set, Iterable<E> iterable, @NullableDecl Comparator<E> comparator) {
    assertSame(comparator, set.comparator());
    verifySetContents(set, iterable);
  }

  /**
   * Utility method that verifies that the given set is equal to and hashes identically to a set
   * constructed with the elements in the given iterable.
   */
  private static <E> void verifySetContents(Set<E> set, Iterable<E> contents) {
    Set<E> expected = null;
    if (contents instanceof Set) {
      expected = (Set<E>) contents;
    } else {
      expected = new HashSet<E>();
      for (E element : contents) {
        expected.add(element);
      }
    }
    assertEquals(expected, set);
  }

  /** Simple base class to verify that we handle generics correctly. */
  static class Base implements Comparable<Base>, Serializable {
    private final String s;

    public Base(String s) {
      this.s = s;
    }

    @Override
    public int hashCode() { // delegate to 's'
      return s.hashCode();
    }

    @Override
    public boolean equals(Object other) {
      if (other == null) {
        return false;
      } else if (other instanceof Base) {
        return s.equals(((Base) other).s);
      } else {
        return false;
      }
    }

    @Override
    public int compareTo(Base o) {
      return s.compareTo(o.s);
    }

    private static final long serialVersionUID = 0;
  }

  /** Simple derived class to verify that we handle generics correctly. */
  static class Derived extends Base {
    public Derived(String s) {
      super(s);
    }

    private static final long serialVersionUID = 0;
  }

  @GwtIncompatible // NavigableSet
  public void testUnmodifiableNavigableSet() {
    TreeSet<Integer> mod = Sets.newTreeSet();
    mod.add(1);
    mod.add(2);
    mod.add(3);

    NavigableSet<Integer> unmod = unmodifiableNavigableSet(mod);

    /* Unmodifiable is a view. */
    mod.add(4);
    assertTrue(unmod.contains(4));
    assertTrue(unmod.descendingSet().contains(4));

    ensureNotDirectlyModifiable(unmod);
    ensureNotDirectlyModifiable(unmod.descendingSet());
    ensureNotDirectlyModifiable(unmod.headSet(2));
    ensureNotDirectlyModifiable(unmod.headSet(2, true));
    ensureNotDirectlyModifiable(unmod.tailSet(2));
    ensureNotDirectlyModifiable(unmod.tailSet(2, true));
    ensureNotDirectlyModifiable(unmod.subSet(1, 3));
    ensureNotDirectlyModifiable(unmod.subSet(1, true, 3, true));

    /* UnsupportedOperationException on indirect modifications. */
    NavigableSet<Integer> reverse = unmod.descendingSet();
    try {
      reverse.add(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      reverse.addAll(Collections.singleton(4));
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      reverse.remove(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
  }

  void ensureNotDirectlyModifiable(SortedSet<Integer> unmod) {
    try {
      unmod.add(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      unmod.remove(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      unmod.addAll(Collections.singleton(4));
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      Iterator<Integer> iterator = unmod.iterator();
      iterator.next();
      iterator.remove();
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
  }

  @GwtIncompatible // NavigableSet
  void ensureNotDirectlyModifiable(NavigableSet<Integer> unmod) {
    try {
      unmod.add(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      unmod.remove(4);
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      unmod.addAll(Collections.singleton(4));
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      unmod.pollFirst();
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      unmod.pollLast();
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      Iterator<Integer> iterator = unmod.iterator();
      iterator.next();
      iterator.remove();
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
    try {
      Iterator<Integer> iterator = unmod.descendingIterator();
      iterator.next();
      iterator.remove();
      fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException expected) {
    }
  }

  @GwtIncompatible // NavigableSet
  public void testSubSet_boundedRange() {
    ImmutableSortedSet<Integer> set = ImmutableSortedSet.of(2, 4, 6, 8, 10);
    ImmutableSortedSet<Integer> empty = ImmutableSortedSet.of();

    assertEquals(set, Sets.subSet(set, Range.closed(0, 12)));
    assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.closed(0, 4)));
    assertEquals(ImmutableSortedSet.of(2, 4, 6), Sets.subSet(set, Range.closed(2, 6)));
    assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.closed(3, 7)));
    assertEquals(empty, Sets.subSet(set, Range.closed(20, 30)));

    assertEquals(set, Sets.subSet(set, Range.open(0, 12)));
    assertEquals(ImmutableSortedSet.of(2), Sets.subSet(set, Range.open(0, 4)));
    assertEquals(ImmutableSortedSet.of(4), Sets.subSet(set, Range.open(2, 6)));
    assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.open(3, 7)));
    assertEquals(empty, Sets.subSet(set, Range.open(20, 30)));

    assertEquals(set, Sets.subSet(set, Range.openClosed(0, 12)));
    assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.openClosed(0, 4)));
    assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.openClosed(2, 6)));
    assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.openClosed(3, 7)));
    assertEquals(empty, Sets.subSet(set, Range.openClosed(20, 30)));

    assertEquals(set, Sets.subSet(set, Range.closedOpen(0, 12)));
    assertEquals(ImmutableSortedSet.of(2), Sets.subSet(set, Range.closedOpen(0, 4)));
    assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.closedOpen(2, 6)));
    assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.closedOpen(3, 7)));
    assertEquals(empty, Sets.subSet(set, Range.closedOpen(20, 30)));
  }

  @GwtIncompatible // NavigableSet
  public void testSubSet_halfBoundedRange() {
    ImmutableSortedSet<Integer> set = ImmutableSortedSet.of(2, 4, 6, 8, 10);
    ImmutableSortedSet<Integer> empty = ImmutableSortedSet.of();

    assertEquals(set, Sets.subSet(set, Range.atLeast(0)));
    assertEquals(ImmutableSortedSet.of(4, 6, 8, 10), Sets.subSet(set, Range.atLeast(4)));
    assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.atLeast(7)));
    assertEquals(empty, Sets.subSet(set, Range.atLeast(20)));

    assertEquals(set, Sets.subSet(set, Range.greaterThan(0)));
    assertEquals(ImmutableSortedSet.of(6, 8, 10), Sets.subSet(set, Range.greaterThan(4)));
    assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.greaterThan(7)));
    assertEquals(empty, Sets.subSet(set, Range.greaterThan(20)));

    assertEquals(empty, Sets.subSet(set, Range.lessThan(0)));
    assertEquals(ImmutableSortedSet.of(2), Sets.subSet(set, Range.lessThan(4)));
    assertEquals(ImmutableSortedSet.of(2, 4, 6), Sets.subSet(set, Range.lessThan(7)));
    assertEquals(set, Sets.subSet(set, Range.lessThan(20)));

    assertEquals(empty, Sets.subSet(set, Range.atMost(0)));
    assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.atMost(4)));
    assertEquals(ImmutableSortedSet.of(2, 4, 6), Sets.subSet(set, Range.atMost(7)));
    assertEquals(set, Sets.subSet(set, Range.atMost(20)));
  }

  @GwtIncompatible // NavigableSet
  public void testSubSet_unboundedRange() {
    ImmutableSortedSet<Integer> set = ImmutableSortedSet.of(2, 4, 6, 8, 10);

    assertEquals(set, Sets.subSet(set, Range.<Integer>all()));
  }

  @GwtIncompatible // NavigableSet
  public void testSubSet_unnaturalOrdering() {
    ImmutableSortedSet<Integer> set =
        ImmutableSortedSet.<Integer>reverseOrder().add(2, 4, 6, 8, 10).build();

    try {
      Sets.subSet(set, Range.closed(4, 8));
      fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException expected) {
    }

    // These results are all incorrect, but there's no way (short of iterating over the result)
    // to verify that with an arbitrary ordering or comparator.
    assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.atLeast(4)));
    assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.atMost(8)));
    assertEquals(ImmutableSortedSet.of(2, 4, 6, 8, 10), Sets.subSet(set, Range.<Integer>all()));
  }
}


text:
/*******************************************************************************
 *  @file      WorkThreadBaseClass.cpp 2013\6\24 10:44:54 $
 *  @author    Wang Xiaotao<wangxiaotao1980@gmail.com> (ÖÐÎıàÂë²âÊÔ)
 ******************************************************************************/

#include "WorkThread/WorkThreadBaseClass.h"
/******************************************************************************/

CPS_WORK_THREAD_NAMESPACE_BEGIN
// -----------------------------------------------------------------------------
//  WorkThreadBaseClass: Public, Constructor

WorkThreadBaseClass::WorkThreadBaseClass()
    : m_pMainThread(0)
    , m_isThreadStart(false)
    , m_isThreadCanPause(false)
{}

// -----------------------------------------------------------------------------
//  WorkThreadBaseClass: Public, Destructor

WorkThreadBaseClass::~WorkThreadBaseClass()
{}

// -----------------------------------------------------------------------------
// public
void WorkThreadBaseClass::setThreadCanPause( bool isPause )
{
    m_isThreadCanPause = isPause;
}

// -----------------------------------------------------------------------------
// public
void WorkThreadBaseClass::ThreadPausePoint()
{
    boost::mutex::scoped_lock tempScopeLock(m_mutexPause);

    if (m_isThreadCanPause)
    {
        m_condPause.wait(tempScopeLock);
    }
}

// -----------------------------------------------------------------------------
// public
void WorkThreadBaseClass::ThreadInterruptPoint()
{
    boost::this_thread::interruption_point();
}

// -----------------------------------------------------------------------------
// public
WorkThreadCode WorkThreadBaseClass::startThread()
{
    WorkThreadCode resultCode = THREAD_OK;

    if (true == m_isThreadStart)
    {
        resultCode = THREAD_HAS_STARTED;
        return resultCode;
    }

    if (0 != m_pMainThread)
    {
        delete m_pMainThread;
        m_pMainThread = 0;
    }

    if (THREAD_OK == resultCode)
    {
        m_pMainThread = new boost::thread(WorkThreadBaseClass::threadFunction, this);
        m_isThreadStart = true;
    }

    return resultCode;

}

// ------------------------------t-----------------------------------------------
// public
WorkThreadCode WorkThreadBaseClass::endThread()
{
    WorkThreadCode resultCode = THREAD_OK;

    if  (false == m_isThreadStart)
    {
        resultCode = THREAD_HAS_ENDED;
        return resultCode;
    }

    // ·¢³öÖжϵÄÏûÏ¢£¬ÆäÖжϽ«ÔÚThreadInterruptPoint ºÍ ThreadPausePoint µãÏàÓ¦ÖжÏÏûÏ¢
    m_pMainThread->interrupt();
    m_pMainThread->join();
    delete m_pMainThread;
    m_pMainThread = 0;

    return resultCode;
}
// -----------------------------------------------------------------------------
// public
WorkThreadCode WorkThreadBaseClass::pauseThread()
{
    WorkThreadCode resultCode = THREAD_OK;

    if  (false == m_isThreadStart)
    {
        resultCode = THREAD_HAS_ENDED;
        return resultCode;
    }

    setThreadCanPause(true);

    return resultCode;
}
// -----------------------------------------------------------------------------
// public
WorkThreadCode WorkThreadBaseClass::resumeThread()
{
    WorkThreadCode resultCode = THREAD_OK;

    if  (false == m_isThreadStart)
    {
        resultCode = THREAD_HAS_ENDED;
        return resultCode;
    }

    setThreadCanPause(false);
    m_condPause.notify_all();

    return resultCode;
}

// -----------------------------------------------------------------------------
// protected  static
WorkThreadCode WorkThreadBaseClass::threadFunction( WorkThreadBaseClass* pThis )
{
    return pThis->processWorkThread();
}

CPS_WORK_THREAD_NAMESPACE_END
//
// -----------------------------------------------------------------------------

redpajama_set_name: RedPajamaGithub


text:
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 2008-2009
 *
 * Haskell Program Coverage
 *
 * Do not #include this file directly: #include "Rts.h" instead.
 *
 * To understand the structure of the RTS headers, see the wiki:
 *   https://gitlab.haskell.org/ghc/ghc/wikis/commentary/source-tree/includes
 *
 * -------------------------------------------------------------------------- */

#pragma once

// Simple linked list of modules
typedef struct _HpcModuleInfo {
  char *modName;                // name of module
  StgWord32 tickCount;          // number of ticks
  StgWord32 hashNo;             // Hash number for this module's mix info
  StgWord64 *tixArr;            // tix Array; local for this module
  bool from_file;               // data was read from the .tix file
  struct _HpcModuleInfo *next;
} HpcModuleInfo;

void hs_hpc_module (char *modName,
                    StgWord32 modCount,
                    StgWord32 modHashNo,
                    StgWord64 *tixArr);

HpcModuleInfo * hs_hpc_rootModule (void);

void startupHpc(void);
void exitHpc(void);


text:
Function Get-StringHash([String] $String,$HashName = "MD5") {
    $StringBuilder = New-Object System.Text.StringBuilder
    [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{
        [Void]$StringBuilder.Append($_.ToString("x2"))
    }
    $StringBuilder.ToString()
}

Function Invoke-P2000Command {
<#
  .SYNOPSIS
  This function run command thru HTTP CLI interfeis of HP MSA P2000
  .DESCRIPTION
  Run commands and return its XML output.
  .EXAMPLE
  Invoke-P2000Command "10.10.24.132" "monitor" "!monitor" "show/sensor-status"
  .EXAMPLE
  "show/vdisks","show/disks" | Invoke-P2000Command "10.10.24.132" "monitor" "!monitor"
#>
 Param(
    [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $False)]
    [String]$Storage,

    [Parameter(Mandatory = $True, Position = 1, ValueFromPipeline = $False)]
    [String]$User,

    [Parameter(Mandatory = $True, Position = 2, ValueFromPipeline = $False)]
    [String]$Password,

    [Parameter(Mandatory = $True, Position = 3, ValueFromPipeline = $True)]
    [String]$Command
)
BEGIN {
    [string]$ToHash = $User + "_" + $Password
    [string]$Url = "http://" + $Storage + "/API/"
    [string]$hash = Get-StringHash $ToHash
    $WebClient = new-object System.Net.WebClient
    [string]$LoginUrl = $Url + "login/" + $hash
    [xml]$LoginXml = $webclient.DownloadString($LoginUrl)
    If ($LoginXml.RESPONSE.OBJECT.PROPERTY[0].innerxml -eq "success") {
        $SessionKey = $LoginXml.RESPONSE.OBJECT.PROPERTY[2].innerxml
        $webClient.Headers.Add("sessionKey", $SessionKey)
    } else {
        Return
    }
}
PROCESS {
    $CommandUrl = $Url + $Command
    [xml]$Xml = $webclient.DownloadString($CommandUrl)
    ,$Xml
}
END {
    $ExitUrl = $Url  + "exit"
    $webclient.DownloadString($ExitUrl)
}
}


text:
var X = 800, Y=600;

var game = new Phaser.Game(X, Y, Phaser.CANVAS, '', {preload: preload, create: create, update: update, render: render} );

var floor1, floor2, platform1, platform2, counterGreen = 0, counterBlue=0;

var textGreen, textBlue;

var correctInput_i = false, correctInput_j = false;

var player, upKey, downKey, leftKey, rightKey;

function preload() {

	game.scale.maxWidth = X;
    game.scale.maxHeight = Y;

	game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
	game.scale.setScreenSize();

	upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
    downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
    leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
    rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);

	game.load.image('green', 'pictures/ekoX.jpg');
	game.load.image('blue', 'pictures/Ammo1B38AB.jpg');
	game.load.image('floor', 'textures/Walls01.jpg');
	game.load.image('player', 'player/Player.jpg');
}

function create() {

	game.stage.backgroundColor = '#000';
	game.physics.startSystem(Phaser.Physics.ARCADE);
	game.physics.arcade.gravity.y = 300;

    textGreen = game.add.text(8*X/9, Y/30, "i="+counterGreen, { font: "40px Verdana", fill: "#4AE057", align: "center" });
    textGreen.anchor.set(0.5);

    textBlue = game.add.text(X/9, 14*Y/15, "j="+counterBlue, { font: "40px Verdana", fill: "#1B38AB", align: "center" });
    textBlue.anchor.set(0.5);

	floor1 = game.add.sprite(0, Y/2, 'floor');
	floor1.width = X/9;
	floor1.height = Y/30;
	game.physics.arcade.enable([floor1]);
	floor1.body.allowGravity = false;
	floor1.body.immovable = true;

	floor2 = game.add.sprite(8*X/9, Y/2, 'floor');
	floor2.width = X/9;
	floor2.height = Y/30;
	game.physics.arcade.enable([floor2]);
	floor2.body.allowGravity = false;
	floor2.body.immovable = true;

	platform1 = game.add.sprite(X/9, 7*Y/10, 'floor');
	platform1.width = 4*X/9;
	platform1.height = Y/30;
	game.physics.arcade.enable([platform1]);
	platform1.body.allowGravity = false;
	platform1.body.immovable = true;

	platform2 = game.add.sprite(5*X/9, 3*Y/10, 'floor');
	platform2.width = 3*X/9;
	platform2.height = Y/30;
	game.physics.arcade.enable([platform2]);
	platform2.body.allowGravity = false;
	platform2.body.immovable = true;

	player = game.add.sprite(X/20, Y/10, 'player');
	game.physics.arcade.enable([player]);
	player.anchor.setTo(0.5, 0.5);
	player.width=50;
	player.height=50;
}

function update() {

game.physics.arcade.collide(player, [floor1, floor2, platform1, platform2]);

if (upKey.isDown) player.y-=3;
if (downKey.isDown) player.y+=2;
if (leftKey.isDown) player.x-=2;
if (rightKey.isDown) player.x+=2;

if(correctInput_i) game.physics.arcade.collide(platform2, green, collisionHandlerGreen, null, this);
if(correctInput_j) game.physics.arcade.collide(platform1, blue, collisionHandlerBlue, null, this);

}

function render() {

}

function collisionHandlerGreen() {

	game.add.tween(platform2).to({ y: platform2.y + X/16 }, 750, Phaser.Easing.Linear.None, true);
	green.destroy();
	counterGreen--;
	textGreen.setText("i="+counterGreen);

    if(counterGreen>0) createGreen();
}

function collisionHandlerBlue() {

	game.add.tween(platform1).to({ y: platform1.y - Y/12 }, 750, Phaser.Easing.Linear.None, true);
	blue.destroy();
	counterBlue++;
	textBlue.setText("j="+counterBlue);

    if(counterBlue<0) createBlue();
}

function createGreen() {

	green = game.add.sprite(7*X/10, Y/16, 'green');
	green.width=20;
	green.height=20;
	game.physics.arcade.enable([green]);
	green.body.allowGravity = false;
	green.body.velocity.y = 100;
	green.body.bounce.set(0);

	correctInput_i = true;
}

function createBlue() {

	blue = game.add.sprite(5*X/16, 530, 'blue');
	blue.width=20;
	blue.height=20;
	game.physics.arcade.enable([blue]);
	blue.body.allowGravity = false;
	blue.body.velocity.y = -100;
	blue.body.bounce.set(0);

	correctInput_j = true;
}

function input() {

	counterGreen = 3;
	textGreen.setText("i="+counterGreen);
	createGreen();

	counterBlue = -3;
	textBlue.setText("j="+counterBlue);
	createBlue();
}


text:
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Homework8 {
	WebDriver driver = new FirefoxDriver();
	By ribbon = (By.className("sticker"));
	@Test
	public void Ribbon(){
	driver.manage().timeouts().implicitlyWait(10, TimeUnit.MICROSECONDS);

	driver.get("http://localhost:8080/litecart/");

		List<WebElement> ribbonlist = driver.findElements(By.className("image-wrapper"));
		 for(int j=0;j < ribbonlist.size();j++){
			 WebElement list = ribbonlist.get(j);
			if (list.findElements(ribbon).size()==1){
			 System.out.println(list.findElement(ribbon).getText());
			}
		 }
		 driver.quit();

}

	boolean areElementPresent(By locator) {
		return driver.findElements(locator).size() == 1;
	}
	}


text:
namespace testing {
class TestInfo;
}

namespace base {

// Instantiates TestSuite, runs it and returns exit code.
int RunUnitTestsUsingBaseTestSuite(int argc, char **argv);

class TestSuite {
 public:
  // Match function used by the GetTestCount method.
  typedef bool (*TestMatch)(const testing::TestInfo&);

  TestSuite(int argc, char** argv);
  virtual ~TestSuite();

  // Returns true if the test is marked as "MAYBE_".
  // When using different prefixes depending on platform, we use MAYBE_ and
  // preprocessor directives to replace MAYBE_ with the target prefix.
  static bool IsMarkedMaybe(const testing::TestInfo& test);

  void CatchMaybeTests();

  void ResetCommandLine();

  void AddTestLauncherResultPrinter();

  int Run();

 protected:
  // This constructor is only accessible to specialized test suite
  // implementations which need to control the creation of an AtExitManager
  // instance for the duration of the test.
  TestSuite(int argc, char** argv, bool create_at_exit_manager);

  // By default fatal log messages (e.g. from DCHECKs) result in error dialogs
  // which gum up buildbots. Use a minimalistic assert handler which just
  // terminates the process.
  static void UnitTestAssertHandler(const std::string& str);

  // Disable crash dialogs so that it doesn't gum up the buildbot
  virtual void SuppressErrorDialogs();

  // Override these for custom initialization and shutdown handling.  Use these
  // instead of putting complex code in your constructor/destructor.

  virtual void Initialize();
  virtual void Shutdown();

  // Make sure that we setup an AtExitManager so Singleton objects will be
  // destroyed.
  scoped_ptr<base::AtExitManager> at_exit_manager_;

 private:
  // Basic initialization for the test suite happens here.
  void PreInitialize(int argc, char** argv, bool create_at_exit_manager);

  bool initialized_command_line_;

  DISALLOW_COPY_AND_ASSIGN(TestSuite);
};

}  // namespace base

// TODO(brettw) remove this. This is a temporary hack to allow WebKit to compile
// until we can update it to use "base::" (preventing a two-sided patch).
using base::TestSuite;

#endif  // BASE_TEST_TEST_SUITE_H_


text:
from __future__ import annotations

import os
import sys
from collections import OrderedDict
from os import environ

import pytest

from dynaconf import settings  # noqa
from dynaconf.loaders.env_loader import load
from dynaconf.loaders.env_loader import load_from_env
from dynaconf.loaders.env_loader import write

# GLOBAL ENV VARS
environ["DYNACONF_HOSTNAME"] = "host.com"
environ["DYNACONF_PORT"] = "@int 5000"
environ["DYNACONF_ALIST"] = '@json ["item1", "item2", "item3", 123]'
environ["DYNACONF_ADICT"] = '@json {"key": "value", "int": 42}'
environ["DYNACONF_DEBUG"] = "@bool true"
environ["DYNACONF_MUSTBEFRESH"] = "first"
environ["DYNACONF_MUSTBEALWAYSFRESH"] = "first"
environ["DYNACONF_SHOULDBEFRESHINCONTEXT"] = "first"
environ["DYNACONF_VALUE"] = "@float 42.1"

# environ['FRESH_VARS_FOR_DYNACONF'] = '@json ["MUSTBEALWAYSFRESH"]'
# settings.configure(IGNORE_UNKNOWN_ENVVARS_FOR_DYNACONF=True)
settings.configure(
    FRESH_VARS_FOR_DYNACONF=["MUSTBEALWAYSFRESH"],
    ROOT_PATH_FOR_DYNACONF=os.path.dirname(os.path.abspath(__file__)),
)

SETTINGS_DATA = OrderedDict()
# Numbers
SETTINGS_DATA["DYNACONF_INTEGER"] = 42
SETTINGS_DATA["DYNACONF_FLOAT"] = 3.14
# Text
# 'DYNACONF_STRING': Hello,
SETTINGS_DATA["DYNACONF_STRING2"] = "Hello"
SETTINGS_DATA["DYNACONF_STRING2_LONG"] = "Hello World!"
SETTINGS_DATA["DYNACONF_BOOL"] = True
SETTINGS_DATA["DYNACONF_BOOL2"] = False
# Use extra quotes to force a string from other type
SETTINGS_DATA["DYNACONF_STRING21"] = '"42"'
SETTINGS_DATA["DYNACONF_STRING22"] = "'true'"
# Arrays must be homogeneous in toml syntax
SETTINGS_DATA["DYNACONF_ARRAY"] = [1, 2, 3]
SETTINGS_DATA["DYNACONF_ARRAY2"] = [1.1, 2.2, 3.3]
SETTINGS_DATA["DYNACONF_ARRAY3"] = ["a", "b", "c"]
# Dictionaries
SETTINGS_DATA["DYNACONF_DICT"] = {"val": 123}

SETTINGS_DATA_GROUND_TRUTH = """DYNACONF_TESTING=true
DYNACONF_INTEGER=42
DYNACONF_FLOAT=3.14
DYNACONF_STRING2=Hello
DYNACONF_STRING2_LONG="Hello World!"
DYNACONF_BOOL=True
DYNACONF_BOOL2=False
DYNACONF_STRING21="42"
DYNACONF_STRING22="true"
DYNACONF_ARRAY="[1, 2, 3]"
DYNACONF_ARRAY2="[1.1, 2.2, 3.3]"
DYNACONF_ARRAY3="[\'a\', \'b\', \'c\']"
DYNACONF_DICT="{\'val\': 123}"
"""

def test_write(tmpdir):
    settings_path = tmpdir.join(".env")

    write(settings_path, SETTINGS_DATA)

    ground_truth = SETTINGS_DATA_GROUND_TRUTH.split("\n")

    with open(str(settings_path)) as fp:
        lines = fp.readlines()
        for idx, line in enumerate(lines):
            line = line.strip()
            if line.split("=")[0] == "DYNACONF_TESTING":
                continue  # this key is written by the test itself; skip.
            assert line == ground_truth[idx].strip()

def test_env_loader():
    assert settings.HOSTNAME == "host.com"
    assert settings.PORT == 5000
    assert settings.ALIST == ["item1", "item2", "item3", 123]
    assert settings.ADICT == {"key": "value", "int": 42}

def test_single_key():
    environ["DYNACONF_HOSTNAME"] = "changedhost.com"
    load(settings, key="HOSTNAME")
    # hostname is reloaded
    assert settings.HOSTNAME == "changedhost.com"

def test_dotenv_loader():
    assert settings.DOTENV_INT == 1
    assert settings.DOTENV_STR == "hello"
    assert settings.DOTENV_FLOAT == 4.2
    assert settings.DOTENV_BOOL is False
    assert settings.DOTENV_JSON == ["1", "2"]
    assert settings.DOTENV_NOTE is None

def test_get_fresh():
    assert settings.MUSTBEFRESH == "first"
    environ["DYNACONF_MUSTBEFRESH"] = "second"
    with pytest.raises(AssertionError):
        # fresh should now be second
        assert settings.exists("MUSTBEFRESH")
        assert settings.get_fresh("MUSTBEFRESH") == "first"
    assert settings.get_fresh("MUSTBEFRESH") == "second"

    environ["DYNACONF_THISMUSTEXIST"] = "@int 1"
    # must tnot exist yet (not loaded)
    assert settings.exists("THISMUSTEXIST") is False
    # must exist because fresh will call loaders
    assert settings.exists("THISMUSTEXIST", fresh=True) is True
    # loaders run only once
    assert settings.get("THISMUSTEXIST") == 1

    environ["DYNACONF_THISMUSTEXIST"] = "@int 23"
    del environ["DYNACONF_THISMUSTEXIST"]
    # this should error because envvar got cleaned
    # but it is not, so cleaners should be fixed
    assert settings.get_fresh("THISMUSTEXIST") is None
    with pytest.raises(AttributeError):
        settings.THISMUSTEXIST
    with pytest.raises(KeyError):
        settings["THISMUSTEXIST"]

    environ["DYNACONF_THISMUSTEXIST"] = "@int 23"
    load(settings)
    assert settings.get("THISMUSTEXIST") == 23

def test_always_fresh():
    # assert environ['FRESH_VARS_FOR_DYNACONF'] == '@json ["MUSTBEALWAYSFRESH"]'  # noqa
    assert settings.FRESH_VARS_FOR_DYNACONF == ["MUSTBEALWAYSFRESH"]
    assert settings.MUSTBEALWAYSFRESH == "first"
    environ["DYNACONF_MUSTBEALWAYSFRESH"] = "second"
    assert settings.MUSTBEALWAYSFRESH == "second"
    environ["DYNACONF_MUSTBEALWAYSFRESH"] = "third"
    assert settings.MUSTBEALWAYSFRESH == "third"

def test_fresh_context():
    assert settings.SHOULDBEFRESHINCONTEXT == "first"
    environ["DYNACONF_SHOULDBEFRESHINCONTEXT"] = "second"
    assert settings.SHOULDBEFRESHINCONTEXT == "first"
    with settings.fresh():
        assert settings.get("DOTENV_INT") == 1
        assert settings.SHOULDBEFRESHINCONTEXT == "second"

def test_cleaner():
    settings.clean()
    with pytest.raises(AttributeError):
        assert settings.HOSTNAME == "host.com"

def test_empty_string_prefix():
    environ["_VALUE"] = "underscored"
    load_from_env(
        identifier="env_global", key=None, prefix="", obj=settings, silent=True
    )
    assert settings.VALUE == "underscored"

def test_no_prefix():
    environ["VALUE"] = "no_prefix"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix=False,
        obj=settings,
        silent=True,
    )
    assert settings.VALUE == "no_prefix"

def test_none_as_string_prefix():
    environ["NONE_VALUE"] = "none as prefix"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="none",
        obj=settings,
        silent=True,
    )
    assert settings.VALUE == "none as prefix"

def test_backwards_compat_using_env_argument():
    environ["BLARG_VALUE"] = "BLARG as prefix"
    load_from_env(
        identifier="env_global",
        key=None,
        env="BLARG",  # renamed to `prefix` on 3.0.0
        obj=settings,
        silent=True,
    )
    assert settings.VALUE == "BLARG as prefix"

def test_env_is_not_str_raises():
    with pytest.raises(TypeError):
        load_from_env(settings, prefix=int)
    with pytest.raises(TypeError):
        load_from_env(settings, prefix=True)

def test_can_load_in_to_dict():
    os.environ["LOADTODICT"] = "true"
    sets = {}
    load_from_env(sets, prefix=False, key="LOADTODICT")
    assert sets["LOADTODICT"] is True

def clean_environ(prefix):
    keys = [k for k in environ if k.startswith(prefix)]
    for key in keys:
        environ.pop(key)

@pytest.mark.skipif(
    sys.platform.startswith("win"),
    reason="Windows env vars are case insensitive",
)
def test_load_dunder(clean_env):
    """Test load and merge with dunder settings"""
    clean_environ("DYNACONF_DATABASES")
    settings.set(
        "DATABASES",
        {
            "default": {
                "NAME": "db",
                "ENGINE": "module.foo.engine",
                "ARGS": {"timeout": 30},
                "PORTS": [123, 456],
            }
        },
    )
    # change engine
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__ENGINE"] = "other.module"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES.default.ENGINE == "other.module"

    # change timeout directly
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__ARGS__timeout"] = "99"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES.default.ARGS.timeout == 99

    # add to ARGS
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__ARGS"] = "@merge {retries=10}"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES.default.ARGS.retries == 10
    assert settings.DATABASES.default.ARGS.timeout == 99

    # Ensure dictionary keeps its format
    assert settings.DATABASES == {
        "default": {
            "NAME": "db",
            "ENGINE": "other.module",
            "ARGS": {"timeout": 99, "retries": 10},
            "PORTS": [123, 456],
        }
    }
    assert "default" in settings["DATABASES"].keys()
    assert "DEFAULT" not in settings["DATABASES"].keys()
    assert "NAME" in settings["DATABASES"]["default"].keys()
    assert "name" not in settings["DATABASES"]["default"].keys()

    # Clean args
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__ARGS"] = "{timeout=8}"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES.default.ARGS == {"timeout": 8}

    # Make args empty
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__ARGS"] = "{}"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES.default.ARGS == {}

    # Remove ARGS key
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__ARGS"] = "@del"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert "ARGS" not in settings.DATABASES.default.keys()

    # add to existing PORTS
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__PORTS"] = "@merge [789, 101112]"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert "ARGS" not in settings.DATABASES.default.keys()
    assert settings.DATABASES.default.PORTS == [123, 456, 789, 101112]

    # reset PORTS
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__PORTS"] = "[789, 101112]"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert "ARGS" not in settings.DATABASES.default.keys()
    assert settings.DATABASES.default.PORTS == [789, 101112]

    # delete PORTS
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default__PORTS"] = "@del"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert "ARGS" not in settings.DATABASES.default.keys()
    assert "PORTS" not in settings.DATABASES.default.keys()

    # reset default key
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default"] = "{}"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES.default == {}

    # remove default
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__default"] = "@del"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES == {}

    # set value to databases
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES__foo"] = "bar"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES == {"foo": "bar"}

    # reset databases
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES"] = "{hello='world'}"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES == {"hello": "world"}

    # also reset databases
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES"] = "{yes='no'}"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert settings.DATABASES == {"yes": "no"}

    # remove databases
    clean_environ("DYNACONF_DATABASES")
    environ["DYNACONF_DATABASES"] = "@del"
    load_from_env(
        identifier="env_global",
        key=None,
        prefix="dynaconf",
        obj=settings,
        silent=True,
    )
    assert "DATABASES" not in settings

def test_filtering_unknown_variables():
    # Predefine some known variable.
    settings.MYCONFIG = "bar"
    # Enable environment filtering.
    settings.IGNORE_UNKNOWN_ENVVARS_FOR_DYNACONF = True

    # Pollute the environment.
    environ["IGNOREME"] = "foo"

    load_from_env(
        obj=settings,
        prefix=False,
        key=None,
        silent=True,
        identifier="env_global",
        env=False,
    )

    # Verify the filter works.
    assert not settings.get("IGNOREME")
    # Smoke test.
    assert settings.get("MYCONFIG") == "bar"

def test_filtering_unknown_variables_with_prefix():
    # Predefine some known variable.
    settings.MYCONFIG = "bar"
    # Enable environment filtering.
    settings.IGNORE_UNKNOWN_ENVVARS_FOR_DYNACONF = True

    # Pollute the environment.
    environ["APP_IGNOREME"] = "foo"
    # Also change legitimate variable.
    environ["APP_MYCONFIG"] = "ham"

    load_from_env(
        obj=settings,
        prefix="APP",
        key=None,
        silent=True,
        identifier="env_global",
        env=False,
    )

    # Verify the filter works.
    assert not settings.get("IGNOREME")
    # Smoke test.
    assert settings.get("MYCONFIG") == "ham"


text:
'use strict';

const DaoBase = require( '../../source/data-access/dao-base.js' );

class BlanketOrderChildDao extends DaoBase {

  constructor() {
    super( 'BlanketOrderChildDao' );
  }

  create( ctx ) {
    console.log( '--- Blanket order child DAO.create' );

    ctx.fulfill( {} );
  }

  insert( ctx, data ) {
    console.log( '--- Blanket order child DAO.insert' );

    data.orderKey = ++global.orderKey;
    data.createdDate = new Date();
    const key = data.orderKey;
    global.orders[ key ] = data;
    ctx.fulfill( data );
  }

  update( ctx, data ) {
    console.log( '--- Blanket order child DAO.update' );

    const key = data.orderKey;
    if (!global.orders[ key ])
      ctx.reject( new Error( 'Blanket order child not found.' ) );
    else {
      data.modifiedDate = new Date();
      global.orders[ key ] = data;
      ctx.fulfill( data );
    }
  }

  remove( ctx, filter ) {
    console.log( '--- Blanket order child DAO.remove' );

    const key = filter;
    if (global.orders[ key ])
      delete global.orders[ key ];
    ctx.fulfill( null );
  }
}

module.exports = BlanketOrderChildDao;


text:
@interface BlazeTilesTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>
{

}

@end

@implementation BlazeTilesTableViewCell

-(void)awakeFromNib
{
    [super awakeFromNib];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}

-(void)updateCell
{
    self.collectionView.ID = self.row.ID;
    [self.collectionView reloadData];

    if(self.row.tileSelectAutomatically) {
        [self.row updatedValue:self.row.value];
    }

    if(self.row.tileCellXibName.length) {
        [self.collectionView registerNib:[UINib nibWithNibName:self.row.tileCellXibName bundle:nil] forCellWithReuseIdentifier:self.row.tileCellXibName];
    }

    //Editable
    self.collectionView.userInteractionEnabled = !self.row.disableEditing;
}

#pragma mark - UICollectionView DataSource Methods

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.row.tilesValues.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BlazeTileCollectionViewCell *cell;
    if(self.row.tileCellXibName.length) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.row.tileCellXibName forIndexPath:indexPath];
    }
    else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:XIBDefaultTileCollectionViewCell forIndexPath:indexPath];
    }
    NSArray *tilesArray = self.row.tilesValues;
    cell.inputTile = tilesArray[indexPath.row];
    if(self.row.tilesMultipleSelection) {
        if(![self.row.value isKindOfClass:[NSMutableArray class]]) {
            cell.selected = false;
        }
        else {
            NSUInteger idx = [self.row.value indexOfObject:@(indexPath.row)];
            if(idx != NSNotFound) {
                cell.selected = true;
            } else {
                cell.selected = false;
            }
        }
    } else {
        cell.selected = indexPath.row == [self.row.value intValue];
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *tilesArray = self.row.tilesValues;
    float width = collectionView.bounds.size.width;
    float tileWidth = width/tilesArray.count;
    if(tilesArray.count>self.row.tilesPerRow) {
        tileWidth = width/self.row.tilesPerRow;
    }
    float tileHeight = self.row.tileHeight;
    if(!self.row.tileHeight) {
        int totalTiles = (int)self.row.tilesValues.count;
        float totalRows = (float)totalTiles/(float)self.row.tilesPerRow;
        tileHeight = floor(collectionView.frame.size.height/(float)totalRows)-1.0f;
    }
    CGSize cellSize = CGSizeMake(floor(tileWidth), tileHeight);
    return cellSize;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.row.disableEditing) {
        return;
    }

    NSArray *tilesArray = self.row.tilesValues;
    NSNumber *tileIndex = @(indexPath.row);

    if(self.row.tilesMultipleSelection) {
        if(![self.row.value isKindOfClass:[NSMutableArray class]]) {
            self.row.value = [NSMutableArray new];
        }

        NSUInteger idx = [self.row.value indexOfObject:tileIndex];
        if(idx != NSNotFound) {
            [self.row.value removeObjectAtIndex:idx];
        } else {
            [self.row.value addObject:tileIndex];
        }
        [self.row updatedValue:self.row.value];
        [collectionView reloadData];
    } else {
        BlazeInputTile *tile = tilesArray[indexPath.row];
        self.row.value = @(indexPath.row);
        [self.row updatedValue:self.row.value];
        for(BlazeTileCollectionViewCell *cell in collectionView.visibleCells) {
            cell.selected = cell.inputTile == tile;
        }
    }
}

@end