Path: blob/master/test/jdk/java/util/BitSet/StickySize.java
41149 views
/*1* Copyright (c) 2006, 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 640471126* @summary Check capacity management27* @author Martin Buchholz28*/2930import java.io.*;31import java.util.*;3233public class StickySize {34static void equalClones(BitSet s, int expectedSize) {35equal(expectedSize, clone(s).size());36equal(expectedSize, serialClone(s).size());37equal(expectedSize, s.size());38equal(clone(s), serialClone(s));39}4041private static void realMain(String[] args) {42BitSet s;4344s = new BitSet(); // non-sticky45equal(s.size(), 64);46equalClones(s, 0);47s.set(3*64);48s.set(7*64);49equal(s.size(), 8*64);50equalClones(s, 8*64);51s.clear(7*64);52equal(s.size(), 8*64);53equalClones(s, 4*64);5455s = new BitSet(8*64); // sticky56equalClones(s, 8*64);57s.set(3*64);58s.set(7*64);59equalClones(s, 8*64);60s.clear(7*64);61equalClones(s, 8*64);62equalClones(clone(s), 8*64);63equalClones(serialClone(s), 8*64);64s.set(17*64); // Expand beyond sticky size65equalClones(s, 18*64);66s.clear(17*64);67equalClones(s, 4*64);68}6970static BitSet clone(BitSet s) {71return (BitSet) s.clone();72}7374//--------------------- Infrastructure ---------------------------75static volatile int passed = 0, failed = 0;76static void pass() {passed++;}77static void fail() {failed++; Thread.dumpStack();}78static void fail(String msg) {System.out.println(msg); fail();}79static void unexpected(Throwable t) {failed++; t.printStackTrace();}80static void check(boolean cond) {if (cond) pass(); else fail();}81static void equal(Object x, Object y) {82if (x == null ? y == null : x.equals(y)) pass();83else fail(x + " not equal to " + y);}84public static void main(String[] args) throws Throwable {85try {realMain(args);} catch (Throwable t) {unexpected(t);}86System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);87if (failed > 0) throw new AssertionError("Some tests failed");}88static byte[] serializedForm(Object obj) {89try {90ByteArrayOutputStream baos = new ByteArrayOutputStream();91new ObjectOutputStream(baos).writeObject(obj);92return baos.toByteArray();93} catch (IOException e) { throw new RuntimeException(e); }}94static Object readObject(byte[] bytes)95throws IOException, ClassNotFoundException {96InputStream is = new ByteArrayInputStream(bytes);97return new ObjectInputStream(is).readObject();}98@SuppressWarnings("unchecked")99static <T> T serialClone(T obj) {100try { return (T) readObject(serializedForm(obj)); }101catch (Exception e) { throw new RuntimeException(e); }}102}103104105