1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8054307
  29  * @summary Validates StringCoding.hasNegatives intrinsic with a small range of tests.
  30  * @run main/bootclasspath java.lang.TestHasNegatives
  31  */
  32 package java.lang;
  33 
  34 import java.lang.StringCoding;
  35 
  36 /*
  37  * @summary Validates StringCoding.hasNegatives intrinsic with a small
  38  *          range of tests.  Must be run with modified bootclasspath
  39  *          to allow existence in java.lang package.
  40  */
  41 public class TestHasNegatives {
  42 
  43     private static byte[] tBa = new byte[4096 + 16];
  44 
  45     /**
  46      * Completely initialize the test array, preparing it for tests of the
  47      * StringCoding.hasNegatives method with a given array segment offset,
  48      * length, and number of negative bytes.
  49      */
  50     public static void initialize(int off, int len, int neg) {
  51         assert (len + off <= tBa.length);
  52         // insert "canary" (negative) values before offset
  53         for (int i = 0; i < off; ++i) {
  54             tBa[i] = (byte) (((i + 15) & 0x7F) | 0x80);
  55         }
  56         // fill the array segment
  57         for (int i = off; i < len + off; ++i) {
  58             tBa[i] = (byte) (((i - off + 15) & 0x7F));
  59         }
  60         if (neg != 0) {
  61             // modify a number (neg) disparate array bytes inside
  62             // segment to be negative.
  63             int div = (neg > 1) ? (len - 1) / (neg - 1) : 0;
  64             int idx;
  65             for (int i = 0; i < neg; ++i) {
  66                 idx = off + (len - 1) - div * i;
  67                 tBa[idx] = (byte) (0x80 | tBa[idx]);
  68             }
  69         }
  70         // insert "canary" negative values after array segment
  71         for (int i = len + off; i < tBa.length; ++i) {
  72             tBa[i] = (byte) (((i + 15) & 0x7F) | 0x80);
  73         }
  74     }
  75 
  76     /** Sizes of array segments to test. */
  77     private static int sizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 17, 19, 23, 37, 61, 131,
  78             4099 };
  79 
  80     /**
  81      * Test different array segment sizes, offsets, and number of negative
  82      * bytes.
  83      */
  84     public static void test_hasNegatives() throws Exception {
  85         int len, off;
  86         int ng;
  87         boolean r;
  88 
  89         for (ng = 0; ng < 57; ++ng) { // number of negatives in array segment
  90             for (off = 0; off < 8; ++off) { // starting offset of array segment
  91                 for (int i = 0; i < sizes.length; ++i) { // array segment size
  92                                                          // choice
  93                     len = sizes[i];
  94                     if (len + off > tBa.length)
  95                         continue;
  96                     initialize(off, len, ng);
  97                     r = StringCoding.hasNegatives(tBa, off, len);
  98                     if (r ^ ((ng == 0) ? false : true)) {
  99                         throw new Exception("Failed test hasNegatives " + "offset: " + off + " "
 100                                 + "length: " + len + " " + "return: " + r + " " + "negatives: "
 101                                 + ng);
 102                     }
 103                 }
 104             }
 105         }
 106     }
 107 
 108     public void run() throws Exception {
 109         // iterate to eventually get intrinsic inlined
 110         for (int j = 0; j < 1000; ++j) {
 111             test_hasNegatives();
 112         }
 113     }
 114 
 115     public static void main(String[] args) throws Exception {
 116         (new TestHasNegatives()).run();
 117         System.out.println("hasNegatives validated");
 118     }
 119 }