jdk/test/java/lang/Integer/BitTwiddle.java
author chegar
Fri, 08 May 2015 11:02:33 +0100
changeset 30436 17827057ef5a
parent 30048 3424bede284d
child 37877 dae28a12fb38
permissions -rw-r--r--
8079782: RandomFactory should be in the jdk.testlibrary package Reviewed-by: bpb
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;
30436
17827057ef5a 8079782: RandomFactory should be in the jdk.testlibrary package
chegar
parents: 30048
diff changeset
    36
import jdk.testlibrary.RandomFactory;
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    37
import static java.lang.Integer.*;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    38
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    39
public class BitTwiddle {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    40
    private static final int N = 1000; // # of repetitions per test
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    41
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    42
    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
    43
        Random rnd = RandomFactory.getRandom();
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    44
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    45
        if (highestOneBit(0) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    46
            throw new RuntimeException("a");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    47
        if (highestOneBit(-1) != MIN_VALUE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    48
            throw new RuntimeException("b");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    49
        if (highestOneBit(1) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    50
            throw new RuntimeException("c");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    51
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    52
        if (lowestOneBit(0) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    53
            throw new RuntimeException("d");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    54
        if (lowestOneBit(-1) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    55
            throw new RuntimeException("e");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    56
        if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    57
            throw new RuntimeException("f");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    58
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    59
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    60
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    61
            if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    62
                throw new RuntimeException("g: " + toHexString(x));
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    65
        if (numberOfLeadingZeros(0) != SIZE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    66
            throw new RuntimeException("h");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    67
        if (numberOfLeadingZeros(-1) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    68
            throw new RuntimeException("i");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    69
        if (numberOfLeadingZeros(1) != (SIZE - 1))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    70
            throw new RuntimeException("j");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    71
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    72
        if (numberOfTrailingZeros(0) != SIZE)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    73
            throw new RuntimeException("k");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    74
        if (numberOfTrailingZeros(1) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    75
            throw new RuntimeException("l");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    76
        if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    77
            throw new RuntimeException("m");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    78
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    79
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    80
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    81
            if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    82
                throw new RuntimeException("n: " + toHexString(x));
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    85
        if (bitCount(0) != 0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    86
                throw new RuntimeException("o");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    87
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    88
        for (int i = 0; i < SIZE; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    89
            int pow2 = 1 << i;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    90
            if (bitCount(pow2) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    91
                throw new RuntimeException("p: " + i);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    92
            if (bitCount(pow2 -1) != i)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    93
                throw new RuntimeException("q: " + i);
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    96
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    97
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    98
            if (bitCount(x) != bitCount(reverse(x)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    99
                throw new RuntimeException("r: " + toHexString(x));
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   102
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   103
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   104
            int dist = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   105
            if (bitCount(x) != bitCount(rotateRight(x, dist)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   106
                throw new RuntimeException("s: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   107
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   108
            if (bitCount(x) != bitCount(rotateLeft(x, dist)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   109
                throw new RuntimeException("t: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   110
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   111
            if (rotateRight(x, dist) != rotateLeft(x, -dist))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   112
                throw new RuntimeException("u: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   113
                                           toHexString(dist));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   114
            if (rotateRight(x, -dist) != rotateLeft(x, dist))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   115
                throw new RuntimeException("v: " + toHexString(x) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   116
                                           toHexString(dist));
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   119
        if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   120
            || signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   121
            throw new RuntimeException("w");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   122
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   123
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   124
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   125
            int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   126
            if (signum(x) != sign)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   127
                throw new RuntimeException("x: " + toHexString(x));
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   130
        if(reverseBytes(0xaabbccdd) != 0xddccbbaa)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   131
            throw new RuntimeException("y");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   132
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   133
        for (int i = 0; i < N; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   134
            int x = rnd.nextInt();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   135
            if (bitCount(x) != bitCount(reverseBytes(x)))
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   136
                throw new RuntimeException("z: " + toHexString(x));
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
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   139
}