diff options
Diffstat (limited to 'test/gtest/possible-contents.cpp')
-rw-r--r-- | test/gtest/possible-contents.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/gtest/possible-contents.cpp b/test/gtest/possible-contents.cpp index 27ab5b8fb..1e1f0e5c9 100644 --- a/test/gtest/possible-contents.cpp +++ b/test/gtest/possible-contents.cpp @@ -85,6 +85,9 @@ protected: PossibleContents::global("i32Global2", Type::i32); PossibleContents f64Global = PossibleContents::global("f64Global", Type::f64); PossibleContents anyGlobal = PossibleContents::global("anyGlobal", anyref); + PossibleContents funcGlobal = PossibleContents::global("funcGlobal", funcref); + PossibleContents nonNullFuncGlobal = + PossibleContents::global("funcGlobal", Type(HeapType::func, NonNullable)); PossibleContents nonNullFunc = PossibleContents::literal( Literal("func", Signature(Type::none, Type::none))); @@ -242,6 +245,53 @@ TEST_F(PossibleContentsTest, TestOracleMinimal) { Literal(int32_t(42))); } +// Asserts a and b have an intersection (or do not), and checks both orderings. +void assertHaveIntersection(PossibleContents a, PossibleContents b) { + EXPECT_TRUE(PossibleContents::haveIntersection(a, b)); + EXPECT_TRUE(PossibleContents::haveIntersection(b, a)); +} +void assertLackIntersection(PossibleContents a, PossibleContents b) { + EXPECT_FALSE(PossibleContents::haveIntersection(a, b)); + EXPECT_FALSE(PossibleContents::haveIntersection(b, a)); +} + +TEST_F(PossibleContentsTest, TestIntersection) { + // None has no contents, so nothing to intersect. + assertLackIntersection(none, none); + assertLackIntersection(none, i32Zero); + assertLackIntersection(none, many); + + // Many intersects with anything (but none). + assertHaveIntersection(many, many); + assertHaveIntersection(many, i32Zero); + + // Different exact types cannot intersect. + assertLackIntersection(exactI32, exactAnyref); + assertLackIntersection(i32Zero, exactAnyref); + + // But nullable ones can - the null can be the intersection. + assertHaveIntersection(exactFuncSignatureType, exactAnyref); + assertHaveIntersection(exactFuncSignatureType, funcNull); + assertHaveIntersection(anyNull, funcNull); + + // Identical types might. + assertHaveIntersection(exactI32, exactI32); + assertHaveIntersection(i32Zero, i32Zero); + assertHaveIntersection(exactFuncSignatureType, exactFuncSignatureType); + assertHaveIntersection(i32Zero, i32One); // TODO: this could be inferred false + + // Due to subtyping, an intersection might exist. + assertHaveIntersection(funcGlobal, funcGlobal); + assertHaveIntersection(funcGlobal, exactFuncSignatureType); + + // Neither is a subtype of the other, but nulls are possible, so a null can be + // the intersection. + assertHaveIntersection(funcGlobal, anyGlobal); + + // Without null on one side, we cannot intersect. + assertLackIntersection(nonNullFuncGlobal, anyGlobal); +} + TEST_F(PossibleContentsTest, TestOracleManyTypes) { // Test for a node with many possible types. The pass limits how many it // notices to not use excessive memory, so even though 4 are possible here, |