Evgeny Kuvardin created COLLECTIONS-906:
-------------------------------------------

             Summary: Delete last last element in PrefixMap in PatricaTrie 
throw NullPointerException
                 Key: COLLECTIONS-906
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-906
             Project: Commons Collections
          Issue Type: Bug
    Affects Versions: 4.6.0, 4.5.0
            Reporter: Evgeny Kuvardin


 # Create PatriciaTrie with N elements
 # Create PrefixMap with 3 elements
 # Delete all elements or the last one

 
Script
 
{code:java}
// public static void main(String[] args) {
        PatriciaTrie<Integer> trie = new PatriciaTrie<>();
        trie.put("a", 1);
        trie.put("ab", 2);        Iterator<String> iterator = 
trie.prefixMap("a").keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println("Removing " + key);
            try {
                iterator.remove();
            } catch (NullPointerException failure) {
                System.out.println("Bug reproduced: iterator.remove() threw 
after deleting " + key);
                failure.printStackTrace(System.out);
                if (!key.equals("ab") || !trie.isEmpty()) {
                    throw new AssertionError("Unexpected failure state", 
failure);
                }
                break;
            }
        }
        if (!trie.isEmpty()) {
            throw new AssertionError("Expected both prefix entries to be 
removed");
        }
        System.out.println("Remaining entries after iterator removal: " + 
trie.size());        // Workaround: finish iterating before deleting from the 
backing trie.
        PatriciaTrie<Integer> safeTrie = new PatriciaTrie<>();
        safeTrie.put("a", 1);
        safeTrie.put("ab", 2);
        safeTrie.put("z", 3);
        String[] matchingKeys = 
safeTrie.prefixMap("a").keySet().toArray(String[]::new);
        for (String key : matchingKeys) {
            safeTrie.remove(key);
        }
        if (!safeTrie.equals(Map.of("z", 3))) {
            throw new AssertionError("Expected only the unrelated entry to 
survive");
        }
        System.out.println("Snapshot workaround passed; unrelated entry z 
remains.");
    }{code}
 



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to