001: /*
002: * Copyright(c) 2006, NEXVU Technologies
003: * All rights reserved.
004: */
005: package gnu.trove;
006:
007: import gnu.trove.decorator.TByteIntHashMapDecorator;
008: import junit.framework.TestCase;
009:
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Set;
013:
014: /**
015: *
016: */
017: public class P2PHashMapTest extends TestCase {
018: final byte KEY_ONE = (byte) 100;
019: final byte KEY_TWO = (byte) 101;
020:
021: public P2PHashMapTest(String name) {
022: super (name);
023: }
024:
025: public void testKeys() {
026: TByteIntHashMap map = new TByteIntHashMap();
027:
028: map.put(KEY_ONE, 1);
029: map.put(KEY_TWO, 2);
030:
031: assertEquals(2, map.size());
032:
033: byte[] keys = map.keys(new byte[map.size()]);
034: assertEquals(2, keys.length);
035: TByteArrayList keys_list = new TByteArrayList(keys);
036:
037: assertTrue(keys_list.contains(KEY_ONE));
038: assertTrue(keys_list.contains(KEY_TWO));
039:
040: byte[] keys2 = map.keys();
041: assertEquals(2, keys2.length);
042: TByteArrayList keys_list2 = new TByteArrayList(keys2);
043:
044: assertTrue(keys_list2.contains(KEY_ONE));
045: assertTrue(keys_list2.contains(KEY_TWO));
046: }
047:
048: public void testDecorator() {
049: TByteIntHashMap map = new TByteIntHashMap();
050:
051: map.put(KEY_ONE, 1);
052: map.put(KEY_TWO, 2);
053:
054: Map<Byte, Integer> decorator = new TByteIntHashMapDecorator(map);
055:
056: assertEquals(2, decorator.size());
057: assertEquals(Integer.valueOf(1), decorator.get(Byte
058: .valueOf(KEY_ONE)));
059: assertEquals(Integer.valueOf(2), decorator.get(Byte
060: .valueOf(KEY_TWO)));
061:
062: Set<Byte> decorator_keys = decorator.keySet();
063: assertEquals(2, decorator_keys.size());
064: Iterator<Byte> it = decorator_keys.iterator();
065: int count = 0;
066: while (it.hasNext()) {
067: count++;
068: System.out.println(it.next());
069: }
070: assertEquals(2, count);
071:
072: assertSame(map, ((TByteIntHashMapDecorator) decorator).getMap());
073: }
074:
075: public void testIterator() {
076: TByteIntHashMap map = new TByteIntHashMap();
077:
078: TByteIntIterator iterator = map.iterator();
079: assertFalse(iterator.hasNext());
080:
081: map.put(KEY_ONE, 1);
082: map.put(KEY_TWO, 2);
083:
084: iterator = map.iterator();
085: assertTrue(iterator.hasNext());
086: iterator.advance();
087: assertTrue(iterator.hasNext());
088: iterator.advance();
089: assertFalse(iterator.hasNext());
090: }
091:
092: public void testAdjustValue() {
093: TByteIntHashMap map = new TByteIntHashMap();
094:
095: map.put(KEY_ONE, 1);
096:
097: boolean changed = map.adjustValue(KEY_ONE, 1);
098: assertTrue(changed);
099: assertEquals(2, map.get(KEY_ONE));
100:
101: changed = map.adjustValue(KEY_ONE, 5);
102: assertTrue(changed);
103: assertEquals(7, map.get(KEY_ONE));
104:
105: changed = map.adjustValue(KEY_ONE, -3);
106: assertTrue(changed);
107: assertEquals(4, map.get(KEY_ONE));
108:
109: changed = map.adjustValue(KEY_TWO, 1);
110: assertFalse(changed);
111: assertFalse(map.containsKey(KEY_TWO));
112: }
113:
114: public void testAdjustOrPutValue() {
115: TByteIntHashMap map = new TByteIntHashMap();
116:
117: map.put(KEY_ONE, 1);
118:
119: long new_value = map.adjustOrPutValue(KEY_ONE, 1, 100);
120: assertEquals(2, new_value);
121: assertEquals(2, map.get(KEY_ONE));
122:
123: new_value = map.adjustOrPutValue(KEY_ONE, 5, 100);
124: assertEquals(7, new_value);
125: assertEquals(7, map.get(KEY_ONE));
126:
127: new_value = map.adjustOrPutValue(KEY_ONE, -3, 100);
128: assertEquals(4, new_value);
129: assertEquals(4, map.get(KEY_ONE));
130:
131: new_value = map.adjustOrPutValue(KEY_TWO, 1, 100);
132: assertEquals(100, new_value);
133: assertTrue(map.containsKey(KEY_TWO));
134: assertEquals(100, map.get(KEY_TWO));
135:
136: new_value = map.adjustOrPutValue(KEY_TWO, 1, 100);
137: assertEquals(101, new_value);
138: assertEquals(101, map.get(KEY_TWO));
139: }
140:
141: /**
142: * Test for tracking issue #1204014. +0.0 and -0.0 have different bit patterns, but
143: * should be counted the same as keys in a map. Checks for doubles and floats.
144: */
145: public void testFloatZeroHashing() {
146: TDoubleObjectHashMap<String> po_double_map = new TDoubleObjectHashMap<String>();
147: TDoubleIntHashMap pp_double_map = new TDoubleIntHashMap();
148: TFloatObjectHashMap<String> po_float_map = new TFloatObjectHashMap<String>();
149: TFloatIntHashMap pp_float_map = new TFloatIntHashMap();
150:
151: final double zero_double = 0.0;
152: final double negative_zero_double = -zero_double;
153: final float zero_float = 0.0f;
154: final float negative_zero_float = -zero_float;
155:
156: // Sanity check... make sure I'm really creating two different values.
157: final String zero_bits_double = Long.toBinaryString(Double
158: .doubleToLongBits(zero_double));
159: final String negative_zero_bits_double = Long
160: .toBinaryString(Double
161: .doubleToLongBits(negative_zero_double));
162: assertFalse(zero_bits_double + " == "
163: + negative_zero_bits_double, zero_bits_double
164: .equals(negative_zero_bits_double));
165:
166: final String zero_bits_float = Integer.toBinaryString(Float
167: .floatToIntBits(zero_float));
168: final String negative_zero_bits_float = Integer
169: .toBinaryString(Float
170: .floatToIntBits(negative_zero_float));
171: assertFalse(
172: zero_bits_float + " == " + negative_zero_bits_float,
173: zero_bits_float.equals(negative_zero_bits_float));
174:
175: po_double_map.put(zero_double, "Zero");
176: po_double_map.put(negative_zero_double, "Negative Zero");
177:
178: pp_double_map.put(zero_double, 0);
179: pp_double_map.put(negative_zero_double, -1);
180:
181: po_float_map.put(zero_float, "Zero");
182: po_float_map.put(negative_zero_float, "Negative Zero");
183:
184: pp_float_map.put(zero_float, 0);
185: pp_float_map.put(negative_zero_float, -1);
186:
187: assertEquals(1, po_double_map.size());
188: assertEquals(po_double_map.get(zero_double), "Negative Zero");
189: assertEquals(po_double_map.get(negative_zero_double),
190: "Negative Zero");
191:
192: assertEquals(1, pp_double_map.size());
193: assertEquals(pp_double_map.get(zero_double), -1);
194: assertEquals(pp_double_map.get(negative_zero_double), -1);
195:
196: assertEquals(1, po_float_map.size());
197: assertEquals(po_float_map.get(zero_float), "Negative Zero");
198: assertEquals(po_float_map.get(negative_zero_float),
199: "Negative Zero");
200:
201: assertEquals(1, pp_float_map.size());
202: assertEquals(pp_float_map.get(zero_float), -1);
203: assertEquals(pp_float_map.get(negative_zero_float), -1);
204:
205: po_double_map.put(zero_double, "Zero");
206: pp_double_map.put(zero_double, 0);
207: po_float_map.put(zero_float, "Zero");
208: pp_float_map.put(zero_float, 0);
209:
210: assertEquals(1, po_double_map.size());
211: assertEquals(po_double_map.get(zero_double), "Zero");
212: assertEquals(po_double_map.get(negative_zero_double), "Zero");
213:
214: assertEquals(1, pp_double_map.size());
215: assertEquals(pp_double_map.get(zero_double), 0);
216: assertEquals(pp_double_map.get(negative_zero_double), 0);
217:
218: assertEquals(1, po_float_map.size());
219: assertEquals(po_float_map.get(zero_float), "Zero");
220: assertEquals(po_float_map.get(negative_zero_float), "Zero");
221:
222: assertEquals(1, pp_float_map.size());
223: assertEquals(pp_float_map.get(zero_float), 0);
224: assertEquals(pp_float_map.get(negative_zero_float), 0);
225: }
226: }
|