Path: blob/master/test/jdk/java/lang/Character/Latin1Digit.java
41149 views
/*1* Copyright (c) 2018, 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* @bug 819674026* @summary Check j.l.Character.digit(int,int) for Latin1 characters27*/2829public class Latin1Digit {3031public static void main(String[] args) throws Exception {32for (int ch = 0; ch < 256; ++ch) {33for (int radix = -256; radix <= 256; ++radix) {34test(ch, radix);35}36test(ch, Integer.MIN_VALUE);37test(ch, Integer.MAX_VALUE);38}39}4041static void test(int ch, int radix) throws Exception {42int d1 = Character.digit(ch, radix);43int d2 = canonicalDigit(ch, radix);44if (d1 != d2) {45throw new Exception("Wrong result for char="46+ ch + " (" + (char)ch + "), radix="47+ radix + "; " + d1 + " != " + d2);48}49}5051// canonical version of Character.digit(int,int) for Latin152static int canonicalDigit(int ch, int radix) {53if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {54return -1;55}56if (ch >= '0' && ch <= '9' && ch < (radix + '0')) {57return ch - '0';58}59if (ch >= 'A' && ch <= 'Z' && ch < (radix + 'A' - 10)) {60return ch - 'A' + 10;61}62if (ch >= 'a' && ch <= 'z' && ch < (radix + 'a' - 10)) {63return ch - 'a' + 10;64}65return -1;66}67}686970