コード例 #1
0
  // Test some basic stuff; add a few keys, remove a few keys
  public void testBasic() {
    assertTrue(_nbhm.isEmpty());
    assertThat(_nbhm.putIfAbsent("k1", "v1"), nullValue());
    checkSizes(1);
    assertThat(_nbhm.putIfAbsent("k2", "v2"), nullValue());
    checkSizes(2);
    assertTrue(_nbhm.containsKey("k2"));
    assertThat(_nbhm.put("k1", "v1a"), is("v1"));
    assertThat(_nbhm.put("k2", "v2a"), is("v2"));
    checkSizes(2);
    assertThat(_nbhm.putIfAbsent("k2", "v2b"), is("v2a"));
    assertThat(_nbhm.remove("k1"), is("v1a"));
    assertFalse(_nbhm.containsKey("k1"));
    checkSizes(1);
    assertThat(_nbhm.remove("k1"), nullValue());
    assertThat(_nbhm.remove("k2"), is("v2a"));
    checkSizes(0);
    assertThat(_nbhm.remove("k2"), nullValue());
    assertThat(_nbhm.remove("k3"), nullValue());
    assertTrue(_nbhm.isEmpty());

    assertThat(_nbhm.put("k0", "v0"), nullValue());
    assertTrue(_nbhm.containsKey("k0"));
    checkSizes(1);
    assertThat(_nbhm.remove("k0"), is("v0"));
    assertFalse(_nbhm.containsKey("k0"));
    checkSizes(0);

    assertThat(_nbhm.replace("k0", "v0"), nullValue());
    assertFalse(_nbhm.containsKey("k0"));
    assertThat(_nbhm.put("k0", "v0"), nullValue());
    assertEquals(_nbhm.replace("k0", "v0a"), "v0");
    assertEquals(_nbhm.get("k0"), "v0a");
    assertThat(_nbhm.remove("k0"), is("v0a"));
    assertFalse(_nbhm.containsKey("k0"));
    checkSizes(0);

    assertThat(_nbhm.replace("k1", "v1"), nullValue());
    assertFalse(_nbhm.containsKey("k1"));
    assertThat(_nbhm.put("k1", "v1"), nullValue());
    assertEquals(_nbhm.replace("k1", "v1a"), "v1");
    assertEquals(_nbhm.get("k1"), "v1a");
    assertThat(_nbhm.remove("k1"), is("v1a"));
    assertFalse(_nbhm.containsKey("k1"));
    checkSizes(0);

    // Insert & Remove KeyBonks until the table resizes and we start
    // finding Tombstone keys- and KeyBonk's equals-call with throw a
    // ClassCastException if it sees a non-KeyBonk.
    NonBlockingIdentityHashMap<KeyBonk, String> dumb =
        new NonBlockingIdentityHashMap<KeyBonk, String>();
    for (int i = 0; i < 10000; i++) {
      final KeyBonk happy1 = new KeyBonk(i);
      assertThat(dumb.put(happy1, "and"), nullValue());
      if ((i & 1) == 0) dumb.remove(happy1);
      final KeyBonk happy2 = new KeyBonk(i); // 'equals' but not '=='
      dumb.get(happy2);
    }
  }
コード例 #2
0
 public Integer call() throws Exception {
   _barrier.await();
   int count = 0;
   for (TestKey item : _items) {
     if (_map.contains(item._id)) {
       System.err.printf("COLLISION DETECTED: %s exists\n", item.toString());
     }
     final TestKey exists = _map.putIfAbsent(item._id, item);
     if (exists == null) {
       count++;
     } else {
       System.err.printf(
           "COLLISION DETECTED: %s exists as %s\n", item.toString(), exists.toString());
     }
   }
   return count;
 }
コード例 #3
0
 void work_helper(
     NonBlockingIdentityHashMap<String, String> nbhm, String thrd, int d, String[] keys) {
   final int ITERS = 20000;
   for (int j = 0; j < 10; j++) {
     long start = System.nanoTime();
     for (int i = d; i < ITERS; i += 2)
       assertThat(
           "this key not in there, so putIfAbsent must work",
           nbhm.putIfAbsent(keys[i], thrd),
           is((String) null));
     for (int i = d; i < ITERS; i += 2) assertTrue(nbhm.remove(keys[i], thrd));
     double delta_nanos = System.nanoTime() - start;
     double delta_secs = delta_nanos / 1000000000.0;
     double ops = ITERS * 2;
     // System.out.println("Thrd"+thrd+" "+(ops/delta_secs)+" ops/sec size="+nbhm.size());
   }
 }