test/jdk/java/lang/Character/Latin1Digit.java
author mikael
Mon, 29 Jul 2019 09:59:04 -0700
changeset 57584 9d82a35b6ff7
parent 48740 7d5826282e8d
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48740
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     1
/*
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     4
 *
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     8
 *
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    13
 * accompanied this code).
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    14
 *
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    18
 *
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    21
 * questions.
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    22
 */
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    23
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    24
/**
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    25
 * @test
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    26
 * @bug 8196740
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    27
 * @summary Check j.l.Character.digit(int,int) for Latin1 characters
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    28
 */
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    29
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    30
public class Latin1Digit {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    31
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    32
    public static void main(String[] args) throws Exception {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    33
        for (int ch = 0; ch < 256; ++ch) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    34
            for (int radix = -256; radix <= 256; ++radix) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    35
                test(ch, radix);
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    36
            }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    37
            test(ch, Integer.MIN_VALUE);
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    38
            test(ch, Integer.MAX_VALUE);
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    39
        }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    40
    }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    41
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    42
    static void test(int ch, int radix) throws Exception {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    43
        int d1 = Character.digit(ch, radix);
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    44
        int d2 = canonicalDigit(ch, radix);
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    45
        if (d1 != d2) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    46
            throw new Exception("Wrong result for char="
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    47
                    + ch + " (" + (char)ch + "), radix="
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    48
                    + radix + "; " + d1 + " != " + d2);
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    49
        }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    50
    }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    51
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    52
    // canonical version of Character.digit(int,int) for Latin1
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    53
    static int canonicalDigit(int ch, int radix) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    54
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    55
            return -1;
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    56
        }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    57
        if (ch >= '0' && ch <= '9' && ch < (radix + '0')) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    58
            return ch - '0';
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    59
        }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    60
        if (ch >= 'A' && ch <= 'Z' && ch < (radix + 'A' - 10)) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    61
            return ch - 'A' + 10;
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    62
        }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    63
        if (ch >= 'a' && ch <= 'z' && ch < (radix + 'a' - 10)) {
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    64
            return ch - 'a' + 10;
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    65
        }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    66
        return -1;
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    67
    }
7d5826282e8d 8196740: Character.digit(int,int) returns wrong value for out of range radix
igerasim
parents:
diff changeset
    68
}