Search in sources :

Example 1 with CharArrayMap

use of org.apache.lucene.analysis.CharArrayMap in project lucene-solr by apache.

the class TestCharArrayMap method testMethods.

public void testMethods() {
    CharArrayMap<Integer> cm = new CharArrayMap<>(2, false);
    HashMap<String, Integer> hm = new HashMap<>();
    hm.put("foo", 1);
    hm.put("bar", 2);
    cm.putAll(hm);
    assertEquals(hm.size(), cm.size());
    hm.put("baz", 3);
    cm.putAll(hm);
    assertEquals(hm.size(), cm.size());
    CharArraySet cs = cm.keySet();
    int n = 0;
    for (Object o : cs) {
        assertTrue(cm.containsKey(o));
        char[] co = (char[]) o;
        assertTrue(cm.containsKey(co, 0, co.length));
        n++;
    }
    assertEquals(hm.size(), n);
    assertEquals(hm.size(), cs.size());
    assertEquals(cm.size(), cs.size());
    cs.clear();
    assertEquals(0, cs.size());
    assertEquals(0, cm.size());
    // keySet() should not allow adding new keys
    expectThrows(UnsupportedOperationException.class, () -> {
        cs.add("test");
    });
    cm.putAll(hm);
    assertEquals(hm.size(), cs.size());
    assertEquals(cm.size(), cs.size());
    Iterator<Map.Entry<Object, Integer>> iter1 = cm.entrySet().iterator();
    n = 0;
    while (iter1.hasNext()) {
        Map.Entry<Object, Integer> entry = iter1.next();
        Object key = entry.getKey();
        Integer val = entry.getValue();
        assertEquals(cm.get(key), val);
        entry.setValue(val * 100);
        assertEquals(val * 100, (int) cm.get(key));
        n++;
    }
    assertEquals(hm.size(), n);
    cm.clear();
    cm.putAll(hm);
    assertEquals(cm.size(), n);
    CharArrayMap<Integer>.EntryIterator<Integer> iter2 = cm.entrySet().iterator();
    n = 0;
    while (iter2.hasNext()) {
        char[] keyc = iter2.nextKey();
        Integer val = iter2.currentValue();
        assertEquals(hm.get(new String(keyc)), val);
        iter2.setValue(val * 100);
        assertEquals(val * 100, (int) cm.get(keyc));
        n++;
    }
    assertEquals(hm.size(), n);
    cm.entrySet().clear();
    assertEquals(0, cm.size());
    assertEquals(0, cm.entrySet().size());
    assertTrue(cm.isEmpty());
}
Also used : CharArraySet(org.apache.lucene.analysis.CharArraySet) CharArrayMap(org.apache.lucene.analysis.CharArrayMap) CharArrayMap(org.apache.lucene.analysis.CharArrayMap)

Aggregations

CharArrayMap (org.apache.lucene.analysis.CharArrayMap)1 CharArraySet (org.apache.lucene.analysis.CharArraySet)1