KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > foundation > Hashtable4TestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.foundation;
22
23 import com.db4o.foundation.Hashtable4;
24 import com.db4o.foundation.Visitor4;
25
26 import db4ounit.Assert;
27 import db4ounit.TestCase;
28
29 public class Hashtable4TestCase implements TestCase {
30     
31     public void testContainsKey() {
32         Hashtable4 table = new Hashtable4();
33         Assert.isFalse(table.containsKey(null));
34         Assert.isFalse(table.containsKey("foo"));
35         
36         table.put("foo", null);
37         Assert.isTrue(table.containsKey("foo"));
38         
39         table.put("bar", "baz");
40         Assert.isTrue(table.containsKey("bar"));
41         Assert.isFalse(table.containsKey("baz"));
42         Assert.isTrue(table.containsKey("foo"));
43         
44         table.remove("foo");
45         Assert.isTrue(table.containsKey("bar"));
46         Assert.isFalse(table.containsKey("foo"));
47     }
48     
49     public void testByteArrayKeys() {
50         byte[] key1 = new byte[] { 1, 2, 3 };
51         byte[] key2 = new byte[] { 3, 2, 1 };
52         byte[] key3 = new byte[] { 3, 2, 1 }; // same values as key2
53

54         Hashtable4 table = new Hashtable4(2);
55         table.put(key1, "foo");
56         table.put(key2, "bar");
57         
58         Assert.areEqual("foo", table.get(key1));
59         Assert.areEqual("bar", table.get(key2));
60         Assert.areEqual(2, countKeys(table));
61         Assert.areEqual(2, table.size());
62         
63         table.put(key3, "baz");
64         Assert.areEqual("foo", table.get(key1));
65         Assert.areEqual("baz", table.get(key2));
66         Assert.areEqual(2, countKeys(table));
67         Assert.areEqual(2, table.size());
68         
69         Assert.areEqual("baz", table.remove(key2));
70         Assert.areEqual(1, countKeys(table));
71         Assert.areEqual(1, table.size());
72         
73         Assert.areEqual("foo", table.remove(key1));
74         Assert.areEqual(0, countKeys(table));
75         Assert.areEqual(0, table.size());
76     }
77     
78     public void testSameKeyTwice() {
79         
80         Integer JavaDoc key = new Integer JavaDoc(1);
81         
82         Hashtable4 table = new Hashtable4();
83         table.put(key, "foo");
84         table.put(key, "bar");
85         
86         Assert.areEqual("bar", table.get(key));
87         Assert.areEqual(1, countKeys(table));
88     }
89     
90     static class Key {
91         private int _hashCode;
92         
93         public Key(int hashCode) {
94             _hashCode = hashCode;
95         }
96         
97         public int hashCode() {
98             return _hashCode;
99         }
100     }
101     
102     public void testDifferentKeysSameHashCode() {
103         Key key1 = new Key(1);
104         Key key2 = new Key(1);
105         Key key3 = new Key(2);
106         
107         Hashtable4 table = new Hashtable4(2);
108         table.put(key1, "foo");
109         table.put(key2, "bar");
110         
111         Assert.areEqual("foo", table.get(key1));
112         Assert.areEqual("bar", table.get(key2));
113         Assert.areEqual(2, countKeys(table));
114         
115         table.put(key2, "baz");
116         Assert.areEqual("foo", table.get(key1));
117         Assert.areEqual("baz", table.get(key2));
118         Assert.areEqual(2, countKeys(table));
119         
120         table.put(key1, "spam");
121         Assert.areEqual("spam", table.get(key1));
122         Assert.areEqual("baz", table.get(key2));
123         Assert.areEqual(2, countKeys(table));
124         
125         table.put(key3, "eggs");
126         Assert.areEqual("spam", table.get(key1));
127         Assert.areEqual("baz", table.get(key2));
128         Assert.areEqual("eggs", table.get(key3));
129         Assert.areEqual(3, countKeys(table));
130         
131         table.put(key2, "mice");
132         Assert.areEqual("spam", table.get(key1));
133         Assert.areEqual("mice", table.get(key2));
134         Assert.areEqual("eggs", table.get(key3));
135         Assert.areEqual(3, countKeys(table));
136     }
137     
138     static class KeyCount {
139         public int keys;
140     }
141
142     private int countKeys(Hashtable4 table) {
143         final KeyCount count = new KeyCount();
144         table.forEachKey(new Visitor4() {
145             public void visit(Object JavaDoc key) {
146                 ++count.keys;
147             }
148         });
149         return count.keys;
150     }
151 }
152
Popular Tags