001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2007 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution;
010:
011: import j2me.lang.CharSequence;
012: import j2me.util.ArrayList;
013: import j2me.util.Iterator;
014: import j2me.util.Collection;
015: import j2me.util.Map;
016: import javolution.lang.Configurable;
017: import javolution.testing.TestCase;
018: import javolution.testing.TestContext;
019: import javolution.testing.TestSuite;
020: import javolution.text.TextBuilder;
021: import javolution.util.FastList;
022: import javolution.util.FastMap;
023: import javolution.util.FastSet;
024: import javolution.util.FastTable;
025: import javolution.util.Index;
026: import javolution.util.FastMap.Entry;
027: import javolution.util.FastList.Node;
028:
029: /**
030: * <p> This class holds {@link javolution.util} benchmark.</p>
031: *
032: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
033: * @version 5.2, August 19, 2007
034: */
035: public final class UtilTestSuite extends TestSuite {
036:
037: public static final Configurable/*<Integer>*/SIZE = new Configurable(
038: new Integer(256));
039:
040: private static final int PADDING = 60;
041:
042: public void run() {
043: int size = ((Integer) SIZE.get()).intValue();
044: Index.setMinimumRange(0, size); // Preallocates indexes.
045:
046: TestContext
047: .info("----------------------------------------------");
048: TestContext
049: .info("-- Test Suite for javolution.util.* classes --");
050: TestContext
051: .info("----------------------------------------------");
052: TestContext
053: .info("Collections/Maps of "
054: + size
055: + " elements (configurable \"javolution.UtilTestSuite#SIZE\")");
056: TestContext.info("");
057:
058: TestContext.info(" - Add elements to collection -");
059: boolean reuse = false;
060: TestContext
061: .test(new CollectionAdd(FastTable.class, size, reuse));
062: /*@JVM-1.4+@ TestContext.test(new CollectionAdd(java.util.ArrayList.class, size, reuse)); /**/
063: TestContext
064: .test(new CollectionAdd(FastList.class, size, reuse));
065: /*@JVM-1.4+@ TestContext.test(new CollectionAdd(java.util.LinkedList.class, size, reuse)); /**/
066: TestContext.test(new CollectionAdd(FastSet.class, size, reuse));
067: /*@JVM-1.4+@ TestContext.test(new CollectionAdd(java.util.HashSet.class, size, reuse)); /**/
068: reuse = true;
069: TestContext
070: .test(new CollectionAdd(FastTable.class, size, reuse));
071: /*@JVM-1.4+@ TestContext.test(new CollectionAdd(java.util.ArrayList.class, size, reuse)); /**/
072: TestContext
073: .test(new CollectionAdd(FastList.class, size, reuse));
074: /*@JVM-1.4+@ TestContext.test(new CollectionAdd(java.util.LinkedList.class, size, reuse)); /**/
075: TestContext.test(new CollectionAdd(FastSet.class, size, reuse));
076: /*@JVM-1.4+@ TestContext.test(new CollectionAdd(java.util.HashSet.class, size, reuse)); /**/
077: TestContext.info("");
078:
079: TestContext.info(" - Iterate over collections -");
080: TestContext
081: .test(new CollectionIteration(FastTable.class, size));
082: /*@JVM-1.4+@ TestContext.test(new CollectionIteration(java.util.ArrayList.class, size)); /**/
083: TestContext.test(new CollectionIteration(FastList.class, size));
084: /*@JVM-1.4+@ TestContext.test(new CollectionIteration(java.util.LinkedList.class, size)); /**/
085: TestContext.test(new CollectionIteration(FastSet.class, size));
086: /*@JVM-1.4+@ TestContext.test(new CollectionIteration(java.util.HashSet.class, size)); /**/
087: TestContext.info("");
088:
089: TestContext
090: .info(" - Put new key/value pairs to map instance -");
091: reuse = false;
092: TestContext.test(new MapPut(FastMap.class, size, reuse));
093: /*@JVM-1.4+@ TestContext.test(new MapPut(java.util.HashMap.class, size, reuse)); /**/
094: /*@JVM-1.4+@ TestContext.test(new MapPut(java.util.LinkedHashMap.class, size, reuse)); /**/
095: TestContext.test(new MapPut(SharedFastMap.class, size, reuse));
096: /*@JVM-1.5+@ TestContext.test(new MapPut(java.util.concurrent.ConcurrentHashMap.class, size, reuse)); /**/
097: reuse = true;
098: TestContext.test(new MapPut(FastMap.class, size, reuse));
099: /*@JVM-1.4+@ TestContext.test(new MapPut(java.util.HashMap.class, size, reuse)); /**/
100: /*@JVM-1.4+@ TestContext.test(new MapPut(java.util.LinkedHashMap.class, size, reuse)); /**/
101: TestContext.info("");
102:
103: TestContext.info(" - Retrieves map value from key - ");
104: TestContext.test(new MapGet(FastMap.class, size));
105: /*@JVM-1.4+@ TestContext.test(new MapGet(java.util.HashMap.class, size)); /**/
106: /*@JVM-1.4+@ TestContext.test(new MapGet(java.util.LinkedHashMap.class, size)); /**/
107: TestContext.test(new MapGet(SharedFastMap.class, size));
108: /*@JVM-1.5+@ TestContext.test(new MapGet(java.util.concurrent.ConcurrentHashMap.class, size)); /**/
109: TestContext.info("");
110:
111: TestContext.info(" - Removes keys from map - ");
112: TestContext.test(new MapRemove(FastMap.class, size));
113: /*@JVM-1.4+@ TestContext.test(new MapRemove(java.util.HashMap.class, size)); /**/
114: /*@JVM-1.4+@ TestContext.test(new MapRemove(java.util.LinkedHashMap.class, size)); /**/
115: TestContext.test(new MapRemove(SharedFastMap.class, size));
116: /*@JVM-1.5+@ TestContext.test(new MapRemove(java.util.concurrent.ConcurrentHashMap.class, size)); /**/
117: TestContext.info("");
118:
119: TestContext.info(" - Iterates over map entries - ");
120: TestContext.test(new MapIteration(FastMap.class, size));
121: /*@JVM-1.4+@ TestContext.test(new MapIteration(java.util.HashMap.class, size)); /**/
122: /*@JVM-1.4+@ TestContext.test(new MapIteration(java.util.LinkedHashMap.class, size)); /**/
123: TestContext.test(new MapIteration(SharedFastMap.class, size));
124: /*@JVM-1.5+@ TestContext.test(new MapIteration(java.util.concurrent.ConcurrentHashMap.class, size)); /**/
125: TestContext.info("");
126:
127: TestContext
128: .info(" - Direct collection/map iterations (no iterator) - ");
129: TestContext.test(new FastTableDirectIteration(size));
130: TestContext.test(new ArrayListDirectIteration(size));
131: TestContext.test(new FastListDirectIteration(size));
132: TestContext.test(new FastMapDirectIteration(size));
133: TestContext.info("");
134:
135: }
136:
137: public static class CollectionAdd extends TestCase {
138:
139: private final Class _class;
140:
141: private final int _size;
142:
143: private final boolean _reuse;
144:
145: private Collection _collection;
146:
147: public CollectionAdd(Class clazz, int size, boolean reuse) {
148: _class = clazz;
149: _size = size;
150: _reuse = reuse;
151: }
152:
153: public CharSequence getDescription() {
154: return TextBuilder.newInstance().append(
155: _reuse ? "Recycled " : "New ").append(
156: _class.getName()).append(".add(element)").toText()
157: .padRight(PADDING);
158: }
159:
160: public void prepare() {
161: if (_reuse && _collection != null) {
162: _collection.clear();
163: } else {
164: _collection = (Collection) UtilTestSuite
165: .newInstanceOf(_class);
166: }
167: }
168:
169: public void execute() {
170: for (int i = 0; i < _size;) {
171: _collection.add(Index.valueOf(i++));
172: }
173: }
174:
175: public int count() {
176: return _size;
177: }
178:
179: public void validate() {
180: TestContext.assertTrue(_collection.size() == _size);
181: for (int i = 0; i < _size;) {
182: if (!TestContext.assertTrue(_collection.contains(Index
183: .valueOf(i++))))
184: break;
185: }
186: }
187: }
188:
189: public static class CollectionIteration extends TestCase {
190:
191: private final Class _class;
192:
193: private final int _size;
194:
195: private final Collection _collection;
196:
197: private int _count;
198:
199: public CollectionIteration(Class clazz, int size) {
200: _class = clazz;
201: _size = size;
202: _collection = (Collection) UtilTestSuite
203: .newInstanceOf(clazz);
204: for (int i = 0; i < _size;) {
205: _collection.add(Index.valueOf(i++));
206: }
207: }
208:
209: public CharSequence getDescription() {
210: return TextBuilder.newInstance().append("Iterates over ")
211: .append(_class.getName()).toText()
212: .padRight(PADDING);
213: }
214:
215: public void execute() {
216: _count = 0;
217: Iterator i = _collection.iterator();
218: while (i.hasNext()) {
219: i.next();
220: _count++;
221: }
222: }
223:
224: public int count() {
225: return _count;
226: }
227:
228: public void validate() {
229: TestContext.assertTrue(_count == _size);
230: }
231: }
232:
233: public static class MapPut extends TestCase {
234:
235: private final Class _class;
236:
237: private final int _size;
238:
239: private final boolean _reuse;
240:
241: private Map _map;
242:
243: public MapPut(Class clazz, int size, boolean reuse) {
244: _class = clazz;
245: _size = size;
246: _reuse = reuse;
247: }
248:
249: public CharSequence getDescription() {
250: return TextBuilder.newInstance().append(
251: _reuse ? "Recycled " : "New ").append(
252: _class.getName()).append(".put(key, value)")
253: .toText().padRight(PADDING);
254: }
255:
256: public void prepare() {
257: if (_reuse && _map != null) {
258: _map.clear();
259: } else {
260: _map = (Map) UtilTestSuite.newInstanceOf(_class);
261: }
262: }
263:
264: public void execute() {
265: for (int i = 0; i < _size;) {
266: _map.put(Index.valueOf(i++), "");
267: }
268: }
269:
270: public int count() {
271: return _size;
272: }
273:
274: public void validate() {
275: TestContext.assertTrue(_map.size() == _size);
276: for (int i = 0; i < _size;) {
277: if (!TestContext.assertTrue(_map.containsKey(Index
278: .valueOf(i++))))
279: break;
280: }
281: }
282: }
283:
284: public static class MapGet extends TestCase {
285:
286: private final Class _class;
287:
288: private final int _size;
289:
290: private Map _map;
291:
292: private Object _last;
293:
294: public MapGet(Class clazz, int size) {
295: _class = clazz;
296: _size = size;
297: _map = (Map) UtilTestSuite.newInstanceOf(clazz);
298: for (int i = 0; i < _size;) {
299: _map.put(Index.valueOf(i), Index.valueOf(++i));
300: }
301: }
302:
303: public CharSequence getDescription() {
304: return TextBuilder.newInstance().append(_class.getName())
305: .append(".get(key):").toText().padRight(PADDING);
306: }
307:
308: public void execute() {
309: for (int i = 0; i < _size;) {
310: _last = _map.get(Index.valueOf(i++));
311: }
312: }
313:
314: public int count() {
315: return _size;
316: }
317:
318: public void validate() {
319: for (int i = 0; i < _size; i++) {
320: if (!TestContext.assertEquals(Index.valueOf(i + 1),
321: _map.get(Index.valueOf(i))))
322: break;
323: }
324: TestContext.assertEquals(Index.valueOf(_size), _last);
325: }
326:
327: }
328:
329: public static class MapRemove extends TestCase {
330:
331: private final Class _class;
332:
333: private final int _size;
334:
335: private Map _map;
336:
337: private Object _last;
338:
339: public MapRemove(Class clazz, int size) {
340: _class = clazz;
341: _size = size;
342: _map = (Map) UtilTestSuite.newInstanceOf(clazz);
343: }
344:
345: public CharSequence getDescription() {
346: return TextBuilder.newInstance().append(_class.getName())
347: .append(".remove(key):").toText().padRight(PADDING);
348: }
349:
350: public void prepare() {
351: for (int i = 0; i < _size;) {
352: _map.put(Index.valueOf(i), Index.valueOf(++i));
353: }
354: }
355:
356: public void execute() {
357: for (int i = 0; i < _size;) {
358: _last = _map.remove(Index.valueOf(i++));
359: }
360: }
361:
362: public int count() {
363: return _size;
364: }
365:
366: public void validate() {
367: TestContext.assertEquals(Index.valueOf(_size), _last);
368: TestContext.assertTrue(_map.size() == 0);
369: }
370:
371: }
372:
373: public static class MapIteration extends TestCase {
374:
375: private final Class _class;
376:
377: private final int _size;
378:
379: private final Map _map;
380:
381: private Object _last;
382:
383: public MapIteration(Class clazz, int size) {
384: _class = clazz;
385: _size = size;
386: _map = (Map) UtilTestSuite.newInstanceOf(clazz);
387: for (int i = 0; i < _size;) {
388: _map.put(Index.valueOf(i), Index.valueOf(i++));
389: }
390: }
391:
392: public CharSequence getDescription() {
393: return TextBuilder.newInstance().append("Iterates over ")
394: .append(_class.getName()).toText()
395: .padRight(PADDING);
396: }
397:
398: public void execute() {
399: Iterator i = _map.entrySet().iterator();
400: while (i.hasNext()) {
401: _last = i.next();
402: }
403: }
404:
405: public int count() {
406: return _size;
407: }
408:
409: public void validate() {
410: Map.Entry entry = (Map.Entry) _last;
411: TestContext.assertTrue(entry.getKey() == entry.getValue());
412: }
413: }
414:
415: public static class ArrayListDirectIteration extends TestCase {
416:
417: private final int _size;
418:
419: private final ArrayList _list;
420:
421: public ArrayListDirectIteration(int size) {
422: _size = size;
423: _list = new ArrayList();
424: for (int i = 0; i < _size; i++) {
425: _list.add(Index.valueOf(i));
426: }
427: }
428:
429: public CharSequence getDescription() {
430: return TextBuilder.newInstance().append(
431: "java.util.ArrayList.get(i)").toText().padRight(
432: PADDING);
433: }
434:
435: public void execute() {
436: Object obj = Index.valueOf(_size - 1);
437: for (int i = 0; i < _size; i++) {
438: if (_list.get(i) == obj)
439: return; // Last one.
440: }
441: throw new Error();
442: }
443:
444: public int count() {
445: return _size;
446: }
447:
448: public void validate() {
449: for (int i = 0; i < _size; i++) {
450: if (!TestContext.assertEquals(Index.valueOf(i), _list
451: .get(i)))
452: break; // No need to continue.
453: }
454: }
455: }
456:
457: public static class FastTableDirectIteration extends TestCase {
458:
459: private final int _size;
460:
461: private final FastTable _list;
462:
463: public FastTableDirectIteration(int size) {
464: _size = size;
465: _list = new FastTable();
466: for (int i = 0; i < _size; i++) {
467: _list.add(Index.valueOf(i));
468: }
469: }
470:
471: public CharSequence getDescription() {
472: return TextBuilder.newInstance().append(
473: "javolution.util.FastTable.get(i)").toText()
474: .padRight(PADDING);
475: }
476:
477: public void execute() {
478: Object obj = Index.valueOf(_size - 1);
479: for (int i = 0; i < _size; i++) {
480: if (_list.get(i) == obj)
481: return; // Last one.
482: }
483: throw new Error();
484: }
485:
486: public int count() {
487: return _size;
488: }
489:
490: public void validate() {
491: for (int i = 0; i < _size; i++) {
492: if (!TestContext.assertEquals(Index.valueOf(i), _list
493: .get(i)))
494: break; // No need to continue.
495: }
496: }
497: }
498:
499: public static class FastListDirectIteration extends TestCase {
500:
501: private final int _size;
502:
503: private final FastList _list;
504:
505: public FastListDirectIteration(int size) {
506: _size = size;
507: _list = new FastList();
508: for (int i = 0; i < _size; i++) {
509: _list.add(Index.valueOf(i));
510: }
511: }
512:
513: public CharSequence getDescription() {
514: return TextBuilder.newInstance().append(
515: "javolution.util.FastList.Node.getNext()").toText()
516: .padRight(PADDING);
517: }
518:
519: public void execute() {
520: Object obj = Index.valueOf(_size - 1);
521: for (Node n = (Node) _list.head(), m = (Node) _list.tail(); (n = (Node) n
522: .getNext()) != m;) {
523: if (n.getValue() == obj)
524: return; // Last one.
525: }
526: throw new Error();
527: }
528:
529: public int count() {
530: return _size;
531: }
532:
533: public void validate() {
534: int count = 0;
535: for (Node n = (Node) _list.head(), m = (Node) _list.tail(); (n = (Node) n
536: .getNext()) != m;) {
537: if (!TestContext.assertEquals(Index.valueOf(count), n
538: .getValue()))
539: break;
540: count++;
541: }
542: TestContext.assertTrue(count == _size);
543: }
544: }
545:
546: public static class FastMapDirectIteration extends TestCase {
547:
548: private final int _size;
549:
550: private final FastMap _map;
551:
552: public FastMapDirectIteration(int size) {
553: _size = size;
554: _map = new FastMap();
555: for (int i = 0; i < _size; i++) {
556: _map.put(Index.valueOf(i), Index.valueOf(i));
557: }
558: }
559:
560: public CharSequence getDescription() {
561: return TextBuilder.newInstance().append(
562: "javolution.util.FastMap.Entry.getNext()").toText()
563: .padRight(PADDING);
564: }
565:
566: public void execute() {
567: Object obj = Index.valueOf(_size - 1);
568: for (Entry e = _map.head(), n = _map.tail(); (e = (Entry) e
569: .getNext()) != n;) {
570: if (e.getKey() == obj)
571: return; // Last one.
572: }
573: throw new Error();
574: }
575:
576: public int count() {
577: return _size;
578: }
579:
580: public void validate() {
581: int count = 0;
582: for (Entry e = _map.head(), n = _map.tail(); (e = (Entry) e
583: .getNext()) != n;) {
584: if (!TestContext.assertEquals(Index.valueOf(count), e
585: .getValue()))
586: break;
587: count++;
588: }
589: TestContext.assertTrue(count == _size);
590: }
591: }
592:
593: private static Object newInstanceOf(Class clazz) {
594: try {
595: return clazz.newInstance();
596: } catch (InstantiationException e) {
597: throw new JavolutionError(e);
598: } catch (IllegalAccessException e) {
599: throw new JavolutionError(e);
600: }
601: }
602:
603: private static final class SharedFastMap extends FastMap {
604: SharedFastMap() {
605: setShared(true);
606: }
607: }
608: }
|