Path: blob/master/test/jdk/java/util/BitSet/PreviousBits.java
41149 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6410729 658663126* @summary Test previousClearBit, previousSetBit27* @key randomness28*/2930import java.util.*;3132public class PreviousBits {3334void testHashCode(final BitSet s) {35long h = 1234;36long[] words = s.toLongArray();37for (int i = words.length; --i >= 0; )38h ^= words[i] * (i + 1);39equal((int)((h >> 32) ^ h), s.hashCode());40}4142void testOutOfBounds(final BitSet s) {43THROWS(IndexOutOfBoundsException.class,44new F(){void f(){ s.previousSetBit(-2);}},45new F(){void f(){ s.previousClearBit(-2);}},46new F(){void f(){ s.previousSetBit(Integer.MIN_VALUE);}},47new F(){void f(){ s.previousClearBit(Integer.MIN_VALUE);}},48new F(){void f(){ s.nextSetBit(-1);}},49new F(){void f(){ s.nextClearBit(-1);}},50new F(){void f(){ s.nextSetBit(Integer.MIN_VALUE);}},51new F(){void f(){ s.nextClearBit(Integer.MIN_VALUE);}});52}5354void test(String[] args) throws Throwable {55final BitSet s = new BitSet();5657// Test empty bitset58testOutOfBounds(s);59testHashCode(s);6061for (int i = -1; i < 93;) {62equal(-1, s.previousSetBit(i));63equal( i, s.previousClearBit(i));64i++;65equal(-1, s.nextSetBit(i));66equal( i, s.nextClearBit(i));67}6869// Test "singleton" bitsets70for (int j = 0; j < 161; j++) {71s.clear();72s.set(j);73testOutOfBounds(s);74testHashCode(s);7576for (int i = -1; i < j; i++) {77equal(-1, s.previousSetBit(i));78equal( i, s.previousClearBit(i));79if (i >= 0) {80equal(j, s.nextSetBit(i));81equal(i, s.nextClearBit(i));82}83}8485equal(j, s.previousSetBit(j));86equal(j-1, s.previousClearBit(j));87equal(j, s.nextSetBit(j));88equal(j+1, s.nextClearBit(j));8990for (int i = j+1; i < j+100; i++) {91equal(j, s.previousSetBit(i));92equal(i, s.previousClearBit(i));93equal(-1, s.nextSetBit(i));94equal(i, s.nextClearBit(i));95}96}9798// set even bits99s.clear();100for (int i = 0; i <= 128; i+=2)101s.set(i);102testHashCode(s);103for (int i = 1; i <= 128; i++) {104equal(s.previousSetBit(i),105((i & 1) == 0) ? i : i - 1);106equal(s.previousClearBit(i),107((i & 1) == 0) ? i - 1 : i);108}109110// set odd bits as well111for (int i = 1; i <= 128; i+=2)112s.set(i);113testHashCode(s);114for (int i = 1; i <= 128; i++) {115equal(s.previousSetBit(i), i);116equal(s.previousClearBit(i), -1);117}118119// Test loops documented in javadoc120Random rnd = new Random();121s.clear();122for (int i = 0; i < 10; i++)123s.set(rnd.nextInt(1066));124List<Integer> down = new ArrayList<Integer>();125for (int i = s.length(); (i = s.previousSetBit(i-1)) >= 0; )126down.add(i);127List<Integer> up = new ArrayList<Integer>();128for (int i = s.nextSetBit(0); i >= 0; i = s.nextSetBit(i+1))129up.add(i);130Collections.reverse(up);131equal(up, down);132}133134//--------------------- Infrastructure ---------------------------135volatile int passed = 0, failed = 0;136void pass() {passed++;}137void fail() {failed++; Thread.dumpStack();}138void fail(String msg) {System.err.println(msg); fail();}139void unexpected(Throwable t) {failed++; t.printStackTrace();}140void check(boolean cond) {if (cond) pass(); else fail();}141void equal(Object x, Object y) {142if (x == null ? y == null : x.equals(y)) pass();143else fail(x + " not equal to " + y);}144public static void main(String[] args) throws Throwable {145new PreviousBits().instanceMain(args);}146void instanceMain(String[] args) throws Throwable {147try {test(args);} catch (Throwable t) {unexpected(t);}148System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);149if (failed > 0) throw new AssertionError("Some tests failed");}150abstract class F {abstract void f() throws Throwable;}151void THROWS(Class<? extends Throwable> k, F... fs) {152for (F f : fs)153try {f.f(); fail("Expected " + k.getName() + " not thrown");}154catch (Throwable t) {155if (k.isAssignableFrom(t.getClass())) pass();156else unexpected(t);}}157}158159160