jdk/test/java/util/BitSet/ImportExport.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5037068
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test import/export constructors and methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class ImportExport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    void equal(byte[] x, byte[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        check(Arrays.equals(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    void equal(long[] x, long[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        check(Arrays.equals(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    void equal(byte[] bytes, BitSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        equal(s, BitSet.valueOf(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        equal(s, BitSet.valueOf(ByteBuffer.wrap(bytes)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        equal(s, BitSet.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                  ByteBuffer.wrap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                      Arrays.copyOf(bytes, bytes.length + 8 + rnd.nextInt(8)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                  .order(ByteOrder.LITTLE_ENDIAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                  .asLongBuffer()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    void checkEmptyBitSet(BitSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        equal(s.toByteArray(), new byte[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        equal(s.toLongArray(), new long[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        check(s.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void test(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        for (int i = 0; i < 17; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            byte[] bytes = new byte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            BitSet s = new BitSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            equal(bytes, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            equal(BitSet.valueOf(bytes).toByteArray(), new byte[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                int k = rnd.nextInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                for (int j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    bytes[k] |= 1 << j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    s.set(8*k+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    equal(bytes, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    byte[] expected = new byte[k+1]; expected[k] = bytes[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    equal(BitSet.valueOf(bytes).toByteArray(), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    ByteBuffer bb = ByteBuffer.wrap(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    bb.position(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    equal(BitSet.valueOf(bb).toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                          new byte[]{bytes[k]});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for (int i = 0; i < 100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            byte[] bytes = new byte[rnd.nextInt(17)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            for (int j = 0; j < bytes.length; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                bytes[j] = (byte) rnd.nextInt(0x100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            BitSet s = BitSet.valueOf(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            byte[] expected = s.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            equal(expected.length, (s.length()+7)/8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (bytes.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (expected.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                check(expected[expected.length-1] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (bytes[bytes.length-1] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                equal(bytes, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            int n = rnd.nextInt(8 * bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            equal(s.get(n), ((bytes[n/8] & (1<<(n%8))) != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            checkEmptyBitSet(BitSet.valueOf(new byte[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            checkEmptyBitSet(BitSet.valueOf(ByteBuffer.wrap(new byte[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            checkEmptyBitSet(BitSet.valueOf(new byte[i*64]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            checkEmptyBitSet(BitSet.valueOf(ByteBuffer.wrap(new byte[i*64])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            checkEmptyBitSet(BitSet.valueOf(new long[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            checkEmptyBitSet(BitSet.valueOf(LongBuffer.wrap(new long[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            long[] longs = new long[rnd.nextInt(10)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            for (int i = 0; i < longs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                longs[i] = rnd.nextLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            LongBuffer b1 = LongBuffer.wrap(longs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            LongBuffer b2 = LongBuffer.allocate(longs.length + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            for (int i = 0; i < b2.limit(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                b2.put(i, rnd.nextLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int beg = rnd.nextInt(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            b2.position(beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            b2.put(longs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            b2.limit(b2.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            b2.position(beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            BitSet s1 = BitSet.valueOf(longs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            BitSet s2 = BitSet.valueOf(b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            BitSet s3 = BitSet.valueOf(b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            equal(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            equal(s1, s3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (longs.length > 0 && longs[longs.length -1] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                equal(longs, s1.toLongArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                equal(longs, s2.toLongArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                equal(longs, s3.toLongArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            for (int i = 0; i < 64 * longs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                equal(s1.get(i), ((longs [i/64] & (1L<<(i%64))) != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                equal(s2.get(i), ((b1.get(i/64) & (1L<<(i%64))) != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                equal(s3.get(i), ((b2.get(b2.position()+i/64) & (1L<<(i%64))) != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        new ImportExport().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}