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