tests/libjava-mauve/src/gnu/testlet/java/util/BitSet/AcuniaBitSetTest.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 /* Copyright (C) 2001 ACUNIA
       
     2 
       
     3    This file is part of Mauve.
       
     4 
       
     5    Mauve is free software; you can redistribute it and/or modify
       
     6    it under the terms of the GNU General Public License as published by
       
     7    the Free Software Foundation; either version 2, or (at your option)
       
     8    any later version.
       
     9 
       
    10    Mauve is distributed in the hope that it will be useful,
       
    11    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13    GNU General Public License for more details.
       
    14 
       
    15    You should have received a copy of the GNU General Public License
       
    16    along with Mauve; see the file COPYING.  If not, write to
       
    17    the Free Software Foundation, 59 Temple Place - Suite 330,
       
    18    Boston, MA 02111-1307, USA.
       
    19 */
       
    20 
       
    21 // Tags: JDK1.2
       
    22 
       
    23 package gnu.testlet.java.util.BitSet;
       
    24 import gnu.testlet.Testlet;
       
    25 import gnu.testlet.TestHarness;
       
    26 import java.util.*;
       
    27 
       
    28 /**
       
    29 *  Written by ACUNIA.	<br>
       
    30 *  <br>
       
    31 *  this file contains test for java.util.BitSet <br>
       
    32 *
       
    33 */
       
    34 public class AcuniaBitSetTest implements Testlet
       
    35 {
       
    36   protected TestHarness th;
       
    37 
       
    38   public void test (TestHarness harness)
       
    39     {
       
    40        th = harness;
       
    41        test_BitSet();
       
    42        test_clone();
       
    43        test_equals();
       
    44        test_hashCode();
       
    45        test_toString();
       
    46        test_and();
       
    47        test_andNot();
       
    48        test_clear();
       
    49        test_get();
       
    50        test_or();
       
    51        test_set();
       
    52        test_xor();
       
    53        test_length();
       
    54 
       
    55      }
       
    56 
       
    57 
       
    58 /**
       
    59 * implemented.
       
    60 *
       
    61 */
       
    62   public void test_BitSet(){
       
    63     th.checkPoint("BitSet()");
       
    64     BitSet bs = new BitSet();
       
    65     boolean ok = true;
       
    66     for (int i=0; i < 64 ; i++)
       
    67     { if (bs.get(i) ) ok =false; }
       
    68     th.check(ok ,"all bits should be 0 -- got:"+bs);
       
    69 
       
    70     th.checkPoint("BitSet(int)");
       
    71     bs = new BitSet(1);
       
    72     ok = true;
       
    73     for (int i=0; i < 64 ; i++)
       
    74     { if (bs.get(i) ) ok =false; }
       
    75     th.check(ok ,"all bits should be 0 -- got:"+bs);
       
    76 
       
    77     try { new BitSet(-1);
       
    78     	  th.fail("should throw NegativeArraySizeException");
       
    79     	}
       
    80     catch(NegativeArraySizeException ne) {th.check(true); }
       
    81 
       
    82   }
       
    83 
       
    84 
       
    85 /**
       
    86 * implemented.
       
    87 *
       
    88 */
       
    89   public void test_clone(){
       
    90     th.checkPoint("clone()java.lang.Object");
       
    91     BitSet bs = new BitSet(3);
       
    92     int i;
       
    93     for (i = 0; i < 64 ; i= i+2) { bs.set(i); }
       
    94     BitSet bsc = (BitSet) bs.clone();
       
    95     boolean ok = true;
       
    96     for (i = 0; i < 64 ; i++)
       
    97     { if (bsc.get(i) !=  (((i % 2) == 0) ? true : false )) ok = false ;}
       
    98     th.check( ok , "all bits should be set" );
       
    99     bs = new BitSet(0);
       
   100     bsc = (BitSet) bs.clone();
       
   101     bs.set(4);
       
   102     th.check( bsc.get(4) == false , "changes in the original don't affect the clone");
       
   103   }
       
   104 
       
   105 
       
   106 /**
       
   107 * implemented.
       
   108 *
       
   109 */
       
   110   public void test_equals(){
       
   111     th.checkPoint("equals(java.lang.Object)boolean");
       
   112     BitSet bs = new BitSet(35);
       
   113     int i;
       
   114     for (i = 0; i < 64 ; i= i+2) { bs.set(i); }
       
   115     th.check( ! bs.equals(null) , "returns false if compared to null" );
       
   116     th.check( ! bs.equals("dsf") , "returns false if compared to another Object" );
       
   117     Vector v = new Vector();
       
   118     for (i = 0; i < 64 ; i= i+2) { v.add(new Integer(1)); v.add(new Integer(0));}
       
   119     th.check( ! bs.equals(v) , "returns false if compared to a vector" );
       
   120     BitSet bsc = (BitSet) bs.clone();
       
   121     th.check(bsc.equals(bs) , "a BitSet equals his clone");
       
   122     bs.set(1);
       
   123     th.check(! bsc.equals(bs) , "one different bit ==> not equal");
       
   124     bs.clear(1);
       
   125     bsc.clear(100);
       
   126     th.check(bsc.equals(bs) , "different size still can be equal -- 1");
       
   127     th.check(bs.equals(bsc) , "different size still can be equal -- 2");
       
   128     bsc.set(127);
       
   129     th.check(!bsc.equals(bs) , "different size don't have to be equal -- 1");
       
   130     th.check(!bs.equals(bsc) , "different size don't have to be equal -- 2");
       
   131 
       
   132   }
       
   133 
       
   134 
       
   135 /**
       
   136 * implemented.	<br>
       
   137 * the hashCode is only depending on the bits set in the BitSet. <br>
       
   138 * this means that two equal bitSets (with different size) still have the same <br>
       
   139 * hashcode
       
   140 */
       
   141   public void test_hashCode(){
       
   142     th.checkPoint("hashCode()int");
       
   143     BitSet bs = new BitSet(34);
       
   144     th.check(bs.hashCode() == 1234 , "checking hashCode for empty BitSet size 64");
       
   145     bs = new BitSet(0);
       
   146     th.check(bs.hashCode() == 1234 , "checking hashCode for empty BitSet size 0");
       
   147     int i;
       
   148     for (i=0 ; i < 8 ; i++) {bs.set(i);}
       
   149     th.check(bs.hashCode() == 1069 , "checking hashCode for  BitSet  FF");
       
   150 
       
   151   }
       
   152 
       
   153 
       
   154 /**
       
   155 * implemented.
       
   156 *
       
   157 */
       
   158   public void test_toString(){
       
   159     th.checkPoint("toString()java.lang.String");
       
   160     BitSet bs = new BitSet();
       
   161     th.check( "{}".equals(bs.toString()) , "check empty BitSet");
       
   162     bs.set(1);
       
   163     th.check( "{1}".equals(bs.toString()) , "check BitSet string representation -- 1");
       
   164     bs.set(60);
       
   165     th.check( "{1, 60}".equals(bs.toString()) , "check BitSet string representation -- 1");
       
   166     bs.set(15);
       
   167     th.check( "{1, 15, 60}".equals(bs.toString()) , "check BitSet string representation -- 1");
       
   168 
       
   169   }
       
   170 
       
   171 
       
   172 /**
       
   173 * implemented.
       
   174 *
       
   175 */
       
   176   public void test_and(){
       
   177     th.checkPoint("and(java.util.BitSet)void");
       
   178     BitSet bs1 = new BitSet();
       
   179     BitSet bs2 = new BitSet();
       
   180     int i;
       
   181     for (i=0 ; i < 64 ; i++ ) { bs2.set(i); }
       
   182     try { bs1.and(null);
       
   183     	  th.fail("Should throw a NullPointerException");
       
   184         }
       
   185     catch( NullPointerException ne) { th.check(true); }
       
   186     bs2.and(bs1);
       
   187     th.check( bs1.equals(bs2) , "all ones anded to zeros give zeros");
       
   188     bs1.set(1); bs2.set(1);
       
   189     bs1.and(bs2);
       
   190     th.check( bs1.get(1) && bs2.equals(bs1), "checking and -- 1");
       
   191     for (i=0 ; i < 64 ; i++ ) { bs2.set(i); bs1.set(i);}
       
   192     for (i=64 ; i < 128 ; i++ ) { bs2.set(i); }
       
   193     bs1.clear(2); bs1.clear(20) ;bs1.clear(25); bs1.clear(28);
       
   194     Object o = bs1.clone();
       
   195     bs1.and(bs2);
       
   196     th.check(bs1.equals(o) , "extra bits from bs2 are unused");
       
   197     bs2.and(bs1);
       
   198     th.check(bs1.equals(bs2) , "extra bits in bs2 are cleared");    	
       
   199   }
       
   200 
       
   201 
       
   202 /**
       
   203 * implemented.	<br>
       
   204 * --> since JDK 1.2
       
   205 */
       
   206   public void test_andNot(){
       
   207     th.checkPoint("andNot(java.util.BitSet)void");
       
   208     BitSet bs1 = new BitSet();
       
   209     BitSet bs2 = new BitSet();
       
   210     int i;
       
   211     for (i=0 ; i < 64 ; i++ ) { bs2.set(i); }
       
   212     try { bs1.and(null);
       
   213     	  th.fail("Should throw a NullPointerException");
       
   214         }
       
   215     catch( NullPointerException ne) { th.check(true); }
       
   216     BitSet bs3 = (BitSet) bs2.clone();
       
   217     bs2.andNot(bs1);
       
   218     th.check( bs3.equals(bs2) , "all ones andnotted to zeros give ones");
       
   219     bs3.andNot(bs2);
       
   220     th.check( !bs3.get(1) && bs3.equals(bs1), "checking andNot -- 1");
       
   221     for (i=0 ; i < 64 ; i++ ) { bs2.set(i); bs1.set(i);}
       
   222     for (i=64 ; i < 128 ; i++ ) { bs2.set(i); }
       
   223     BitSet bs4 = (BitSet)bs2.clone();
       
   224     bs3 = (BitSet)bs1.clone();
       
   225     bs4.xor(bs1);
       
   226     bs1.andNot(bs2);
       
   227     th.check(bs1.equals(new BitSet(64)) , "extra bits from bs2 are unused");
       
   228     bs2.andNot(bs3);
       
   229     th.check(bs4.equals(bs2) , "extra bits in bs2 are not altered");    	
       
   230     bs1.clear(0); bs2.clear(0);
       
   231     bs2.andNot(bs1);
       
   232     th.check(!bs2.get(0) , "checking or -- 1");
       
   233     bs2.set(0);
       
   234     bs2.andNot(bs1);
       
   235     th.check(bs2.get(0) , "checking or -- 2");
       
   236     bs1.set(0);
       
   237     bs2.andNot(bs1);
       
   238     th.check(!bs2.get(0) , "checking or -- 3");
       
   239     bs2.andNot(bs1);
       
   240     th.check(!bs2.get(0) , "checking or -- 4");
       
   241 
       
   242   }
       
   243 
       
   244 
       
   245 /**
       
   246 * implemented.
       
   247 *
       
   248 */
       
   249   public void test_clear(){
       
   250     th.checkPoint("clear(int)void");
       
   251     BitSet bs = new BitSet();
       
   252     Object o = bs.clone();
       
   253     int i;
       
   254     for (i=0 ; i < 64 ; i++ ) { bs.set(i); bs.clear(i);}
       
   255     th.check(bs.equals(o) , "checking set/clear");
       
   256     bs.set(4);
       
   257     th.check( bs.get(4) ,"make sure the set worked" );
       
   258     bs.clear(4);
       
   259     th.check( !bs.get(4) ,"make sure the clear worked -- 1" );
       
   260     bs.clear(4);
       
   261     th.check( !bs.get(4) ,"make sure the clear worked -- 2" );
       
   262     bs.clear(123);
       
   263     try { bs.clear(-1);
       
   264     	  th.fail("should throw an IndexsOutOfBoundsException");
       
   265         }
       
   266     catch(IndexOutOfBoundsException ie) {th.check(true);}
       
   267 
       
   268     bs = new BitSet(0);
       
   269     try { bs.clear(0);
       
   270           bs.clear(64);
       
   271           bs.clear(128);
       
   272           bs.set(146);
       
   273           bs.clear(146);
       
   274           th.check(true);
       
   275         }
       
   276     catch(Exception e) { th.fail("should not throw an exception");}
       
   277   }
       
   278 
       
   279 
       
   280 /**
       
   281 * implemented.
       
   282 *
       
   283 */
       
   284   public void test_get(){
       
   285     th.checkPoint("get(int)boolean");
       
   286     BitSet bs = new BitSet();
       
   287     try { bs.get(-1);
       
   288     	  th.fail("should throw an IndexsOutOfBoundsException");
       
   289         }
       
   290     catch(IndexOutOfBoundsException ie) {th.check(true);}
       
   291     th.check(!bs.get(Integer.MAX_VALUE) , "returns false if pos > size");
       
   292     bs.set(3);
       
   293     th.check(bs.get(3) , "returns true if pos is set");
       
   294     bs.clear(3);
       
   295     th.check(!bs.get(3) , "returns false if pos is cleared");
       
   296     th.check(!bs.get(0) , "returns false if pos is cleared/or not set -- 1");
       
   297     th.check(!bs.get(63) , "returns false if pos is cleared/or not set -- 2");
       
   298   }
       
   299 
       
   300 
       
   301 /**
       
   302 * implemented.
       
   303 *
       
   304 */
       
   305   public void test_or(){
       
   306     th.checkPoint("or(java.util.BitSet)void");
       
   307     BitSet bs1 = new BitSet();
       
   308     BitSet bs2 = new BitSet();
       
   309     int i;
       
   310     for (i=0 ; i < 64 ; i++ ) { bs2.set(i); }
       
   311     try { bs1.or(null);
       
   312     	  th.fail("Should throw a NullPointerException");
       
   313         }
       
   314     catch( NullPointerException ne) { th.check(true); }
       
   315     bs1.or(bs2);
       
   316     th.check( bs1.equals(bs2) , "all ones ored with zeros give ones");
       
   317     for (i=64 ; i < 128 ; i++ ) { bs2.set(i); }
       
   318     BitSet bs3 = new BitSet(3);
       
   319     BitSet bs4 = new BitSet(127);
       
   320     bs3.or(bs2);
       
   321     th.check(!bs1.equals(bs3) , "extra bits from bs2 are used -- got: "+bs3);
       
   322     th.check(bs2.equals(bs3) , "lots of ones ored with nothing gives ones");
       
   323     bs4.or(bs1);
       
   324     th.check(bs4.equals(bs1) , "extra bits in bs4 are left");
       
   325     bs1.clear(0); bs2.clear(0);
       
   326     bs2.or(bs1);
       
   327     th.check(!bs2.get(0) , "checking or -- 1");
       
   328     bs1.set(0);
       
   329     bs2.or(bs1);
       
   330     th.check(bs2.get(0) , "checking or -- 2");
       
   331     bs2.or(bs1);
       
   332     th.check(bs2.get(0) , "checking or -- 3");
       
   333     bs1.clear(0);
       
   334     bs2.or(bs1);
       
   335     th.check(bs2.get(0) , "checking or -- 4");
       
   336   }
       
   337 
       
   338 
       
   339 /**
       
   340 * implemented.	<br>
       
   341 * is tested together with clear
       
   342 */
       
   343   public void test_set(){
       
   344     th.checkPoint("set(int)void");
       
   345     BitSet bs = new BitSet(3);
       
   346     try { bs.set(-1);
       
   347     	  th.fail("should throw an IndexsOutOfBoundsException");
       
   348         }
       
   349     catch(IndexOutOfBoundsException ie) {th.check(true);}
       
   350     bs = new BitSet(0);
       
   351     bs.set(0);
       
   352     bs.set(23);
       
   353   }
       
   354 
       
   355 
       
   356 /**
       
   357 *implemented.
       
   358 *
       
   359 */
       
   360   public void test_xor(){
       
   361     th.checkPoint("xor(java.util.BitSet)void");
       
   362     BitSet bs1 = new BitSet();
       
   363     BitSet bs2 = new BitSet();
       
   364     int i;
       
   365     for (i=0 ; i < 32 ; i++ ) { bs2.set(i); }
       
   366     try { bs1.xor(null);
       
   367     	  th.fail("Should throw a NullPointerException");
       
   368         }
       
   369     catch( NullPointerException ne) { th.check(true); }
       
   370     bs1.xor(bs2);
       
   371     th.check( bs1.equals(bs2) , "checking global xor");
       
   372     for (i=64 ; i < 128 ; i++ ) { bs1.set(i); }
       
   373     BitSet bs3 = new BitSet(3);
       
   374     bs3.xor(bs1);
       
   375     th.check(!bs2.equals(bs3) , "extra bits from bs1 are used -- got: "+bs3);
       
   376     th.check(bs3.equals(bs1) , "lots of ones xored with nothing gives ones");
       
   377     bs1.xor(bs2);
       
   378     boolean ok=true;
       
   379     for (i=0 ; i < 64 ; i++ ) { if (bs1.get(i)) ok = false; }
       
   380     for (i=64 ; i < 128 ; i++ ) { if (!bs1.get(i)) ok = false; }
       
   381     if (!ok) th.debug("got wrong bitpattern:"+bs1);
       
   382     th.check(ok , "extra bits in bs4 are left");
       
   383     bs1.clear(0); bs2.clear(0);
       
   384     bs2.xor(bs1);
       
   385     th.check(!bs2.get(0) , "checking xor -- 1");
       
   386     bs1.set(0);
       
   387     bs2.xor(bs1);
       
   388     th.check(bs2.get(0) , "checking xor -- 2");
       
   389     bs2.xor(bs1);
       
   390     th.check(!bs2.get(0) , "checking xor -- 3");
       
   391     bs2.xor(bs1);
       
   392     th.check(bs2.get(0) , "checking xor -- 4");
       
   393     bs1.clear(0);
       
   394     bs2.xor(bs1);
       
   395     th.check(bs2.get(0) , "checking xor -- 5");
       
   396 
       
   397   }
       
   398 
       
   399 
       
   400 /**
       
   401 * implemented.	<br>
       
   402 * --> since jdk 1.2
       
   403 */
       
   404   public void test_length(){
       
   405     th.checkPoint("length()void");
       
   406     BitSet bs = new BitSet(0);
       
   407     th.check(bs.length()==0);
       
   408     bs.clear(100);
       
   409     th.check(bs.length()==0);
       
   410     bs.set(50);	
       
   411     th.check(bs.length()==51);
       
   412     bs.set(120);
       
   413     th.check(bs.length()==121);
       
   414     bs.set(150);
       
   415     th.check(bs.length()==151);
       
   416     bs.set(150);
       
   417     th.check(bs.length()==151);
       
   418     bs.clear(150);
       
   419     th.check(bs.length()==121);
       
   420     bs.clear(120);
       
   421     th.check(bs.length()==51);
       
   422     bs.clear(50);
       
   423     th.check(bs.length()==0);
       
   424 
       
   425   }
       
   426 
       
   427 
       
   428 }