1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2019, BELLSOFT. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * @test 27 * @requires os.arch=="aarch64" 28 * @summary String::compareTo implementation uses different algorithms for 29 * different string length. This test creates string with specified 30 * size and longer string, which is same at beginning. 31 * Expecting length delta to be returned. Test class takes 2 32 * parameters: <string length>, <maximum string length delta> 33 * Input parameters for this test are set according to Aarch64 34 * String::compareTo intrinsic implementation specifics. Aarch64 35 * implementation has 1, 4, 8 -characters loops for length < 72 and 36 * 16, 32, 64 -characters loops for length >= 72. Code is also affected 37 * by SoftwarePrefetchHintDistance vm flag value. 38 * @run main/othervm -XX:SoftwarePrefetchHintDistance=192 compiler.intrinsics.string.TestStringCompareToDifferentLength 4 2 5 10 13 17 20 25 71 72 73 88 90 192 193 208 209 39 * @run main/othervm -XX:SoftwarePrefetchHintDistance=16 compiler.intrinsics.string.TestStringCompareToDifferentLength 4 2 5 10 13 17 20 25 71 72 73 88 90 40 * @run main/othervm -XX:SoftwarePrefetchHintDistance=-1 compiler.intrinsics.string.TestStringCompareToDifferentLength 4 2 5 10 13 17 20 25 71 72 73 88 90 41 */ 42 43 package compiler.intrinsics.string; 44 45 public class TestStringCompareToDifferentLength { 46 private final int size; 47 48 public static void main(String args[]) { 49 if (args.length > 1) { 50 int maxLengthDelta = Integer.parseInt(args[0]); 51 for (int i = 1; i < args.length; i++) { 52 int size = Integer.parseInt(args[i]); 53 TestStringCompareToDifferentLength test 54 = new TestStringCompareToDifferentLength(size); 55 for (int delta = 1; delta <= maxLengthDelta; delta++) { 56 test.testCompareTo(delta); 57 } 58 } 59 } else { 60 System.out.println("Usage: $testClass $maxLengthDelta $testLength [$testLength2 [$testLength3 [...]]]"); 61 } 62 } 63 64 private TestStringCompareToDifferentLength(int size) { 65 this.size = size; 66 } 67 68 private void testCompareTo(int delta) { 69 char strsrc[] = new char[size + delta]; 70 // generate ASCII string 71 for (int i = 0; i < size + delta; i++) { 72 strsrc[i] = (char) ('a' + (i % 26)); 73 } 74 75 String longLatin1 = new String(strsrc); 76 String shortLatin1 = longLatin1.substring(0, size); 77 78 String longUTF16LastChar = longLatin1.substring(0, longLatin1.length() - 1) + '\uBEEF'; 79 String longUTF16FirstChar = '\uBEEF' + longLatin1.substring(1, longLatin1.length()); 80 String shortUTF16FirstChar = longUTF16FirstChar.substring(0, size); 81 82 for (int i = 0; i < 10000; i++) { 83 checkCase(longLatin1, shortLatin1, delta, "LL"); // Latin1-Latin1. 84 checkCase(longUTF16LastChar, shortLatin1, delta, "UL"); // Latin1-UTF-16 case. 85 checkCase(longUTF16FirstChar, shortUTF16FirstChar, delta, "UU"); // UTF-16-UTF-16 case 86 } 87 } 88 89 private void checkCase(String str2, String str1, int expected, String caseName) { 90 int result = str2.compareTo(str1); 91 int reversedResult = str1.compareTo(str2); 92 if (expected != result || result != -reversedResult) { 93 throw new AssertionError(String.format("%s CASE FAILED: size = %d, " 94 + "expected = %d, but got result = %d, " 95 + "reversedResult = %d for string1 = '%s', string2 = '%s'", 96 caseName, size, expected, result, 97 reversedResult, str1, str2)); 98 } 99 } 100 } 101