diff --git a/src/main/java/org/htmlunit/util/OrderedFastHashMap.java b/src/main/java/org/htmlunit/util/OrderedFastHashMap.java index 7dd3c8975a0..a5b5d8fa362 100644 --- a/src/main/java/org/htmlunit/util/OrderedFastHashMap.java +++ b/src/main/java/org/htmlunit/util/OrderedFastHashMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Gargoyle Software Inc. + * Copyright (c) 2002-2024 Gargoyle Software Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ */ package org.htmlunit.util; +import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -54,7 +55,7 @@ * * @author René Schwietzke */ -public class OrderedFastHashMap implements Map { +public class OrderedFastHashMap implements Map, Serializable { // our placeholders in the map private static Object FREE_KEY_ = null; private static Object REMOVED_KEY_ = new Object(); diff --git a/src/test/java/org/htmlunit/util/OrderedFastHashMapTest.java b/src/test/java/org/htmlunit/util/OrderedFastHashMapTest.java index bef9e1e5580..1cb7f70c2b1 100644 --- a/src/test/java/org/htmlunit/util/OrderedFastHashMapTest.java +++ b/src/test/java/org/htmlunit/util/OrderedFastHashMapTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Gargoyle Software Inc. + * Copyright (c) 2002-2024 Gargoyle Software Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,8 +40,8 @@ import org.junit.Test; -@SuppressWarnings("checkstyle:AvoidNestedBlocks") public class OrderedFastHashMapTest { + @Test public void ctr() { final OrderedFastHashMap m = new OrderedFastHashMap<>(); @@ -218,38 +218,34 @@ public void keys() { f.put("dd", 4); f.put("ee", 5); - { - final List k = f.keys(); - assertEquals(5, k.size()); - assertTrue(k.contains("aa")); - assertTrue(k.contains("bb")); - assertTrue(k.contains("cc")); - assertTrue(k.contains("dd")); - assertTrue(k.contains("ee")); - } + final List k = f.keys(); + assertEquals(5, k.size()); + assertTrue(k.contains("aa")); + assertTrue(k.contains("bb")); + assertTrue(k.contains("cc")); + assertTrue(k.contains("dd")); + assertTrue(k.contains("ee")); + // remove from the middle assertEquals(Integer.valueOf(3), f.remove("cc")); f.remove("c"); - { - final List k = f.keys(); - assertEquals(4, k.size()); - assertTrue(k.contains("aa")); - assertTrue(k.contains("bb")); - assertTrue(k.contains("dd")); - assertTrue(k.contains("ee")); - } - + final List k2 = f.keys(); + assertEquals(4, k2.size()); + assertTrue(k2.contains("aa")); + assertTrue(k2.contains("bb")); + assertTrue(k2.contains("dd")); + assertTrue(k2.contains("ee")); + + // add to the end, remove unknown f.put("zz", 10); f.remove("c"); - { - final List k = f.keys(); - assertEquals(5, k.size()); - assertTrue(k.contains("aa")); - assertTrue(k.contains("bb")); - assertTrue(k.contains("dd")); - assertTrue(k.contains("ee")); - assertTrue(k.contains("zz")); - } + final List k3 = f.keys(); + assertEquals(5, k3.size()); + assertTrue(k3.contains("aa")); + assertTrue(k3.contains("bb")); + assertTrue(k3.contains("dd")); + assertTrue(k3.contains("ee")); + assertTrue(k3.contains("zz")); // ask for something unknown assertNull(f.get("unknown")); @@ -264,69 +260,69 @@ public void values() { f.put("dd", 4); f.put("ee", 5); - { - final List values = f.values(); - assertEquals(5, values.size()); - assertTrue(values.contains(1)); - assertTrue(values.contains(2)); - assertTrue(values.contains(3)); - assertTrue(values.contains(4)); - assertTrue(values.contains(5)); - } + // initial values + final List values = f.values(); + assertEquals(5, values.size()); + assertTrue(values.contains(1)); + assertTrue(values.contains(2)); + assertTrue(values.contains(3)); + assertTrue(values.contains(4)); + assertTrue(values.contains(5)); + // something removed assertEquals(Integer.valueOf(3), f.remove("cc")); + // something unknnown removed f.remove("c"); - { - final List values = f.values(); - assertEquals(4, values.size()); - assertTrue(values.contains(1)); - assertTrue(values.contains(2)); - assertTrue(values.contains(4)); - assertTrue(values.contains(5)); - } + final List v2 = f.values(); + assertEquals(4, v2.size()); + assertTrue(v2.contains(1)); + assertTrue(v2.contains(2)); + assertTrue(v2.contains(4)); + assertTrue(v2.contains(5)); } @Test - public void remove_simple() { - { - final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); - // remove instantly - m1.put("b", "value"); - assertEquals("value", m1.remove("b")); - assertEquals(0, m1.size()); - } + public void remove_simple_only_one() { + final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); + // remove instantly + m1.put("b", "value"); + assertEquals("value", m1.remove("b")); + assertEquals(0, m1.size()); + } - // remove the one before - { - final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); - m1.put("b", "bvalue"); - m1.put("c", "cvalue"); - assertEquals("bvalue", m1.remove("b")); - assertEquals("cvalue", m1.get("c")); - assertEquals(1, m1.size()); - } + @Test + public void remove_simple_first() { + // remove first + final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); + m1.put("b", "bvalue"); + m1.put("c", "cvalue"); + assertEquals("bvalue", m1.remove("b")); + assertEquals("cvalue", m1.get("c")); + assertEquals(1, m1.size()); + } + @Test + public void remove_simple_from_the_middle() { // remove the one in the middle - { - final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); - m1.put("a", "avalue"); - m1.put("b", "bvalue"); - m1.put("c", "cvalue"); - assertEquals("bvalue", m1.remove("b")); - assertEquals("avalue", m1.get("a")); - assertEquals("cvalue", m1.get("c")); - assertEquals(2, m1.size()); - assertEquals("a", m1.getKey(0)); - assertEquals("c", m1.getKey(1)); - } + final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); + m1.put("a", "avalue"); + m1.put("b", "bvalue"); + m1.put("c", "cvalue"); + assertEquals("bvalue", m1.remove("b")); + assertEquals("avalue", m1.get("a")); + assertEquals("cvalue", m1.get("c")); + assertEquals(2, m1.size()); + assertEquals("a", m1.getKey(0)); + assertEquals("c", m1.getKey(1)); + } + @Test + public void remove_simple_unknown() { // remove unknown - { - final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); - assertNull(m1.remove("a")); - m1.put("b", "value"); - assertNull(m1.remove("a")); - } + final OrderedFastHashMap m1 = new OrderedFastHashMap<>(); + assertNull(m1.remove("a")); + m1.put("b", "value"); + assertNull(m1.remove("a")); } @Test @@ -456,51 +452,55 @@ public void removeTryingToCoverEdges_Middle() { } @Test - public void removeByIndex() { - { - final OrderedFastHashMap m = new OrderedFastHashMap(); - m.put("a", 1); - m.put("b", 2); - m.put("c", 3); - m.remove(0); + public void removeByIndex_first() { + final OrderedFastHashMap m = new OrderedFastHashMap(); + m.put("a", 1); + m.put("b", 2); + m.put("c", 3); + m.remove(0); - assertEquals("b", m.getKey(0)); - assertEquals("c", m.getKey(1)); - assertEquals(2, m.size()); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap(); - m.put("a", 1); - m.put("b", 2); - m.put("c", 3); - m.remove(1); - - assertEquals("a", m.getKey(0)); - assertEquals("c", m.getKey(1)); - assertEquals(2, m.size()); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap(); - m.put("a", 1); - m.put("b", 2); - m.put("c", 3); - m.remove(2); - - assertEquals("a", m.getKey(0)); - assertEquals("b", m.getKey(1)); - assertEquals(2, m.size()); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap(); - m.put("a", 1); - m.put("b", 2); - m.put("c", 3); - m.remove(1); - m.remove(1); - m.remove(0); - - assertEquals(0, m.size()); - } + assertEquals("b", m.getKey(0)); + assertEquals("c", m.getKey(1)); + assertEquals(2, m.size()); + } + + @Test + public void removeByIndex_second() { + final OrderedFastHashMap m = new OrderedFastHashMap(); + m.put("a", 1); + m.put("b", 2); + m.put("c", 3); + m.remove(1); + + assertEquals("a", m.getKey(0)); + assertEquals("c", m.getKey(1)); + assertEquals(2, m.size()); + } + + @Test + public void removeByIndex_last() { + final OrderedFastHashMap m = new OrderedFastHashMap(); + m.put("a", 1); + m.put("b", 2); + m.put("c", 3); + m.remove(2); + + assertEquals("a", m.getKey(0)); + assertEquals("b", m.getKey(1)); + assertEquals(2, m.size()); + } + + @Test + public void removeByIndex_middle_to_empty() { + final OrderedFastHashMap m = new OrderedFastHashMap(); + m.put("a", 1); + m.put("b", 2); + m.put("c", 3); + m.remove(1); + m.remove(1); + m.remove(0); + + assertEquals(0, m.size()); } @Test @@ -556,7 +556,7 @@ public void removeLast() { } assertEquals(3 * 1972, m.size()); - BiConsumer t = (k, index) -> { + final BiConsumer t = (k, index) -> { assertEquals("V" + k, m.get(k)); assertEquals(k, m.getKey(index)); assertEquals("V" + k, m.getValue(index)); @@ -625,7 +625,7 @@ public void removeFirst_WithGrowth() { } // add these all and remove first - for (String k : keys) { + for (final String k : keys) { m.put(k, "V" + k); } // remove first @@ -647,65 +647,70 @@ public void removeFirst_WithGrowth() { } @Test - public void addFirst() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.addFirst("a", "1"); - assertEquals(1, m.size()); - assertEquals("a", m.getKey(0)); - assertEquals("1", m.getValue(0)); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.addFirst("a", "1"); - m.addFirst("b", "2"); - assertEquals(2, m.size()); - assertEquals("a", m.getKey(1)); - assertEquals("1", m.getValue(1)); - assertEquals("b", m.getKey(0)); - assertEquals("2", m.getValue(0)); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.add("a", "1"); - m.addFirst("b", "2"); - assertEquals(2, m.size()); - assertEquals("a", m.getKey(1)); - assertEquals("1", m.getValue(1)); - assertEquals("b", m.getKey(0)); - assertEquals("2", m.getValue(0)); - } + public void addFirst_to_empty() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.addFirst("a", "1"); + assertEquals(1, m.size()); + assertEquals("a", m.getKey(0)); + assertEquals("1", m.getValue(0)); } + @Test - public void addLast() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.addLast("a", "1"); - assertEquals(1, m.size()); - assertEquals("a", m.getKey(0)); - assertEquals("1", m.getValue(0)); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.addLast("a", "1"); - m.addLast("b", "2"); - assertEquals(2, m.size()); - assertEquals("a", m.getKey(0)); - assertEquals("1", m.getValue(0)); - assertEquals("b", m.getKey(1)); - assertEquals("2", m.getValue(1)); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.add("a", "1"); - m.addLast("b", "2"); - assertEquals(2, m.size()); - assertEquals("a", m.getKey(0)); - assertEquals("1", m.getValue(0)); - assertEquals("b", m.getKey(1)); - assertEquals("2", m.getValue(1)); - } + public void addFirst_twice() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.addFirst("a", "1"); + m.addFirst("b", "2"); + assertEquals(2, m.size()); + assertEquals("a", m.getKey(1)); + assertEquals("1", m.getValue(1)); + assertEquals("b", m.getKey(0)); + assertEquals("2", m.getValue(0)); + } + + @Test + public void addFirst__second_to_normal_added() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.add("a", "1"); + m.addFirst("b", "2"); + assertEquals(2, m.size()); + assertEquals("a", m.getKey(1)); + assertEquals("1", m.getValue(1)); + assertEquals("b", m.getKey(0)); + assertEquals("2", m.getValue(0)); + } + + @Test + public void addLast_to_empty() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.addLast("a", "1"); + assertEquals(1, m.size()); + assertEquals("a", m.getKey(0)); + assertEquals("1", m.getValue(0)); + } + + @Test + public void addLast_twice() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.addLast("a", "1"); + m.addLast("b", "2"); + assertEquals(2, m.size()); + assertEquals("a", m.getKey(0)); + assertEquals("1", m.getValue(0)); + assertEquals("b", m.getKey(1)); + assertEquals("2", m.getValue(1)); + } + + @Test + public void addLast_to_normally_added() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.add("a", "1"); + m.addLast("b", "2"); + assertEquals(2, m.size()); + assertEquals("a", m.getKey(0)); + assertEquals("1", m.getValue(0)); + assertEquals("b", m.getKey(1)); + assertEquals("2", m.getValue(1)); } @Test @@ -715,59 +720,65 @@ public void containsKey_Empty() { } @Test - public void containsKey_True() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey", "any"); - assertTrue(m.containsKey("akey")); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey1", "any"); - m.put("akey2", "any"); - m.put("akey3", "any"); - m.put("akey4", "any"); - assertTrue(m.containsKey("akey2")); - assertTrue(m.containsKey(new String("akey2"))); - } - { - // same hash and same content - final OrderedFastHashMap, String> m = new OrderedFastHashMap<>(); - final MockKey mockKey1 = new MockKey<>(10, "akey1"); - m.put(mockKey1, "any1"); - m.put(new MockKey(10, "akey2"), "any2"); - m.put(new MockKey(10, "akey3"), "any3"); - m.put(new MockKey(10, "akey4"), "any4"); - assertTrue(m.containsKey(mockKey1)); - assertTrue(m.containsKey(new MockKey<>(10, "akey1"))); - } + public void containsKey_True_single() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey", "any"); + assertTrue(m.containsKey("akey")); } @Test - public void containsKey_False() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey", "any"); - assertFalse(m.containsKey("akey1")); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey1", "any"); - m.put("akey2", "any"); - m.put("akey3", "any"); - assertFalse(m.containsKey("akey")); - } - { - // same hash but different content and same content different hash - final OrderedFastHashMap, String> m = new OrderedFastHashMap<>(); - m.put(new MockKey(10, "akey2"), "any2"); - m.put(new MockKey(10, "akey3"), "any3"); - m.put(new MockKey(10, "akey4"), "any4"); - assertFalse(m.containsKey(new MockKey<>(10, "akey"))); - assertFalse(m.containsKey(new MockKey<>(114, "akey2"))); - assertFalse(m.containsKey(new MockKey<>(113, "akey3"))); - assertFalse(m.containsKey(new MockKey<>(112, "akey4"))); - } + public void containsKey_True_many() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey1", "any"); + m.put("akey2", "any"); + m.put("akey3", "any"); + m.put("akey4", "any"); + assertTrue(m.containsKey("akey2")); + assertTrue(m.containsKey(new String("akey2"))); + } + + @Test + public void containsKey_True_content_based() { + // same hash and different content + final OrderedFastHashMap, String> m = new OrderedFastHashMap<>(); + final MockKey mockKey1 = new MockKey<>(10, "akey1"); + m.put(mockKey1, "any1"); + m.put(new MockKey(10, "akey2"), "any2"); + m.put(new MockKey(10, "akey3"), "any3"); + m.put(new MockKey(10, "akey4"), "any4"); + assertTrue(m.containsKey(mockKey1)); + assertTrue(m.containsKey(new MockKey<>(10, "akey1"))); + } + + @Test + public void containsKey_False_single() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey", "any"); + assertFalse(m.containsKey("akey1")); + } + + @Test + public void containsKey_False_many() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey1", "any"); + m.put("akey2", "any"); + m.put("akey3", "any"); + assertFalse(m.containsKey("akey")); + } + + @Test + public void containsKey_False_content_based() { + // same hash but different content + final OrderedFastHashMap, String> m = new OrderedFastHashMap<>(); + m.put(new MockKey(10, "akey2"), "any2"); + m.put(new MockKey(10, "akey3"), "any3"); + m.put(new MockKey(10, "akey4"), "any4"); + assertFalse(m.containsKey(new MockKey<>(10, "akey"))); + + // different hash, same content + assertFalse(m.containsKey(new MockKey<>(114, "akey2"))); + assertFalse(m.containsKey(new MockKey<>(113, "akey3"))); + assertFalse(m.containsKey(new MockKey<>(112, "akey4"))); } @Test @@ -777,55 +788,75 @@ public void containsValue_Empty() { } @Test - public void containsValue_True() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey3", "any"); - assertTrue(m.containsValue("any")); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey1", "any1"); - m.put("akey2", "any2"); - m.put("akey3", "any3"); - assertTrue(m.containsValue("any1")); - assertTrue(m.containsValue("any2")); - assertTrue(m.containsValue("any3")); - assertTrue(m.containsValue(new String("any1"))); - assertTrue(m.containsValue(new String("any2"))); - assertTrue(m.containsValue(new String("any3"))); - } + public void containsValue_True_single() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey3", "any"); + assertTrue(m.containsValue("any")); } @Test - public void containsValue_False() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey3", "any"); - assertFalse(m.containsValue("asdf")); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("akey1", "any1"); - m.put("akey2", "any2"); - m.put("akey3", "any3"); - assertFalse(m.containsValue("akey1")); - assertFalse(m.containsValue("akey2")); - assertFalse(m.containsValue("akey3")); - } + public void containsValue_True_content_based() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey1", "any1"); + m.put("akey2", "any2"); + m.put("akey3", "any3"); + assertTrue(m.containsValue("any1")); + assertTrue(m.containsValue("any2")); + assertTrue(m.containsValue("any3")); + assertTrue(m.containsValue(new String("any1"))); + assertTrue(m.containsValue(new String("any2"))); + assertTrue(m.containsValue(new String("any3"))); } @Test - public void isEmpty() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - assertTrue(m.isEmpty()); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("aaa", "a"); - assertFalse(m.isEmpty()); - } + public void containsValue_False_single() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey3", "any"); + assertFalse(m.containsValue("asdf")); + } + + @Test + public void containsValue_False_many() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("akey1", "any1"); + m.put("akey2", "any2"); + m.put("akey3", "any3"); + // not mistaking the key as value + assertFalse(m.containsValue("akey1")); + assertFalse(m.containsValue("akey2")); + assertFalse(m.containsValue("akey3")); + + // just some other values + assertFalse(m.containsValue("any5")); + assertFalse(m.containsValue("any6")); + assertFalse(m.containsValue("any7")); + } + + @Test + public void isEmpty_empty() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + assertTrue(m.isEmpty()); + } + + @Test + public void isEmpty_size_zero() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(0); + assertTrue(m.isEmpty()); + } + + @Test + public void isEmpty_false() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("aaa", "a"); + assertFalse(m.isEmpty()); + } + + @Test + public void isEmpty_after_clear() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("aaa", "a"); + m.clear(); + assertTrue(m.isEmpty()); } @Test @@ -865,333 +896,384 @@ public void entry() { assertThrows(UnsupportedOperationException.class, () -> e.setValue("foo")); } + @Test + public void entrySet_empty() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + final Set> set = m.entrySet(); + assertEquals(0, set.size()); + assertTrue(set.isEmpty()); + } + + @Test + public void entrySet_zero_size() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(0); + final Set> set = m.entrySet(); + assertEquals(0, set.size()); + assertTrue(set.isEmpty()); + } + @Test public void entrySet() { - { - OrderedFastHashMap m = new OrderedFastHashMap<>(); - final Set> set = m.entrySet(); - assertEquals(0, set.size()); - assertTrue(set.isEmpty()); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); - final Set> set = m.entrySet(); - assertEquals(1, set.size()); - assertFalse(set.isEmpty()); - - final Entry e1 = set.iterator().next(); - assertEquals("K1", e1.getKey()); - assertEquals("V1", e1.getValue()); - - final Map map = new LinkedHashMap<>(); - map.put("K1", "V1"); - map.put("K2", "V1"); - Iterator> i = map.entrySet().iterator(); - final Entry e2 = i.next(); - final Entry e3 = i.next(); - - assertTrue(set.contains(e1)); - assertTrue(set.contains(e2)); - - assertFalse(set.contains("a")); - assertFalse(set.contains(e3)); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - IntStream.rangeClosed(0, 11).forEach(i -> m.put("K" + i, "V" + i)); - final Set> set = m.entrySet(); - assertEquals(12, set.size()); - assertFalse(set.isEmpty()); - - final Iterator> iter = set.iterator(); - IntStream.rangeClosed(0, 11).forEach(i -> { - assertTrue(iter.hasNext()); - final Entry e = iter.next(); - assertEquals("K" + i, e.getKey()); - assertEquals("V" + i, e.getValue()); - }); - assertFalse(iter.hasNext()); - - // overread - assertThrows(NoSuchElementException.class, () -> iter.next()); - } + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + final Set> set = m.entrySet(); + assertEquals(1, set.size()); + assertFalse(set.isEmpty()); + + final Entry e1 = set.iterator().next(); + assertEquals("K1", e1.getKey()); + assertEquals("V1", e1.getValue()); + + final Map map = new OrderedFastHashMap<>(); + map.put("K1", "V1"); + map.put("K2", "V1"); + final Iterator> i = map.entrySet().iterator(); + final Entry e2 = i.next(); + final Entry e3 = i.next(); + + assertTrue(set.contains(e1)); + assertTrue(set.contains(e2)); + + assertFalse(set.contains("a")); + assertFalse(set.contains(e3)); } @Test - public void entrySet_toArray() { - { - final Map m = new OrderedFastHashMap<>(); - final Set> set = m.entrySet(); - assertEquals(0, set.toArray().length); - assertEquals(0, set.toArray(new Map.Entry[0]).length); - assertEquals(10, set.toArray(new String[10]).length); + public void entrySet_large() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + IntStream.rangeClosed(0, 11).forEach(i -> m.put("K" + i, "V" + i)); + final Set> set = m.entrySet(); + assertEquals(12, set.size()); + assertFalse(set.isEmpty()); - // unfilled despite length 10 - final String[] a = set.toArray(new String[10]); - for (int i = 0; i < 10; i++) { - assertNull(a[i]); - } - } - { - final Map m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); - final Set> set = m.entrySet(); - assertEquals(1, set.toArray().length); - assertEquals(1, set.toArray(new Map.Entry[0]).length); - assertEquals(1, set.toArray(new Map.Entry[1]).length); - - assertNotNull(set.toArray()[0]); - assertEquals("K1", ((Map.Entry) set.toArray()[0]).getKey()); - assertEquals("V1", ((Map.Entry) set.toArray()[0]).getValue()); - - assertEquals("K1", set.toArray(new Map.Entry[0])[0].getKey()); - assertEquals("V1", set.toArray(new Map.Entry[0])[0].getValue()); - - assertEquals("K1", set.toArray(new Map.Entry[1])[0].getKey()); - assertEquals("V1", set.toArray(new Map.Entry[1])[0].getValue()); - } - { - // ensure we get our array back when it is sufficiently sized - final Map m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); - final Set> set = m.entrySet(); - - // right sized - final Map.Entry[] array1 = new Map.Entry[1]; - assertSame(array1, set.toArray(array1)); - - // oversized - final Map.Entry[] array2 = new Map.Entry[10]; - assertSame(array2, set.toArray(array2)); - } - { - final Map m = new OrderedFastHashMap<>(); - IntStream.rangeClosed(0, 44).forEach(i -> m.put("K" + i, "V" + i)); - final Object[] a1 = m.entrySet().toArray(); - final Map.Entry[] a2 = m.entrySet().toArray(new Map.Entry[45]); - assertEquals(45, a1.length); - assertEquals(45, a2.length); - - for (int i = 0; i <= 44; i++) { - assertEquals("K" + i, ((Map.Entry) a1[i]).getKey()); - assertEquals("V" + i, ((Map.Entry) a1[i]).getValue()); - - assertEquals("K" + i, a2[i].getKey()); - assertEquals("V" + i, a2[i].getValue()); - } - ; - } + final Iterator> iter = set.iterator(); + IntStream.rangeClosed(0, 11).forEach(i -> { + assertTrue(iter.hasNext()); + final Entry e = iter.next(); + assertEquals("K" + i, e.getKey()); + assertEquals("V" + i, e.getValue()); + }); + assertFalse(iter.hasNext()); + + // overread + assertThrows(NoSuchElementException.class, () -> iter.next()); } @Test - public void keySet() { - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - final Set set = m.keySet(); - assertEquals(0, set.size()); - assertTrue(set.isEmpty()); - } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); - final Set set = m.keySet(); - assertEquals(1, set.size()); - assertFalse(set.isEmpty()); + public void entrySet_toArray_empty() { + final Map m = new OrderedFastHashMap<>(); + final Set> set = m.entrySet(); + assertEquals(0, set.toArray().length); + assertEquals(0, set.toArray(new Map.Entry[0]).length); + assertEquals(10, set.toArray(new String[10]).length); - final String e1 = set.iterator().next(); - assertEquals("K1", e1); + // unfilled despite length 10 + final String[] a = set.toArray(new String[10]); + for (int i = 0; i < 10; i++) { + assertNull(a[i]); } - { - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - IntStream.rangeClosed(0, 44).forEach(i -> m.put("K" + i, "V" + i)); - final Set set = m.keySet(); - assertEquals(45, set.size()); - assertFalse(set.isEmpty()); - - final Iterator iter = set.iterator(); - IntStream.rangeClosed(0, 44).forEach(i -> { - assertTrue(iter.hasNext()); - final String e = iter.next(); - assertEquals("K" + i, e); - assertTrue(set.contains("K" + i)); - }); - assertFalse(iter.hasNext()); - - // overread - assertThrows(NoSuchElementException.class, () -> iter.next()); + } + + @Test + public void entrySet_toArray_zero_sized() { + final Map m = new OrderedFastHashMap<>(0); + final Set> set = m.entrySet(); + assertEquals(0, set.toArray().length); + assertEquals(0, set.toArray(new Map.Entry[0]).length); + assertEquals(10, set.toArray(new String[10]).length); + + // unfilled despite length 10 + final String[] a = set.toArray(new String[10]); + for (int i = 0; i < 10; i++) { + assertNull(a[i]); } } @Test - public void keySet_toArray() { - { - final Map m = new OrderedFastHashMap<>(); - final Set set = m.keySet(); - assertEquals(0, set.toArray().length); - assertEquals(0, set.toArray(new String[0]).length); - assertEquals(10, set.toArray(new String[10]).length); + public void entrySet_toArray_normal() { + final Map m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + final Set> set = m.entrySet(); + assertEquals(1, set.toArray().length); + assertEquals(1, set.toArray(new Map.Entry[0]).length); + assertEquals(1, set.toArray(new Map.Entry[1]).length); - // unfilled despite length 10 - String[] a = set.toArray(new String[10]); - for (int i = 0; i < 10; i++) { - assertNull(a[i]); - } - } - { - final Map m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); - final Set set = m.keySet(); - assertEquals(1, set.toArray().length); - assertEquals(1, set.toArray(new String[0]).length); - assertEquals(1, set.toArray(new String[1]).length); - - assertEquals("K1", set.toArray()[0]); - assertEquals("K1", set.toArray(new String[0])[0]); - assertEquals("K1", set.toArray(new String[1])[0]); - } - { - // ensure we get our array back when it is sufficiently sized - final Map m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); - final Set set = m.keySet(); - - // right sized - String[] array1 = new String[1]; - assertSame(array1, set.toArray(array1)); - - // oversized - String[] array2 = new String[10]; - assertSame(array2, set.toArray(array2)); - } - { - final Map m = new OrderedFastHashMap<>(); - IntStream.rangeClosed(0, 44).forEach(i -> m.put("K" + i, "V" + i)); - final Object[] a1 = m.keySet().toArray(); - final String[] a2 = m.keySet().toArray(new String[45]); - assertEquals(45, a1.length); - assertEquals(45, a2.length); - - for (int i = 0; i <= 44; i++) { - assertEquals("K" + i, a1[i]); - assertEquals("K" + i, a2[i]); - } - ; - } + assertNotNull(set.toArray()[0]); + assertEquals("K1", ((Map.Entry) set.toArray()[0]).getKey()); + assertEquals("V1", ((Map.Entry) set.toArray()[0]).getValue()); + + assertEquals("K1", set.toArray(new Map.Entry[0])[0].getKey()); + assertEquals("V1", set.toArray(new Map.Entry[0])[0].getValue()); + + assertEquals("K1", set.toArray(new Map.Entry[1])[0].getKey()); + assertEquals("V1", set.toArray(new Map.Entry[1])[0].getValue()); + } + + @Test + public void entrySet_toArray_get_same_back() { + // ensure we get our array back when it is sufficiently sized + final Map m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + final Set> set = m.entrySet(); + + // right sized + final Map.Entry[] array1 = new Map.Entry[1]; + assertSame(array1, set.toArray(array1)); + + // oversized + final Map.Entry[] array2 = new Map.Entry[10]; + assertSame(array2, set.toArray(array2)); } @Test - public void putAll() { - { - // we cannot add ourselves! - IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { - OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.putAll(m); - }); - assertEquals("Cannot add myself", thrown.getMessage()); + public void entrySet_toArray_large() { + final Map m = new OrderedFastHashMap<>(); + IntStream.rangeClosed(0, 44).forEach(i -> m.put("K" + i, "V" + i)); + final Object[] a1 = m.entrySet().toArray(); + final Map.Entry[] a2 = m.entrySet().toArray(new Map.Entry[45]); + assertEquals(45, a1.length); + assertEquals(45, a2.length); + + for (int i = 0; i <= 44; i++) { + assertEquals("K" + i, ((Map.Entry) a1[i]).getKey()); + assertEquals("V" + i, ((Map.Entry) a1[i]).getValue()); + + assertEquals("K" + i, a2[i].getKey()); + assertEquals("V" + i, a2[i].getValue()); } - { - // empty - final Map src = new HashMap<>(); - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.putAll(src); - assertEquals(0, m.size()); + } + + @Test + public void keySet_empty() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + final Set set = m.keySet(); + assertEquals(0, set.size()); + assertTrue(set.isEmpty()); + } + + @Test + public void keySet_size_zero() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(0); + final Set set = m.keySet(); + assertEquals(0, set.size()); + assertTrue(set.isEmpty()); + } + + @Test + public void keySet_one() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + final Set set = m.keySet(); + assertEquals(1, set.size()); + assertFalse(set.isEmpty()); + + final String e1 = set.iterator().next(); + assertEquals("K1", e1); + } + + @Test + public void keySet_large() { + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + IntStream.rangeClosed(0, 44).forEach(i -> m.put("K" + i, "V" + i)); + final Set set = m.keySet(); + assertEquals(45, set.size()); + assertFalse(set.isEmpty()); + + final Iterator iter = set.iterator(); + IntStream.rangeClosed(0, 44).forEach(i -> { + assertTrue(iter.hasNext()); + final String e = iter.next(); + assertEquals("K" + i, e); + assertTrue(set.contains("K" + i)); + }); + assertFalse(iter.hasNext()); + + // overread + assertThrows(NoSuchElementException.class, () -> iter.next()); + } + + @Test + public void keySet_toArray_empty() { + final Map m = new OrderedFastHashMap<>(); + final Set set = m.keySet(); + assertEquals(0, set.toArray().length); + assertEquals(0, set.toArray(new String[0]).length); + assertEquals(10, set.toArray(new String[10]).length); + + // unfilled despite length 10 + final String[] a = set.toArray(new String[10]); + for (int i = 0; i < 10; i++) { + assertNull(a[i]); } - { - // target empty - final Map src = new LinkedHashMap<>(); - IntStream.rangeClosed(0, 10).forEach(i -> src.put("K" + i, "V" + i)); + } - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.putAll(src); - assertEquals(11, m.size()); - - final Iterator> iter = m.entrySet().iterator(); - IntStream.rangeClosed(0, 10).forEach(i -> { - assertTrue(iter.hasNext()); - final Entry e = iter.next(); - assertEquals("K" + i, e.getKey()); - assertEquals("V" + i, e.getValue()); - }); - assertFalse(iter.hasNext()); + @Test + public void keySet_toArray_one() { + final Map m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + final Set set = m.keySet(); + assertEquals(1, set.toArray().length); + assertEquals(1, set.toArray(new String[0]).length); + assertEquals(1, set.toArray(new String[1]).length); + + assertEquals("K1", set.toArray()[0]); + assertEquals("K1", set.toArray(new String[0])[0]); + assertEquals("K1", set.toArray(new String[1])[0]); + } + + @Test + public void keySet_toArray_same_array() { + // ensure we get our array back when it is sufficiently sized + final Map m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + final Set set = m.keySet(); + + // right sized + final String[] array1 = new String[1]; + assertSame(array1, set.toArray(array1)); + + // oversized + final String[] array2 = new String[10]; + assertSame(array2, set.toArray(array2)); + } + + @Test + public void keySet_toArray_many() { + final Map m = new OrderedFastHashMap<>(); + IntStream.rangeClosed(0, 44).forEach(i -> m.put("K" + i, "V" + i)); + final Object[] a1 = m.keySet().toArray(); + final String[] a2 = m.keySet().toArray(new String[45]); + assertEquals(45, a1.length); + assertEquals(45, a2.length); + + for (int i = 0; i <= 44; i++) { + assertEquals("K" + i, a1[i]); + assertEquals("K" + i, a2[i]); } - { - // src empty - final Map src = new LinkedHashMap<>(); + } + + @Test + public void putAll_cannot_add_self() { + // we cannot add ourselves! + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("K1", "V1"); + m.putAll(m); + }); + assertEquals("Cannot add myself", thrown.getMessage()); + } - m.putAll(src); - assertEquals(1, m.size()); + @Test + public void putAll_empty() { + // empty + final Map src = new HashMap<>(); + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.putAll(src); + assertEquals(0, m.size()); + } - final Iterator> iter = m.entrySet().iterator(); + @Test + public void putAll_target_empty() { + // target empty + final Map src = new LinkedHashMap<>(); + IntStream.rangeClosed(0, 10).forEach(i -> src.put("K" + i, "V" + i)); + + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.putAll(src); + assertEquals(11, m.size()); + + final Iterator> iter = m.entrySet().iterator(); + IntStream.rangeClosed(0, 10).forEach(i -> { assertTrue(iter.hasNext()); final Entry e = iter.next(); - assertEquals("K1", e.getKey()); - assertEquals("V1", e.getValue()); - assertFalse(iter.hasNext()); - } - { - // target not empty - final Map src = new LinkedHashMap<>(); - IntStream.rangeClosed(0, 17).forEach(i -> src.put("SRCK" + i, "SRCV" + i)); + assertEquals("K" + i, e.getKey()); + assertEquals("V" + i, e.getValue()); + }); + assertFalse(iter.hasNext()); + } - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - IntStream.rangeClosed(100, 111).forEach(i -> m.put("K" + i, "V" + i)); - - m.putAll(src); - assertEquals(12 + 18, m.size()); - - final Iterator> iter = m.entrySet().iterator(); - IntStream.rangeClosed(100, 111).forEach(i -> { - assertTrue(iter.hasNext()); - final Entry e = iter.next(); - assertEquals("K" + i, e.getKey()); - assertEquals("V" + i, e.getValue()); - }); + @Test + public void putAll_source_empty() { + // src empty + final Map src = new LinkedHashMap<>(); + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("K1", "V1"); + + m.putAll(src); + assertEquals(1, m.size()); + + final Iterator> iter = m.entrySet().iterator(); + assertTrue(iter.hasNext()); + final Entry e = iter.next(); + assertEquals("K1", e.getKey()); + assertEquals("V1", e.getValue()); + assertFalse(iter.hasNext()); + } + + @Test + public void putAll_target_not_empty() { + // target not empty + final Map src = new LinkedHashMap<>(); + IntStream.rangeClosed(0, 17).forEach(i -> src.put("SRCK" + i, "SRCV" + i)); + + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + IntStream.rangeClosed(100, 111).forEach(i -> m.put("K" + i, "V" + i)); + + m.putAll(src); + assertEquals(12 + 18, m.size()); + + final Iterator> iter = m.entrySet().iterator(); + IntStream.rangeClosed(100, 111).forEach(i -> { assertTrue(iter.hasNext()); - IntStream.rangeClosed(0, 17).forEach(i -> { - assertTrue(iter.hasNext()); - final Entry e = iter.next(); - assertEquals("SRCK" + i, e.getKey()); - assertEquals("SRCV" + i, e.getValue()); - }); - assertFalse(iter.hasNext()); - } - { - // same type but not same map - final OrderedFastHashMap src = new OrderedFastHashMap<>(); - IntStream.rangeClosed(19, 99).forEach(i -> src.put("K" + i, "V" + i)); + final Entry e = iter.next(); + assertEquals("K" + i, e.getKey()); + assertEquals("V" + i, e.getValue()); + }); + assertTrue(iter.hasNext()); + IntStream.rangeClosed(0, 17).forEach(i -> { + assertTrue(iter.hasNext()); + final Entry e = iter.next(); + assertEquals("SRCK" + i, e.getKey()); + assertEquals("SRCV" + i, e.getValue()); + }); + assertFalse(iter.hasNext()); + } - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - IntStream.rangeClosed(0, 18).forEach(i -> m.put("K" + i, "V" + i)); - - m.putAll(src); - assertEquals(100, m.size()); - - final Set> set = m.entrySet(); - final Iterator> iter = set.iterator(); - IntStream.rangeClosed(0, 99).forEach(i -> { - assertTrue(iter.hasNext()); - final Entry e = iter.next(); - assertEquals("K" + i, e.getKey()); - assertEquals("K" + i, e.getKey()); - assertEquals("V" + i, e.getValue()); - }); - assertFalse(iter.hasNext()); - } - { - // I can be added to other Map.putAll - final Map src = new OrderedFastHashMap<>(); - final Map m = new HashMap<>(); - m.putAll(src); - assertEquals(0, m.size()); - } + @Test + public void putAll_same_type_not_same_object() { + // same type but not same map + final OrderedFastHashMap src = new OrderedFastHashMap<>(); + IntStream.rangeClosed(19, 99).forEach(i -> src.put("K" + i, "V" + i)); + + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + IntStream.rangeClosed(0, 18).forEach(i -> m.put("K" + i, "V" + i)); + + m.putAll(src); + assertEquals(100, m.size()); + + final Set> set = m.entrySet(); + final Iterator> iter = set.iterator(); + IntStream.rangeClosed(0, 99).forEach(i -> { + assertTrue(iter.hasNext()); + final Entry e = iter.next(); + assertEquals("K" + i, e.getKey()); + assertEquals("K" + i, e.getKey()); + assertEquals("V" + i, e.getValue()); + }); + assertFalse(iter.hasNext()); + } + + @Test + public void putAll_to_another_map() { + // I can be added to other Map.putAll + final Map src = new OrderedFastHashMap<>(); + final Map m = new HashMap<>(); + m.putAll(src); + assertEquals(0, m.size()); } @Test public void collision() { - OrderedFastHashMap, String> f = new OrderedFastHashMap, String>(13); + final OrderedFastHashMap, String> f = new OrderedFastHashMap, String>(13); IntStream.range(0, 15).forEach(i -> { f.put(new MockKey(12, "k" + i), "v" + i); }); @@ -1248,24 +1330,19 @@ public void overflow() { @Test public void growFromSmall_InfiniteLoopIsssue() { for (int initSize = 0; initSize < 100; initSize++) { -// System.out.println("Initial Size: " + initSize); final OrderedFastHashMap m = new OrderedFastHashMap<>(initSize); for (int i = 0; i < 300; i++) { -// System.out.println(" Iteration: " + i); - // add one m.put(i, i); // ask for all for (int j = 0; j <= i; j++) { -// System.out.println(" Get: " + j); assertEquals(Integer.valueOf(j), m.get(j)); } // ask for something else for (int j = -1; j >= -100; j--) { -// System.out.println(" Get Null: " + j); assertNull(m.get(j)); } } @@ -1273,7 +1350,7 @@ public void growFromSmall_InfiniteLoopIsssue() { } /** - * Try to hit all slots with bad hashcodes + * Try to hit all slots with bad hashcodes. */ @Test public void hitEachSlot() { @@ -1325,72 +1402,84 @@ public void hitEachSlot() { } @Test - public void reverse() { - { - // can reverse empty without exception - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.reverse(); - } - { - // can reverse 0 sized map - final OrderedFastHashMap m = new OrderedFastHashMap<>(0); - m.reverse(); - } - { - // reverse one entry map - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("k0", "v0"); - m.reverse(); - assertEquals("k0", m.getEntry(0).getKey()); - assertEquals("v0", m.getEntry(0).getValue()); - } - { - // reverse two entries map - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("k0", "v0"); - m.put("k1", "v1"); - m.reverse(); - assertEquals("k1", m.getEntry(0).getKey()); - assertEquals("v1", m.getEntry(0).getValue()); - assertEquals("k0", m.getEntry(1).getKey()); - assertEquals("v0", m.getEntry(1).getValue()); - } - { - // reverse three entries map - final OrderedFastHashMap m = new OrderedFastHashMap<>(); - m.put("k0", "v0"); - m.put("k1", "v1"); - m.put("k2", "v2"); - m.reverse(); - assertEquals("k2", m.getEntry(0).getKey()); - assertEquals("v2", m.getEntry(0).getValue()); - assertEquals("k1", m.getEntry(1).getKey()); - assertEquals("v1", m.getEntry(1).getValue()); - assertEquals("k0", m.getEntry(2).getKey()); - assertEquals("v0", m.getEntry(2).getValue()); - } - { - // many entries, odd - final OrderedFastHashMap m = new OrderedFastHashMap<>(15); - IntStream.range(0, 117).mapToObj(Integer::valueOf).forEach(i -> m.put(i, i)); - m.reverse(); - IntStream.range(0, 117).mapToObj(Integer::valueOf).forEach(i -> { - OrderedFastHashMap.Entry e = m.getEntry(m.size() - i - 1); - assertEquals(i, e.getKey()); - assertEquals(i, e.getValue()); - }); - } - { - // many entries, even - final OrderedFastHashMap m = new OrderedFastHashMap<>(15); - IntStream.range(0, 80).mapToObj(Integer::valueOf).forEach(i -> m.put(i, i)); - m.reverse(); - IntStream.range(0, 80).mapToObj(Integer::valueOf).forEach(i -> { - OrderedFastHashMap.Entry e = m.getEntry(m.size() - i - 1); - assertEquals(i, e.getKey()); - assertEquals(i, e.getValue()); - }); - } + public void reverse_empty() { + // can reverse empty without exception + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.reverse(); + } + + @Test + public void reverse_zero_sized() { + // can reverse 0 sized map + final OrderedFastHashMap m = new OrderedFastHashMap<>(0); + m.reverse(); + } + + @Test + public void reverse_one() { + // reverse one entry map + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("k0", "v0"); + m.reverse(); + assertEquals("k0", m.getEntry(0).getKey()); + assertEquals("v0", m.getEntry(0).getValue()); + } + + @Test + public void reverse_two() { + // reverse two entries map + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("k0", "v0"); + m.put("k1", "v1"); + m.reverse(); + assertEquals("k1", m.getEntry(0).getKey()); + assertEquals("v1", m.getEntry(0).getValue()); + assertEquals("k0", m.getEntry(1).getKey()); + assertEquals("v0", m.getEntry(1).getValue()); + } + + @Test + public void reverse_three() { + // reverse three entries map + final OrderedFastHashMap m = new OrderedFastHashMap<>(); + m.put("k0", "v0"); + m.put("k1", "v1"); + m.put("k2", "v2"); + m.reverse(); + assertEquals("k2", m.getEntry(0).getKey()); + assertEquals("v2", m.getEntry(0).getValue()); + assertEquals("k1", m.getEntry(1).getKey()); + assertEquals("v1", m.getEntry(1).getValue()); + assertEquals("k0", m.getEntry(2).getKey()); + assertEquals("v0", m.getEntry(2).getValue()); + } + + @Test + public void reverse_many_odd() { + // many entries, odd + final OrderedFastHashMap m = new OrderedFastHashMap<>(15); + IntStream.range(0, 117).mapToObj(Integer::valueOf).forEach(i -> m.put(i, i)); + m.reverse(); + + IntStream.range(0, 117).mapToObj(Integer::valueOf).forEach(i -> { + final OrderedFastHashMap.Entry e = m.getEntry(m.size() - i - 1); + assertEquals(i, e.getKey()); + assertEquals(i, e.getValue()); + }); + } + + @Test + public void reverse_many_even() { + // many entries, even + final OrderedFastHashMap m = new OrderedFastHashMap<>(15); + IntStream.range(0, 80).mapToObj(Integer::valueOf).forEach(i -> m.put(i, i)); + m.reverse(); + + IntStream.range(0, 80).mapToObj(Integer::valueOf).forEach(i -> { + final OrderedFastHashMap.Entry e = m.getEntry(m.size() - i - 1); + assertEquals(i, e.getKey()); + assertEquals(i, e.getValue()); + }); } static class MockKey> implements Comparable> { @@ -1419,7 +1508,7 @@ public String toString() { } @Override - public int compareTo(MockKey o) { + public int compareTo(final MockKey o) { return o.key_.compareTo(this.key_); } diff --git a/src/test/java/org/htmlunit/util/RandomUtils.java b/src/test/java/org/htmlunit/util/RandomUtils.java index 753211f454b..aae411ae723 100644 --- a/src/test/java/org/htmlunit/util/RandomUtils.java +++ b/src/test/java/org/htmlunit/util/RandomUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Gargoyle Software Inc. + * Copyright (c) 2002-2024 Gargoyle Software Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.