// Do some simple concurrent testing
  public void testConcurrentSimple() throws InterruptedException {
    final NonBlockingIdentityHashMap<String, String> nbhm =
        new NonBlockingIdentityHashMap<String, String>();
    final String[] keys = new String[20000];
    for (int i = 0; i < 20000; i++) keys[i] = "k" + i;

    // In 2 threads, add & remove even & odd elements concurrently
    Thread t1 =
        new Thread() {
          public void run() {
            work_helper(nbhm, "T1", 1, keys);
          }
        };
    t1.start();
    work_helper(nbhm, "T0", 0, keys);
    t1.join();

    // In the end, all members should be removed
    StringBuffer buf = new StringBuffer();
    buf.append("Should be emptyset but has these elements: {");
    boolean found = false;
    for (String x : nbhm.keySet()) {
      buf.append(" ").append(x);
      found = true;
    }
    if (found) System.out.println(buf + " }");
    assertThat("concurrent size=0", nbhm.size(), is(0));
    for (String x : nbhm.keySet()) {
      assertTrue("No elements so never get here", false);
    }
  }
 // Check all iterators for correct size counts
 private void checkSizes(int expectedSize) {
   assertEquals("size()", _nbhm.size(), expectedSize);
   Collection<String> vals = _nbhm.values();
   checkSizes("values()", vals.size(), vals.iterator(), expectedSize);
   Set<String> keys = _nbhm.keySet();
   checkSizes("keySet()", keys.size(), keys.iterator(), expectedSize);
   Set<Entry<String, String>> ents = _nbhm.entrySet();
   checkSizes("entrySet()", ents.size(), ents.iterator(), expectedSize);
 }
  public void testIterationBig() {
    final int CNT = 10000;
    String[] keys = new String[CNT];
    String[] vals = new String[CNT];
    assertThat(_nbhm.size(), is(0));
    for (int i = 0; i < CNT; i++) _nbhm.put(keys[i] = ("k" + i), vals[i] = ("v" + i));
    assertThat(_nbhm.size(), is(CNT));

    int sz = 0;
    int sum = 0;
    for (String s : _nbhm.keySet()) {
      sz++;
      assertThat("", s.charAt(0), is('k'));
      int x = Integer.parseInt(s.substring(1));
      sum += x;
      assertTrue(x >= 0 && x <= (CNT - 1));
    }
    assertThat("Found 10000 ints", sz, is(CNT));
    assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2));

    assertThat("can remove 3", _nbhm.remove(keys[3]), is(vals[3]));
    assertThat("can remove 4", _nbhm.remove(keys[4]), is(vals[4]));
    sz = 0;
    sum = 0;
    for (String s : _nbhm.keySet()) {
      sz++;
      assertThat("", s.charAt(0), is('k'));
      int x = Integer.parseInt(s.substring(1));
      sum += x;
      assertTrue(x >= 0 && x <= (CNT - 1));
      String v = _nbhm.get(s);
      assertThat("", v.charAt(0), is('v'));
      assertThat("", s.substring(1), is(v.substring(1)));
    }
    assertThat("Found " + (CNT - 2) + " ints", sz, is(CNT - 2));
    assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2 - (3 + 4)));
  }
 public void testIterationBig2() {
   final int CNT = 10000;
   NonBlockingIdentityHashMap<Integer, String> nbhm =
       new NonBlockingIdentityHashMap<Integer, String>();
   final String v = "v";
   for (int i = 0; i < CNT; i++) {
     final Integer z = new Integer(i);
     String s0 = nbhm.get(z);
     assertThat(s0, nullValue());
     nbhm.put(z, v);
     String s1 = nbhm.get(z);
     assertThat(s1, is(v));
   }
   assertThat(nbhm.size(), is(CNT));
 }
  // ---
  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);
  }