langtools/test/tools/javac/util/BitsTest.java
author shade
Tue, 02 Feb 2016 18:15:15 +0300
changeset 35639 f34e7e8b4eac
parent 30730 d3ce7619db2c
permissions -rw-r--r--
8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested Reviewed-by: vlivanov, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22689
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 22689
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
22689
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     4
 *
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     8
 *
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    13
 * accompanied this code).
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    14
 *
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    18
 *
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    21
 * questions.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    22
 */
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    23
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    24
/**
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    25
 * @test
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    26
 * @bug 8028267
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    27
 * @summary Unit tests for the com.sun.tools.javac.util.Bits class.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 22689
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.util
22689
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    29
 * @run main BitsTest
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    30
 */
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    31
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    32
import java.util.Arrays;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    33
import java.util.HashSet;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    34
import java.util.Set;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    35
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    36
import com.sun.tools.javac.util.Bits;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    37
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    38
public class BitsTest {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    39
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    40
    final static int[] samples = { 0, 1, 7, 16, 19, 31, 32, 33, 63, 64 };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    41
    final static int LENGTH = samples[samples.length - 1] + 50;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    42
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    43
    public static void main(String... args) throws Exception {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    44
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    45
        testIncl();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    46
        testInclRange();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    47
        testDup();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    48
        testClear();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    49
        testExcl();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    50
        testExcludeFrom();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    51
        testBinOps();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    52
        testNextBit();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    53
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    54
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    55
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    56
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    57
    // Test Bits.incl
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    58
    static void testIncl() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    59
        for (int a : samples) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    60
            for (int b : samples) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    61
                Bits bits = new Bits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    62
                bits.incl(a);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    63
                if (a != b)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    64
                    bits.incl(b);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    65
                for (int i = 0; i < LENGTH; i++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    66
                    assert bits.isMember(i) == (i == a || i == b);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    67
            }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    68
        }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    69
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    70
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    71
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    72
    // Test Bits.excl
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    73
    static void testExcl() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    74
        for (int a : samples) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    75
            for (int b : samples) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    76
                Bits bits = new Bits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    77
                bits.inclRange(0, LENGTH);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    78
                bits.excl(a);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    79
                if (a != b)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    80
                    bits.excl(b);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    81
                for (int i = 0; i < LENGTH; i++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    82
                    assert !bits.isMember(i) == (i == a || i == b);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    83
            }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    84
        }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    85
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    86
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    87
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    88
    // Test Bits.inclRange with various ranges.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    89
    static void testInclRange() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    90
        for (int i = 0; i < samples.length; i++) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    91
            for (int j = i; j < samples.length; j++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    92
                testInclRangeHelper(samples[i], samples[j]);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    93
        }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    94
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    95
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    96
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    97
    // Tests Bits.inclRange for the given range.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    98
    static void testInclRangeHelper(int from, int to) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
    99
        Bits bits = new Bits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   100
        bits.inclRange(from, to);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   101
        for (int i = 0; i < LENGTH; i++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   102
            assert bits.isMember(i) == (from <= i && i < to);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   103
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   104
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   105
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   106
    // Test Bits.dup
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   107
    static void testDup() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   108
        Bits bits = sampleBits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   109
        Bits dupBits = bits.dup();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   110
        assertEquals(LENGTH, bits, dupBits);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   111
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   112
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   113
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   114
    // Make sure Bits.clear clears all bits.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   115
    static void testClear() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   116
        Bits bits = sampleBits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   117
        bits.clear();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   118
        for (int i = 0; i < LENGTH; i++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   119
            assert !bits.isMember(i);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   120
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   121
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   122
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   123
    // Test Bits.excludeFrom
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   124
    static void testExcludeFrom() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   125
        Bits bits = sampleBits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   126
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   127
        int half = samples.length / 2;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   128
        Set<Integer> expected = new HashSet<Integer>();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   129
        for (int i : Arrays.copyOf(samples, half))
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   130
            expected.add(i);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   131
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   132
        bits.excludeFrom(samples[half]);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   133
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   134
        for (int i = 0; i < LENGTH; i++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   135
            assert bits.isMember(i) == expected.contains(i);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   136
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   137
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   138
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   139
    // Test andSet, orSet, diffSet, xorSet
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   140
    static void testBinOps() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   141
        int[] a = { 1630757163, -592623705 };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   142
        int[] b = { 1062404889, 1969380693 };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   143
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   144
        int[] or   = { a[0] | b[0],  a[1] | b[1] };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   145
        int[] and  = { a[0] & b[0],  a[1] & b[1] };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   146
        int[] xor  = { a[0] ^ b[0],  a[1] ^ b[1] };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   147
        int[] diff = { a[0] & ~b[0], a[1] & ~b[1] };
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   148
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   149
        assertEquals(64, fromInts(a).orSet  (fromInts(b)), fromInts(or));
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   150
        assertEquals(64, fromInts(a).andSet (fromInts(b)), fromInts(and));
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   151
        assertEquals(64, fromInts(a).xorSet (fromInts(b)), fromInts(xor));
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   152
        assertEquals(64, fromInts(a).diffSet(fromInts(b)), fromInts(diff));
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   153
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   154
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   155
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   156
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   157
    // Create a Bits-instance based on bits in 'ints' argument.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   158
    static Bits fromInts(int[] ints) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   159
        Bits bits = new Bits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   160
        for (int bit = 0; bit < ints.length * 32; bit++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   161
            if ((ints[bit / 32] & (1 << (bit % 32))) != 0)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   162
                bits.incl(bit);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   163
        return bits;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   164
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   165
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   166
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   167
    // Asserts that two Bits-instances are equal up to first 'len' bits.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   168
    static void assertEquals(int len, Bits a, Bits b) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   169
        for (int i = 0; i < len; i++)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   170
            assert a.isMember(i) == b.isMember(i);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   171
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   172
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   173
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   174
    // Test Bits.nextBit
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   175
    static void testNextBit() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   176
        Bits bits = sampleBits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   177
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   178
        int index = 0;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   179
        for (int bit = 0; bit < LENGTH; bit++) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   180
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   181
            int expected;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   182
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   183
            // Passed last sample index?
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   184
            if (index < samples.length) {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   185
                expected = samples[index];
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   186
                if (bit == samples[index])
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   187
                    index++;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   188
            } else {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   189
                expected = -1;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   190
            }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   191
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   192
            assert bits.nextBit(bit) == expected;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   193
        }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   194
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   195
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   196
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   197
    // Convenience method: Generate a Bits-instance based on samples.
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   198
    static Bits sampleBits() {
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   199
        Bits bits = new Bits();
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   200
        for (int i : samples)
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   201
            bits.incl(i);
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   202
        return bits;
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   203
    }
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   204
570f79791f52 8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
diff changeset
   205
}