test/java/util/BitSet/BSMethods.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2005, 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 /* @test
  25  * @bug 4098239 4107540 4080736 4261102 4274710 4305272
  26  *      4979017 4979028 4979031 5030267 6222207
  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 


 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     }
 901 
 902     private static void testLogicalIdentities() {
 903         int failCount = 0;
 904 
 905         // Verify that (!b1)|(!b2) == !(b1&b2)
 906         for (int i=0; i<50; i++) {
 907             // Construct two fairly random bitsets
 908             BitSet b1 = new BitSet();
 909             BitSet b2 = new BitSet();
 910 
 911             int numberOfSetBits = generator.nextInt(100) + 1;
 912             int highestPossibleSetBit = generator.nextInt(1000) + 1;
 913 
 914             for (int x=0; x<numberOfSetBits; x++) {
 915                 b1.set(generator.nextInt(highestPossibleSetBit));
 916                 b2.set(generator.nextInt(highestPossibleSetBit));
 917             }
 918 
 919             BitSet b3 = (BitSet) b1.clone();


   1 /*
   2  * Copyright (c) 1998, 2014, 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 /* @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 


 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 
 929             BitSet b3 = (BitSet) b1.clone();