Skip to content

Commit

Permalink
Add test cases for indexOf and contains method of ArrayUtils class (#215
Browse files Browse the repository at this point in the history
)

* add a test case for indexOf method of ArrayUtils class

* add a test case for indeOf method

* add test cases for contains method
  • Loading branch information
HiFromAjay committed Jan 20, 2024
1 parent 4ae3ce2 commit 259a834
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/test/java/org/apache/commons/collections4/ArrayUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class ArrayUtilsTest {

@Test
public void testIndexOf() {
final Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.indexOf(null, null));
assertEquals(-1, ArrayUtils.indexOf(null, "0"));
assertEquals(-1, ArrayUtils.indexOf(new Object[0], "0"));
assertEquals(0, ArrayUtils.indexOf(array, "0"));
assertEquals(1, ArrayUtils.indexOf(array, "1"));
assertEquals(2, ArrayUtils.indexOf(array, "2"));
assertEquals(3, ArrayUtils.indexOf(array, "3"));
assertEquals(4, ArrayUtils.indexOf(array, null));
assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
}

@Test
public void testContains() {
final Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertFalse(ArrayUtils.contains(null, null));
assertFalse(ArrayUtils.contains(null, "1"));
assertTrue(ArrayUtils.contains(array, "0"));
assertTrue(ArrayUtils.contains(array, "1"));
assertTrue(ArrayUtils.contains(array, "2"));
assertTrue(ArrayUtils.contains(array, "3"));
assertTrue(ArrayUtils.contains(array, null));
assertFalse(ArrayUtils.contains(array, "notInArray"));
}

@Test
public void testContains_LANG_1261() {
class LANG1261ParentObject {
@Override
public boolean equals(final Object o) {
return true;
}
}
class LANG1261ChildObject extends LANG1261ParentObject {
}
final Object[] array = new LANG1261ChildObject[] { new LANG1261ChildObject() };
assertTrue(ArrayUtils.contains(array, new LANG1261ParentObject()));
}
}

0 comments on commit 259a834

Please sign in to comment.