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 5037068 27 * @summary Test import/export constructors and methods 28 * @author Martin Buchholz 29 * @key randomness 30 */ 31 32 package test.java.util.BitSet; 33 34 import java.nio.*; 35 import java.util.*; 36 37 public class ImportExport { 38 final Random rnd = new Random(); 39 equal(byte[] x, byte[] y)40 void equal(byte[] x, byte[] y) { 41 check(Arrays.equals(x, y)); 42 } 43 equal(long[] x, long[] y)44 void equal(long[] x, long[] y) { 45 check(Arrays.equals(x, y)); 46 } 47 equal(byte[] bytes, BitSet s)48 void equal(byte[] bytes, BitSet s) { 49 equal(s, BitSet.valueOf(bytes)); 50 equal(s, BitSet.valueOf(ByteBuffer.wrap(bytes))); 51 equal(s, BitSet.valueOf( 52 ByteBuffer.wrap( 53 Arrays.copyOf(bytes, bytes.length + 8 + rnd.nextInt(8))) 54 .order(ByteOrder.LITTLE_ENDIAN) 55 .asLongBuffer())); 56 } 57 checkEmptyBitSet(BitSet s)58 void checkEmptyBitSet(BitSet s) { 59 equal(s.toByteArray(), new byte[0]); 60 equal(s.toLongArray(), new long[0]); 61 check(s.isEmpty()); 62 } 63 test(String[] args)64 void test(String[] args) throws Throwable { 65 for (int i = 0; i < 17; i++) { 66 byte[] bytes = new byte[i]; 67 BitSet s = new BitSet(); 68 equal(bytes, s); 69 equal(BitSet.valueOf(bytes).toByteArray(), new byte[0]); 70 if (i > 0) { 71 int k = rnd.nextInt(i); 72 for (int j = 0; j < 8; j++) { 73 bytes[k] |= 1 << j; 74 s.set(8*k+j); 75 equal(bytes, s); 76 byte[] expected = new byte[k+1]; expected[k] = bytes[k]; 77 equal(BitSet.valueOf(bytes).toByteArray(), expected); 78 ByteBuffer bb = ByteBuffer.wrap(bytes); 79 bb.position(k); 80 equal(BitSet.valueOf(bb).toByteArray(), 81 new byte[]{bytes[k]}); 82 } 83 } 84 } 85 for (int i = 0; i < 100; i++) { 86 byte[] bytes = new byte[rnd.nextInt(17)]; 87 for (int j = 0; j < bytes.length; j++) 88 bytes[j] = (byte) rnd.nextInt(0x100); 89 BitSet s = BitSet.valueOf(bytes); 90 byte[] expected = s.toByteArray(); 91 equal(expected.length, (s.length()+7)/8); 92 if (bytes.length == 0) 93 continue; 94 if (expected.length > 0) 95 check(expected[expected.length-1] != 0); 96 if (bytes[bytes.length-1] != 0) 97 equal(bytes, expected); 98 int n = rnd.nextInt(8 * bytes.length); 99 equal(s.get(n), ((bytes[n/8] & (1<<(n%8))) != 0)); 100 } 101 102 for (int i = 0; i < 3; i++) { 103 checkEmptyBitSet(BitSet.valueOf(new byte[i])); 104 checkEmptyBitSet(BitSet.valueOf(ByteBuffer.wrap(new byte[i]))); 105 checkEmptyBitSet(BitSet.valueOf(new byte[i*64])); 106 checkEmptyBitSet(BitSet.valueOf(ByteBuffer.wrap(new byte[i*64]))); 107 checkEmptyBitSet(BitSet.valueOf(new long[i])); 108 checkEmptyBitSet(BitSet.valueOf(LongBuffer.wrap(new long[i]))); 109 } 110 111 { 112 long[] longs = new long[rnd.nextInt(10)]; 113 for (int i = 0; i < longs.length; i++) 114 longs[i] = rnd.nextLong(); 115 LongBuffer b1 = LongBuffer.wrap(longs); 116 LongBuffer b2 = LongBuffer.allocate(longs.length + 10); 117 for (int i = 0; i < b2.limit(); i++) 118 b2.put(i, rnd.nextLong()); 119 int beg = rnd.nextInt(10); 120 b2.position(beg); 121 b2.put(longs); 122 b2.limit(b2.position()); 123 b2.position(beg); 124 BitSet s1 = BitSet.valueOf(longs); 125 BitSet s2 = BitSet.valueOf(b1); 126 BitSet s3 = BitSet.valueOf(b2); 127 equal(s1, s2); 128 equal(s1, s3); 129 if (longs.length > 0 && longs[longs.length -1] != 0) { 130 equal(longs, s1.toLongArray()); 131 equal(longs, s2.toLongArray()); 132 equal(longs, s3.toLongArray()); 133 } 134 for (int i = 0; i < 64 * longs.length; i++) { 135 equal(s1.get(i), ((longs [i/64] & (1L<<(i%64))) != 0)); 136 equal(s2.get(i), ((b1.get(i/64) & (1L<<(i%64))) != 0)); 137 equal(s3.get(i), ((b2.get(b2.position()+i/64) & (1L<<(i%64))) != 0)); 138 } 139 } 140 } 141 142 //--------------------- Infrastructure --------------------------- 143 volatile int passed = 0, failed = 0; pass()144 void pass() {passed++;} fail()145 void fail() {failed++; Thread.dumpStack();} fail(String msg)146 void fail(String msg) {System.err.println(msg); fail();} unexpected(Throwable t)147 void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)148 void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)149 void equal(Object x, Object y) { 150 if (x == null ? y == null : x.equals(y)) pass(); 151 else fail(x + " not equal to " + y);} main(String[] args)152 public static void main(String[] args) throws Throwable { 153 new ImportExport().instanceMain(args);} instanceMain(String[] args)154 void instanceMain(String[] args) throws Throwable { 155 try {test(args);} catch (Throwable t) {unexpected(t);} 156 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 157 if (failed > 0) throw new AssertionError("Some tests failed");} 158 } 159