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 /* @test
25 * @bug 4098239 4107540 4080736 4261102 4274710 4305272
26 * 4979017 4979028 4979031 5030267 6222207 8040806
27 * @summary Test the operation of the methods of BitSet class
28 * @author Mike McCloskey, Martin Buchholz
29 */
30
31 import java.util.*;
32
33 /**
34 * This is a simple test class created to run tests on the BitSet class.
35 *
36 */
37 public class BSMethods {
38
39 private static Random generator = new Random();
40 private static boolean failure = false;
41
42 private static void fail(String diagnostic) {
43 new Error(diagnostic).printStackTrace();
44 failure = true;
45 }
46
47 private static void check(boolean condition) {
48 check(condition, "something's fishy");
880 BitSet empty = new BitSet();
881 {BitSet t = new BitSet(); t.and (empty); checkEmpty(t);}
882 {BitSet t = new BitSet(); t.or (empty); checkEmpty(t);}
883 {BitSet t = new BitSet(); t.xor (empty); checkEmpty(t);}
884 {BitSet t = new BitSet(); t.andNot(empty); checkEmpty(t);}
885 {BitSet t = new BitSet(); t.and (t); checkEmpty(t);}
886 {BitSet t = new BitSet(); t.or (t); checkEmpty(t);}
887 {BitSet t = new BitSet(); t.xor (t); checkEmpty(t);}
888 {BitSet t = new BitSet(); t.andNot(t); checkEmpty(t);}
889 {BitSet t = new BitSet(); t.and(makeSet(1)); checkEmpty(t);}
890 {BitSet t = new BitSet(); t.and(makeSet(127)); checkEmpty(t);}
891 {BitSet t = new BitSet(); t.and(makeSet(128)); checkEmpty(t);}
892 {BitSet t = new BitSet(); t.flip(7);t.flip(7); checkEmpty(t);}
893 {BitSet t = new BitSet(); checkEmpty(t.get(200,300));}
894 {BitSet t = makeSet(2,5); check(t.get(2,6).equals(makeSet(0,3)),"");}
895 }
896
897 private static void testToString() {
898 check(new BitSet().toString().equals("{}"));
899 check(makeSet(2,3,42,43,234).toString().equals("{2, 3, 42, 43, 234}"));
900 try {
901 check(makeSet(Integer.MAX_VALUE-1).toString().equals(
902 "{" + (Integer.MAX_VALUE-1) + "}"));
903 check(makeSet(Integer.MAX_VALUE).toString().equals(
904 "{" + Integer.MAX_VALUE + "}"));
905 check(makeSet(0, 1, Integer.MAX_VALUE-1, Integer.MAX_VALUE).toString().equals(
906 "{0, 1, " + (Integer.MAX_VALUE-1) + ", " + Integer.MAX_VALUE + "}"));
907 } catch (IndexOutOfBoundsException exc) {
908 fail("toString() with indices near MAX_VALUE");
909 }
910 }
911
912 private static void testLogicalIdentities() {
913 int failCount = 0;
914
915 // Verify that (!b1)|(!b2) == !(b1&b2)
916 for (int i=0; i<50; i++) {
917 // Construct two fairly random bitsets
918 BitSet b1 = new BitSet();
919 BitSet b2 = new BitSet();
920
921 int numberOfSetBits = generator.nextInt(100) + 1;
922 int highestPossibleSetBit = generator.nextInt(1000) + 1;
923
924 for (int x=0; x<numberOfSetBits; x++) {
925 b1.set(generator.nextInt(highestPossibleSetBit));
926 b2.set(generator.nextInt(highestPossibleSetBit));
927 }
928
|
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 /* @test
25 * @bug 4098239 4107540 4080736 4261102 4274710 4305272
26 * 4979017 4979028 4979031 5030267 6222207 8040806
27 * @summary Test the operation of the methods of BitSet class
28 * @author Mike McCloskey, Martin Buchholz
29 * @run main/othervm BSMethods
30 */
31
32 import java.util.*;
33
34 /**
35 * This is a simple test class created to run tests on the BitSet class.
36 *
37 */
38 public class BSMethods {
39
40 private static Random generator = new Random();
41 private static boolean failure = false;
42
43 private static void fail(String diagnostic) {
44 new Error(diagnostic).printStackTrace();
45 failure = true;
46 }
47
48 private static void check(boolean condition) {
49 check(condition, "something's fishy");
881 BitSet empty = new BitSet();
882 {BitSet t = new BitSet(); t.and (empty); checkEmpty(t);}
883 {BitSet t = new BitSet(); t.or (empty); checkEmpty(t);}
884 {BitSet t = new BitSet(); t.xor (empty); checkEmpty(t);}
885 {BitSet t = new BitSet(); t.andNot(empty); checkEmpty(t);}
886 {BitSet t = new BitSet(); t.and (t); checkEmpty(t);}
887 {BitSet t = new BitSet(); t.or (t); checkEmpty(t);}
888 {BitSet t = new BitSet(); t.xor (t); checkEmpty(t);}
889 {BitSet t = new BitSet(); t.andNot(t); checkEmpty(t);}
890 {BitSet t = new BitSet(); t.and(makeSet(1)); checkEmpty(t);}
891 {BitSet t = new BitSet(); t.and(makeSet(127)); checkEmpty(t);}
892 {BitSet t = new BitSet(); t.and(makeSet(128)); checkEmpty(t);}
893 {BitSet t = new BitSet(); t.flip(7);t.flip(7); checkEmpty(t);}
894 {BitSet t = new BitSet(); checkEmpty(t.get(200,300));}
895 {BitSet t = makeSet(2,5); check(t.get(2,6).equals(makeSet(0,3)),"");}
896 }
897
898 private static void testToString() {
899 check(new BitSet().toString().equals("{}"));
900 check(makeSet(2,3,42,43,234).toString().equals("{2, 3, 42, 43, 234}"));
901
902 if (Runtime.getRuntime().maxMemory() >= 0x20_000_000) { // 512M
903 System.out.println("Ok");
904 // only run it if we have enough memory
905 try {
906 check(makeSet(Integer.MAX_VALUE-1).toString().equals(
907 "{" + (Integer.MAX_VALUE-1) + "}"));
908 check(makeSet(Integer.MAX_VALUE).toString().equals(
909 "{" + Integer.MAX_VALUE + "}"));
910 check(makeSet(0, 1, Integer.MAX_VALUE-1, Integer.MAX_VALUE).toString().equals(
911 "{0, 1, " + (Integer.MAX_VALUE-1) + ", " + Integer.MAX_VALUE + "}"));
912 } catch (IndexOutOfBoundsException exc) {
913 fail("toString() with indices near MAX_VALUE");
914 }
915 }
916 }
917
918 private static void testLogicalIdentities() {
919 int failCount = 0;
920
921 // Verify that (!b1)|(!b2) == !(b1&b2)
922 for (int i=0; i<50; i++) {
923 // Construct two fairly random bitsets
924 BitSet b1 = new BitSet();
925 BitSet b2 = new BitSet();
926
927 int numberOfSetBits = generator.nextInt(100) + 1;
928 int highestPossibleSetBit = generator.nextInt(1000) + 1;
929
930 for (int x=0; x<numberOfSetBits; x++) {
931 b1.set(generator.nextInt(highestPossibleSetBit));
932 b2.set(generator.nextInt(highestPossibleSetBit));
933 }
934
|