001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.foundation;
022:
023: import com.db4o.foundation.*;
024:
025: import db4ounit.Assert;
026: import db4ounit.TestCase;
027: import db4ounit.TestRunner;
028:
029: public class Hashtable4TestCase implements TestCase {
030:
031: public static void main(String[] args) {
032: new TestRunner(Hashtable4TestCase.class).run();
033: }
034:
035: public void testContainsKey() {
036: Hashtable4 table = new Hashtable4();
037: Assert.isFalse(table.containsKey(null));
038: Assert.isFalse(table.containsKey("foo"));
039:
040: table.put("foo", null);
041: Assert.isTrue(table.containsKey("foo"));
042:
043: table.put("bar", "baz");
044: Assert.isTrue(table.containsKey("bar"));
045: Assert.isFalse(table.containsKey("baz"));
046: Assert.isTrue(table.containsKey("foo"));
047:
048: table.remove("foo");
049: Assert.isTrue(table.containsKey("bar"));
050: Assert.isFalse(table.containsKey("foo"));
051: }
052:
053: public void testByteArrayKeys() {
054: byte[] key1 = new byte[] { 1, 2, 3 };
055: byte[] key2 = new byte[] { 3, 2, 1 };
056: byte[] key3 = new byte[] { 3, 2, 1 }; // same values as key2
057:
058: Hashtable4 table = new Hashtable4(2);
059: table.put(key1, "foo");
060: table.put(key2, "bar");
061:
062: Assert.areEqual("foo", table.get(key1));
063: Assert.areEqual("bar", table.get(key2));
064: Assert.areEqual(2, countKeys(table));
065: Assert.areEqual(2, table.size());
066:
067: table.put(key3, "baz");
068: Assert.areEqual("foo", table.get(key1));
069: Assert.areEqual("baz", table.get(key2));
070: Assert.areEqual(2, countKeys(table));
071: Assert.areEqual(2, table.size());
072:
073: Assert.areEqual("baz", table.remove(key2));
074: Assert.areEqual(1, countKeys(table));
075: Assert.areEqual(1, table.size());
076:
077: Assert.areEqual("foo", table.remove(key1));
078: Assert.areEqual(0, countKeys(table));
079: Assert.areEqual(0, table.size());
080: }
081:
082: public void testIterator() {
083: assertIsIteratable(new Object[0]);
084: assertIsIteratable(new Object[] { "one" });
085: assertIsIteratable(new Object[] { new Integer(1),
086: new Integer(3), new Integer(2), });
087:
088: assertIsIteratable(new Object[] { "one", "three", "two", });
089:
090: assertIsIteratable(new Object[] { new Key(1), new Key(3),
091: new Key(2), });
092:
093: }
094:
095: public void testSameKeyTwice() {
096:
097: Integer key = new Integer(1);
098:
099: Hashtable4 table = new Hashtable4();
100: table.put(key, "foo");
101: table.put(key, "bar");
102:
103: Assert.areEqual("bar", table.get(key));
104: Assert.areEqual(1, countKeys(table));
105: }
106:
107: public void testSameHashCodeIterator() {
108: Key[] keys = createKeys(1, 5);
109: assertIsIteratable(keys);
110: }
111:
112: private Key[] createKeys(int begin, int end) {
113: final int factor = 10;
114: int count = (end - begin);
115: Key[] keys = new Key[count * factor];
116: for (int i = 0; i < count; ++i) {
117: final int baseIndex = i * factor;
118: for (int j = 0; j < factor; ++j) {
119: keys[baseIndex + j] = new Key(begin + i);
120: }
121: }
122: return keys;
123: }
124:
125: public void testDifferentKeysSameHashCode() {
126: Key key1 = new Key(1);
127: Key key2 = new Key(1);
128: Key key3 = new Key(2);
129:
130: Hashtable4 table = new Hashtable4(2);
131: table.put(key1, "foo");
132: table.put(key2, "bar");
133:
134: Assert.areEqual("foo", table.get(key1));
135: Assert.areEqual("bar", table.get(key2));
136: Assert.areEqual(2, countKeys(table));
137:
138: table.put(key2, "baz");
139: Assert.areEqual("foo", table.get(key1));
140: Assert.areEqual("baz", table.get(key2));
141: Assert.areEqual(2, countKeys(table));
142:
143: table.put(key1, "spam");
144: Assert.areEqual("spam", table.get(key1));
145: Assert.areEqual("baz", table.get(key2));
146: Assert.areEqual(2, countKeys(table));
147:
148: table.put(key3, "eggs");
149: Assert.areEqual("spam", table.get(key1));
150: Assert.areEqual("baz", table.get(key2));
151: Assert.areEqual("eggs", table.get(key3));
152: Assert.areEqual(3, countKeys(table));
153:
154: table.put(key2, "mice");
155: Assert.areEqual("spam", table.get(key1));
156: Assert.areEqual("mice", table.get(key2));
157: Assert.areEqual("eggs", table.get(key3));
158: Assert.areEqual(3, countKeys(table));
159: }
160:
161: static class KeyCount {
162: public int keys;
163: }
164:
165: private int countKeys(Hashtable4 table) {
166: int count = 0;
167: Iterator4 i = table.iterator();
168: while (i.moveNext()) {
169: count++;
170: }
171: return count;
172: }
173:
174: public void assertIsIteratable(Object[] keys) {
175: Collection4 expected = new Collection4(keys);
176: Iterator4 iter = tableFromKeys(keys).iterator();
177: while (iter.moveNext()) {
178: Entry4 entry = (Entry4) iter.current();
179: Object removed = expected.remove(entry.key());
180: Assert.isNotNull(removed);
181: }
182: Assert.isTrue(expected.isEmpty(), expected.toString());
183: }
184:
185: private Hashtable4 tableFromKeys(Object[] keys) {
186: Hashtable4 ht = new Hashtable4();
187: for (int i = 0; i < keys.length; i++) {
188: ht.put(keys[i], keys[i]);
189: }
190: return ht;
191: }
192:
193: static class Key {
194: private int _hashCode;
195:
196: public Key(int hashCode) {
197: _hashCode = hashCode;
198: }
199:
200: public int hashCode() {
201: return _hashCode;
202: }
203:
204: }
205:
206: }
|