diff options
Diffstat (limited to 'src/test/java/org/zwobble')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java index 526f6bb..b802386 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java @@ -57,7 +57,7 @@ public class ArbitraryValueAnalysisTests { } @Test - public void firstNonRecursiveVariantIsChosen() { + public void firstNonRecursivelyConstructibleVariantIsChosen() { var sumType = new SumType(NamespaceName.of(), "X"); var recursiveVariantType = new StructType(NamespaceName.of(), "Y"); var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z"); @@ -82,4 +82,112 @@ public class ArbitraryValueAnalysisTests { assertThat(variant, equalTo(nonRecursiveVariantType)); } + + @Test + public void boxIsRecursivelyConstructibleIffInnerTypeIsRecursivelyConstructible() { + var sumType = new SumType(NamespaceName.of(), "X"); + var recursiveVariantType = new StructType(NamespaceName.of(), "Y"); + var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z"); + var typesInfo = TypesInfoInMemory.empty(); + typesInfo.defineSumType( + sumType, + List.of( + new SumVariant(0, recursiveVariantType, recursiveVariantType), + new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType) + ), + List.of(), + NullSource.INSTANCE + ); + typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.box(sumType), NullSource.INSTANCE) + ))); + typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.box(NativeTypes.INT_64), NullSource.INSTANCE) + ))); + + var variant = selectArbitraryVariant(sumType, typesInfo); + + assertThat(variant, equalTo(nonRecursiveVariantType)); + } + + @Test + public void listIsAlwaysNonRecursivelyConstructible() { + var sumType = new SumType(NamespaceName.of(), "X"); + var recursiveVariantType = new StructType(NamespaceName.of(), "Y"); + var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z"); + var typesInfo = TypesInfoInMemory.empty(); + typesInfo.defineSumType( + sumType, + List.of( + new SumVariant(0, recursiveVariantType, recursiveVariantType), + new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType) + ), + List.of(), + NullSource.INSTANCE + ); + typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.list(sumType), NullSource.INSTANCE) + ))); + typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.list(NativeTypes.INT_64), NullSource.INSTANCE) + ))); + + var variant = selectArbitraryVariant(sumType, typesInfo); + + assertThat(variant, equalTo(recursiveVariantType)); + } + + @Test + public void optionIsAlwaysNonRecursivelyConstructible() { + var sumType = new SumType(NamespaceName.of(), "X"); + var recursiveVariantType = new StructType(NamespaceName.of(), "Y"); + var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z"); + var typesInfo = TypesInfoInMemory.empty(); + typesInfo.defineSumType( + sumType, + List.of( + new SumVariant(0, recursiveVariantType, recursiveVariantType), + new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType) + ), + List.of(), + NullSource.INSTANCE + ); + typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.option(sumType), NullSource.INSTANCE) + ))); + typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.option(NativeTypes.INT_64), NullSource.INSTANCE) + ))); + + var variant = selectArbitraryVariant(sumType, typesInfo); + + assertThat(variant, equalTo(recursiveVariantType)); + } + + @Test + public void sharedIsRecursivelyConstructibleIffInnerTypeIsRecursivelyConstructible() { + var sumType = new SumType(NamespaceName.of(), "X"); + var recursiveVariantType = new StructType(NamespaceName.of(), "Y"); + var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z"); + var typesInfo = TypesInfoInMemory.empty(); + typesInfo.defineSumType( + sumType, + List.of( + new SumVariant(0, recursiveVariantType, recursiveVariantType), + new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType) + ), + List.of(), + NullSource.INSTANCE + ); + typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.shared(sumType), NullSource.INSTANCE) + ))); + typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of( + new Field("x", NativeTypes.shared(NativeTypes.INT_64), NullSource.INSTANCE) + ))); + + var variant = selectArbitraryVariant(sumType, typesInfo); + + assertThat(variant, equalTo(nonRecursiveVariantType)); + } } |
