Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Objects/CheckIndex.java
38811 views
/*1* Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @summary com.sun.crypto.provider.Preconditions.checkIndex tests26* @run testng CheckIndex27* @bug 8135248 8142493 815579428*/2930import com.sun.crypto.provider.Preconditions;31import org.testng.annotations.DataProvider;32import org.testng.annotations.Test;3334import java.util.ArrayList;35import java.util.Arrays;36import java.util.Collections;37import java.util.List;38import java.util.Objects;39import java.util.function.BiConsumer;40import java.util.function.BiFunction;41import java.util.function.IntSupplier;4243import static org.testng.Assert.*;4445public class CheckIndex {4647static class AssertingOutOfBoundsException extends RuntimeException {48public AssertingOutOfBoundsException(String message) {49super(message);50}51}5253static BiFunction<String, List<Integer>, AssertingOutOfBoundsException> assertingOutOfBounds(54String message, String expCheckKind, Integer... expArgs) {55return (checkKind, args) -> {56assertEquals(checkKind, expCheckKind);57assertEquals(args, Collections.unmodifiableList(Arrays.asList(expArgs)));58try {59args.clear();60fail("Out of bounds List<Integer> argument should be unmodifiable");61} catch (Exception e) {62}63return new AssertingOutOfBoundsException(message);64};65}6667static BiFunction<String, List<Integer>, AssertingOutOfBoundsException> assertingOutOfBoundsReturnNull(68String expCheckKind, Integer... expArgs) {69return (checkKind, args) -> {70assertEquals(checkKind, expCheckKind);71assertEquals(args, Collections.unmodifiableList(Arrays.asList(expArgs)));72return null;73};74}7576static final int[] VALUES = {0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, -1, Integer.MIN_VALUE + 1, Integer.MIN_VALUE};7778@DataProvider79static Object[][] checkIndexProvider() {80List<Object[]> l = new ArrayList<>();81for (int index : VALUES) {82for (int length : VALUES) {83boolean withinBounds = index >= 0 &&84length >= 0 &&85index < length;86l.add(new Object[]{index, length, withinBounds});87}88}89return l.toArray(new Object[0][0]);90}9192interface X {93int apply(int a, int b, int c);94}9596@Test(dataProvider = "checkIndexProvider")97public void testCheckIndex(int index, int length, boolean withinBounds) {98List<Integer> list = Collections.unmodifiableList(Arrays.asList(new Integer[] { index, length }));99String expectedMessage = withinBounds100? null101: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).102apply("checkIndex", list).getMessage();103104BiConsumer<Class<? extends RuntimeException>, IntSupplier> checker = (ec, s) -> {105try {106int rIndex = s.getAsInt();107if (!withinBounds)108fail(String.format(109"Index %d is out of bounds of [0, %d), but was reported to be within bounds", index, length));110assertEquals(rIndex, index);111}112catch (RuntimeException e) {113assertTrue(ec.isInstance(e));114if (withinBounds)115fail(String.format(116"Index %d is within bounds of [0, %d), but was reported to be out of bounds", index, length));117else118assertEquals(e.getMessage(), expectedMessage);119}120};121122checker.accept(AssertingOutOfBoundsException.class,123() -> Preconditions.checkIndex(index, length,124assertingOutOfBounds(expectedMessage, "checkIndex", index, length)));125checker.accept(IndexOutOfBoundsException.class,126() -> Preconditions.checkIndex(index, length,127assertingOutOfBoundsReturnNull("checkIndex", index, length)));128checker.accept(IndexOutOfBoundsException.class,129() -> Preconditions.checkIndex(index, length, null));130checker.accept(ArrayIndexOutOfBoundsException.class,131() -> Preconditions.checkIndex(index, length,132Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));133checker.accept(StringIndexOutOfBoundsException.class,134() -> Preconditions.checkIndex(index, length,135Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));136}137138139@DataProvider140static Object[][] checkFromToIndexProvider() {141List<Object[]> l = new ArrayList<>();142for (int fromIndex : VALUES) {143for (int toIndex : VALUES) {144for (int length : VALUES) {145boolean withinBounds = fromIndex >= 0 &&146toIndex >= 0 &&147length >= 0 &&148fromIndex <= toIndex &&149toIndex <= length;150l.add(new Object[]{fromIndex, toIndex, length, withinBounds});151}152}153}154return l.toArray(new Object[0][0]);155}156157@Test(dataProvider = "checkFromToIndexProvider")158public void testCheckFromToIndex(int fromIndex, int toIndex, int length, boolean withinBounds) {159List<Integer> list = Collections.unmodifiableList(Arrays.asList(new Integer[] { fromIndex, toIndex, length }));160String expectedMessage = withinBounds161? null162: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).163apply("checkFromToIndex", list).getMessage();164165BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> {166try {167int rIndex = s.getAsInt();168if (!withinBounds)169fail(String.format(170"Range [%d, %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, toIndex, length));171assertEquals(rIndex, fromIndex);172}173catch (RuntimeException e) {174assertTrue(ec.isInstance(e));175if (withinBounds)176fail(String.format(177"Range [%d, %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, toIndex, length));178else179assertEquals(e.getMessage(), expectedMessage);180}181};182183check.accept(AssertingOutOfBoundsException.class,184() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,185assertingOutOfBounds(expectedMessage, "checkFromToIndex", fromIndex, toIndex, length)));186check.accept(IndexOutOfBoundsException.class,187() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,188assertingOutOfBoundsReturnNull("checkFromToIndex", fromIndex, toIndex, length)));189check.accept(IndexOutOfBoundsException.class,190() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, null));191check.accept(ArrayIndexOutOfBoundsException.class,192() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,193Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));194check.accept(StringIndexOutOfBoundsException.class,195() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,196Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));197}198199200@DataProvider201static Object[][] checkFromIndexSizeProvider() {202List<Object[]> l = new ArrayList<>();203for (int fromIndex : VALUES) {204for (int size : VALUES) {205for (int length : VALUES) {206// Explicitly convert to long207long lFromIndex = fromIndex;208long lSize = size;209long lLength = length;210// Avoid overflow211long lToIndex = lFromIndex + lSize;212213boolean withinBounds = lFromIndex >= 0L &&214lSize >= 0L &&215lLength >= 0L &&216lFromIndex <= lToIndex &&217lToIndex <= lLength;218l.add(new Object[]{fromIndex, size, length, withinBounds});219}220}221}222return l.toArray(new Object[0][0]);223}224225@Test(dataProvider = "checkFromIndexSizeProvider")226public void testCheckFromIndexSize(int fromIndex, int size, int length, boolean withinBounds) {227List<Integer> list = Collections.unmodifiableList(Arrays.asList(new Integer[] { fromIndex, size, length }));228String expectedMessage = withinBounds229? null230: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).231apply("checkFromIndexSize", list).getMessage();232233BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> {234try {235int rIndex = s.getAsInt();236if (!withinBounds)237fail(String.format(238"Range [%d, %d + %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, fromIndex, size, length));239assertEquals(rIndex, fromIndex);240}241catch (RuntimeException e) {242assertTrue(ec.isInstance(e));243if (withinBounds)244fail(String.format(245"Range [%d, %d + %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, fromIndex, size, length));246else247assertEquals(e.getMessage(), expectedMessage);248}249};250251check.accept(AssertingOutOfBoundsException.class,252() -> Preconditions.checkFromIndexSize(fromIndex, size, length,253assertingOutOfBounds(expectedMessage, "checkFromIndexSize", fromIndex, size, length)));254check.accept(IndexOutOfBoundsException.class,255() -> Preconditions.checkFromIndexSize(fromIndex, size, length,256assertingOutOfBoundsReturnNull("checkFromIndexSize", fromIndex, size, length)));257check.accept(IndexOutOfBoundsException.class,258() -> Preconditions.checkFromIndexSize(fromIndex, size, length, null));259check.accept(ArrayIndexOutOfBoundsException.class,260() -> Preconditions.checkFromIndexSize(fromIndex, size, length,261Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));262check.accept(StringIndexOutOfBoundsException.class,263() -> Preconditions.checkFromIndexSize(fromIndex, size, length,264Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));265}266267@Test268public void uniqueMessagesForCheckKinds() {269BiFunction<String, List<Integer>, IndexOutOfBoundsException> f =270Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new);271272List<String> messages = new ArrayList<>();273List<Integer> arg1 = Collections.unmodifiableList(Arrays.asList(new Integer[] { -1 }));274List<Integer> arg2 = Collections.unmodifiableList(Arrays.asList(new Integer[] { -1, 0 }));275List<Integer> arg3 = Collections.unmodifiableList(Arrays.asList(new Integer[] { -1, 0, 0 }));276List<Integer> arg4 = Collections.unmodifiableList(Arrays.asList(new Integer[] { -1, 0, 0, 0 }));277// Exact arguments278messages.add(f.apply("checkIndex", arg2).getMessage());279messages.add(f.apply("checkFromToIndex", arg3).getMessage());280messages.add(f.apply("checkFromIndexSize", arg3).getMessage());281// Unknown check kind282messages.add(f.apply("checkUnknown", arg3).getMessage());283// Known check kind with more arguments284messages.add(f.apply("checkIndex", arg3).getMessage());285messages.add(f.apply("checkFromToIndex", arg4).getMessage());286messages.add(f.apply("checkFromIndexSize", arg4).getMessage());287// Known check kind with fewer arguments288messages.add(f.apply("checkIndex", arg1).getMessage());289messages.add(f.apply("checkFromToIndex", arg2).getMessage());290messages.add(f.apply("checkFromIndexSize", arg2).getMessage());291// Null arguments292messages.add(f.apply(null, null).getMessage());293messages.add(f.apply("checkNullArguments", null).getMessage());294messages.add(f.apply(null, arg1).getMessage());295296assertEquals(messages.size(), messages.stream().distinct().count());297}298}299300301