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