1 /* 2 * Copyright (c) 2006, 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 6404711 27 * @summary Check capacity management 28 * @author Martin Buchholz 29 */ 30 31 package test.java.util.BitSet; 32 33 import java.io.*; 34 import java.util.*; 35 36 public class StickySize { equalClones(BitSet s, int expectedSize)37 static void equalClones(BitSet s, int expectedSize) { 38 equal(expectedSize, clone(s).size()); 39 equal(expectedSize, serialClone(s).size()); 40 equal(expectedSize, s.size()); 41 equal(clone(s), serialClone(s)); 42 } 43 realMain(String[] args)44 private static void realMain(String[] args) { 45 BitSet s; 46 47 s = new BitSet(); // non-sticky 48 equal(s.size(), 64); 49 equalClones(s, 0); 50 s.set(3*64); 51 s.set(7*64); 52 equal(s.size(), 8*64); 53 equalClones(s, 8*64); 54 s.clear(7*64); 55 equal(s.size(), 8*64); 56 equalClones(s, 4*64); 57 58 s = new BitSet(8*64); // sticky 59 equalClones(s, 8*64); 60 s.set(3*64); 61 s.set(7*64); 62 equalClones(s, 8*64); 63 s.clear(7*64); 64 equalClones(s, 8*64); 65 equalClones(clone(s), 8*64); 66 equalClones(serialClone(s), 8*64); 67 s.set(17*64); // Expand beyond sticky size 68 equalClones(s, 18*64); 69 s.clear(17*64); 70 equalClones(s, 4*64); 71 } 72 clone(BitSet s)73 static BitSet clone(BitSet s) { 74 return (BitSet) s.clone(); 75 } 76 77 //--------------------- Infrastructure --------------------------- 78 static volatile int passed = 0, failed = 0; pass()79 static void pass() {passed++;} fail()80 static void fail() {failed++; Thread.dumpStack();} fail(String msg)81 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)82 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)83 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)84 static void equal(Object x, Object y) { 85 if (x == null ? y == null : x.equals(y)) pass(); 86 else fail(x + " not equal to " + y);} main(String[] args)87 public static void main(String[] args) throws Throwable { 88 try {realMain(args);} catch (Throwable t) {unexpected(t);} 89 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 90 if (failed > 0) throw new AssertionError("Some tests failed");} serializedForm(Object obj)91 static byte[] serializedForm(Object obj) { 92 try { 93 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 94 new ObjectOutputStream(baos).writeObject(obj); 95 return baos.toByteArray(); 96 } catch (IOException e) { throw new RuntimeException(e); }} readObject(byte[] bytes)97 static Object readObject(byte[] bytes) 98 throws IOException, ClassNotFoundException { 99 InputStream is = new ByteArrayInputStream(bytes); 100 return new ObjectInputStream(is).readObject();} 101 @SuppressWarnings("unchecked") serialClone(T obj)102 static <T> T serialClone(T obj) { 103 try { return (T) readObject(serializedForm(obj)); } 104 catch (Exception e) { throw new RuntimeException(e); }} 105 } 106