CavalliumDBEngine/src/test/java/it/cavallium/dbengine/tests/TestDictionaryMapDeepHashMap.java

121 lines
3.9 KiB
Java
Raw Normal View History

2023-02-22 22:31:36 +01:00
package it.cavallium.dbengine.tests;
import static it.cavallium.dbengine.tests.DbTestUtils.BIG_STRING;
import static it.cavallium.dbengine.tests.DbTestUtils.ensureNoLeaks;
import static it.cavallium.dbengine.tests.DbTestUtils.isCIMode;
2023-02-24 00:18:02 +01:00
import static it.cavallium.dbengine.tests.DbTestUtils.run;
import static it.cavallium.dbengine.tests.DbTestUtils.tempDatabaseMapDictionaryDeepMapHashMap;
import static it.cavallium.dbengine.tests.DbTestUtils.tempDb;
2023-02-22 22:31:36 +01:00
import static it.cavallium.dbengine.tests.DbTestUtils.tempDictionary;
import static it.cavallium.dbengine.utils.StreamUtils.toList;
2023-02-22 22:31:36 +01:00
import com.google.common.collect.Streams;
2021-05-08 03:09:00 +02:00
import it.cavallium.dbengine.database.UpdateMode;
2021-09-10 13:34:26 +02:00
import java.util.List;
2023-02-24 00:18:02 +01:00
import java.util.Map.Entry;
2021-05-08 03:09:00 +02:00
import java.util.stream.Stream;
2021-08-28 22:42:51 +02:00
import org.junit.jupiter.api.AfterEach;
2023-02-24 00:18:02 +01:00
import org.junit.jupiter.api.Assertions;
2021-08-28 22:42:51 +02:00
import org.junit.jupiter.api.BeforeEach;
2023-02-24 00:18:02 +01:00
import org.junit.jupiter.params.ParameterizedTest;
2021-05-08 03:09:00 +02:00
import org.junit.jupiter.params.provider.Arguments;
2023-02-24 00:18:02 +01:00
import org.junit.jupiter.params.provider.MethodSource;
2021-05-08 03:09:00 +02:00
2021-09-06 01:15:13 +02:00
public abstract class TestDictionaryMapDeepHashMap {
2021-09-08 00:22:39 +02:00
private boolean checkLeaks = true;
2021-08-29 01:15:51 +02:00
2021-05-08 03:09:00 +02:00
private static boolean isTestBadKeysEnabled() {
2021-09-10 13:34:26 +02:00
return !isCIMode() && System.getProperty("badkeys", "true").equalsIgnoreCase("true");
2021-05-08 03:09:00 +02:00
}
2021-09-06 01:15:13 +02:00
protected abstract TemporaryDbGenerator getTempDbGenerator();
2021-05-08 03:09:00 +02:00
2023-02-22 22:31:36 +01:00
record Tuple2<X, Y>(X getT1, Y getT2) {}
2023-02-22 23:31:05 +01:00
record Tuple3<X, Y, Z>(X getT1, Y getT2, Z getT3) {}
record Tuple4<X, Y, Z, W>(X getT1, Y getT2, Z getT3, W getT4) {}
record Tuple5<X, Y, Z, W, X1>(X getT1, Y getT2, Z getT3, W getT4, X1 getT5) {}
2023-02-22 22:31:36 +01:00
2021-05-08 03:09:00 +02:00
private static Stream<Arguments> provideArgumentsPut() {
2021-09-10 13:34:26 +02:00
var goodKeys1 = isCIMode() ? List.of("12345") : List.of("12345", "zebra");
List<String> badKeys1;
2021-05-08 03:09:00 +02:00
if (isTestBadKeysEnabled()) {
2021-09-10 13:34:26 +02:00
badKeys1 = List.of("", "a", "aaaa", "aaaaaa");
2021-05-08 03:09:00 +02:00
} else {
2021-09-10 13:34:26 +02:00
badKeys1 = List.of();
2021-05-08 03:09:00 +02:00
}
2021-09-10 13:34:26 +02:00
var goodKeys2 = isCIMode() ? List.of("123456") : List.of("123456", "anatra", "", "a", "aaaaa", "aaaaaaa");
2021-05-08 03:09:00 +02:00
2021-09-10 13:34:26 +02:00
var values = isCIMode() ? List.of("val") : List.of("a", "", "\0", "\0\0", "z", "azzszgzczqz", BIG_STRING);
2021-05-08 03:09:00 +02:00
2023-02-22 22:31:36 +01:00
Stream<Tuple4<String, String, String, Boolean>> failOnKeys1 = badKeys1.stream()
.map(badKey1 -> new Tuple4<>(
2021-05-08 03:09:00 +02:00
badKey1,
goodKeys2.stream().findAny().orElseThrow(),
values.stream().findAny().orElseThrow(),
true
));
2023-02-22 22:31:36 +01:00
Stream<Tuple4<String, String, String, Boolean>> goodKeys1And2 = values.stream()
.map(value -> new Tuple4<>(
2021-05-08 03:09:00 +02:00
goodKeys1.stream().findAny().orElseThrow(),
goodKeys2.stream().findAny().orElseThrow(),
value,
false
));
2023-02-22 22:31:36 +01:00
Stream<Tuple4<String, String, String, Boolean>> keys1And2 = Streams
2021-05-08 03:09:00 +02:00
.concat(
goodKeys1And2,
failOnKeys1
);
return keys1And2
2023-02-22 22:31:36 +01:00
.flatMap(entryTuple -> Stream.of(UpdateMode.values())
.map(updateMode -> new Tuple5<>(updateMode,
2021-05-08 03:09:00 +02:00
entryTuple.getT1(),
entryTuple.getT2(),
entryTuple.getT3(),
entryTuple.getT4()
))
)
.map(fullTuple -> Arguments.of(fullTuple.getT1(),
fullTuple.getT2(),
fullTuple.getT3(),
fullTuple.getT4(),
fullTuple.getT1() != UpdateMode.ALLOW || fullTuple.getT5()
2023-02-22 22:31:36 +01:00
));
2021-05-08 03:09:00 +02:00
}
2021-08-28 22:42:51 +02:00
@BeforeEach
public void beforeEach() {
2023-02-28 23:10:31 +01:00
ensureNoLeaks();
2021-08-28 22:42:51 +02:00
}
@AfterEach
public void afterEach() {
2021-09-10 13:42:02 +02:00
if (!isCIMode() && checkLeaks) {
2023-02-28 23:10:31 +01:00
ensureNoLeaks();
2021-09-08 00:22:39 +02:00
}
2021-08-28 22:42:51 +02:00
}
2021-05-08 03:09:00 +02:00
@ParameterizedTest
@MethodSource("provideArgumentsPut")
public void testAtPutValueGetAllValues(UpdateMode updateMode, String key1, String key2, String value, boolean shouldFail) {
2023-02-24 00:18:02 +01:00
var stpVer = run(shouldFail, () -> tempDb(getTempDbGenerator(), db -> {
var map = tempDatabaseMapDictionaryDeepMapHashMap(tempDictionary(db, updateMode), 5);
map.at(null, key1).putValue(key2, value);
return toList(map
2023-02-28 23:10:31 +01:00
.getAllEntries(null, false)
2023-02-24 00:18:02 +01:00
.map(Entry::getValue)
.flatMap(maps -> maps.entrySet().stream())
.map(Entry::getValue));
2023-02-24 00:18:02 +01:00
}));
2021-05-08 03:09:00 +02:00
if (shouldFail) {
2021-09-08 00:22:39 +02:00
this.checkLeaks = false;
2021-05-08 03:09:00 +02:00
} else {
2023-02-24 00:18:02 +01:00
Assertions.assertEquals(List.of(value), stpVer);
2021-05-08 03:09:00 +02:00
}
}
}