001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.testing.MinimalSet;
023import com.google.common.collect.testing.features.CollectionFeature;
024import com.google.common.collect.testing.features.CollectionSize;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028
029/**
030 * Tests {@link List#equals}.
031 *
032 * @author George van den Driessche
033 */
034@GwtCompatible
035public class ListEqualsTester<E> extends AbstractListTester<E> {
036  public void testEquals_otherListWithSameElements() {
037    assertTrue(
038        "A List should equal any other List containing the same elements.",
039        getList().equals(new ArrayList<E>(getOrderedElements())));
040  }
041
042  @CollectionSize.Require(absent = CollectionSize.ZERO)
043  public void testEquals_otherListWithDifferentElements() {
044    ArrayList<E> other = new ArrayList<>(getSampleElements());
045    other.set(other.size() / 2, getSubjectGenerator().samples().e3());
046    assertFalse(
047        "A List should not equal another List containing different elements.",
048        getList().equals(other));
049  }
050
051  @CollectionSize.Require(absent = CollectionSize.ZERO)
052  public void testEquals_otherListContainingNull() {
053    List<E> other = new ArrayList<>(getSampleElements());
054    other.set(other.size() / 2, null);
055    assertFalse(
056        "Two Lists should not be equal if exactly one of them has null at a given index.",
057        getList().equals(other));
058  }
059
060  @CollectionSize.Require(absent = CollectionSize.ZERO)
061  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
062  public void testEquals_containingNull() {
063    ArrayList<E> elements = new ArrayList<>(getSampleElements());
064    elements.set(elements.size() / 2, null);
065    collection = getSubjectGenerator().create(elements.toArray());
066    List<E> other = new ArrayList<>(getSampleElements());
067    assertFalse(
068        "Two Lists should not be equal if exactly one of them has null at a given index.",
069        getList().equals(other));
070  }
071
072  @CollectionSize.Require(absent = CollectionSize.ZERO)
073  public void testEquals_shorterList() {
074    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
075    assertFalse(
076        "Lists of different sizes should not be equal.",
077        getList().equals(new ArrayList<E>(fewerElements)));
078  }
079
080  public void testEquals_longerList() {
081    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
082    assertFalse(
083        "Lists of different sizes should not be equal.",
084        getList().equals(new ArrayList<E>(moreElements)));
085  }
086
087  public void testEquals_set() {
088    assertFalse("A List should never equal a Set.", getList().equals(MinimalSet.from(getList())));
089  }
090}