1 /*
   2  * Copyright (c) 1999, 2020, 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 4179153 4652234 6529796
  26    @summary Read code mapping table and check code conversion
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.charset.Charset;
  31 import java.util.HashSet;
  32 
  33 public class TestConv {
  34     static int errorNum = 0;
  35     private static final int maxBytesPerChar = 10;
  36 
  37     public static void main(String args[])
  38         throws Exception
  39     {
  40         File d = new File(System.getProperty("test.src", "."));
  41         if (args.length == 0) {
  42             String[] files = d.list();
  43             String encoding;
  44             for (int i = 0; i < files.length; i++) {
  45                 if (files[i].endsWith(".b2c")) {
  46                     encoding = files[i].substring(0, files[i].length() - 4 );
  47                     check(d, encoding);
  48                 }
  49             }
  50         } else {
  51             for (int i = 0; i < args.length; i++)
  52                 check(d, args[i]);
  53         }
  54     }
  55 
  56     static class Parser2 extends CoderTest.Parser {
  57         int warnOff;
  58         String regwarnCP;
  59         Parser2 (InputStream is) throws IOException {
  60             super(is);
  61         }
  62         protected boolean isDirective(String line) {
  63             if ((warnOff = line.indexOf("REGWARN")) != -1)
  64                 regwarnCP = line.substring(warnOff+7);
  65             else
  66                 regwarnCP = null;
  67             return false;
  68         }
  69     }
  70 
  71     private static void check(File dir, String encoding) throws Exception
  72     {
  73         byte[] inByte;
  74         byte[] outByte;
  75         char[] inChar;
  76         String inStr;
  77         String outStr;
  78 
  79         System.out.println("\nChecking " + encoding + "...");
  80         errorNum = 0;
  81 
  82         if (!Charset.isSupported(encoding)) {
  83             System.out.println("Not supported: " + encoding);
  84             return;
  85         }
  86 
  87         Parser2 p = null;
  88         try {
  89             p = new Parser2(new FileInputStream(new File(dir, encoding + ".b2c")));
  90         } catch (Exception e) {
  91             throw new Exception("Can't open file " + encoding + ".b2c");
  92         }
  93         CoderTest.Entry e = new CoderTest.Entry();
  94 
  95         while ((e = (CoderTest.Entry)p.next(e)) != null) {
  96             if (e.cp2 != 0)
  97                 continue;  // skip composite (base+cc) for now
  98             inByte = e.bb;
  99             inChar = Character.toChars(e.cp);
 100             inStr = new String(inChar);
 101             outStr = new String(inByte, encoding);
 102             outByte = inStr.getBytes(encoding);
 103             int r = compareInOut(inStr, outStr, inByte, outByte);
 104             if (r == 1) {
 105                 if (p.warnOff == -1)
 106                     errorNum++;
 107                 else {
 108                    System.out.println ("Regression Warning code point " +
 109                                        p.regwarnCP);
 110                 }
 111                 System.out.println("Warning " + errorNum
 112                                    + ": " + byteString(inByte)
 113                                    + " -> \\u" + toHex(outStr)
 114                                    + "  multi-mapping? \\u" + toHex(inStr));
 115             } else if (r == 2) {
 116                 if (p.warnOff == -1)
 117                     errorNum++;
 118             }
 119         }
 120 
 121         if (errorNum == 0) {
 122             System.out.println("OK.");
 123         } else {
 124             throw new RuntimeException(errorNum + " Warning(s).");
 125         }
 126     }
 127 
 128     private static int compareInOut(String inStr, String outStr,
 129                                 byte[] inByte, byte[] outByte)
 130     {
 131         if (inStr.compareTo(outStr) != 0)
 132             return 1;
 133 
 134         if (inByte.length != outByte.length) {
 135             return 2;
 136         }
 137 
 138         for (int i = 0; i < inByte.length; i++) {
 139             if (inByte[i] != outByte[i])
 140                 return 2;
 141         }
 142 
 143         return 0;
 144     }
 145 
 146     private static String toHex(String str)
 147     {
 148         if (str.length() == 0)
 149             return "";
 150         String s = Integer.toHexString(str.charAt(0)).toUpperCase();
 151         if (s.length() == 1 || s.length() == 3)
 152             return "0" + s;
 153         return s;
 154     }
 155 
 156     private static String byteString(byte[] b)
 157     {
 158         String s = "0x";
 159         for (int i = 0; i < b.length; i++)
 160             s += Integer.toHexString(b[i] & 0xff).toUpperCase();
 161         return s;
 162     }
 163 
 164 }