Path: blob/master/test/jdk/java/util/Objects/CheckLongIndex.java
41149 views
/*1* Copyright (c) 2020, 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 Objects.checkIndex/jdk.internal.util.Preconditions.checkIndex tests for long values26* @run testng CheckLongIndex27* @modules java.base/jdk.internal.util28*/2930import jdk.internal.util.Preconditions;31import org.testng.annotations.DataProvider;32import org.testng.annotations.Test;3334import java.util.ArrayList;35import java.util.List;36import java.util.Objects;37import java.util.function.BiConsumer;38import java.util.function.BiFunction;39import java.util.function.LongSupplier;4041import static org.testng.Assert.*;4243public class CheckLongIndex {4445static class AssertingOutOfBoundsException extends RuntimeException {46public AssertingOutOfBoundsException(String message) {47super(message);48}49}5051static BiFunction<String, List<Number>, AssertingOutOfBoundsException> assertingOutOfBounds(52String message, String expCheckKind, Long... expArgs) {53return (checkKind, args) -> {54assertEquals(checkKind, expCheckKind);55assertEquals(args, List.of(expArgs));56try {57args.clear();58fail("Out of bounds List<Long> argument should be unmodifiable");59} catch (Exception e) {60}61return new AssertingOutOfBoundsException(message);62};63}6465static BiFunction<String, List<Number>, AssertingOutOfBoundsException> assertingOutOfBoundsReturnNull(66String expCheckKind, Long... expArgs) {67return (checkKind, args) -> {68assertEquals(checkKind, expCheckKind);69assertEquals(args, List.of(expArgs));70return null;71};72}7374static final long[] VALUES = {0, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE, -1, Long.MIN_VALUE + 1, Long.MIN_VALUE};7576@DataProvider77static Object[][] checkIndexProvider() {78List<Object[]> l = new ArrayList<>();79for (long index : VALUES) {80for (long length : VALUES) {81boolean withinBounds = index >= 0 &&82length >= 0 &&83index < length;84l.add(new Object[]{index, length, withinBounds});85}86}87return l.toArray(Object[][]::new);88}8990@Test(dataProvider = "checkIndexProvider")91public void testCheckIndex(long index, long length, boolean withinBounds) {92String expectedMessage = withinBounds93? null94: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).95apply("checkIndex", List.of(index, length)).getMessage();9697BiConsumer<Class<? extends RuntimeException>, LongSupplier> checker = (ec, s) -> {98try {99long rIndex = s.getAsLong();100if (!withinBounds)101fail(String.format(102"Index %d is out of bounds of [0, %d), but was reported to be within bounds", index, length));103assertEquals(rIndex, index);104}105catch (RuntimeException e) {106assertTrue(ec.isInstance(e));107if (withinBounds)108fail(String.format(109"Index %d is within bounds of [0, %d), but was reported to be out of bounds", index, length));110else111assertEquals(e.getMessage(), expectedMessage);112}113};114115checker.accept(AssertingOutOfBoundsException.class,116() -> Preconditions.checkIndex(index, length,117assertingOutOfBounds(expectedMessage, "checkIndex", index, length)));118checker.accept(IndexOutOfBoundsException.class,119() -> Preconditions.checkIndex(index, length,120assertingOutOfBoundsReturnNull("checkIndex", index, length)));121checker.accept(IndexOutOfBoundsException.class,122() -> Preconditions.checkIndex(index, length, null));123checker.accept(IndexOutOfBoundsException.class,124() -> Objects.checkIndex(index, length));125checker.accept(ArrayIndexOutOfBoundsException.class,126() -> Preconditions.checkIndex(index, length,127Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));128checker.accept(StringIndexOutOfBoundsException.class,129() -> Preconditions.checkIndex(index, length,130Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));131}132133134@DataProvider135static Object[][] checkFromToIndexProvider() {136List<Object[]> l = new ArrayList<>();137for (long fromIndex : VALUES) {138for (long toIndex : VALUES) {139for (long length : VALUES) {140boolean withinBounds = fromIndex >= 0 &&141toIndex >= 0 &&142length >= 0 &&143fromIndex <= toIndex &&144toIndex <= length;145l.add(new Object[]{fromIndex, toIndex, length, withinBounds});146}147}148}149return l.toArray(Object[][]::new);150}151152@Test(dataProvider = "checkFromToIndexProvider")153public void testCheckFromToIndex(long fromIndex, long toIndex, long length, boolean withinBounds) {154String expectedMessage = withinBounds155? null156: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).157apply("checkFromToIndex", List.of(fromIndex, toIndex, length)).getMessage();158159BiConsumer<Class<? extends RuntimeException>, LongSupplier> check = (ec, s) -> {160try {161long rIndex = s.getAsLong();162if (!withinBounds)163fail(String.format(164"Range [%d, %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, toIndex, length));165assertEquals(rIndex, fromIndex);166}167catch (RuntimeException e) {168assertTrue(ec.isInstance(e));169if (withinBounds)170fail(String.format(171"Range [%d, %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, toIndex, length));172else173assertEquals(e.getMessage(), expectedMessage);174}175};176177check.accept(AssertingOutOfBoundsException.class,178() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,179assertingOutOfBounds(expectedMessage, "checkFromToIndex", fromIndex, toIndex, length)));180check.accept(IndexOutOfBoundsException.class,181() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,182assertingOutOfBoundsReturnNull("checkFromToIndex", fromIndex, toIndex, length)));183check.accept(IndexOutOfBoundsException.class,184() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, null));185check.accept(IndexOutOfBoundsException.class,186() -> Objects.checkFromToIndex(fromIndex, toIndex, length));187check.accept(ArrayIndexOutOfBoundsException.class,188() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,189Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));190check.accept(StringIndexOutOfBoundsException.class,191() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,192Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));193}194195196@DataProvider197static Object[][] checkFromIndexSizeProvider() {198List<Object[]> l = new ArrayList<>();199for (long fromIndex : VALUES) {200for (long size : VALUES) {201for (long length : VALUES) {202long toIndex = fromIndex + size;203204boolean withinBounds = fromIndex >= 0L &&205size >= 0L &&206length >= 0L &&207fromIndex <= toIndex && // overflow208toIndex <= length;209l.add(new Object[]{fromIndex, size, length, withinBounds});210}211}212}213return l.toArray(Object[][]::new);214}215216@Test(dataProvider = "checkFromIndexSizeProvider")217public void testCheckFromIndexSize(long fromIndex, long size, long length, boolean withinBounds) {218String expectedMessage = withinBounds219? null220: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).221apply("checkFromIndexSize", List.of(fromIndex, size, length)).getMessage();222223BiConsumer<Class<? extends RuntimeException>, LongSupplier> check = (ec, s) -> {224try {225long rIndex = s.getAsLong();226if (!withinBounds)227fail(String.format(228"Range [%d, %d + %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, fromIndex, size, length));229assertEquals(rIndex, fromIndex);230}231catch (RuntimeException e) {232assertTrue(ec.isInstance(e));233if (withinBounds)234fail(String.format(235"Range [%d, %d + %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, fromIndex, size, length));236else237assertEquals(e.getMessage(), expectedMessage);238}239};240241check.accept(AssertingOutOfBoundsException.class,242() -> Preconditions.checkFromIndexSize(fromIndex, size, length,243assertingOutOfBounds(expectedMessage, "checkFromIndexSize", fromIndex, size, length)));244check.accept(IndexOutOfBoundsException.class,245() -> Preconditions.checkFromIndexSize(fromIndex, size, length,246assertingOutOfBoundsReturnNull("checkFromIndexSize", fromIndex, size, length)));247check.accept(IndexOutOfBoundsException.class,248() -> Preconditions.checkFromIndexSize(fromIndex, size, length, null));249check.accept(IndexOutOfBoundsException.class,250() -> Objects.checkFromIndexSize(fromIndex, size, length));251check.accept(ArrayIndexOutOfBoundsException.class,252() -> Preconditions.checkFromIndexSize(fromIndex, size, length,253Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));254check.accept(StringIndexOutOfBoundsException.class,255() -> Preconditions.checkFromIndexSize(fromIndex, size, length,256Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));257}258259@Test260public void uniqueMessagesForCheckKinds() {261BiFunction<String, List<Number>, IndexOutOfBoundsException> f =262Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new);263264List<String> messages = new ArrayList<>();265// Exact arguments266messages.add(f.apply("checkIndex", List.of(-1L, 0L)).getMessage());267messages.add(f.apply("checkFromToIndex", List.of(-1L, 0L, 0L)).getMessage());268messages.add(f.apply("checkFromIndexSize", List.of(-1L, 0L, 0L)).getMessage());269// Unknown check kind270messages.add(f.apply("checkUnknown", List.of(-1L, 0L, 0L)).getMessage());271// Known check kind with more arguments272messages.add(f.apply("checkIndex", List.of(-1L, 0L, 0L)).getMessage());273messages.add(f.apply("checkFromToIndex", List.of(-1L, 0L, 0L, 0L)).getMessage());274messages.add(f.apply("checkFromIndexSize", List.of(-1L, 0L, 0L, 0L)).getMessage());275// Known check kind with fewer arguments276messages.add(f.apply("checkIndex", List.of(-1L)).getMessage());277messages.add(f.apply("checkFromToIndex", List.of(-1L, 0L)).getMessage());278messages.add(f.apply("checkFromIndexSize", List.of(-1L, 0L)).getMessage());279// Null arguments280messages.add(f.apply(null, null).getMessage());281messages.add(f.apply("checkNullArguments", null).getMessage());282messages.add(f.apply(null, List.of(-1L)).getMessage());283284assertEquals(messages.size(), messages.stream().distinct().count());285}286}287288289