// Check that the iterator iterates the correct number of times private void checkSizes(String msg, int sz, Iterator it, int expectedSize) { assertEquals(msg, expectedSize, sz); int result = 0; while (it.hasNext()) { result++; it.next(); } assertEquals(msg, expectedSize, result); }
public void testIteration() { assertTrue(_nbhm.isEmpty()); assertThat(_nbhm.put("k1", "v1"), nullValue()); assertThat(_nbhm.put("k2", "v2"), nullValue()); String str1 = ""; for (Iterator<Map.Entry<String, String>> i = _nbhm.entrySet().iterator(); i.hasNext(); ) { Map.Entry<String, String> e = i.next(); str1 += e.getKey(); } assertThat("found all entries", str1, anyOf(is("k1k2"), is("k2k1"))); String str2 = ""; for (Iterator<String> i = _nbhm.keySet().iterator(); i.hasNext(); ) { String key = i.next(); str2 += key; } assertThat("found all keys", str2, anyOf(is("k1k2"), is("k2k1"))); String str3 = ""; for (Iterator<String> i = _nbhm.values().iterator(); i.hasNext(); ) { String val = i.next(); str3 += val; } assertThat("found all vals", str3, anyOf(is("v1v2"), is("v2v1"))); assertThat( "toString works", _nbhm.toString(), anyOf(is("{k1=v1, k2=v2}"), is("{k2=v2, k1=v1}"))); }
// --- public void testNonBlockingIdentityHashMapIteratorMultithreaded() throws InterruptedException, ExecutionException { TestKeyFeeder feeder = getTestKeyFeeder(); final int itemCount = feeder.size(); // validate results final NonBlockingIdentityHashMap<Long, TestKey> items = feeder.getMapMultithreaded(); assertEquals("size()", itemCount, items.size()); assertEquals("values().size()", itemCount, items.values().size()); assertEquals("entrySet().size()", itemCount, items.entrySet().size()); int iteratorCount = 0; for (TestKey m : items.values()) iteratorCount++; // sometimes a different result comes back the second time int iteratorCount2 = 0; for (Iterator<TestKey> it = items.values().iterator(); it.hasNext(); ) { iteratorCount2++; it.next(); } assertEquals("iterator counts differ", iteratorCount, iteratorCount2); assertEquals("values().iterator() count", itemCount, iteratorCount); }