11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License.
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect.testing;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.ArrayList;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Arrays;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collection;
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collections;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.List;
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Base class for testers of classes (including {@link Collection}
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * and {@link java.util.Map Map}) that contain elements.
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>This class is GWT compatible.
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param <C> the type of the container
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param <E> the type of the container's contents
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author George van den Driessche
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic abstract class AbstractContainerTester<C, E>
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    extends AbstractTester<OneSizeTestContainerGenerator<C, E>> {
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected SampleElements<E> samples;
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected C container;
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public void setUp() throws Exception {
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    super.setUp();
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    samples = this.getSubjectGenerator().samples();
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    resetContainer();
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the contents of the container under test, for use by
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link #expectContents(Object[]) expectContents(E...)} and its friends.
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected abstract Collection<E> actualContents();
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Replaces the existing container under test with a new container created
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * by the subject generator.
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @see #resetContainer(Object) resetContainer(C)
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the new container instance.
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected C resetContainer() {
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return resetContainer(getSubjectGenerator().createTestSubject());
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Replaces the existing container under test with a new container.
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * This is useful when a single test method needs to create multiple
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * containers while retaining the ability to use
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link #expectContents(Object[]) expectContents(E...)} and other
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * convenience methods. The creation of multiple containers in a single
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * method is discouraged in most cases, but it is vital to the iterator tests.
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the new container instance
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param newValue the new container instance
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected C resetContainer(C newValue) {
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    container = newValue;
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return container;
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @see #expectContents(java.util.Collection)
831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param elements expected contents of {@link #container}
851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected final void expectContents(E... elements) {
871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expectContents(Arrays.asList(elements));
881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Asserts that the collection under test contains exactly the given elements,
921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * respecting cardinality but not order. Subclasses may override this method
931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * to provide stronger assertions, e.g., to check ordering in lists, but
941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * realize that <strong>unless a test extends
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link com.google.common.collect.testing.testers.AbstractListTester
961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * AbstractListTester}, a call to {@code expectContents()} invokes this
971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * version</strong>.
981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param expected expected value of {@link #container}
1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /*
1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * TODO: improve this and other implementations and move out of this framework
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * for wider use
1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * TODO: could we incorporate the overriding logic from AbstractListTester, by
1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * examining whether the features include KNOWN_ORDER?
1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected void expectContents(Collection<E> expected) {
1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Helpers.assertEqualIgnoringOrder(expected, actualContents());
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected void expectUnchanged() {
1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expectContents(getSampleElements());
1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Asserts that the collection under test contains exactly the elements it was
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * initialized with plus the given elements, according to
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link #expectContents(java.util.Collection)}. In other words, for the
1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * default {@code expectContents()} implementation, the number of occurrences
1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * of each given element has increased by one since the test collection was
1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * created, and the number of occurrences of all other elements has not
1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * changed.
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: This means that a test like the following will fail if
1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code collection} is a {@code Set}:
1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>
1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * collection.add(existingElement);
1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * expectAdded(existingElement);</pre>
1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * In this case, {@code collection} was not modified as a result of the
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code add()} call, and the test will fail because the number of
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * occurrences of {@code existingElement} is unchanged.
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param elements expected additional contents of {@link #container}
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected final void expectAdded(E... elements) {
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    List<E> expected = Helpers.copyToList(getSampleElements());
1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expected.addAll(Arrays.asList(elements));
1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expectContents(expected);
1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected final void expectAdded(int index, E... elements) {
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expectAdded(index, Arrays.asList(elements));
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected final void expectAdded(int index, Collection<E> elements) {
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    List<E> expected = Helpers.copyToList(getSampleElements());
1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expected.addAll(index, elements);
1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    expectContents(expected);
1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /*
1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * TODO: if we're testing a list, we could check indexOf(). (Doing it in
1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * AbstractListTester isn't enough because many tests that run on lists don't
1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * extends AbstractListTester.) We could also iterate over all elements to
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * verify absence
1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected void expectMissing(E... elements) {
1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (E element : elements) {
1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      assertFalse("Should not contain " + element,
1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          actualContents().contains(element));
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected E[] createSamplesArray() {
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    E[] array = getSubjectGenerator().createArray(getNumElements());
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    getSampleElements().toArray(array);
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return array;
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static class ArrayWithDuplicate<E> {
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public final E[] elements;
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public final E duplicate;
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private ArrayWithDuplicate(E[] elements, E duplicate) {
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.elements = elements;
1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.duplicate = duplicate;
1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return an array of the proper size with a duplicate element.
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * The size must be at least three.
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected ArrayWithDuplicate<E> createArrayWithDuplicateElement() {
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    E[] elements = createSamplesArray();
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    E duplicate = elements[(elements.length / 2) - 1];
1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    elements[(elements.length / 2) + 1] = duplicate;
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new ArrayWithDuplicate<E>(elements, duplicate);
1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // Helper methods to improve readability of derived classes
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected int getNumElements() {
1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return getSubjectGenerator().getCollectionSize().getNumElements();
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected Collection<E> getSampleElements(int howMany) {
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return getSubjectGenerator().getSampleElements(howMany);
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected Collection<E> getSampleElements() {
2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return getSampleElements(getNumElements());
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the {@linkplain #getSampleElements() sample elements} as ordered by
2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link TestContainerGenerator#order(List)}. Tests should used this method
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * only if they declare requirement {@link
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * com.google.common.collect.testing.features.CollectionFeature#KNOWN_ORDER}.
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected List<E> getOrderedElements() {
2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    List<E> list = new ArrayList<E>();
2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (E e : getSubjectGenerator().order(
2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        new ArrayList<E>(getSampleElements()))) {
2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      list.add(e);
2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return Collections.unmodifiableList(list);
2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a suitable location for a null element, to use when initializing
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * containers for tests that involve a null element being present.
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected int getNullLocation() {
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return getNumElements() / 2;
2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @SuppressWarnings("unchecked")
2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected MinimalCollection<E> createDisjointCollection() {
2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return MinimalCollection.of(samples.e3, samples.e4);
2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @SuppressWarnings("unchecked")
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected MinimalCollection<E> emptyCollection() {
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return MinimalCollection.<E>of();
2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
241