|
22 | 22 | import org.elasticsearch.test.ESTestCase;
|
23 | 23 |
|
24 | 24 | import java.util.ArrayList;
|
| 25 | +import java.util.Arrays; |
25 | 26 | import java.util.Collection;
|
| 27 | +import java.util.HashMap; |
26 | 28 | import java.util.HashSet;
|
27 | 29 | import java.util.Map;
|
| 30 | +import java.util.function.Supplier; |
28 | 31 | import java.util.stream.Collectors;
|
| 32 | +import java.util.stream.IntStream; |
29 | 33 | import java.util.stream.Stream;
|
30 | 34 |
|
31 | 35 | import static java.util.Map.entry;
|
| 36 | +import static java.util.stream.Collectors.toMap; |
32 | 37 | import static org.hamcrest.Matchers.equalTo;
|
33 | 38 | import static org.hamcrest.Matchers.hasItem;
|
34 | 39 |
|
@@ -103,6 +108,31 @@ public void testOfEntries() {
|
103 | 108 | assertMapEntriesAndImmutability(map, entries);
|
104 | 109 | }
|
105 | 110 |
|
| 111 | + public void testDeepEquals() { |
| 112 | + final Supplier<String> keyGenerator = () -> randomAlphaOfLengthBetween(1, 5); |
| 113 | + final Supplier<int[]> arrayValueGenerator = () -> random().ints(randomInt(5)).toArray(); |
| 114 | + final Map<String, int[]> map = randomMap(randomInt(5), keyGenerator, arrayValueGenerator); |
| 115 | + final Map<String, int[]> mapCopy = map.entrySet().stream() |
| 116 | + .collect(toMap(Map.Entry::getKey, e -> Arrays.copyOf(e.getValue(), e.getValue().length))); |
| 117 | + |
| 118 | + assertTrue(Maps.deepEquals(map, mapCopy)); |
| 119 | + |
| 120 | + final Map<String, int[]> mapModified = mapCopy; |
| 121 | + if (mapModified.isEmpty()) { |
| 122 | + mapModified.put(keyGenerator.get(), arrayValueGenerator.get()); |
| 123 | + } else { |
| 124 | + if (randomBoolean()) { |
| 125 | + final String randomKey = mapModified.keySet().toArray(new String[0])[randomInt(mapModified.size() - 1)]; |
| 126 | + final int[] value = mapModified.get(randomKey); |
| 127 | + mapModified.put(randomKey, randomValueOtherThanMany((v) -> Arrays.equals(v, value), arrayValueGenerator)); |
| 128 | + } else { |
| 129 | + mapModified.put(randomValueOtherThanMany(mapModified::containsKey, keyGenerator), arrayValueGenerator.get()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + assertFalse(Maps.deepEquals(map, mapModified)); |
| 134 | + } |
| 135 | + |
106 | 136 | private void assertMapEntries(final Map<String, String> map, final Collection<Map.Entry<String, String>> entries) {
|
107 | 137 | for (var entry : entries) {
|
108 | 138 | assertThat("map [" + map + "] does not contain key [" + entry.getKey() + "]", map.keySet(), hasItem(entry.getKey()));
|
@@ -160,4 +190,10 @@ private void assertMapEntriesAndImmutability(
|
160 | 190 | assertMapImmutability(map);
|
161 | 191 | }
|
162 | 192 |
|
| 193 | + private static <K, V> Map<K, V> randomMap(int size, Supplier<K> keyGenerator, Supplier<V> valueGenerator) { |
| 194 | + final Map<K, V> map = new HashMap<>(); |
| 195 | + IntStream.range(0, size).forEach(i -> map.put(keyGenerator.get(), valueGenerator.get())); |
| 196 | + return map; |
| 197 | + } |
| 198 | + |
163 | 199 | }
|
0 commit comments