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  * @library /testlibrary
  31  * @run main TestStringIntrinsicMemoryFlow
  32  */
  33 public class TestStringIntrinsicMemoryFlow {
  34 
  35     public static void main(String[] args) {
  36         for (int i = 0; i < 100_000; ++i) {
  37             String s = "MyString";
  38             char[] c = {'M'};
  39             char res = testInflate1(s);
  40             Asserts.assertEquals(res, 'M', "testInflate1 failed");
  41             res = testInflate2(s);
  42             Asserts.assertEquals(res, (char)42, "testInflate2 failed");
  43             res = testCompress1(c);
  44             Asserts.assertEquals(res, 'M', "testCompress1 failed");
  45             byte resB = testCompress2(c);
  46             Asserts.assertEquals(resB, (byte)42, "testCompress2 failed");
  47         }
  48     }
  49 
  50     private static char testInflate1(String s) {
  51         char c[] = new char[1];
  52         // Inflate String from byte[] to char[]
  53         s.getChars(0, 1, c, 0);
  54         // Read char[] memory written by inflate intrinsic
  55         return c[0];
  56     }
  57 
  58     private static char testInflate2(String s) {
  59         char c1[] = new char[1];
  60         char c2[] = new char[1];
  61         c2[0] = 42;
  62         // Inflate String from byte[] to char[]
  63         s.getChars(0, 1, c1, 0);
  64         // Read char[] memory written before inflation
  65         return c2[0];
  66     }
  67 
  68     private static char testCompress1(char[] c) {
  69         // Compress String from char[] to byte[]
  70         String s = new String(c);
  71         // Read the memory written by compress intrinsic
  72         return s.charAt(0);
  73     }
  74 
  75     private static byte testCompress2(char[] c) {
  76         byte b1[] = new byte[1];
  77         b1[0] = 42;
  78         // Compress String from char[] to byte[]
  79         new String(c);
  80         // Read byte[] memory written before compression
  81         return b1[0];
  82     }
  83 }