1 /*
2  * Copyright (c) 2014, 2015, 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 8028267
27  * @summary Unit tests for the com.sun.tools.javac.util.Bits class.
28  * @modules jdk.compiler/com.sun.tools.javac.util
29  * @run main BitsTest
30  */
31 
32 import java.util.Arrays;
33 import java.util.HashSet;
34 import java.util.Set;
35 
36 import com.sun.tools.javac.util.Bits;
37 
38 public class BitsTest {
39 
40     final static int[] samples = { 0, 1, 7, 16, 19, 31, 32, 33, 63, 64 };
41     final static int LENGTH = samples[samples.length - 1] + 50;
42 
main(String... args)43     public static void main(String... args) throws Exception {
44 
45         testIncl();
46         testInclRange();
47         testDup();
48         testClear();
49         testExcl();
50         testExcludeFrom();
51         testBinOps();
52         testNextBit();
53 
54     }
55 
56 
57     // Test Bits.incl
testIncl()58     static void testIncl() {
59         for (int a : samples) {
60             for (int b : samples) {
61                 Bits bits = new Bits();
62                 bits.incl(a);
63                 if (a != b)
64                     bits.incl(b);
65                 for (int i = 0; i < LENGTH; i++)
66                     assert bits.isMember(i) == (i == a || i == b);
67             }
68         }
69     }
70 
71 
72     // Test Bits.excl
testExcl()73     static void testExcl() {
74         for (int a : samples) {
75             for (int b : samples) {
76                 Bits bits = new Bits();
77                 bits.inclRange(0, LENGTH);
78                 bits.excl(a);
79                 if (a != b)
80                     bits.excl(b);
81                 for (int i = 0; i < LENGTH; i++)
82                     assert !bits.isMember(i) == (i == a || i == b);
83             }
84         }
85     }
86 
87 
88     // Test Bits.inclRange with various ranges.
testInclRange()89     static void testInclRange() {
90         for (int i = 0; i < samples.length; i++) {
91             for (int j = i; j < samples.length; j++)
92                 testInclRangeHelper(samples[i], samples[j]);
93         }
94     }
95 
96 
97     // Tests Bits.inclRange for the given range.
testInclRangeHelper(int from, int to)98     static void testInclRangeHelper(int from, int to) {
99         Bits bits = new Bits();
100         bits.inclRange(from, to);
101         for (int i = 0; i < LENGTH; i++)
102             assert bits.isMember(i) == (from <= i && i < to);
103     }
104 
105 
106     // Test Bits.dup
testDup()107     static void testDup() {
108         Bits bits = sampleBits();
109         Bits dupBits = bits.dup();
110         assertEquals(LENGTH, bits, dupBits);
111     }
112 
113 
114     // Make sure Bits.clear clears all bits.
testClear()115     static void testClear() {
116         Bits bits = sampleBits();
117         bits.clear();
118         for (int i = 0; i < LENGTH; i++)
119             assert !bits.isMember(i);
120     }
121 
122 
123     // Test Bits.excludeFrom
testExcludeFrom()124     static void testExcludeFrom() {
125         Bits bits = sampleBits();
126 
127         int half = samples.length / 2;
128         Set<Integer> expected = new HashSet<Integer>();
129         for (int i : Arrays.copyOf(samples, half))
130             expected.add(i);
131 
132         bits.excludeFrom(samples[half]);
133 
134         for (int i = 0; i < LENGTH; i++)
135             assert bits.isMember(i) == expected.contains(i);
136     }
137 
138 
139     // Test andSet, orSet, diffSet, xorSet
testBinOps()140     static void testBinOps() {
141         int[] a = { 1630757163, -592623705 };
142         int[] b = { 1062404889, 1969380693 };
143 
144         int[] or   = { a[0] | b[0],  a[1] | b[1] };
145         int[] and  = { a[0] & b[0],  a[1] & b[1] };
146         int[] xor  = { a[0] ^ b[0],  a[1] ^ b[1] };
147         int[] diff = { a[0] & ~b[0], a[1] & ~b[1] };
148 
149         assertEquals(64, fromInts(a).orSet  (fromInts(b)), fromInts(or));
150         assertEquals(64, fromInts(a).andSet (fromInts(b)), fromInts(and));
151         assertEquals(64, fromInts(a).xorSet (fromInts(b)), fromInts(xor));
152         assertEquals(64, fromInts(a).diffSet(fromInts(b)), fromInts(diff));
153 
154     }
155 
156 
157     // Create a Bits-instance based on bits in 'ints' argument.
fromInts(int[] ints)158     static Bits fromInts(int[] ints) {
159         Bits bits = new Bits();
160         for (int bit = 0; bit < ints.length * 32; bit++)
161             if ((ints[bit / 32] & (1 << (bit % 32))) != 0)
162                 bits.incl(bit);
163         return bits;
164     }
165 
166 
167     // Asserts that two Bits-instances are equal up to first 'len' bits.
assertEquals(int len, Bits a, Bits b)168     static void assertEquals(int len, Bits a, Bits b) {
169         for (int i = 0; i < len; i++)
170             assert a.isMember(i) == b.isMember(i);
171     }
172 
173 
174     // Test Bits.nextBit
testNextBit()175     static void testNextBit() {
176         Bits bits = sampleBits();
177 
178         int index = 0;
179         for (int bit = 0; bit < LENGTH; bit++) {
180 
181             int expected;
182 
183             // Passed last sample index?
184             if (index < samples.length) {
185                 expected = samples[index];
186                 if (bit == samples[index])
187                     index++;
188             } else {
189                 expected = -1;
190             }
191 
192             assert bits.nextBit(bit) == expected;
193         }
194     }
195 
196 
197     // Convenience method: Generate a Bits-instance based on samples.
sampleBits()198     static Bits sampleBits() {
199         Bits bits = new Bits();
200         for (int i : samples)
201             bits.incl(i);
202         return bits;
203     }
204 
205 }
206