Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsicMemoryFlow.java
41153 views
/*1* Copyright (c) 2016, 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 814421226* @summary Check for correct memory flow with the String compress/inflate intrinsics.27* @modules java.base/jdk.internal.misc28* @library /test/lib29*30* @run main compiler.intrinsics.string.TestStringIntrinsicMemoryFlow31*/3233package compiler.intrinsics.string;3435import jdk.test.lib.Asserts;3637public class TestStringIntrinsicMemoryFlow {3839public static void main(String[] args) {40for (int i = 0; i < 100_000; ++i) {41String s = "MyString";42char[] c = {'M'};43char res = testInflate1(s);44Asserts.assertEquals(res, 'M', "testInflate1 failed");45res = testInflate2(s);46Asserts.assertEquals(res, (char)42, "testInflate2 failed");47res = testCompress1(c);48Asserts.assertEquals(res, 'M', "testCompress1 failed");49byte resB = testCompress2(c);50Asserts.assertEquals(resB, (byte)42, "testCompress2 failed");51}52}5354private static char testInflate1(String s) {55char c[] = new char[1];56// Inflate String from byte[] to char[]57s.getChars(0, 1, c, 0);58// Read char[] memory written by inflate intrinsic59return c[0];60}6162private static char testInflate2(String s) {63char c1[] = new char[1];64char c2[] = new char[1];65c2[0] = 42;66// Inflate String from byte[] to char[]67s.getChars(0, 1, c1, 0);68// Read char[] memory written before inflation69return c2[0];70}7172private static char testCompress1(char[] c) {73// Compress String from char[] to byte[]74String s = new String(c);75// Read the memory written by compress intrinsic76return s.charAt(0);77}7879private static byte testCompress2(char[] c) {80byte b1[] = new byte[1];81b1[0] = 42;82// Compress String from char[] to byte[]83new String(c);84// Read byte[] memory written before compression85return b1[0];86}87}888990