jdk/test/java/lang/Integer/BitTwiddle.java
author bpb
Wed, 29 Apr 2015 16:34:49 -0700
changeset 30048 3424bede284d
parent 30046 cf2c86e1819e
child 30436 17827057ef5a
permissions -rw-r--r--
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests Summary: Add ability to initial the random number generator from the system property "seed" and print to STDOUT the seed value actually used. Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     1
/*
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     4
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     8
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    13
 * accompanied this code).
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    14
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    21
 * questions.
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    22
 */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    23
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    24
/*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    25
 * @test
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    26
 * @library /lib/testlibrary/
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    27
 * @build jdk.testlibrary.*
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    28
 * @run main BitTwiddle
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    29
 * @bug     4495754 8078672
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    30
 * @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed)
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    31
 * @author  Josh Bloch
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    32
 * @key randomness
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    33
 */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    34
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    35
import java.util.Random;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    36
import static java.lang.Integer.*;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    37
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    38
public class BitTwiddle {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    39
    private static final int N = 1000; // # of repetitions per test
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    40
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    41
    public static void main(String args[]) {
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    42
        Random rnd = RandomFactory.getRandom();
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    43
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    44
        if (highestOneBit(0) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    45
            throw new RuntimeException("a");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    46
        if (highestOneBit(-1) != MIN_VALUE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    47
            throw new RuntimeException("b");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    48
        if (highestOneBit(1) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    49
            throw new RuntimeException("c");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    50
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    51
        if (lowestOneBit(0) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    52
            throw new RuntimeException("d");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    53
        if (lowestOneBit(-1) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    54
            throw new RuntimeException("e");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    55
        if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    56
            throw new RuntimeException("f");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    57
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    58
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    59
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    60
            if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    61
                throw new RuntimeException("g: " + toHexString(x));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    62
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    63
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    64
        if (numberOfLeadingZeros(0) != SIZE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    65
            throw new RuntimeException("h");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    66
        if (numberOfLeadingZeros(-1) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    67
            throw new RuntimeException("i");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    68
        if (numberOfLeadingZeros(1) != (SIZE - 1))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    69
            throw new RuntimeException("j");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    70
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    71
        if (numberOfTrailingZeros(0) != SIZE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    72
            throw new RuntimeException("k");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    73
        if (numberOfTrailingZeros(1) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    74
            throw new RuntimeException("l");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    75
        if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    76
            throw new RuntimeException("m");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    77
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    78
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    79
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    80
            if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    81
                throw new RuntimeException("n: " + toHexString(x));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    82
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    83
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    84
        if (bitCount(0) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    85
                throw new RuntimeException("o");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    86
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    87
        for (int i = 0; i < SIZE; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    88
            int pow2 = 1 << i;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    89
            if (bitCount(pow2) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    90
                throw new RuntimeException("p: " + i);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    91
            if (bitCount(pow2 -1) != i)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    92
                throw new RuntimeException("q: " + i);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    93
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    94
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    95
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    96
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    97
            if (bitCount(x) != bitCount(reverse(x)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    98
                throw new RuntimeException("r: " + toHexString(x));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    99
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   100
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   101
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   102
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   103
            int dist = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   104
            if (bitCount(x) != bitCount(rotateRight(x, dist)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   105
                throw new RuntimeException("s: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   106
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   107
            if (bitCount(x) != bitCount(rotateLeft(x, dist)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   108
                throw new RuntimeException("t: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   109
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   110
            if (rotateRight(x, dist) != rotateLeft(x, -dist))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   111
                throw new RuntimeException("u: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   112
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   113
            if (rotateRight(x, -dist) != rotateLeft(x, dist))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   114
                throw new RuntimeException("v: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   115
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   116
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   117
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   118
        if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   119
            || signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   120
            throw new RuntimeException("w");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   121
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   122
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   123
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   124
            int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   125
            if (signum(x) != sign)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   126
                throw new RuntimeException("x: " + toHexString(x));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   127
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   128
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   129
        if(reverseBytes(0xaabbccdd) != 0xddccbbaa)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   130
            throw new RuntimeException("y");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   131
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   132
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   133
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   134
            if (bitCount(x) != bitCount(reverseBytes(x)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   135
                throw new RuntimeException("z: " + toHexString(x));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   136
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   137
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   138
}