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