Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestHasNegatives.java
41153 views
/*1* Copyright (c) 2016, 2018, 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* @bug 805430726* @summary Validates StringCoding.hasNegatives intrinsic with a small range of tests.27* @library /compiler/patches28*29* @build java.base/java.lang.Helper30* @run main compiler.intrinsics.string.TestHasNegatives31*/3233package compiler.intrinsics.string;3435/*36* @summary Validates StringCoding.hasNegatives intrinsic with a small37* range of tests.38*/39public class TestHasNegatives {4041private static byte[] tBa = new byte[4096 + 16];4243/**44* Completely initialize the test array, preparing it for tests of the45* StringCoding.hasNegatives method with a given array segment offset,46* length, and number of negative bytes.47*/48public static void initialize(int off, int len, int neg) {49assert (len + off <= tBa.length);50// insert "canary" (negative) values before offset51for (int i = 0; i < off; ++i) {52tBa[i] = (byte) (((i + 15) & 0x7F) | 0x80);53}54// fill the array segment55for (int i = off; i < len + off; ++i) {56tBa[i] = (byte) (((i - off + 15) & 0x7F));57}58if (neg != 0) {59// modify a number (neg) disparate array bytes inside60// segment to be negative.61int div = (neg > 1) ? (len - 1) / (neg - 1) : 0;62int idx;63for (int i = 0; i < neg; ++i) {64idx = off + (len - 1) - div * i;65tBa[idx] = (byte) (0x80 | tBa[idx]);66}67}68// insert "canary" negative values after array segment69for (int i = len + off; i < tBa.length; ++i) {70tBa[i] = (byte) (((i + 15) & 0x7F) | 0x80);71}72}7374/** Sizes of array segments to test. */75private static int sizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 17, 19, 23, 37, 61, 131,764099 };7778/**79* Test different array segment sizes, offsets, and number of negative80* bytes.81*/82public static void test_hasNegatives() throws Exception {83int len, off;84int ng;85boolean r;8687for (ng = 0; ng < 57; ++ng) { // number of negatives in array segment88for (off = 0; off < 8; ++off) { // starting offset of array segment89for (int i = 0; i < sizes.length; ++i) { // array segment size90// choice91len = sizes[i];92if (len + off > tBa.length)93continue;94initialize(off, len, ng);95r = Helper.StringCodingHasNegatives(tBa, off, len);96if (r ^ ((ng == 0) ? false : true)) {97throw new Exception("Failed test hasNegatives " + "offset: " + off + " "98+ "length: " + len + " " + "return: " + r + " " + "negatives: "99+ ng);100}101}102}103}104}105106public void run() throws Exception {107// iterate to eventually get intrinsic inlined108for (int j = 0; j < 1000; ++j) {109test_hasNegatives();110}111}112113public static void main(String[] args) throws Exception {114(new TestHasNegatives()).run();115System.out.println("hasNegatives validated");116}117}118119120