jdk/test/sun/misc/JavaLangAccess/FormatUnsigned.java
author redestad
Fri, 18 Jul 2014 22:32:24 +0200
changeset 25660 01fa3ccedf50
child 30820 0d4717a011d3
permissions -rw-r--r--
8050114: Expose Integer/Long formatUnsigned methods internally Reviewed-by: mduigou
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25660
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     1
/*
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     4
 *
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     8
 *
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    13
 * accompanied this code).
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    14
 *
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    18
 *
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    21
 * questions.
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    22
 */
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    23
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    24
import sun.misc.JavaLangAccess;
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    25
import sun.misc.SharedSecrets;
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    26
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    27
/*
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    28
 * @test
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    29
 * @summary Test JavaLangAccess.formatUnsignedInt/-Long
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    30
 * @bug 8050114
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    31
 */
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    32
public class FormatUnsigned {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    33
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    34
    static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    35
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    36
    public static void testFormatUnsignedInt() {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    37
        testFormatUnsignedInt("7fffffff", Integer.MAX_VALUE, 8, 4, 0, 8);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    38
        testFormatUnsignedInt("80000000", Integer.MIN_VALUE, 8, 4, 0, 8);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    39
        testFormatUnsignedInt("4711", 04711, 4, 3, 0, 4);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    40
        testFormatUnsignedInt("4711", 0x4711, 4, 4, 0, 4);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    41
        testFormatUnsignedInt("1010", 0b1010, 4, 1, 0, 4);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    42
        testFormatUnsignedInt("00001010", 0b1010, 8, 1, 0, 8);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    43
        testFormatUnsignedInt("\u0000\u000000001010", 0b1010, 10, 1, 2, 8);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    44
    }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    45
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    46
    public static void testFormatUnsignedLong() {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    47
        testFormatUnsignedLong("7fffffffffffffff", Long.MAX_VALUE, 16, 4, 0, 16);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    48
        testFormatUnsignedLong("8000000000000000", Long.MIN_VALUE, 16, 4, 0, 16);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    49
        testFormatUnsignedLong("4711", 04711L, 4, 3, 0, 4);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    50
        testFormatUnsignedLong("4711", 0x4711L, 4, 4, 0, 4);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    51
        testFormatUnsignedLong("1010", 0b1010L, 4, 1, 0, 4);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    52
        testFormatUnsignedLong("00001010", 0b1010L, 8, 1, 0, 8);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    53
        testFormatUnsignedLong("\u0000\u000000001010", 0b1010L, 10, 1, 2, 8);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    54
    }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    55
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    56
    public static void testFormatUnsignedInt(String expected, int value, int arraySize, int shift, int offset, int length) {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    57
        char[] chars = new char[arraySize];
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    58
        jla.formatUnsignedInt(value, shift, chars, offset, length);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    59
        String s = new String(chars);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    60
        if (!expected.equals(s)) {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    61
            throw new Error(s + " should be equal to expected " + expected);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    62
        }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    63
    }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    64
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    65
    public static void testFormatUnsignedLong(String expected, long value, int arraySize, int shift, int offset, int length) {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    66
        char[] chars = new char[arraySize];
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    67
        jla.formatUnsignedLong(value, shift, chars, offset, length);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    68
        String s = new String(chars);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    69
        if (!expected.equals(s)) {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    70
            throw new Error(s + " should be equal to expected " + expected);
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    71
        }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    72
    }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    73
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    74
    public static void main(String[] args) {
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    75
        testFormatUnsignedInt();
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    76
        testFormatUnsignedLong();
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    77
    }
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
diff changeset
    78
}