/* * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 8186801 8186751 * @summary Test the charset mappings * @modules jdk.charsets
*/
// Stateful dbcs encoding has leading shift byte '0x0e' // and trailing shift byte '0x0f'. // The flag variable shiftHackDBCS is 'true' for stateful // EBCDIC encodings, which indicates the need of adding/ // removing the shift bytes. privateboolean shiftHackDBCS = false;
// Find the test for the given bytes/char value, // expanding the test array if needed // private Test testFor(int bpc) { if (bpc > maxBytesPerChar) {
Test[] ts = new Test[bpc];
System.arraycopy(tests, 0, ts, 0, maxBytesPerChar); for (int i = maxBytesPerChar; i < bpc; i++)
ts[i] = new Test(i + 1);
tests = ts;
maxBytesPerChar = bpc;
} return tests[bpc - 1];
}
privateboolean testStringConv() throws Exception { if (shiftHackDBCS) {
log.println(" string de/encoding skipped for ebcdic"); returntrue;
} boolean rv = true;
log.println(" string de/encoding"); // for new String()
ByteArrayOutputStream baosDec = new ByteArrayOutputStream();
StringBuilder sbDec = new StringBuilder(); // for String.getBytes()
ByteArrayOutputStream baosEnc = new ByteArrayOutputStream();
StringBuilder sbEnc = new StringBuilder();
for (Entry e : csinfo.mappings) {
baosDec.write(e.bs);
sbDec.append(Character.toChars(e.cp)); if (e.cp2 != 0)
sbDec.append(e.cp2);
// non-roundtrip b2c, and c2b if (csinfo.nr != null && csinfo.nr.containsKey(e.bb) ||
csinfo.c2b != null && !csinfo.c2b.containsKey(e.cp)) continue;
baosEnc.write(e.bs);
sbEnc.append(Character.toChars(e.cp)); if (e.cp2 != 0)
sbEnc.append(e.cp2);
}
log.println(" new String()"); if (!new String(baosDec.toByteArray(), csinfo.csName).equals(sbDec.toString())) {
log.println(" Error: new String() failed");
rv = false;
}
log.println(" String.getBytes()"); if (!Arrays.equals(baosEnc.toByteArray(), sbEnc.toString().getBytes(csinfo.csName))) {
log.println(" Error: String().getBytes() failed");
rv = false;
} return rv;
}
// (1) new String()/String.getBytes()
rv &= testStringConv();
// (2) DECODE:
clearTests(); if (shiftHackDBCS) {
testFor(2).put(newbyte[] { 0x0e });
}
csinfo.mappings.forEach(e -> { if (e.cp2 != 0) return; // skip composite (base+cc) for now byte[] bs = e.bs; char[] cc = Character.toChars(e.cp);
testFor(bs.length).put(bs, cc);
}); if (shiftHackDBCS) {
testFor(2).put(newbyte[] { 0x0f });
} for (int i = 0; i < maxBytesPerChar; i++) {
rv &= tests[i].run(DECODE);
}
// (3) ENCODE:
clearTests(); if (shiftHackDBCS) {
testFor(2).put(newbyte[] { 0x0e });
}
csinfo.mappings.forEach(e -> { if (e.cp2 != 0) return; // skip composite (base+cc) for now if (csinfo.nr != null && csinfo.nr.containsKey(e.bb)) return; // non-roundtrip b2c if (csinfo.c2b != null && csinfo.c2b.containsKey(e.cp)) return; // c2b only mapping byte[] bs = e.bs; char[] cc = Character.toChars(e.cp);
testFor(bs.length).put(bs, cc);
}); if (csinfo.c2b != null)
csinfo.c2b.values().forEach(e -> { byte[] bs = e.bs; char[] cc = Character.toChars(e.cp);
testFor(bs.length).put(bs, cc);
}); if (shiftHackDBCS) {
testFor(2).put(newbyte[] { 0x0f });
} for (int i = 0; i < maxBytesPerChar; i++) {
rv &= tests[i].run(ENCODE);
} return rv;
}
privatestaticclass Entry { byte[] bs; // byte sequence reps int cp; // Unicode codepoint int cp2; // CC of composite long bb; // bs in "long" form for nr lookup;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.