Skip to content

Commit 58234fe

Browse files
Test iterators for both ProofMap variants [ECR-3871]: (#1280)
Move tests of iterators to the base test to ensure they work for both ProofMap variants.
1 parent b6e1653 commit 58234fe

4 files changed

Lines changed: 71 additions & 72 deletions

File tree

exonum-java-binding/core/src/test/java/com/exonum/binding/core/storage/indices/BaseProofMapIndexProxyIntegrationTestable.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import static com.exonum.binding.test.Bytes.bytes;
3434
import static com.google.common.base.Preconditions.checkArgument;
3535
import static java.util.Collections.singletonList;
36+
import static java.util.Comparator.comparing;
37+
import static java.util.stream.Collectors.toList;
3638
import static org.hamcrest.CoreMatchers.containsString;
3739
import static org.hamcrest.CoreMatchers.equalTo;
3840
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -58,12 +60,12 @@
5860
import com.google.common.primitives.UnsignedBytes;
5961
import java.util.Arrays;
6062
import java.util.Collections;
63+
import java.util.Iterator;
6164
import java.util.List;
6265
import java.util.Map;
6366
import java.util.function.BiConsumer;
6467
import java.util.function.Consumer;
6568
import java.util.function.Function;
66-
import java.util.stream.Collectors;
6769
import java.util.stream.IntStream;
6870
import java.util.stream.Stream;
6971
import org.junit.jupiter.api.Test;
@@ -366,6 +368,52 @@ void isEmptyShouldReturnFalseForNonEmptyMap() {
366368
});
367369
}
368370

