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