@Test // (timeout = 10000) public void testIteration() { HTreeMap m = new HTreeMap<Integer, Integer>(recman, true) { @Override protected int hash(Object key) { return (Integer) key; } }; final int max = 140; final int inc = 111111; for (Integer i = 0; i < max; i++) { m.put(i, i + inc); } Iterator<Integer> keys = m.keySet().iterator(); for (Integer i = 0; i < max; i++) { assertTrue(keys.hasNext()); assertEquals(i, keys.next()); } assertTrue(!keys.hasNext()); Iterator<Integer> vals = m.values().iterator(); for (Integer i = inc; i < max + inc; i++) { assertTrue(vals.hasNext()); assertEquals(i, vals.next()); } assertTrue(!vals.hasNext()); // great it worked, test stuff spread across segments m.clear(); assertTrue(m.isEmpty()); for (int i = 0; i < max; i++) { m.put((1 << 30) + i, i + inc); m.put((2 << 30) + i, i + inc); m.put((3 << 30) + i, i + inc); } assertEquals(max * 3, m.size()); int countSegments = 0; for (long segmentRecid : m.segmentRecids) { if (recman.recordGet(segmentRecid, HTreeMap.DIR_SERIALIZER) != null) countSegments++; } assertEquals(3, countSegments); keys = m.keySet().iterator(); for (int i = 1; i <= 3; i++) { for (int j = 0; j < max; j++) { assertTrue(keys.hasNext()); assertEquals(Integer.valueOf((i << 30) + j), keys.next()); } } assertTrue(!keys.hasNext()); }
@Test public void clear() { HTreeMap m = new HTreeMap(recman, true); for (Integer i = 0; i < 100; i++) { m.put(i, i); } m.clear(); assertTrue(m.isEmpty()); assertEquals(0, m.size()); }
@Test public void test_delete() { HTreeMap m = new HTreeMap(recman, true) { @Override protected int hash(Object key) { return 0; } }; for (long i = 0; i < 20; i++) { m.put(i, i + 100); } for (long i = 0; i < 20; i++) { assertTrue(m.containsKey(i)); assertEquals(i + 100, m.get(i)); } for (long i = 0; i < 20; i++) { m.remove(i); } for (long i = 0; i < 20; i++) { assertTrue(!m.containsKey(i)); assertEquals(null, m.get(i)); } }
@Test public void test_simple_put() { HTreeMap m = new HTreeMap(recman, true); m.put(111L, 222L); m.put(333L, 444L); assertTrue(m.containsKey(111L)); assertTrue(!m.containsKey(222L)); assertTrue(m.containsKey(333L)); assertTrue(!m.containsKey(444L)); assertEquals(222L, m.get(111L)); assertEquals(null, m.get(222L)); assertEquals(444l, m.get(333L)); }
@Test public void test_hash_collision() { HTreeMap m = new HTreeMap(recman, true) { @Override protected int hash(Object key) { return 0; } }; for (long i = 0; i < 20; i++) { m.put(i, i + 100); } for (long i = 0; i < 20; i++) { assertTrue(m.containsKey(i)); assertEquals(i + 100, m.get(i)); } m.put(11L, 1111L); assertEquals(1111L, m.get(11L)); }
@Test public void test_hash_dir_expand() { HTreeMap m = new HTreeMap(recman, true) { @Override protected int hash(Object key) { return 0; } }; for (long i = 0; i < HTreeMap.BUCKET_OVERFLOW; i++) { m.put(i, i); } // segment should not be expanded long[][] l = recman.recordGet(m.segmentRecids[0], HTreeMap.DIR_SERIALIZER); assertNotNull(l[0]); assertEquals(1, l[0][0] & 1); // last bite indicates leaf for (int j = 1; j < 8; j++) { // all others should be null assertEquals(0, l[0][j]); } long recid = l[0][0] >>> 1; for (long i = HTreeMap.BUCKET_OVERFLOW - 1; i >= 0; i--) { assertTrue(recid != 0); HTreeMap.LinkedNode n = (HTreeMap.LinkedNode) recman.recordGet(recid, m.LN_SERIALIZER); assertEquals(i, n.key); assertEquals(i, n.value); recid = n.next; } // adding one more item should trigger dir expansion to next level m.put((long) HTreeMap.BUCKET_OVERFLOW, (long) HTreeMap.BUCKET_OVERFLOW); recid = m.segmentRecids[0]; l = recman.recordGet(recid, HTreeMap.DIR_SERIALIZER); assertNotNull(l[0]); for (int j = 1; j < 8; j++) { // all others should be null assertEquals(null, l[j]); } assertEquals(0, l[0][0] & 1); // last bite indicates leaf for (int j = 1; j < 8; j++) { // all others should be zero assertEquals(0, l[0][j]); } recid = l[0][0] >>> 1; l = recman.recordGet(recid, HTreeMap.DIR_SERIALIZER); assertNotNull(l[0]); for (int j = 1; j < 8; j++) { // all others should be null assertEquals(null, l[j]); } assertEquals(1, l[0][0] & 1); // last bite indicates leaf for (int j = 1; j < 8; j++) { // all others should be zero assertEquals(0, l[0][j]); } recid = l[0][0] >>> 1; for (long i = 0; i <= HTreeMap.BUCKET_OVERFLOW; i++) { assertTrue(recid != 0); HTreeMap.LinkedNode n = (HTreeMap.LinkedNode) recman.recordGet(recid, m.LN_SERIALIZER); assertNotNull(n); assertEquals(i, n.key); assertEquals(i, n.value); recid = n.next; } }