Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringCompareToSameLength.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 various strings of29* specified size, which are different at all possible index values and30* compares them. Expecting separately calculated result to be returned.31* String size is specified via commandline. Various size values can32* be specified during intrinsic development in order to test cases33* specific for new or modified intrinsic implementation. Aarch6434* implementation has 1, 4, 8 -bytes loops for length < 72 and35* 16, 32, 64 -bytes loops for string length >= 72. Code is also36* affected by SoftwarePrefetchHintDistance flag value.37* Test class can also accept "-fullmode" parameter38* with maxLength paramter after it. Then it will iterate through all39* string length values up to maxLength parameter (inclusive). It takes40* a lot of time but is useful for development.41* @run main/othervm -XX:SoftwarePrefetchHintDistance=192 compiler.intrinsics.string.TestStringCompareToSameLength 2 5 10 13 17 20 25 35 36 37 71 72 73 88 90 192 193 208 20942* @run main/othervm -XX:SoftwarePrefetchHintDistance=16 compiler.intrinsics.string.TestStringCompareToSameLength 2 5 10 13 17 20 25 35 36 37 71 72 73 88 9043* @run main/othervm -XX:SoftwarePrefetchHintDistance=-1 compiler.intrinsics.string.TestStringCompareToSameLength 2 5 10 13 17 20 25 35 36 37 71 72 73 88 9044*/4546package compiler.intrinsics.string;4748public class TestStringCompareToSameLength {49private final int size;5051public static void main(String args[]) {52if (args.length == 0) {53throw new IllegalArgumentException("Usage: $testClass $testLength1"54+ " [$testLength2 [...]] | -fullmode $maxLength");55}56if (args.length == 2 && "-fullmode".equals(args[0])) {57int maxLength = Integer.parseInt(args[1]);58for (int length = 1; length <= maxLength; length++) {59TestStringCompareToSameLength test = new TestStringCompareToSameLength(length);60for (int mismatchIdx = 0; mismatchIdx <= length; mismatchIdx++) {61test.testCompareTo(mismatchIdx);62}63}64} else {65for (String arg : args) {66int size = Integer.parseInt(arg);67TestStringCompareToSameLength test = new TestStringCompareToSameLength(size);68for (int mismatchIdx = 0; mismatchIdx <= size; mismatchIdx++) {69test.testCompareTo(mismatchIdx);70}71}72}73}7475private TestStringCompareToSameLength(int size) {76this.size = size;77}7879private void testCompareTo(int mismatchIdx) {80// Create Latin1 strings: latin1, latin2, which are different at index.81// Case of index == size is a case of equal strings82char latinSrc[] = new char[size];83// generate ASCII string84for (int i = 0; i < size; i++) {85latinSrc[i] = (char) ('a' + (i % 26));86}87String latinStr1 = new String(latinSrc);88if (mismatchIdx != size) latinSrc[mismatchIdx] = (char) ('a' - 1);89String latinStr2 = new String(latinSrc);9091// Create 3 utf strings: utfStr1, utfStr2: same as latinStr1, but has UTF-16 character92// utfStr1 and utfStr2 are different at requested index and character value is greater93// than same index character in latinStr1.94// utfStr3 is different at requested index and character value is less than same95// index character in latinStr1. Will be a Latin1-encoded string in case difference96// is requested at last character. This case not applicable and is skipped below.97char cArray[] = latinStr1.toCharArray();98cArray[cArray.length - 1] = '\uBEEF'; // at least last character is UTF-1699if (mismatchIdx != size) cArray[mismatchIdx] = '\u1234';100String utfStr1 = new String(cArray);101if (mismatchIdx != size) cArray[mismatchIdx] = '\u5678';102String utfStr2 = new String(cArray);103if (mismatchIdx != size) cArray[mismatchIdx] = (char) ('a' - 2); // less than Latin1 index position104// utfStr3 will be Latin1 if last character differ. Will skip this case105String utfStr3 = new String(cArray);106107for (int i = 0; i < 10000; i++) {108checkCase(mismatchIdx, latinStr1, latinStr2, "LL"); // compare Latin1 with Latin1109110checkCase(mismatchIdx, utfStr1, utfStr2, "UU"); // compare UTF-16 vs UTF-16111112if (size != mismatchIdx) { // UTF-16 and Latin1 strings can't be equal. Then skip this case113// compare UTF16 string, which is expected to be > than Latin1114checkCase(mismatchIdx, latinStr1, utfStr1, "U(large)L");115if (mismatchIdx != size - 1) {116// compare UTF16 string, which is expected to be < than Latin1117checkCase(mismatchIdx, latinStr1, utfStr3, "U(small)L");118}119}120}121}122123private void checkCase(int mismatchIdx, String str1, String str2, String caseName) {124int expected;125if (mismatchIdx != size) {126expected = str1.charAt(mismatchIdx) - str2.charAt(mismatchIdx);127} else {128expected = str1.length() - str2.length();129}130int result = str1.compareTo(str2);131int reversedResult = str2.compareTo(str1);132if (expected != result || result != -reversedResult) {133throw new AssertionError(String.format("%s CASE FAILED: size = %d, "134+ "mismatchIdx = %d, expected = %d, but got result = %d, "135+ "reversedResult = %d for string1 = '%s', string2 = '%s'",136caseName, size, mismatchIdx, expected, result,137reversedResult, str1, str2));138}139}140}141142143144