File: AcuniaAbstractMapTest.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (408 lines) | stat: -rw-r--r-- 10,934 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* Copyright (C) 2001 ACUNIA

   This file is part of Mauve.

   Mauve is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   Mauve is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Mauve; see the file COPYING.  If not, write to
   the Free Software Foundation, 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

// Tags: JDK1.2
// Uses: Entry ESet EIterator

package gnu.testlet.java.util.AbstractMap;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;

import java.util.*;

/**
*  Written by ACUNIA. <br>
*                        <br>
*  this file contains test for java.util.AbstractMap   <br>
*
*/
public class AcuniaAbstractMapTest extends AbstractMap implements Testlet
{
  protected TestHarness th;

  public void test (TestHarness harness)
  {
       th = harness;
       test_get();
       test_containsKey();
       test_containsValue();
       test_isEmpty();
       test_size();
       test_clear();
       test_put();
       test_putAll();
       test_remove();
       test_entrySet();
       test_keySet();
       test_values();
       test_equals();
       test_hashCode();
       test_toString();
  }

  protected AcuniaAbstractMapTest buildHT() {
   	AcuniaAbstractMapTest t = new AcuniaAbstractMapTest();
   	String s;
   	for (int i=0 ; i < 15 ; i++) {
   	 	s = "a"+i;
   	 	t.put(s,s+" value");
   	}
   	return t;
  }


/**
* implemented. <br>
*
*/
  public void test_get(){
    th.checkPoint("get(java.lang.Object)java.lang.Object");
    AcuniaAbstractMapTest ehm = buildHT();
    Object o;
    String s="a1";
    o = ehm.get(s);
    th.check( (s+" value").equals(o) , "checking return value");
    o = ehm.get(null);
    th.check( o == null );
    o = ehm.get(s+" value");
    th.check( o == null );
    ehm.put(null,s);
    o = ehm.get(null);
    th.check( s.equals(o));

  }

/**
* implemented. <br>
*
*/
  public void test_containsKey(){
    th.checkPoint("containsKey(java.lang.Object)boolean");
    AcuniaAbstractMapTest ehm = buildHT();
    th.check(!ehm.containsKey(null) , "null not there");
    ehm.put(null,"test");
    th.check(ehm.containsKey(null) , "null is in there");
    th.check(ehm.containsKey("a1") , "object is in there");
    th.check(!ehm.containsKey("a1 value") , "object is not in there -- 1");
    th.check(!ehm.containsKey(new Object()) , "object is not in there -- 2");

  }

/**
* implemented. <br>
*
*/
  public void test_containsValue(){
    th.checkPoint("containsValue(java.lang.Object)boolean");
    AcuniaAbstractMapTest ehm = buildHT();
    th.check(!ehm.containsValue(null) , "null not there");
    ehm.put(null,null);
    th.check(ehm.containsValue(null) , "null is in there");
    th.check(!ehm.containsValue("a1") , "object is not in there -- 1");
    th.check(ehm.containsValue("a1 value") , "object is in there -- 1");
    th.check(!ehm.containsValue(new Object()) , "object is not in there -- 2");

  }

/**
* implemented. <br>
*
*/
  public void test_isEmpty(){
    th.checkPoint("isEmpty()boolean");
    AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
    th.check(ehm.isEmpty() , "true");
    ehm = buildHT();
    th.check(!ehm.isEmpty() , "false");

  }

/**
*  not implemented. <br>
*  Abstract Method
*/
  public void test_size(){
    th.checkPoint("()");

  }

/**
* implemented. <br>
*
*/
  public void test_clear(){
    th.checkPoint("clear()void");
    AcuniaAbstractMapTest ehm = buildHT();
    ehm.clear();
    th.check(ehm.isEmpty() , "true");
  }

/**
* implemented. <br>
*
*/
  public void test_put(){
    th.checkPoint("put(java.lang.Object,java.lang.Object)java.lang.Object");
    AcuniaAbstractMapTest ehm = buildHT();
    ehm.set_edit(false);
    try {
    	ehm.put("a","b");
    	th.fail("should throw an UnsupportedOperationException");
    }
    catch (UnsupportedOperationException uoe) { th.check(true); }		
  }

/**
* implemented. <br>
*
*/
  public void test_putAll(){
    th.checkPoint("putAll(java.util.Map)void");
    Hashtable ht = new Hashtable();
    AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
    th.check( ehm.equals(ht) , "true -- both empty");
    ht.put("a","b");	ht.put("c","d");	ht.put("e","f");
    ehm.putAll(ht);
    th.check( ehm.equals(ht) , "true -- 1");
    ht.put("a1","f");
    ht.put("e","b");
    ehm.putAll(ht);
    th.check( ehm.equals(ht) , "true -- 2");
    ehm = buildHT();
    try {
      ehm.putAll(ht);
      th.check(true, "putAll: " + ht);
    }
    catch (NoSuchElementException nse) { th.check(false, "putAll: " + ht); }
    th.check(ehm.size() == 18 , "added three elements");
    th.check("f".equals(ehm.get("a1")) , "overwritten old value");
  }

/**
* implemented. <br>
*
*/
  public void test_remove(){
    th.checkPoint("remove(java.lang.Object)java.lang.Object");
    AcuniaAbstractMapTest ehm = buildHT();
    ehm.remove("a1");
    th.check(!ehm.containsKey("a1") , "key removed -- 1");
    th.check(!ehm.containsValue("a1 value") , "value removed -- 1");
    ehm.remove("a0");
    th.check(!ehm.containsKey("a0") , "key removed -- 2");
    th.check(!ehm.containsValue("a0 value") , "value removed -- 2");
    for (int i=2 ; i < 15 ; i++ ) {
	    ehm.remove("a"+i);
    }
    th.check(ehm.isEmpty());
  }

/**
*   not implemented. <br>
*   Abstract Method
*/
  public void test_entrySet(){
    th.checkPoint("()");

  }

/**
* implemented. <br>
* check only on methods not inherited from AbstractSet
*/
  public void test_keySet(){
    th.checkPoint("keySet()java.util.Set");
    AcuniaAbstractMapTest ehm = buildHT();
    Set s = ehm.keySet();
    th.check(s.size() == 15);
    ehm.put(null,"test");
    th.check(s.size() == 16);
    th.check(s.contains("a1"),"does contain a1");
    th.check(s.contains(null),"does contain null");
    th.check(!s.contains(new Object()),"does contain new Object");
    th.check(!s.contains("test"),"does contain test");
    th.check( s == ehm.keySet() , "same Set is returned");
    Iterator it = s.iterator();
    Vector v = ehm.getKeyV();
    int i;
    Object o;
    for (i=0 ; i < 16 ; i++) {
    	o = it.next();
    	th.check(v.indexOf(o) == 0, "order is not respected");
    	if (!v.remove(o)) th.debug("didn't find "+o);
     	
    }
    it = s.iterator();
    while (it.hasNext()) {
     	it.next();
     	it.remove();
    }
    th.check(s.isEmpty(), "everything is removed");
    s = ehm.keySet();
    th.check(s.isEmpty(), "new Set is also empty");
    ehm.put("a","B");
    th.check(!s.isEmpty(), "Set is updated by underlying actions");
  }

/**
* implemented. <br>
* check only on methods not inherited from AbstractCollection
*/
  public void test_values(){
    th.checkPoint("values()java.util.Collection");
    AcuniaAbstractMapTest ehm = buildHT();
    Collection s = ehm.values();
    th.check(s.size() == 15);
    ehm.put(null,"test");
    ehm.put("a10",null);
    th.check(s.size() == 16);
    th.check(s.contains("a1 value"),"does contain a1 value");
    th.check(s.contains(null),"does contain null");
    th.check(!s.contains(new Object()),"does contain new Object");
    th.check(s.contains("test"),"does contain test");
    th.check(!s.contains("a1"),"does not contain a1");
    th.check( s == ehm.values() , "same Set is returned");
    Iterator it = s.iterator();
    Vector v = ehm.getValuesV();
    int i;
    Object o;
    for (i=0 ; i < 16 ; i++) {
    	o = it.next();
    	th.check(v.indexOf(o) == 0, "order is not respected");
    	if (!v.remove(o)) th.debug("didn't find "+o);
     	
    }
    it = s.iterator();
    while (it.hasNext()) {
     	it.next();
     	it.remove();
    }
    th.check(s.isEmpty(), "everything is removed");
    s = ehm.values();
    th.check(s.isEmpty(), "new Set is also empty");
    ehm.put("a","B");
    th.check(!s.isEmpty(), "Set is updated by underlying actions");

  }

/**
* implemented. <br>
*
*/
  public void test_equals(){
    th.checkPoint("equals(java.lang.Object)boolean");
    Hashtable ht = new Hashtable();
    AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
    th.check( ehm.equals(ht) , "true -- both empty");
    ht.put("a","b");	ht.put("c","d");	ht.put("e","f");
    ehm.put("a","b");	ehm.put("c","d");	ehm.put("e","f");
    th.check( ehm.equals(ht) , "true -- same key && values");
    ht.put("a","f");
    th.check(! ehm.equals(ht) , "false -- same key && diff values");
    ht.put("e","b");
    th.check(! ehm.equals(ht) , "false --  key with diff values");
    th.check(! ehm.equals(ht.entrySet()) , "false --  no Map");
    th.check(! ehm.equals(new Object()) , "false -- Object is no Map");
    th.check(! ehm.equals(null) , "false -- Object is null");



  }

/**
* implemented. <br>
*
*/
  public void test_hashCode(){
    th.checkPoint("hashCode()int");
    AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
    th.check( ehm.hashCode() == 0 , "hashCode of Empty Map is 0, got "+ehm.hashCode());
    int hash = 0;
    Iterator s = ehm.entrySet().iterator();
    while (s.hasNext()) { hash += s.next().hashCode(); }
    th.check( ehm.hashCode() , hash , "hashCode of Empty Map -- checking Algorithm");

  }

/**
* implemented. <br>
*
*/
  public void test_toString(){
    th.checkPoint("toString()java.lang.String");
    AcuniaAbstractMapTest ehm = new AcuniaAbstractMapTest();
    th.check("{}".equals(ehm.toString()) , "checking empty Map");
    ehm.put("a","b");	
    th.debug(ehm.toString());
    th.check("{a=b}".equals(ehm.toString()) , "checking Map with one element");
    ehm.put("c","d");	ehm.put("e","f");
    th.debug(ehm.toString());
    th.check("{a=b, c=d, e=f}".equals(ehm.toString()) , "checking Map with three elements");
  }

  public String toString() {
	  return super.toString();
  }

// The following field and methods are needed to use this class as an
// implementation test for AbstractMap
//
	Vector keys = new Vector();
	Vector values = new Vector();
	private boolean edit = true;
	
	boolean deleteInAM(Object e) {
	 	if  (!keys.contains(e)) return false;
	 	values.remove(keys.indexOf(e));
	 	return keys.remove(e);
	}
	
	public Vector getKeyV() {
		return (Vector)keys.clone();
	}
	public Vector getValuesV() {
		return (Vector)values.clone();
	}
	
	public AcuniaAbstractMapTest(){
		super();
	}
	
	public Set entrySet() {
		return  new ESet(this);
	}

	public Object put(Object key, Object value) {
		if (edit) {
			if (keys.contains(key)) {
				return values.set(keys.indexOf(key),value);
			}
			values.add(value);
			keys.add(key);
			return null;
		}
		return super.put(key,value);
	}
	
	public void set_edit(boolean b) {
		edit = b;
	}
}