1 /* 2 * Copyright (c) 2008, 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 /* @test 25 @bug 4890726 26 @summary Check the correctness of KOI8_U by comparing to KOI8_R 27 */ 28 29 import java.util.*; 30 import static java.lang.Character.UnicodeBlock; 31 32 public class UkrainianIsNotRussian { decode(byte[] bytes, String encoding)33 private static String decode(byte[] bytes, String encoding) throws Throwable { 34 String s = new String(bytes, encoding); 35 equal(s.length(), 1); 36 check(Arrays.equals(s.getBytes(encoding), bytes)); 37 return s; 38 } 39 realMain(String[] args)40 private static void realMain(String[] args) throws Throwable { 41 final byte[] bytes = new byte[1]; 42 int differences = 0; 43 for (int i = 0; i < 0xff; i++) { 44 bytes[0] = (byte) i; 45 final String r = decode(bytes, "KOI8_R"); 46 final String u = decode(bytes, "KOI8_U"); 47 if (! r.equals(u)) { 48 differences++; 49 final char rc = r.charAt(0); 50 final char uc = u.charAt(0); 51 final UnicodeBlock rcb = UnicodeBlock.of(rc); 52 final UnicodeBlock ucb = UnicodeBlock.of(uc); 53 System.out.printf("%02x => %04x %s, %04x %s%n", 54 i, (int) rc, rcb, (int) uc, ucb); 55 check(rcb == UnicodeBlock.BOX_DRAWING && 56 ucb == UnicodeBlock.CYRILLIC); 57 } 58 } 59 equal(differences, 8); 60 } 61 62 //--------------------- Infrastructure --------------------------- 63 static volatile int passed = 0, failed = 0; pass()64 static void pass() {passed++;} fail()65 static void fail() {failed++; Thread.dumpStack();} fail(String msg)66 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)67 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)68 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)69 static void equal(Object x, Object y) { 70 if (x == null ? y == null : x.equals(y)) pass(); 71 else fail(x + " not equal to " + y);} main(String[] args)72 public static void main(String[] args) throws Throwable { 73 try {realMain(args);} catch (Throwable t) {unexpected(t);} 74 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 75 if (failed > 0) throw new AssertionError("Some tests failed");} 76 } 77