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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import jdk.test.lib.Asserts;
  25 
  26 /*
  27  * @test
  28  * @bug 8144212
  29  * @summary Check for correct memory flow with the String compress/inflate intrinsics.
  30  * @modules java.base/jdk.internal.misc
  31  * @library /testlibrary
  32  * @run main TestStringIntrinsicMemoryFlow
  33  */
  34 public class TestStringIntrinsicMemoryFlow {
  35 
  36     public static void main(String[] args) {
  37         for (int i = 0; i < 100_000; ++i) {
  38             String s = "MyString";
  39             char[] c = {'M'};
  40             char res = testInflate1(s);
  41             Asserts.assertEquals(res, 'M', "testInflate1 failed");
  42             res = testInflate2(s);
  43             Asserts.assertEquals(res, (char)42, "testInflate2 failed");
  44             res = testCompress1(c);
  45             Asserts.assertEquals(res, 'M', "testCompress1 failed");
  46             byte resB = testCompress2(c);
  47             Asserts.assertEquals(resB, (byte)42, "testCompress2 failed");
  48         }
  49     }
  50 
  51     private static char testInflate1(String s) {
  52         char c[] = new char[1];
  53         // Inflate String from byte[] to char[]
  54         s.getChars(0, 1, c, 0);
  55         // Read char[] memory written by inflate intrinsic
  56         return c[0];
  57     }
  58 
  59     private static char testInflate2(String s) {
  60         char c1[] = new char[1];
  61         char c2[] = new char[1];
  62         c2[0] = 42;
  63         // Inflate String from byte[] to char[]
  64         s.getChars(0, 1, c1, 0);
  65         // Read char[] memory written before inflation
  66         return c2[0];
  67     }
  68 
  69     private static char testCompress1(char[] c) {
  70         // Compress String from char[] to byte[]
  71         String s = new String(c);
  72         // Read the memory written by compress intrinsic
  73         return s.charAt(0);
  74     }
  75 
  76     private static byte testCompress2(char[] c) {
  77         byte b1[] = new byte[1];
  78         b1[0] = 42;
  79         // Compress String from char[] to byte[]
  80         new String(c);
  81         // Read byte[] memory written before compression
  82         return b1[0];
  83     }
  84 }