示例#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 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));
 }
示例#3
0
  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)));
  }