371+
@Test
372+
void keysTest() {
373+
runTestWithView(database::createFork, (map) -> {
374+
List<MapEntry<HashCode, String>> entries = createSortedMapEntries();
375+
376+
putAll(map, entries);
377+
378+
Iterator<HashCode> keysIterator = map.keys();
379+
List<HashCode> keysFromIter = ImmutableList.copyOf(keysIterator);
380+
List<HashCode> keysInMap = MapEntries.extractKeys(entries);
381+
382+
// Keys must appear in a lexicographical order.
383+
assertThat(keysFromIter, equalTo(keysInMap));
384+
});
385+
}
386+
387+
@Test
388+
void valuesTest() {
389+
runTestWithView(database::createFork, (map) -> {
390+
List<MapEntry<HashCode, String>> entries = createSortedMapEntries();
391+
392+
putAll(map, entries);
393+
394+
Iterator<String> valuesIterator = map.values();
395+
List<String> valuesFromIter = ImmutableList.copyOf(valuesIterator);
396+
List<String> valuesInMap = MapEntries.extractValues(entries);
397+
398+
// Values must appear in a lexicographical order of keys.
399+
assertThat(valuesFromIter, equalTo(valuesInMap));
400+
});
401+
}
402+
403+
@Test
404+
void entriesTest() {
405+
runTestWithView(database::createFork, (map) -> {
406+
List<MapEntry<HashCode, String>> entries = createSortedMapEntries();
407+
408+
putAll(map, entries);
409+
410+
Iterator<MapEntry<HashCode, String>> entriesIterator = map.entries();
411+
List<MapEntry> entriesFromIter = ImmutableList.copyOf(entriesIterator);
412+
// Entries must appear in a lexicographical order of keys.
413+
assertThat(entriesFromIter, equalTo(entries));
414+
});
415+
}
416+
369417
@Test
370418
@DisabledProofTest
371419
void getProofFromSingleKey() {
@@ -478,7 +526,17 @@ void update(ProofMapIndexProxy<HashCode, String> index) {
478526
index.put(key1, V1);
479527
}
480528

481-
List<MapEntry<HashCode, String>> createMapEntries() {
529+
/**
530+
* Creates `numOfEntries` map entries, sorted by key in lexicographical order:
531+
* [(key0, V1), (key2, V2), … (key_i, Vi)].
532+
*/
533+
private List<MapEntry<HashCode, String>> createSortedMapEntries() {
534+
Stream<HashCode> sortedKeys = getTestKeys().stream()
535+
.sorted(comparing(HashCode::asBytes, UnsignedBytes.lexicographicalComparator()));
536+
return createMapEntries(sortedKeys);
537+
}
538+
539+
private List<MapEntry<HashCode, String>> createMapEntries() {
482540
return createMapEntries(getTestKeys().stream());
483541
}
484542

@@ -491,6 +549,6 @@ List<MapEntry<HashCode, String>> createMapEntries(Stream<HashCode> proofKeys) {
491549
Stream<String> roundRobinValues = IntStream.range(0, Integer.MAX_VALUE)
492550
.mapToObj(i -> values.get(i % values.size()));
493551
return Streams.zip(keys, roundRobinValues, MapEntry::valueOf)
494-
.collect(Collectors.toList());
552+
.collect(toList());
495553
}
496554
}

exonum-java-binding/core/src/test/java/com/exonum/binding/core/storage/indices/ProofMapIndexProxyGroupNoKeyHashingIntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.exonum.binding.core.storage.indices;
1818

19-
import static com.exonum.binding.core.storage.indices.ProofMapIndexProxyNoKeyHashingIntegrationTest.SORTED_TEST_KEYS;
19+
import static com.exonum.binding.core.storage.indices.ProofMapIndexProxyNoKeyHashingIntegrationTest.TEST_KEYS;
2020

2121
import com.exonum.binding.common.hash.HashCode;
2222
import com.exonum.binding.common.serialization.StandardSerializers;
@@ -26,9 +26,9 @@
2626
class ProofMapIndexProxyGroupNoKeyHashingIntegrationTest
2727
extends BaseMapIndexGroupTestable<HashCode> {
2828

29-
private static final HashCode PK1 = SORTED_TEST_KEYS.get(0);
30-
private static final HashCode PK2 = SORTED_TEST_KEYS.get(1);
31-
private static final HashCode PK3 = SORTED_TEST_KEYS.get(2);
29+
private static final HashCode PK1 = TEST_KEYS.get(0);
30+
private static final HashCode PK2 = TEST_KEYS.get(1);
31+
private static final HashCode PK3 = TEST_KEYS.get(2);
3232

3333
private static final String GROUP_NAME = "proof_map_group_IT";
3434

exonum-java-binding/core/src/test/java/com/exonum/binding/core/storage/indices/ProofMapIndexProxyIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.exonum.binding.core.storage.indices;
1818

19-
import static java.util.stream.Collectors.toList;
19+
import static com.google.common.collect.ImmutableList.toImmutableList;
2020

2121
import com.exonum.binding.common.hash.HashCode;
2222
import com.exonum.binding.common.serialization.StandardSerializers;
@@ -45,7 +45,7 @@ class ProofMapIndexProxyIntegrationTest
4545
Bytes.bytes(0x10, 0x10)
4646
)
4747
.map(HashCode::fromBytes)
48-
.collect(toList());
48+
.collect(toImmutableList());
4949

5050
@Override
5151
List<HashCode> getTestKeys() {

exonum-java-binding/core/src/test/java/com/exonum/binding/core/storage/indices/ProofMapIndexProxyNoKeyHashingIntegrationTest.java

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import static com.exonum.binding.core.storage.indices.TestStorageItems.V1;
2424
import static com.exonum.binding.core.storage.indices.TestStorageItems.values;
2525
import static com.exonum.binding.test.Bytes.createPrefixed;
26-
import static org.hamcrest.CoreMatchers.equalTo;
26+
import static com.google.common.collect.ImmutableList.toImmutableList;
2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.junit.jupiter.api.Assertions.assertThrows;
2929

@@ -33,21 +33,17 @@
3333
import com.exonum.binding.core.storage.database.View;
3434
import com.exonum.binding.test.Bytes;
3535
import com.exonum.binding.test.CiOnly;
36-
import com.google.common.collect.ImmutableList;
37-
import com.google.common.primitives.UnsignedBytes;
3836
import java.util.ArrayList;
3937
import java.util.Arrays;
4038
import java.util.BitSet;
41-
import java.util.Iterator;
4239
import java.util.List;
43-
import java.util.stream.Collectors;
4440
import java.util.stream.Stream;
4541
import org.junit.jupiter.api.Test;
4642

4743
class ProofMapIndexProxyNoKeyHashingIntegrationTest
4844
extends BaseProofMapIndexProxyIntegrationTestable {
4945

50-
static final List<HashCode> SORTED_TEST_KEYS = Stream.of(
46+
static final List<HashCode> TEST_KEYS = Stream.of(
5147
Bytes.bytes(0x00),
5248
Bytes.bytes(0x01),
5349
Bytes.bytes(0x02),
@@ -64,15 +60,14 @@ class ProofMapIndexProxyNoKeyHashingIntegrationTest
6460
Bytes.bytes(0x10, 0x10)
6561
)
6662
.map(BaseProofMapIndexProxyIntegrationTestable::createRawProofKey)
67-
.sorted(UnsignedBytes.lexicographicalComparator())
6863
.map(HashCode::fromBytes)
69-
.collect(Collectors.toList());
64+
.collect(toImmutableList());
7065

7166
private static final HashCode INVALID_PROOF_KEY = HashCode.fromString("1234");
7267

7368
@Override
7469
List<HashCode> getTestKeys() {
75-
return SORTED_TEST_KEYS;
70+
return TEST_KEYS;
7671
}
7772

7873
@Override
@@ -110,52 +105,6 @@ void removeFailsIfInvalidKey() {
110105
(map) -> assertThrows(IllegalArgumentException.class, () -> map.remove(INVALID_PROOF_KEY)));
111106
}
112107

113-
@Test
114-
void keysTest() {
115-
runTestWithView(database::createFork, (map) -> {
116-
List<MapEntry<HashCode, String>> entries = createSortedMapEntries();
117-
118-
putAll(map, entries);
119-
120-
Iterator<HashCode> keysIterator = map.keys();
121-
List<HashCode> keysFromIter = ImmutableList.copyOf(keysIterator);
122-
List<HashCode> keysInMap = MapEntries.extractKeys(entries);
123-
124-
// Keys must appear in a lexicographical order.
125-
assertThat(keysFromIter, equalTo(keysInMap));
126-
});
127-
}
128-
129-
@Test
130-
void valuesTest() {
131-
runTestWithView(database::createFork, (map) -> {
132-
List<MapEntry<HashCode, String>> entries = createSortedMapEntries();
133-
134-
putAll(map, entries);
135-
136-
Iterator<String> valuesIterator = map.values();
137-
List<String> valuesFromIter = ImmutableList.copyOf(valuesIterator);
138-
List<String> valuesInMap = MapEntries.extractValues(entries);
139-
140-
// Values must appear in a lexicographical order of keys.
141-
assertThat(valuesFromIter, equalTo(valuesInMap));
142-
});
143-
}
144-
145-
@Test
146-
void entriesTest() {
147-
runTestWithView(database::createFork, (map) -> {
148-
List<MapEntry<HashCode, String>> entries = createSortedMapEntries();
149-
150-
putAll(map, entries);
151-
152-
Iterator<MapEntry<HashCode, String>> entriesIterator = map.entries();
153-
List<MapEntry> entriesFromIter = ImmutableList.copyOf(entriesIterator);
154-
// Entries must appear in a lexicographical order of keys.
155-
assertThat(entriesFromIter, equalTo(entries));
156-
});
157-
}
158-
159108
@Test
160109
@DisabledProofTest
161110
void getProof_FourEntryMap_LastByte_Contains1() {
@@ -429,14 +378,6 @@ private static byte[] keyFromString(String prefix) {
429378
return createPrefixed(keyPrefixBits.toByteArray(), PROOF_MAP_KEY_SIZE);
430379
}
431380

432-
/**
433-
* Creates `numOfEntries` map entries, sorted by key:
434-
* [(00…0PK1, V1), (00…0PK2, V2), … (00…0PKi, Vi)].
435-
*/
436-
List<MapEntry<HashCode, String>> createSortedMapEntries() {
437-
return createMapEntries(SORTED_TEST_KEYS.stream());
438-
}
439-
440381
/**
441382
* Creates 257 entries for a ProofMap that, when added to it, will make the underlying
442383
* Merkle-Patricia tree of the maximum height (256). Leaf nodes will be at depths

0 commit comments

Comments
 (0)