Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestEncodeIntrinsics.java
64495 views
/*1* Copyright (c) 2013, 2021, 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* @key randomness26* @bug 6896617 827424227* @summary Verify potentially intrinsified encoders behave well before and after compilation28* @library /test/lib29*30* @run main/othervm/timeout=1200 --add-opens=java.base/sun.nio.cs=ALL-UNNAMED -Xbatch -Xmx256m compiler.intrinsics.string.TestEncodeIntrinsics31*/3233package compiler.intrinsics.string;3435import jdk.test.lib.Utils;3637import java.lang.reflect.Method;38import java.nio.ByteBuffer;39import java.nio.CharBuffer;40import java.nio.charset.Charset;41import java.nio.charset.CharsetDecoder;42import java.nio.charset.CharsetEncoder;43import java.nio.charset.CodingErrorAction;44import java.util.Arrays;45import java.util.Random;4647public class TestEncodeIntrinsics {48final static int SIZE = 256;4950public static void main(String[] args) {5152test("ISO-8859-1", false);53test("UTF-8", true);54test("US-ASCII", true);55test("CESU-8", true);56}5758private static void test(String csn, boolean asciiOnly) {59try {60System.out.println("Testing " + csn);61Charset cs = Charset.forName(csn);62CharsetEncoder enc = cs.newEncoder();63enc.onMalformedInput(CodingErrorAction.REPLACE)64.onUnmappableCharacter(CodingErrorAction.REPLACE);65CharsetDecoder dec = cs.newDecoder();66dec.onMalformedInput(CodingErrorAction.REPLACE)67.onUnmappableCharacter(CodingErrorAction.REPLACE);6869byte repl = (byte) '?';70enc.replaceWith(new byte[]{repl});7172// Populate char[] with chars which can be encoded by ISO_8859_1 (<= 0xFF)73// - or ASCII (<= 0x7F) if requested74Random rnd = Utils.getRandomInstance();75int maxchar = asciiOnly ? 0x7F : 0xFF;76char[] a = new char[SIZE];77byte[] b = new byte[SIZE];78char[] at = new char[SIZE];79byte[] bt = new byte[SIZE];80for (int i = 0; i < SIZE; i++) {81char c = (char) rnd.nextInt(maxchar);82if (!enc.canEncode(c)) {83System.out.printf("Something wrong: can't encode c=%03x\n", (int) c);84System.exit(97);85}86a[i] = c;87b[i] = (byte) c;88at[i] = (char) -1;89bt[i] = (byte) -1;90}9192Method encodeArray = null;93if (csn.equals("ISO-8859-1")) {94// Use internal API for tests95encodeArray = enc.getClass().getDeclaredMethod("encodeISOArray",96char[].class, int.class, byte[].class, int.class, int.class);97encodeArray.setAccessible(true);98if ((int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE || !Arrays.equals(b, bt)) {99System.out.println("Something wrong: ArrayEncoder.encode failed");100System.exit(97);101}102for (int i = 0; i < SIZE; i++) {103at[i] = (char) -1;104}105}106107ByteBuffer bb = ByteBuffer.wrap(b);108CharBuffer ba = CharBuffer.wrap(a);109ByteBuffer bbt = ByteBuffer.wrap(bt);110CharBuffer bat = CharBuffer.wrap(at);111if (!enc.encode(ba, bbt, true).isUnderflow() || !Arrays.equals(b, bt)) {112System.out.println("Something wrong: Encoder.encode failed");113System.exit(97);114}115if (!dec.decode(bb, bat, true).isUnderflow() || !Arrays.equals(a, at)) {116System.out.println("Something wrong: Decoder.decode failed (a == at: " + !Arrays.equals(a, at) + ")");117System.exit(97);118}119for (int i = 0; i < SIZE; i++) {120at[i] = (char) -1;121bt[i] = (byte) -1;122}123124// Warm up125boolean failed = false;126127if (csn.equals("ISO-8859-1")) {128for (int i = 0; i < 10000; i++) {129failed |= (int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE;130}131for (int i = 0; i < 10000; i++) {132failed |= (int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE;133}134for (int i = 0; i < 10000; i++) {135failed |= (int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE;136}137if (failed || !Arrays.equals(b, bt)) {138failed = true;139System.out.println("Failed: ISO_8859_1$Encoder.encode char[" + SIZE + "]");140}141}142143for (int i = 0; i < SIZE; i++) {144at[i] = (char) -1;145bt[i] = (byte) -1;146}147148boolean is_underflow = true;149for (int i = 0; i < 10000; i++) {150ba.clear();151bb.clear();152bat.clear();153bbt.clear();154boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();155boolean dec_res = dec.decode(bb, bat, true).isUnderflow();156is_underflow = is_underflow && enc_res && dec_res;157}158for (int i = 0; i < SIZE; i++) {159at[i] = (char) -1;160bt[i] = (byte) -1;161}162for (int i = 0; i < 10000; i++) {163ba.clear();164bb.clear();165bat.clear();166bbt.clear();167boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();168boolean dec_res = dec.decode(bb, bat, true).isUnderflow();169is_underflow = is_underflow && enc_res && dec_res;170}171for (int i = 0; i < SIZE; i++) {172at[i] = (char) -1;173bt[i] = (byte) -1;174}175for (int i = 0; i < 10000; i++) {176ba.clear();177bb.clear();178bat.clear();179bbt.clear();180boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();181boolean dec_res = dec.decode(bb, bat, true).isUnderflow();182is_underflow = is_underflow && enc_res && dec_res;183}184if (!is_underflow) {185failed = true;186System.out.println("Failed: got a non-underflow");187}188if (!Arrays.equals(b, bt)) {189failed = true;190System.out.println("Failed: b != bt");191}192if (!Arrays.equals(a, at)) {193failed = true;194System.out.println("Failed: a != at");195}196197// Test encoder with chars outside of the range the intrinsic deals with198System.out.println("Testing big char");199200bt = new byte[SIZE + 10]; // add some spare room to deal with encoding multi-byte201ba = CharBuffer.wrap(a);202bbt = ByteBuffer.wrap(bt);203for (int i = 1; i <= SIZE; i++) {204for (int j = 0; j < i; j++) {205char bigChar = (char)((asciiOnly ? 0x7F : 0xFF) + 1 + rnd.nextInt(0x100));206char aOrig = a[j];207a[j] = bigChar;208// make sure to replace with a different byte209bt[j] = (byte)(bt[j] + 1);210ba.clear();211ba.limit(i);212bbt.clear();213if (!enc.encode(ba, bbt, true).isUnderflow()) {214failed = true;215System.out.println("Failed: encode char[" + i + "] to byte[" + i + "]: expected underflow");216}217if (bt[j] == b[j] && b[j] != repl) { // b[j] can be equal to repl; ignore218failed = true;219System.out.println("Failed: different byte expected at pos bt[" + j + "]");220}221if (!enc.canEncode(bigChar) && bt[j] != repl) {222failed = true;223System.out.println("Failed: encoded replace byte[" + j + "] (" + bt[j] + ") != " + repl);224}225226// Check that all bytes prior to the replaced one was encoded properly227for (int k = 0; k < j; k++) {228if (bt[k] != b[k]) {229failed = true;230System.out.println("Failed: encoded byte[" + k + "] (" + bt[k] + ") != " + b[k]);231}232}233a[j] = aOrig; // Restore234}235}236237if (failed) {238System.out.println("FAILED");239System.exit(97);240}241System.out.println("PASSED");242} catch (Exception e) {243e.printStackTrace();244System.out.println("FAILED");245System.exit(97);246}247}248}249250251