From e1c093e32e229925151e86d6c0d0d4347f5aa458 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 3 Mar 2024 09:01:19 +0100 Subject: jgvariant-core: Property tests for some maps and lists. Change-Id: I1edb66339290f02bf147582f9541b7fd2f3533ce --- .../mulk/jgvariant/core/DecoderPropertyTest.java | 105 +++++++++++++++------ 1 file changed, 74 insertions(+), 31 deletions(-) (limited to 'jgvariant-core') diff --git a/jgvariant-core/src/test/java/eu/mulk/jgvariant/core/DecoderPropertyTest.java b/jgvariant-core/src/test/java/eu/mulk/jgvariant/core/DecoderPropertyTest.java index 5e07ea0..dc195ae 100644 --- a/jgvariant-core/src/test/java/eu/mulk/jgvariant/core/DecoderPropertyTest.java +++ b/jgvariant-core/src/test/java/eu/mulk/jgvariant/core/DecoderPropertyTest.java @@ -1,7 +1,8 @@ package eu.mulk.jgvariant.core; import java.text.ParseException; -import java.util.Optional; +import java.util.*; +import java.util.function.Supplier; import net.jqwik.api.*; @SuppressWarnings("java:S2187") @@ -28,7 +29,7 @@ class DecoderPropertyTest { var decoder = decoder(); var bytes = decoder.encode(entityLeft); var entityRight = decoder.decode(bytes); - return entityLeft.equals(entityRight); + return Objects.equals(entityLeft, entityRight); } Decoder decoder(); @@ -39,6 +40,7 @@ class DecoderPropertyTest { @Provide Arbitrary anyVariant() { + // Base cases var anyString = Arbitraries.strings().map(s -> new Variant(parseSignature("s"), s)); var anyInt = Arbitraries.integers().map(i -> new Variant(parseSignature("i"), i)); var anyLong = Arbitraries.longs().map(l -> new Variant(parseSignature("x"), l)); @@ -48,35 +50,76 @@ class DecoderPropertyTest { var anyByte = Arbitraries.bytes().map(b -> new Variant(parseSignature("y"), b)); var anyShort = Arbitraries.shorts().map(s -> new Variant(parseSignature("n"), s)); var anyByteArray = Arbitraries.bytes().list().map(b -> new Variant(parseSignature("ay"), b)); - var anySome = - Arbitraries.lazyOf( - () -> - anyVariant() - .map( - x -> - new Variant( - parseSignature("m" + x.signature().toString()), - Optional.of(x.value())))); - var anyNone = - Arbitraries.lazyOf( - () -> - anyVariant() - .map( - x -> - new Variant( - parseSignature("m" + x.signature().toString()), Optional.empty()))); - // FIXME missing: list, tuple, dictionary, variant - return Arbitraries.oneOf( - anyString, - anyInt, - anyLong, - anyDouble, - anyBoolean, - anyByte, - anyShort, - anyByteArray, - anySome, - anyNone); + + // Singly recursive cases + Supplier> anySome = + () -> + anyVariant() + .map( + x -> + new Variant( + parseSignature("m" + x.signature().toString()), + Optional.of(x.value()))); + Supplier> anyNone = + () -> + anyVariant() + .map( + x -> + new Variant( + parseSignature("m" + x.signature().toString()), Optional.empty())); + Supplier> anyNestedVariant = + () -> anyVariant().map(v -> new Variant(parseSignature("v"), v)); + + // Fixed-multiplicity recursive cases + /* fixme: Object[] does not work for comparison by Object#equals() */ + Supplier> anyPair = + () -> + Combinators.combine(anyVariant(), anyVariant()) + .as( + (v1, v2) -> + new Variant( + parseSignature("(%s%s)".formatted(v1.signature(), v2.signature())), + new Object[] {v1.value(), v2.value()})); + Supplier> anyTriple = + () -> + Combinators.combine(anyVariant(), anyVariant(), anyVariant()) + .as( + (v1, v2, v3) -> + new Variant( + parseSignature( + "(%s%s%s)" + .formatted(v1.signature(), v2.signature(), v3.signature())), + new Object[] {v1.value(), v2.value(), v3.value()})); + + // Indefinite-multiplicity recursive cases + Supplier> anyVariantList = + () -> + anyVariant().list().map(ArrayList::new).map(l -> new Variant(parseSignature("av"), l)); + Supplier> anyStringVariantMap = + () -> + Arbitraries.maps(Arbitraries.strings(), anyVariant()) + .map(LinkedHashMap::new) + .map(m -> new Variant(parseSignature("a{sv}"), m)); + + // fixme: anyPair, anyTriple (see above) + return Arbitraries.frequencyOf( + Tuple.of(10, anyString), + Tuple.of(10, anyInt), + Tuple.of(10, anyLong), + Tuple.of(10, anyDouble), + Tuple.of(10, anyBoolean), + Tuple.of(10, anyByte), + Tuple.of(10, anyShort), + Tuple.of(10, anyByteArray), + Tuple.of(10, Arbitraries.lazy(anySome)), + Tuple.of(10, Arbitraries.lazy(anyNone)), + Tuple.of(10, Arbitraries.lazy(anyNestedVariant)), + Tuple.of(1, Arbitraries.lazy(anyStringVariantMap)), + Tuple.of(1, Arbitraries.lazy(anyVariantList)) + /*, + anyPair, + anyTriple + */ ); } private Signature parseSignature(String s) { -- cgit v1.2.3