jdk/test/sun/misc/JavaLangAccess/FormatUnsigned.java
changeset 32916 21a06d94b53b
parent 32915 eb290482d42f
parent 32906 ee125f62429b
child 32917 8392405ab038
equal deleted inserted replaced
32915:eb290482d42f 32916:21a06d94b53b
     1 /*
       
     2  * Copyright (c) 2012, 2014, 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 import sun.misc.JavaLangAccess;
       
    25 import sun.misc.SharedSecrets;
       
    26 
       
    27 /*
       
    28  * @test
       
    29  * @bug 8050114
       
    30  * @summary Test JavaLangAccess.formatUnsignedInt/-Long
       
    31  * @modules java.base/sun.misc
       
    32  */
       
    33 public class FormatUnsigned {
       
    34 
       
    35     static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
       
    36 
       
    37     public static void testFormatUnsignedInt() {
       
    38         testFormatUnsignedInt("7fffffff", Integer.MAX_VALUE, 8, 4, 0, 8);
       
    39         testFormatUnsignedInt("80000000", Integer.MIN_VALUE, 8, 4, 0, 8);
       
    40         testFormatUnsignedInt("4711", 04711, 4, 3, 0, 4);
       
    41         testFormatUnsignedInt("4711", 0x4711, 4, 4, 0, 4);
       
    42         testFormatUnsignedInt("1010", 0b1010, 4, 1, 0, 4);
       
    43         testFormatUnsignedInt("00001010", 0b1010, 8, 1, 0, 8);
       
    44         testFormatUnsignedInt("\u0000\u000000001010", 0b1010, 10, 1, 2, 8);
       
    45     }
       
    46 
       
    47     public static void testFormatUnsignedLong() {
       
    48         testFormatUnsignedLong("7fffffffffffffff", Long.MAX_VALUE, 16, 4, 0, 16);
       
    49         testFormatUnsignedLong("8000000000000000", Long.MIN_VALUE, 16, 4, 0, 16);
       
    50         testFormatUnsignedLong("4711", 04711L, 4, 3, 0, 4);
       
    51         testFormatUnsignedLong("4711", 0x4711L, 4, 4, 0, 4);
       
    52         testFormatUnsignedLong("1010", 0b1010L, 4, 1, 0, 4);
       
    53         testFormatUnsignedLong("00001010", 0b1010L, 8, 1, 0, 8);
       
    54         testFormatUnsignedLong("\u0000\u000000001010", 0b1010L, 10, 1, 2, 8);
       
    55     }
       
    56 
       
    57     public static void testFormatUnsignedInt(String expected, int value, int arraySize, int shift, int offset, int length) {
       
    58         char[] chars = new char[arraySize];
       
    59         jla.formatUnsignedInt(value, shift, chars, offset, length);
       
    60         String s = new String(chars);
       
    61         if (!expected.equals(s)) {
       
    62             throw new Error(s + " should be equal to expected " + expected);
       
    63         }
       
    64     }
       
    65 
       
    66     public static void testFormatUnsignedLong(String expected, long value, int arraySize, int shift, int offset, int length) {
       
    67         char[] chars = new char[arraySize];
       
    68         jla.formatUnsignedLong(value, shift, chars, offset, length);
       
    69         String s = new String(chars);
       
    70         if (!expected.equals(s)) {
       
    71             throw new Error(s + " should be equal to expected " + expected);
       
    72         }
       
    73     }
       
    74 
       
    75     public static void main(String[] args) {
       
    76         testFormatUnsignedInt();
       
    77         testFormatUnsignedLong();
       
    78     }
       
    79 }