private void testIterate() { String fileName = getBaseDir() + "/testIterate.h3"; FileUtils.delete(fileName); MVStore s = openStore(fileName); MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class); Iterator<Integer> it = m.keyIterator(null); assertFalse(it.hasNext()); for (int i = 0; i < 10; i++) { m.put(i, "hello " + i); } s.store(); it = m.keyIterator(null); it.next(); assertThrows(UnsupportedOperationException.class, it).remove(); it = m.keyIterator(null); for (int i = 0; i < 10; i++) { assertTrue(it.hasNext()); assertEquals(i, it.next().intValue()); } assertFalse(it.hasNext()); assertNull(it.next()); for (int j = 0; j < 10; j++) { it = m.keyIterator(j); for (int i = j; i < 10; i++) { assertTrue(it.hasNext()); assertEquals(i, it.next().intValue()); } assertFalse(it.hasNext()); } s.close(); }
private void testRandom() { String fileName = getBaseDir() + "/testRandom.h3"; FileUtils.delete(fileName); MVStore s = openStore(fileName); MVMap<Integer, Integer> m = s.openMap("data", Integer.class, Integer.class); TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); Random r = new Random(1); int operationCount = 1000; int maxValue = 30; Integer expected, got; for (int i = 0; i < operationCount; i++) { int k = r.nextInt(maxValue); int v = r.nextInt(); boolean compareAll; switch (r.nextInt(3)) { case 0: log(i + ": put " + k + " = " + v); expected = map.put(k, v); got = m.put(k, v); if (expected == null) { assertNull(got); } else { assertEquals(expected, got); } compareAll = true; break; case 1: log(i + ": remove " + k); expected = map.remove(k); got = m.remove(k); if (expected == null) { assertNull(got); } else { assertEquals(expected, got); } compareAll = true; break; default: Integer a = map.get(k); Integer b = m.get(k); if (a == null || b == null) { assertTrue(a == b); } else { assertEquals(a.intValue(), b.intValue()); } compareAll = false; break; } if (compareAll) { Iterator<Integer> it = m.keyIterator(null); Iterator<Integer> itExpected = map.keySet().iterator(); while (itExpected.hasNext()) { assertTrue(it.hasNext()); expected = itExpected.next(); got = it.next(); assertEquals(expected, got); } assertFalse(it.hasNext()); } } s.close(); }