1 /* 2 * Copyright (c) 2007, 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 /* 25 * @test 26 * @bug 6410729 6586631 27 * @summary Test previousClearBit, previousSetBit 28 * @key randomness 29 */ 30 31 package test.java.util.BitSet; 32 33 import java.util.*; 34 35 public class PreviousBits { 36 testHashCode(final BitSet s)37 void testHashCode(final BitSet s) { 38 long h = 1234; 39 long[] words = s.toLongArray(); 40 for (int i = words.length; --i >= 0; ) 41 h ^= words[i] * (i + 1); 42 equal((int)((h >> 32) ^ h), s.hashCode()); 43 } 44 testOutOfBounds(final BitSet s)45 void testOutOfBounds(final BitSet s) { 46 THROWS(IndexOutOfBoundsException.class, 47 new F(){void f(){ s.previousSetBit(-2);}}, 48 new F(){void f(){ s.previousClearBit(-2);}}, 49 new F(){void f(){ s.previousSetBit(Integer.MIN_VALUE);}}, 50 new F(){void f(){ s.previousClearBit(Integer.MIN_VALUE);}}, 51 new F(){void f(){ s.nextSetBit(-1);}}, 52 new F(){void f(){ s.nextClearBit(-1);}}, 53 new F(){void f(){ s.nextSetBit(Integer.MIN_VALUE);}}, 54 new F(){void f(){ s.nextClearBit(Integer.MIN_VALUE);}}); 55 } 56 test(String[] args)57 void test(String[] args) throws Throwable { 58 final BitSet s = new BitSet(); 59 60 // Test empty bitset 61 testOutOfBounds(s); 62 testHashCode(s); 63 64 for (int i = -1; i < 93;) { 65 equal(-1, s.previousSetBit(i)); 66 equal( i, s.previousClearBit(i)); 67 i++; 68 equal(-1, s.nextSetBit(i)); 69 equal( i, s.nextClearBit(i)); 70 } 71 72 // Test "singleton" bitsets 73 for (int j = 0; j < 161; j++) { 74 s.clear(); 75 s.set(j); 76 testOutOfBounds(s); 77 testHashCode(s); 78 79 for (int i = -1; i < j; i++) { 80 equal(-1, s.previousSetBit(i)); 81 equal( i, s.previousClearBit(i)); 82 if (i >= 0) { 83 equal(j, s.nextSetBit(i)); 84 equal(i, s.nextClearBit(i)); 85 } 86 } 87 88 equal(j, s.previousSetBit(j)); 89 equal(j-1, s.previousClearBit(j)); 90 equal(j, s.nextSetBit(j)); 91 equal(j+1, s.nextClearBit(j)); 92 93 for (int i = j+1; i < j+100; i++) { 94 equal(j, s.previousSetBit(i)); 95 equal(i, s.previousClearBit(i)); 96 equal(-1, s.nextSetBit(i)); 97 equal(i, s.nextClearBit(i)); 98 } 99 } 100 101 // set even bits 102 s.clear(); 103 for (int i = 0; i <= 128; i+=2) 104 s.set(i); 105 testHashCode(s); 106 for (int i = 1; i <= 128; i++) { 107 equal(s.previousSetBit(i), 108 ((i & 1) == 0) ? i : i - 1); 109 equal(s.previousClearBit(i), 110 ((i & 1) == 0) ? i - 1 : i); 111 } 112 113 // set odd bits as well 114 for (int i = 1; i <= 128; i+=2) 115 s.set(i); 116 testHashCode(s); 117 for (int i = 1; i <= 128; i++) { 118 equal(s.previousSetBit(i), i); 119 equal(s.previousClearBit(i), -1); 120 } 121 122 // Test loops documented in javadoc 123 Random rnd = new Random(); 124 s.clear(); 125 for (int i = 0; i < 10; i++) 126 s.set(rnd.nextInt(1066)); 127 List<Integer> down = new ArrayList<Integer>(); 128 for (int i = s.length(); (i = s.previousSetBit(i-1)) >= 0; ) 129 down.add(i); 130 List<Integer> up = new ArrayList<Integer>(); 131 for (int i = s.nextSetBit(0); i >= 0; i = s.nextSetBit(i+1)) 132 up.add(i); 133 Collections.reverse(up); 134 equal(up, down); 135 } 136 137 //--------------------- Infrastructure --------------------------- 138 volatile int passed = 0, failed = 0; pass()139 void pass() {passed++;} fail()140 void fail() {failed++; Thread.dumpStack();} fail(String msg)141 void fail(String msg) {System.err.println(msg); fail();} unexpected(Throwable t)142 void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)143 void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)144 void equal(Object x, Object y) { 145 if (x == null ? y == null : x.equals(y)) pass(); 146 else fail(x + " not equal to " + y);} main(String[] args)147 public static void main(String[] args) throws Throwable { 148 new PreviousBits().instanceMain(args);} instanceMain(String[] args)149 void instanceMain(String[] args) throws Throwable { 150 try {test(args);} catch (Throwable t) {unexpected(t);} 151 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 152 if (failed > 0) throw new AssertionError("Some tests failed");} f()153 abstract class F {abstract void f() throws Throwable;} THROWS(Class<? extends Throwable> k, F... fs)154 void THROWS(Class<? extends Throwable> k, F... fs) { 155 for (F f : fs) 156 try {f.f(); fail("Expected " + k.getName() + " not thrown");} 157 catch (Throwable t) { 158 if (k.isAssignableFrom(t.getClass())) pass(); 159 else unexpected(t);}} 160 } 161