jdk/test/java/util/BitSet/PreviousBits.java
author ctornqvi
Thu, 16 May 2013 15:31:00 +0200
changeset 17598 f78d22d3c6c2
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
permissions -rw-r--r--
8008169: test/runtime/7158804/Test7158804.sh has bad copyright header Summary: Re-wrote test in Java in addition to fixing the Copyright notice. Also reviewed by leonid.mesnik@oracle.com Reviewed-by: coleenp, ctornqvi Contributed-by: Mikhailo Seledtsov <mikhailo.seledtsov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6410729 6586631
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test previousClearBit, previousSetBit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class PreviousBits {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    void testHashCode(final BitSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        long h = 1234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        long[] words = s.toLongArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        for (int i = words.length; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
            h ^= words[i] * (i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        equal((int)((h >> 32) ^ h), s.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    void testOutOfBounds(final BitSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        THROWS(IndexOutOfBoundsException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
               new F(){void f(){ s.previousSetBit(-2);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
               new F(){void f(){ s.previousClearBit(-2);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
               new F(){void f(){ s.previousSetBit(Integer.MIN_VALUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
               new F(){void f(){ s.previousClearBit(Integer.MIN_VALUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
               new F(){void f(){ s.nextSetBit(-1);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
               new F(){void f(){ s.nextClearBit(-1);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
               new F(){void f(){ s.nextSetBit(Integer.MIN_VALUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
               new F(){void f(){ s.nextClearBit(Integer.MIN_VALUE);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    void test(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        final BitSet s = new BitSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Test empty bitset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        testOutOfBounds(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        testHashCode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i = -1; i < 93;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            equal(-1, s.previousSetBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            equal( i, s.previousClearBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            equal(-1, s.nextSetBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            equal( i, s.nextClearBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Test "singleton" bitsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        for (int j = 0; j < 161; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            s.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            s.set(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            testOutOfBounds(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            testHashCode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            for (int i = -1; i < j; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                equal(-1, s.previousSetBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                equal( i, s.previousClearBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    equal(j, s.nextSetBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    equal(i, s.nextClearBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            equal(j,   s.previousSetBit(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            equal(j-1, s.previousClearBit(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            equal(j,   s.nextSetBit(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            equal(j+1, s.nextClearBit(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for (int i = j+1; i < j+100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                equal(j, s.previousSetBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                equal(i, s.previousClearBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                equal(-1, s.nextSetBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                equal(i, s.nextClearBit(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // set even bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        s.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (int i = 0; i <= 128; i+=2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            s.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        testHashCode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 1; i <= 128; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            equal(s.previousSetBit(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                  ((i & 1) == 0) ? i : i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            equal(s.previousClearBit(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                  ((i & 1) == 0) ? i - 1 : i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // set odd bits as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 1; i <= 128; i+=2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            s.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        testHashCode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i = 1; i <= 128; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            equal(s.previousSetBit(i), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            equal(s.previousClearBit(i), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Test loops documented in javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        s.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < 10; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            s.set(rnd.nextInt(1066));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        List<Integer> down = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for (int i = s.length(); (i = s.previousSetBit(i-1)) >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            down.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        List<Integer> up = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        for (int i = s.nextSetBit(0); i >= 0; i = s.nextSetBit(i+1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            up.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Collections.reverse(up);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        equal(up, down);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        new PreviousBits().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    abstract class F {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    void THROWS(Class<? extends Throwable> k, F... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for (F f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            try {f.f(); fail("Expected " + k.getName() + " not thrown");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}