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