Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringCompareToDifferentLength.java
41153 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2019, BELLSOFT. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @requires os.arch=="aarch64"27* @summary String::compareTo implementation uses different algorithms for28* different string length. This test creates string with specified29* size and longer string, which is same at beginning.30* Expecting length delta to be returned. Test class takes 231* parameters: <string length>, <maximum string length delta>32* Input parameters for this test are set according to Aarch6433* String::compareTo intrinsic implementation specifics. Aarch6434* implementation has 1, 4, 8 -bytes loops for length < 72 and35* 16, 32, 64 -characters loops for length >= 72. Code is also affected36* by SoftwarePrefetchHintDistance vm flag value.37* @run main/othervm -XX:SoftwarePrefetchHintDistance=192 compiler.intrinsics.string.TestStringCompareToDifferentLength 4 2 5 10 13 17 20 23 24 25 71 72 73 88 90 192 193 208 20938* @run main/othervm -XX:SoftwarePrefetchHintDistance=16 compiler.intrinsics.string.TestStringCompareToDifferentLength 4 2 5 10 13 17 20 23 24 25 71 72 73 88 9039* @run main/othervm -XX:SoftwarePrefetchHintDistance=-1 compiler.intrinsics.string.TestStringCompareToDifferentLength 4 2 5 10 13 17 20 23 24 25 71 72 73 88 9040*/4142package compiler.intrinsics.string;4344public class TestStringCompareToDifferentLength {45private final int size;4647public static void main(String args[]) {48if (args.length > 1) {49int maxLengthDelta = Integer.parseInt(args[0]);50for (int i = 1; i < args.length; i++) {51int size = Integer.parseInt(args[i]);52TestStringCompareToDifferentLength test53= new TestStringCompareToDifferentLength(size);54for (int delta = 1; delta <= maxLengthDelta; delta++) {55test.testCompareTo(delta);56}57}58} else {59System.out.println("Usage: $testClass $maxLengthDelta $testLength [$testLength2 [$testLength3 [...]]]");60}61}6263private TestStringCompareToDifferentLength(int size) {64this.size = size;65}6667private void testCompareTo(int delta) {68char strsrc[] = new char[size + delta];69// generate ASCII string70for (int i = 0; i < size + delta; i++) {71strsrc[i] = (char) ('a' + (i % 26));72}7374String longLatin1 = new String(strsrc);75String shortLatin1 = longLatin1.substring(0, size);7677String longUTF16LastChar = longLatin1.substring(0, longLatin1.length() - 1) + '\uBEEF';78String longUTF16FirstChar = '\uBEEF' + longLatin1.substring(1, longLatin1.length());79String shortUTF16FirstChar = longUTF16FirstChar.substring(0, size);8081for (int i = 0; i < 10000; i++) {82checkCase(longLatin1, shortLatin1, delta, "LL"); // Latin1-Latin1.83checkCase(longUTF16LastChar, shortLatin1, delta, "UL"); // Latin1-UTF-16 case.84checkCase(longUTF16FirstChar, shortUTF16FirstChar, delta, "UU"); // UTF-16-UTF-16 case85}86}8788private void checkCase(String str2, String str1, int expected, String caseName) {89int result = str2.compareTo(str1);90int reversedResult = str1.compareTo(str2);91if (expected != result || result != -reversedResult) {92throw new AssertionError(String.format("%s CASE FAILED: size = %d, "93+ "expected = %d, but got result = %d, "94+ "reversedResult = %d for string1 = '%s', string2 = '%s'",95caseName, size, expected, result,96reversedResult, str1, str2));97}98}99}100101102103