Path: blob/master/test/jdk/java/lang/String/StringRepeat.java
41152 views
/*1* Copyright (c) 2018, 2021, 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 This exercises String#repeat patterns and limits.26* @run main/othervm StringRepeat27*/2829/*30* @test31* @summary This exercises String#repeat patterns with 16 * 1024 * 1024 repeats.32* @requires os.maxMemory >= 2G33* @run main/othervm -Xmx2g StringRepeat 1677721634*/3536import java.nio.CharBuffer;3738public class StringRepeat {39public static void main(String... args) {40if (args.length > 0) {41REPEATS = new int[args.length];42for (int i = 0; i < args.length; ++i) {43REPEATS[i] = Integer.parseInt(args[i]);44}45}46test1();47test2();48}4950/*51* Default varitions of repeat count.52*/53static int[] REPEATS = {540, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,5532, 64, 128, 256, 512, 1024, 64 * 1024, 1024 * 102456};5758/*59* Varitions of Strings.60*/61static String[] STRINGS = new String[] {62"", "\0", " ", "a", "$", "\u2022",63"ab", "abc", "abcd", "abcde",64"The quick brown fox jumps over the lazy dog."65};6667/*68* Repeat String function tests.69*/70static void test1() {71for (int repeat : REPEATS) {72for (String string : STRINGS) {73long limit = (long)string.length() * (long)repeat;7475if ((long)(Integer.MAX_VALUE >> 1) <= limit) {76break;77}7879verify(string.repeat(repeat), string, repeat);80}81}82}8384/*85* Repeat String exception tests.86*/87static void test2() {88try {89"abc".repeat(-1);90throw new RuntimeException("No exception for negative repeat count");91} catch (IllegalArgumentException ex) {92// Correct93}9495try {96"abc".repeat(Integer.MAX_VALUE - 1);97throw new RuntimeException("No exception for large repeat count");98} catch (OutOfMemoryError ex) {99// Correct100}101}102103static String truncate(String string) {104if (string.length() < 80) {105return string;106}107return string.substring(0, 80) + "...";108}109110/*111* Verify string repeat patterns.112*/113static void verify(String result, String string, int repeat) {114if (string.isEmpty() || repeat == 0) {115if (!result.isEmpty()) {116System.err.format("\"%s\".repeat(%d)%n", truncate(string), repeat);117System.err.format("Result \"%s\"%n", truncate(result));118System.err.format("Result expected to be empty, found string of length %d%n", result.length());119throw new RuntimeException();120}121} else {122int expected = 0;123int count = 0;124for (int offset = result.indexOf(string, expected);1250 <= offset;126offset = result.indexOf(string, expected)) {127count++;128if (offset != expected) {129System.err.format("\"%s\".repeat(%d)%n", truncate(string), repeat);130System.err.format("Result \"%s\"%n", truncate(result));131System.err.format("Repeat expected at %d, found at = %d%n", expected, offset);132throw new RuntimeException();133}134expected += string.length();135}136if (count != repeat) {137System.err.format("\"%s\".repeat(%d)%n", truncate(string), repeat);138System.err.format("Result \"%s\"%n", truncate(result));139System.err.format("Repeat count expected to be %d, found %d%n", repeat, count);140throw new RuntimeException();141}142}143}144}145146147