1 /*
   2  * Copyright (c) 2018, 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 /**
  25  * @test
  26  * @bug 8196740
  27  * @summary Check j.l.Character.digit(int,int) for Latin1 characters
  28  */
  29 
  30 public class Latin1Digit {
  31 
  32     public static void main(String[] args) throws Exception {
  33         for (int ch = 0; ch < 256; ++ch) {
  34             for (int radix = -256; radix <= 256; ++radix) {
  35                 test(ch, radix);
  36             }
  37             test(ch, Integer.MIN_VALUE);
  38             test(ch, Integer.MAX_VALUE);
  39         }
  40     }
  41 
  42     static void test(int ch, int radix) throws Exception {
  43         int d1 = Character.digit(ch, radix);
  44         int d2 = canonicalDigit(ch, radix);
  45         if (d1 != d2) {
  46             throw new Exception("Wrong result for char="
  47                     + ch + " (" + (char)ch + "), radix="
  48                     + radix + "; " + d1 + " != " + d2);
  49         }
  50     }
  51 
  52     // canonical version of Character.digit(int,int) for Latin1
  53     static int canonicalDigit(int ch, int radix) {
  54         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
  55             return -1;
  56         }
  57         if (ch >= '0' && ch <= '9' && ch < (radix + '0')) {
  58             return ch - '0';
  59         }
  60         if (ch >= 'A' && ch <= 'Z' && ch < (radix + 'A' - 10)) {
  61             return ch - 'A' + 10;
  62         }
  63         if (ch >= 'a' && ch <= 'z' && ch < (radix + 'a' - 10)) {
  64             return ch - 'a' + 10;
  65         }
  66         return -1;
  67     }
  68 }