tests/libjava-mauve/src/gnu/testlet/java/util/AbstractMap/AcuniaAbstractMapTest.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 // Uses: Entry ESet EIterator
       
    23 
       
    24 package gnu.testlet.java.util.AbstractMap;
       
    25 
       
    26 import gnu.testlet.Testlet;
       
    27 import gnu.testlet.TestHarness;
       
    28 
       
    29 import java.util.*;
       
    30 
       
    31 /**
       
    32 *  Written by ACUNIA. <br>
       
    33 *                        <br>
       
    34 *  this file contains test for java.util.AbstractMap   <br>
       
    35 *
       
    36 */
       
    37 public class AcuniaAbstractMapTest extends AbstractMap implements Testlet
       
    38 {
       
    39   protected TestHarness th;
       
    40 
       
    41   public void test (TestHarness harness)
       
    42   {
       
    43        th = harness;
       
    44        test_get();
       
    45        test_containsKey();
       
    46        test_containsValue();
       
    47        test_isEmpty();
       
    48        test_size();
       
    49        test_clear();
       
    50        test_put();
       
    51        test_putAll();
       
    52        test_remove();
       
    53        test_entrySet();
       
    54        test_keySet();
       
    55        test_values();
       
    56        test_equals();
       
    57        test_hashCode();
       
    58        test_toString();
       
    59   }
       
    60 
       
    61   protected AcuniaAbstractMapTest buildHT() {
       
    62    	AcuniaAbstractMapTest t = new AcuniaAbstractMapTest();
       
    63    	String s;
       
    64    	for (int i=0 ; i < 15 ; i++) {
       
    65    	 	s = "a"+i;
       
    66    	 	t.put(s,s+" value");
       
    67    	}
       
    68    	return t;
       
    69   }
       
    70 
       
    71 
       
    72 /**
       
    73 * implemented. <br>
       
    74 *
       
    75 */
       
    76   public void test_get(){
       
    77     th.checkPoint("get(java.lang.Object)java.lang.Object");
       
    78     AcuniaAbstractMapTest ehm = buildHT();
       
    79     Object o;
       
    80     String s="a1";
       
    81     o = ehm.get(s);
       
    82     th.check( (s+" value").equals(o) , "checking return value");
       
    83     o = ehm.get(null);
       
    84     th.check( o == null );
       
    85     o = ehm.get(s+" value");
       
    86     th.check( o == null );
       
    87     ehm.put(null,s);
       
    88     o = ehm.get(null);
       
    89     th.check( s.equals(o));
       
    90 
       
    91   }
       
    92 
       
    93 /**
       
    94 * implemented. <br>
       
    95 *
       
    96 */
       
    97   public void test_containsKey(){
       
    98     th.checkPoint("containsKey(java.lang.Object)boolean");
       
    99     AcuniaAbstractMapTest ehm = buildHT();
       
   100     th.check(!ehm.containsKey(null) , "null not there");
       
   101     ehm.put(null,"test");
       
   102     th.check(ehm.containsKey(null) , "null is in there");
       
   103     th.check(ehm.containsKey("a1") , "object is in there");
       
   104     th.check(!ehm.containsKey("a1 value") , "object is not in there -- 1");
       
   105     th.check(!ehm.containsKey(new Object()) , "object is not in there -- 2");
       
   106 
       
   107   }
       
   108 
       
   109 /**
       
   110 * implemented. <br>
       
   111 *
       
   112 */
       
   113   public void test_containsValue(){
       
   114     th.checkPoint("containsValue(java.lang.Object)boolean");
       
   115     AcuniaAbstractMapTest ehm = buildHT();
       
   116     th.check(!ehm.containsValue(null) , "null not there");
       
   117     ehm.put(null,null);
       
   118     th.check(ehm.containsValue(null) , "null is in there");
       
   119     th.check(!ehm.containsValue("a1") , "object is not in there -- 1");
       
   120     th.check(ehm.containsValue("a1 value") , "object is in there -- 1");
       
   121     th.check(!ehm.containsValue(new Object()) , "object is not in there -- 2");
       
   122 
       
   123   }
       
   124 
       
   125 /**
       
   126 * implemented. <br>
       
   127 *
       
   128 */
       
   129   public void test_isEmpty(){
       
   130     th.checkPoint("isEmpty()boolean");
       
   131     AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
       
   132     th.check(ehm.isEmpty() , "true");
       
   133     ehm = buildHT();
       
   134     th.check(!ehm.isEmpty() , "false");
       
   135 
       
   136   }
       
   137 
       
   138 /**
       
   139 *  not implemented. <br>
       
   140 *  Abstract Method
       
   141 */
       
   142   public void test_size(){
       
   143     th.checkPoint("()");
       
   144 
       
   145   }
       
   146 
       
   147 /**
       
   148 * implemented. <br>
       
   149 *
       
   150 */
       
   151   public void test_clear(){
       
   152     th.checkPoint("clear()void");
       
   153     AcuniaAbstractMapTest ehm = buildHT();
       
   154     ehm.clear();
       
   155     th.check(ehm.isEmpty() , "true");
       
   156   }
       
   157 
       
   158 /**
       
   159 * implemented. <br>
       
   160 *
       
   161 */
       
   162   public void test_put(){
       
   163     th.checkPoint("put(java.lang.Object,java.lang.Object)java.lang.Object");
       
   164     AcuniaAbstractMapTest ehm = buildHT();
       
   165     ehm.set_edit(false);
       
   166     try {
       
   167     	ehm.put("a","b");
       
   168     	th.fail("should throw an UnsupportedOperationException");
       
   169     }
       
   170     catch (UnsupportedOperationException uoe) { th.check(true); }		
       
   171   }
       
   172 
       
   173 /**
       
   174 * implemented. <br>
       
   175 *
       
   176 */
       
   177   public void test_putAll(){
       
   178     th.checkPoint("putAll(java.util.Map)void");
       
   179     Hashtable ht = new Hashtable();
       
   180     AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
       
   181     th.check( ehm.equals(ht) , "true -- both empty");
       
   182     ht.put("a","b");	ht.put("c","d");	ht.put("e","f");
       
   183     ehm.putAll(ht);
       
   184     th.check( ehm.equals(ht) , "true -- 1");
       
   185     ht.put("a1","f");
       
   186     ht.put("e","b");
       
   187     ehm.putAll(ht);
       
   188     th.check( ehm.equals(ht) , "true -- 2");
       
   189     ehm = buildHT();
       
   190     try {
       
   191       ehm.putAll(ht);
       
   192       th.check(true, "putAll: " + ht);
       
   193     }
       
   194     catch (NoSuchElementException nse) { th.check(false, "putAll: " + ht); }
       
   195     th.check(ehm.size() == 18 , "added three elements");
       
   196     th.check("f".equals(ehm.get("a1")) , "overwritten old value");
       
   197   }
       
   198 
       
   199 /**
       
   200 * implemented. <br>
       
   201 *
       
   202 */
       
   203   public void test_remove(){
       
   204     th.checkPoint("remove(java.lang.Object)java.lang.Object");
       
   205     AcuniaAbstractMapTest ehm = buildHT();
       
   206     ehm.remove("a1");
       
   207     th.check(!ehm.containsKey("a1") , "key removed -- 1");
       
   208     th.check(!ehm.containsValue("a1 value") , "value removed -- 1");
       
   209     ehm.remove("a0");
       
   210     th.check(!ehm.containsKey("a0") , "key removed -- 2");
       
   211     th.check(!ehm.containsValue("a0 value") , "value removed -- 2");
       
   212     for (int i=2 ; i < 15 ; i++ ) {
       
   213 	    ehm.remove("a"+i);
       
   214     }
       
   215     th.check(ehm.isEmpty());
       
   216   }
       
   217 
       
   218 /**
       
   219 *   not implemented. <br>
       
   220 *   Abstract Method
       
   221 */
       
   222   public void test_entrySet(){
       
   223     th.checkPoint("()");
       
   224 
       
   225   }
       
   226 
       
   227 /**
       
   228 * implemented. <br>
       
   229 * check only on methods not inherited from AbstractSet
       
   230 */
       
   231   public void test_keySet(){
       
   232     th.checkPoint("keySet()java.util.Set");
       
   233     AcuniaAbstractMapTest ehm = buildHT();
       
   234     Set s = ehm.keySet();
       
   235     th.check(s.size() == 15);
       
   236     ehm.put(null,"test");
       
   237     th.check(s.size() == 16);
       
   238     th.check(s.contains("a1"),"does contain a1");
       
   239     th.check(s.contains(null),"does contain null");
       
   240     th.check(!s.contains(new Object()),"does contain new Object");
       
   241     th.check(!s.contains("test"),"does contain test");
       
   242     th.check( s == ehm.keySet() , "same Set is returned");
       
   243     Iterator it = s.iterator();
       
   244     Vector v = ehm.getKeyV();
       
   245     int i;
       
   246     Object o;
       
   247     for (i=0 ; i < 16 ; i++) {
       
   248     	o = it.next();
       
   249     	th.check(v.indexOf(o) == 0, "order is not respected");
       
   250     	if (!v.remove(o)) th.debug("didn't find "+o);
       
   251      	
       
   252     }
       
   253     it = s.iterator();
       
   254     while (it.hasNext()) {
       
   255      	it.next();
       
   256      	it.remove();
       
   257     }
       
   258     th.check(s.isEmpty(), "everything is removed");
       
   259     s = ehm.keySet();
       
   260     th.check(s.isEmpty(), "new Set is also empty");
       
   261     ehm.put("a","B");
       
   262     th.check(!s.isEmpty(), "Set is updated by underlying actions");
       
   263   }
       
   264 
       
   265 /**
       
   266 * implemented. <br>
       
   267 * check only on methods not inherited from AbstractCollection
       
   268 */
       
   269   public void test_values(){
       
   270     th.checkPoint("values()java.util.Collection");
       
   271     AcuniaAbstractMapTest ehm = buildHT();
       
   272     Collection s = ehm.values();
       
   273     th.check(s.size() == 15);
       
   274     ehm.put(null,"test");
       
   275     ehm.put("a10",null);
       
   276     th.check(s.size() == 16);
       
   277     th.check(s.contains("a1 value"),"does contain a1 value");
       
   278     th.check(s.contains(null),"does contain null");
       
   279     th.check(!s.contains(new Object()),"does contain new Object");
       
   280     th.check(s.contains("test"),"does contain test");
       
   281     th.check(!s.contains("a1"),"does not contain a1");
       
   282     th.check( s == ehm.values() , "same Set is returned");
       
   283     Iterator it = s.iterator();
       
   284     Vector v = ehm.getValuesV();
       
   285     int i;
       
   286     Object o;
       
   287     for (i=0 ; i < 16 ; i++) {
       
   288     	o = it.next();
       
   289     	th.check(v.indexOf(o) == 0, "order is not respected");
       
   290     	if (!v.remove(o)) th.debug("didn't find "+o);
       
   291      	
       
   292     }
       
   293     it = s.iterator();
       
   294     while (it.hasNext()) {
       
   295      	it.next();
       
   296      	it.remove();
       
   297     }
       
   298     th.check(s.isEmpty(), "everything is removed");
       
   299     s = ehm.values();
       
   300     th.check(s.isEmpty(), "new Set is also empty");
       
   301     ehm.put("a","B");
       
   302     th.check(!s.isEmpty(), "Set is updated by underlying actions");
       
   303 
       
   304   }
       
   305 
       
   306 /**
       
   307 * implemented. <br>
       
   308 *
       
   309 */
       
   310   public void test_equals(){
       
   311     th.checkPoint("equals(java.lang.Object)boolean");
       
   312     Hashtable ht = new Hashtable();
       
   313     AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
       
   314     th.check( ehm.equals(ht) , "true -- both empty");
       
   315     ht.put("a","b");	ht.put("c","d");	ht.put("e","f");
       
   316     ehm.put("a","b");	ehm.put("c","d");	ehm.put("e","f");
       
   317     th.check( ehm.equals(ht) , "true -- same key && values");
       
   318     ht.put("a","f");
       
   319     th.check(! ehm.equals(ht) , "false -- same key && diff values");
       
   320     ht.put("e","b");
       
   321     th.check(! ehm.equals(ht) , "false --  key with diff values");
       
   322     th.check(! ehm.equals(ht.entrySet()) , "false --  no Map");
       
   323     th.check(! ehm.equals(new Object()) , "false -- Object is no Map");
       
   324     th.check(! ehm.equals(null) , "false -- Object is null");
       
   325 
       
   326 
       
   327 
       
   328   }
       
   329 
       
   330 /**
       
   331 * implemented. <br>
       
   332 *
       
   333 */
       
   334   public void test_hashCode(){
       
   335     th.checkPoint("hashCode()int");
       
   336     AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
       
   337     th.check( ehm.hashCode() == 0 , "hashCode of Empty Map is 0, got "+ehm.hashCode());
       
   338     int hash = 0;
       
   339     Iterator s = ehm.entrySet().iterator();
       
   340     while (s.hasNext()) { hash += s.next().hashCode(); }
       
   341     th.check( ehm.hashCode() , hash , "hashCode of Empty Map -- checking Algorithm");
       
   342 
       
   343   }
       
   344 
       
   345 /**
       
   346 * implemented. <br>
       
   347 *
       
   348 */
       
   349   public void test_toString(){
       
   350     th.checkPoint("toString()java.lang.String");
       
   351     AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
       
   352     th.check("{}".equals(ehm.toString()) , "checking empty Map");
       
   353     ehm.put("a","b");	
       
   354     th.debug(ehm.toString());
       
   355     th.check("{a=b}".equals(ehm.toString()) , "checking Map with one element");
       
   356     ehm.put("c","d");	ehm.put("e","f");
       
   357     th.debug(ehm.toString());
       
   358     th.check("{a=b, c=d, e=f}".equals(ehm.toString()) , "checking Map with three elements");
       
   359   }
       
   360 
       
   361   public String toString() {
       
   362 	  return super.toString();
       
   363   }
       
   364 
       
   365 // The following field and methods are needed to use this class as an
       
   366 // implementation test for AbstractMap
       
   367 //
       
   368 	Vector keys = new Vector();
       
   369 	Vector values = new Vector();
       
   370 	private boolean edit = true;
       
   371 	
       
   372 	boolean deleteInAM(Object e) {
       
   373 	 	if  (!keys.contains(e)) return false;
       
   374 	 	values.remove(keys.indexOf(e));
       
   375 	 	return keys.remove(e);
       
   376 	}
       
   377 	
       
   378 	public Vector getKeyV() {
       
   379 		return (Vector)keys.clone();
       
   380 	}
       
   381 	public Vector getValuesV() {
       
   382 		return (Vector)values.clone();
       
   383 	}
       
   384 	
       
   385 	public AcuniaAbstractMapTest(){
       
   386 		super();
       
   387 	}
       
   388 	
       
   389 	public Set entrySet() {
       
   390 		return  new ESet(this);
       
   391 	}
       
   392 
       
   393 	public Object put(Object key, Object value) {
       
   394 		if (edit) {
       
   395 			if (keys.contains(key)) {
       
   396 				return values.set(keys.indexOf(key),value);
       
   397 			}
       
   398 			values.add(value);
       
   399 			keys.add(key);
       
   400 			return null;
       
   401 		}
       
   402 		return super.put(key,value);
       
   403 	}
       
   404 	
       
   405 	public void set_edit(boolean b) {
       
   406 		edit = b;
       
   407 	}
       
   408 }