001/*
002 * Copyright (C) 2007 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;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.collect.testing.WrongType;
024import com.google.common.collect.testing.features.CollectionFeature;
025import com.google.common.collect.testing.features.CollectionSize;
026import org.jspecify.annotations.Nullable;
027
028/**
029 * Common parent class for {@link ListIndexOfTester} and {@link ListLastIndexOfTester}.
030 *
031 * @author Chris Povirk
032 */
033@GwtCompatible
034public abstract class AbstractListIndexOfTester<E> extends AbstractListTester<E> {
035  /** Override to call {@code indexOf()} or {@code lastIndexOf()}. */
036  protected abstract int find(@Nullable Object o);
037
038  /** Override to return "indexOf" or "lastIndexOf()" for use in failure messages. */
039  protected abstract String getMethodName();
040
041  @CollectionSize.Require(absent = ZERO)
042  public void testFind_yes() {
043    assertEquals(
044        getMethodName() + "(firstElement) should return 0", 0, find(getOrderedElements().get(0)));
045  }
046
047  public void testFind_no() {
048    assertEquals(getMethodName() + "(notPresent) should return -1", -1, find(e3()));
049  }
050
051  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
052  public void testFind_nullNotContainedButSupported() {
053    assertEquals(getMethodName() + "(nullNotPresent) should return -1", -1, find(null));
054  }
055
056  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
057  public void testFind_nullNotContainedAndUnsupported() {
058    try {
059      assertEquals(getMethodName() + "(nullNotPresent) should return -1 or throw", -1, find(null));
060    } catch (NullPointerException tolerated) {
061    }
062  }
063
064  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
065  @CollectionSize.Require(absent = ZERO)
066  public void testFind_nonNullWhenNullContained() {
067    initCollectionWithNullElement();
068    assertEquals(getMethodName() + "(notPresent) should return -1", -1, find(e3()));
069  }
070
071  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
072  @CollectionSize.Require(absent = ZERO)
073  public void testFind_nullContained() {
074    initCollectionWithNullElement();
075    assertEquals(
076        getMethodName() + "(null) should return " + getNullLocation(),
077        getNullLocation(),
078        find(null));
079  }
080
081  public void testFind_wrongType() {
082    try {
083      assertEquals(
084          getMethodName() + "(wrongType) should return -1 or throw", -1, find(WrongType.VALUE));
085    } catch (ClassCastException tolerated) {
086    }
087  }
088}