This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 670382189c3f6a692deb3d658edffae245a91c59 Author: Gary Gregory <[email protected]> AuthorDate: Fri Jul 17 07:42:49 2026 -0700 Sort members. --- .../commons/collections4/map/Flat3MapTest.java | 30 +-- .../collections4/trie/PatriciaTrieTest.java | 242 ++++++++++----------- 2 files changed, 136 insertions(+), 136 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java b/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java index 31080f92d..ee29b6a3f 100644 --- a/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java @@ -278,21 +278,6 @@ public class Flat3MapTest<K, V> extends AbstractIterableMapTest<K, V> { assertTrue(contains); } - @Test - void testEntrySetRemoveChecksValue() { - final Flat3Map<Integer, String> m = new Flat3Map<>(); - m.put(ONE, TEN); - m.put(TWO, TWENTY); - // key present but value differs: entrySet().remove must not remove - assertFalse(m.entrySet().remove(new AbstractMap.SimpleEntry<>(ONE, TWENTY))); - assertEquals(2, m.size()); - assertEquals(TEN, m.get(ONE)); - // matching key and value: removes - assertTrue(m.entrySet().remove(new AbstractMap.SimpleEntry<>(ONE, TEN))); - assertFalse(m.containsKey(ONE)); - assertEquals(1, m.size()); - } - @Test @SuppressWarnings("unchecked") void testEntryIteratorSetValue1() throws Exception { @@ -364,6 +349,21 @@ public class Flat3MapTest<K, V> extends AbstractIterableMapTest<K, V> { putAndRemove(new Flat3Map<>()); } + @Test + void testEntrySetRemoveChecksValue() { + final Flat3Map<Integer, String> m = new Flat3Map<>(); + m.put(ONE, TEN); + m.put(TWO, TWENTY); + // key present but value differs: entrySet().remove must not remove + assertFalse(m.entrySet().remove(new AbstractMap.SimpleEntry<>(ONE, TWENTY))); + assertEquals(2, m.size()); + assertEquals(TEN, m.get(ONE)); + // matching key and value: removes + assertTrue(m.entrySet().remove(new AbstractMap.SimpleEntry<>(ONE, TEN))); + assertFalse(m.containsKey(ONE)); + assertEquals(1, m.size()); + } + @Test @SuppressWarnings("unchecked") void testEquals1() { diff --git a/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java b/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java index 46f1bef91..9ac993ab7 100644 --- a/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java +++ b/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java @@ -136,6 +136,102 @@ public class PatriciaTrieTest<V> extends AbstractSortedMapTest<String, V> { } } + @Test + public void testControlCharactersRegression() { + final String[] keys = { + "", + "\u0000", + "\u0001", + "\u0002", + "\u001F", + "\u007F", + "x", + "x\u0000", + "x\u0001", + "x\u0002", + "x\u001F", + "x\u007F", + "x\u0000y", + "x\u0001y" + }; + + // 1. Insertion, Retrieval, containsKey, size compared with HashMap + final java.util.Map<String, String> hashMap = new java.util.HashMap<>(); + final PatriciaTrie<String> trie = new PatriciaTrie<>(); + + for (int i = 0; i < keys.length; i++) { + final String key = keys[i]; + final String value = "val_" + i; + + assertFalse(trie.containsKey(key)); + assertNull(trie.get(key)); + + trie.put(key, value); + hashMap.put(key, value); + + assertEquals(hashMap.size(), trie.size()); + assertTrue(trie.containsKey(key)); + assertEquals(value, trie.get(key)); + } + + // Verify all elements present and distinct + for (int i = 0; i < keys.length; i++) { + final String key = keys[i]; + assertTrue(trie.containsKey(key)); + assertEquals("val_" + i, trie.get(key)); + } + + // 2. Iteration order (compared with TreeMap) + final java.util.TreeMap<String, String> treeMap = new java.util.TreeMap<>(hashMap); + final java.util.List<String> trieKeys = new java.util.ArrayList<>(trie.keySet()); + final java.util.List<String> expectedKeys = new java.util.ArrayList<>(treeMap.keySet()); + assertEquals(expectedKeys, trieKeys); + + // 3. prefixMap + final String[] prefixes = {"", "x", "x\u0000", "x\u0001", "x\u0002", "x\u001F", "x\u007F", "y"}; + for (final String prefix : prefixes) { + final java.util.SortedMap<String, String> prefixMap = trie.prefixMap(prefix); + final java.util.TreeMap<String, String> expectedPrefixMap = new java.util.TreeMap<>(); + for (final java.util.Map.Entry<String, String> entry : treeMap.entrySet()) { + if (entry.getKey().startsWith(prefix)) { + expectedPrefixMap.put(entry.getKey(), entry.getValue()); + } + } + assertEquals(expectedPrefixMap.size(), prefixMap.size()); + assertEquals(expectedPrefixMap, new java.util.TreeMap<>(prefixMap)); + } + + // 4. headMap, tailMap, subMap (compared with TreeMap) + for (final String k1 : keys) { + assertEquals(new java.util.TreeMap<>(treeMap.headMap(k1)), new java.util.TreeMap<>(trie.headMap(k1))); + assertEquals(new java.util.TreeMap<>(treeMap.tailMap(k1)), new java.util.TreeMap<>(trie.tailMap(k1))); + for (final String k2 : keys) { + if (k1.compareTo(k2) <= 0) { + assertEquals(new java.util.TreeMap<>(treeMap.subMap(k1, k2)), new java.util.TreeMap<>(trie.subMap(k1, k2))); + } else { + assertThrows(IllegalArgumentException.class, () -> trie.subMap(k1, k2)); + } + } + } + + // 5. Remove (compared with HashMap) + for (int i = 0; i < keys.length; i++) { + final String key = keys[i]; + final String value = "val_" + i; + + assertTrue(trie.containsKey(key)); + assertEquals(value, trie.remove(key)); + assertFalse(trie.containsKey(key)); + assertNull(trie.get(key)); + + hashMap.remove(key); + assertEquals(hashMap.size(), trie.size()); + } + + assertTrue(trie.isEmpty()); + assertEquals(0, trie.size()); + } + @Test void testHeadMap() { final PatriciaTrie<String> trie = new PatriciaTrie<>(); @@ -187,6 +283,30 @@ public class PatriciaTrieTest<V> extends AbstractSortedMapTest<String, V> { assertNull(headMap.get("ge")); } + @Test + void testNullTerminatedKey1() { + final PatriciaTrie<Integer> trie = new PatriciaTrie<>(); + trie.put("x", 0); + trie.put("x\u0000", 1); + assertTrue(trie.containsKey("x")); + assertTrue(trie.containsKey("x\u0000")); + assertEquals(2, trie.size()); + assertEquals(0, trie.get("x")); + assertEquals(1, trie.get("x\u0000")); + } + + @Test + void testNullTerminatedKey2() { + final PatriciaTrie<Integer> trie = new PatriciaTrie<>(); + trie.put("x\u0000", 1); + trie.put("x", 0); + assertTrue(trie.containsKey("x")); + assertTrue(trie.containsKey("x\u0000")); + assertEquals(2, trie.size()); + assertEquals(0, trie.get("x")); + assertEquals(1, trie.get("x\u0000")); + } + @Test void testPrefixMap() { final PatriciaTrie<String> trie = new PatriciaTrie<>(); @@ -521,6 +641,7 @@ public class PatriciaTrieTest<V> extends AbstractSortedMapTest<String, V> { assertFalse(iter.hasNext()); } + @Test void testPrefixMapSizes() { // COLLECTIONS-525 @@ -624,7 +745,6 @@ public class PatriciaTrieTest<V> extends AbstractSortedMapTest<String, V> { assertNull(subMap.get("ge")); } - @Test void testTailMap() { final PatriciaTrie<String> trie = new PatriciaTrie<>(); @@ -667,126 +787,6 @@ public class PatriciaTrieTest<V> extends AbstractSortedMapTest<String, V> { assertEquals("ge", tailMap.get("ge")); } - @Test - void testNullTerminatedKey1() { - final PatriciaTrie<Integer> trie = new PatriciaTrie<>(); - trie.put("x", 0); - trie.put("x\u0000", 1); - assertTrue(trie.containsKey("x")); - assertTrue(trie.containsKey("x\u0000")); - assertEquals(2, trie.size()); - assertEquals(0, trie.get("x")); - assertEquals(1, trie.get("x\u0000")); - } - - @Test - void testNullTerminatedKey2() { - final PatriciaTrie<Integer> trie = new PatriciaTrie<>(); - trie.put("x\u0000", 1); - trie.put("x", 0); - assertTrue(trie.containsKey("x")); - assertTrue(trie.containsKey("x\u0000")); - assertEquals(2, trie.size()); - assertEquals(0, trie.get("x")); - assertEquals(1, trie.get("x\u0000")); - } - - @Test - public void testControlCharactersRegression() { - final String[] keys = { - "", - "\u0000", - "\u0001", - "\u0002", - "\u001F", - "\u007F", - "x", - "x\u0000", - "x\u0001", - "x\u0002", - "x\u001F", - "x\u007F", - "x\u0000y", - "x\u0001y" - }; - - // 1. Insertion, Retrieval, containsKey, size compared with HashMap - final java.util.Map<String, String> hashMap = new java.util.HashMap<>(); - final PatriciaTrie<String> trie = new PatriciaTrie<>(); - - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - final String value = "val_" + i; - - assertFalse(trie.containsKey(key)); - assertNull(trie.get(key)); - - trie.put(key, value); - hashMap.put(key, value); - - assertEquals(hashMap.size(), trie.size()); - assertTrue(trie.containsKey(key)); - assertEquals(value, trie.get(key)); - } - - // Verify all elements present and distinct - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - assertTrue(trie.containsKey(key)); - assertEquals("val_" + i, trie.get(key)); - } - - // 2. Iteration order (compared with TreeMap) - final java.util.TreeMap<String, String> treeMap = new java.util.TreeMap<>(hashMap); - final java.util.List<String> trieKeys = new java.util.ArrayList<>(trie.keySet()); - final java.util.List<String> expectedKeys = new java.util.ArrayList<>(treeMap.keySet()); - assertEquals(expectedKeys, trieKeys); - - // 3. prefixMap - final String[] prefixes = {"", "x", "x\u0000", "x\u0001", "x\u0002", "x\u001F", "x\u007F", "y"}; - for (final String prefix : prefixes) { - final java.util.SortedMap<String, String> prefixMap = trie.prefixMap(prefix); - final java.util.TreeMap<String, String> expectedPrefixMap = new java.util.TreeMap<>(); - for (final java.util.Map.Entry<String, String> entry : treeMap.entrySet()) { - if (entry.getKey().startsWith(prefix)) { - expectedPrefixMap.put(entry.getKey(), entry.getValue()); - } - } - assertEquals(expectedPrefixMap.size(), prefixMap.size()); - assertEquals(expectedPrefixMap, new java.util.TreeMap<>(prefixMap)); - } - - // 4. headMap, tailMap, subMap (compared with TreeMap) - for (final String k1 : keys) { - assertEquals(new java.util.TreeMap<>(treeMap.headMap(k1)), new java.util.TreeMap<>(trie.headMap(k1))); - assertEquals(new java.util.TreeMap<>(treeMap.tailMap(k1)), new java.util.TreeMap<>(trie.tailMap(k1))); - for (final String k2 : keys) { - if (k1.compareTo(k2) <= 0) { - assertEquals(new java.util.TreeMap<>(treeMap.subMap(k1, k2)), new java.util.TreeMap<>(trie.subMap(k1, k2))); - } else { - assertThrows(IllegalArgumentException.class, () -> trie.subMap(k1, k2)); - } - } - } - - // 5. Remove (compared with HashMap) - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - final String value = "val_" + i; - - assertTrue(trie.containsKey(key)); - assertEquals(value, trie.remove(key)); - assertFalse(trie.containsKey(key)); - assertNull(trie.get(key)); - - hashMap.remove(key); - assertEquals(hashMap.size(), trie.size()); - } - - assertTrue(trie.isEmpty()); - assertEquals(0, trie.size()); - } - // void testCreate() throws Exception { // resetEmpty(); // writeExternalFormToDisk(
