001: package org.drools.util;
002:
003: import junit.framework.TestCase;
004:
005: import org.drools.Cheese;
006: import org.drools.util.ObjectHashMap.ObjectEntry;
007:
008: public class ObjectHashMapTest extends TestCase {
009: public void testChechExistsFalse() {
010: final ObjectHashMap map = new ObjectHashMap();
011: final Cheese stilton = new Cheese("stilton", 5);
012: map.put(new Integer(1), stilton, false);
013:
014: Cheese c = (Cheese) map.get(new Integer(1));
015: assertSame(stilton, c);
016:
017: // we haven't told the map to check if the key exists, so we should end up with two entries.
018: // the second one is nolonger reacheable
019: final Cheese cheddar = new Cheese("cheddar", 5);
020: map.put(new Integer(1), cheddar, false);
021: c = (Cheese) map.get(new Integer(1));
022: assertSame(cheddar, c);
023:
024: Entry entry = map.getBucket(new Integer(1));
025: int size = 0;
026: while (entry != null) {
027: size++;
028: entry = entry.getNext();
029: }
030:
031: assertEquals(2, size);
032:
033: // Check remove works, should leave one unreachable key
034: map.remove(new Integer(1));
035: entry = map.getBucket(new Integer(1));
036: size = 0;
037: while (entry != null) {
038: size++;
039: entry = entry.getNext();
040: }
041:
042: assertEquals(1, size);
043: }
044:
045: public void testChechExistsTrue() {
046: final ObjectHashMap map = new ObjectHashMap();
047: final Cheese stilton = new Cheese("stilton", 5);
048: map.put(new Integer(1), stilton, true);
049:
050: Cheese c = (Cheese) map.get(new Integer(1));
051: assertSame(stilton, c);
052:
053: // we haven't told the map to check if the key exists, so we should end up with two entries.
054: // the second one is nolonger reacheable
055: final Cheese cheddar = new Cheese("cheddar", 5);
056: map.put(new Integer(1), cheddar);
057: c = (Cheese) map.get(new Integer(1));
058: assertSame(cheddar, c);
059:
060: Entry entry = map.getBucket(new Integer(1));
061: int size = 0;
062: while (entry != null) {
063: size++;
064: entry = entry.getNext();
065: }
066:
067: assertEquals(1, size);
068:
069: // Check remove works
070: map.remove(new Integer(1));
071: entry = map.getBucket(new Integer(1));
072: size = 0;
073: while (entry != null) {
074: size++;
075: entry = entry.getNext();
076: }
077:
078: assertEquals(0, size);
079: }
080:
081: public void testEmptyIterator() {
082: final ObjectHashMap map = new ObjectHashMap();
083: final Iterator it = map.iterator();
084: for (ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it
085: .next()) {
086: fail("Map is empty, there should be no iteration");
087: }
088: }
089:
090: public void testStringData() {
091: final ObjectHashMap map = new ObjectHashMap();
092: assertNotNull(map);
093: final int count = 1000;
094: for (int idx = 0; idx < count; idx++) {
095: final String key = "key" + idx;
096: final String val = "value" + idx;
097: map.put(key, val);
098: assertEquals(val, map.get(key));
099: }
100: }
101:
102: public void testIntegerData() {
103: final ObjectHashMap map = new ObjectHashMap();
104: assertNotNull(map);
105: final int count = 1000;
106: for (int idx = 0; idx < count; idx++) {
107: final Integer key = new Integer(idx);
108: final Integer val = new Integer(idx);
109: map.put(key, val);
110: assertEquals(val, map.get(key));
111: }
112: }
113: }
|