diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 2 | ||||
-rw-r--r-- | test/gtest/type-builder.cpp | 86 |
2 files changed, 87 insertions, 1 deletions
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 267e0763d..bc6b4b873 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -2161,7 +2161,7 @@ void test_typebuilder() { fieldMutables, 2); } - TypeBuilderSetSubType(builder, tempSubStructIndex, tempStructIndex); + TypeBuilderSetSubType(builder, tempSubStructIndex, tempStructHeapType); // TODO: Rtts (post-MVP?) diff --git a/test/gtest/type-builder.cpp b/test/gtest/type-builder.cpp index 4b282ea7c..525a45c31 100644 --- a/test/gtest/type-builder.cpp +++ b/test/gtest/type-builder.cpp @@ -533,3 +533,89 @@ TEST_F(NominalTest, TestSubTypes) { auto subTypes1 = subTypes.getStrictSubTypes(built[1]); EXPECT_EQ(subTypes1.size(), 0u); } + +// Test reuse of a previously built type as supertype. +TEST_F(NominalTest, TestExistingSuperType) { + // Build an initial type A + Type A; + { + TypeBuilder builder(1); + builder[0] = Struct(); + auto result = builder.build(); + ASSERT_TRUE(result); + auto built = *result; + A = Type(built[0], Nullable); + } + + // Build a type B <: A using a new builder + Type B; + { + TypeBuilder builder(1); + builder[0] = Struct(); + builder.setSubType(0, A.getHeapType()); + auto result = builder.build(); + ASSERT_TRUE(result); + auto built = *result; + B = Type(built[0], Nullable); + } + + // Test that B <: A where A is the initial type A + auto superOfB = B.getHeapType().getSuperType(); + ASSERT_TRUE(superOfB); + EXPECT_EQ(*superOfB, A.getHeapType()); + EXPECT_NE(B.getHeapType(), A.getHeapType()); +} + +// Test reuse of a previously built type as supertype, where in isorecursive +// mode canonicalization is performed. +TEST_F(IsorecursiveTest, TestExistingSuperType) { + // Build an initial type A1 + Type A1; + { + TypeBuilder builder(1); + builder[0] = Struct(); + auto result = builder.build(); + ASSERT_TRUE(result); + auto built = *result; + A1 = Type(built[0], Nullable); + } + + // Build a separate type A2 identical to A1 + Type A2; + { + TypeBuilder builder(1); + builder[0] = Struct(); + auto result = builder.build(); + ASSERT_TRUE(result); + auto built = *result; + A2 = Type(built[0], Nullable); + } + + // Build a type B1 <: A1 using a new builder + Type B1; + { + TypeBuilder builder(1); + builder[0] = Struct(); + builder.setSubType(0, A1.getHeapType()); + auto result = builder.build(); + ASSERT_TRUE(result); + auto built = *result; + B1 = Type(built[0], Nullable); + } + + // Build a type B2 <: A2 using a new builder + Type B2; + { + TypeBuilder builder(1); + builder[0] = Struct(); + builder.setSubType(0, A2.getHeapType()); + auto result = builder.build(); + ASSERT_TRUE(result); + auto built = *result; + B2 = Type(built[0], Nullable); + } + + // Test that A1 == A2 and B1 == B2 + EXPECT_EQ(A1.getHeapType(), A2.getHeapType()); + EXPECT_EQ(B1.getHeapType(), B2.getHeapType()); +} |