summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/example/type-builder.cpp65
-rw-r--r--test/example/type-builder.txt44
-rw-r--r--test/gtest/CMakeLists.txt8
-rw-r--r--test/gtest/type-builder.cpp64
4 files changed, 72 insertions, 109 deletions
diff --git a/test/example/type-builder.cpp b/test/example/type-builder.cpp
index d86643cf0..0fba565f1 100644
--- a/test/example/type-builder.cpp
+++ b/test/example/type-builder.cpp
@@ -5,70 +5,6 @@
using namespace wasm;
-// Construct Signature, Struct, and Array heap types using undefined types.
-void test_builder() {
- std::cout << ";; Test TypeBuilder\n";
-
- // (type $sig (func (param (ref $struct)) (result (ref $array) i32)))
- // (type $struct (struct (field (ref null $array) (mut rtt 0 $array))))
- // (type $array (array (mut externref)))
-
- TypeBuilder builder;
- assert(builder.size() == 0);
- builder.grow(3);
- assert(builder.size() == 3);
-
- Type refSig = builder.getTempRefType(builder[0], NonNullable);
- Type refStruct = builder.getTempRefType(builder[1], NonNullable);
- Type refArray = builder.getTempRefType(builder[2], NonNullable);
- Type refNullArray = builder.getTempRefType(builder[2], Nullable);
- Type rttArray = builder.getTempRttType(Rtt(0, builder[2]));
- Type refNullExt(HeapType::ext, Nullable);
-
- Signature sig(refStruct, builder.getTempTupleType({refArray, Type::i32}));
- Struct struct_({Field(refNullArray, Immutable), Field(rttArray, Mutable)});
- Array array(Field(refNullExt, Mutable));
-
- std::cout << "Before setting heap types:\n";
- std::cout << "(ref $sig) => " << refSig << "\n";
- std::cout << "(ref $struct) => " << refStruct << "\n";
- std::cout << "(ref $array) => " << refArray << "\n";
- std::cout << "(ref null $array) => " << refNullArray << "\n";
- std::cout << "(rtt 0 $array) => " << rttArray << "\n\n";
-
- builder[0] = sig;
- builder[1] = struct_;
- builder[2] = array;
-
- std::cout << "After setting heap types:\n";
- std::cout << "(ref $sig) => " << refSig << "\n";
- std::cout << "(ref $struct) => " << refStruct << "\n";
- std::cout << "(ref $array) => " << refArray << "\n";
- std::cout << "(ref null $array) => " << refNullArray << "\n";
- std::cout << "(rtt 0 $array) => " << rttArray << "\n\n";
-
- std::vector<HeapType> built = builder.build();
-
- Type newRefSig = Type(built[0], NonNullable);
- Type newRefStruct = Type(built[1], NonNullable);
- Type newRefArray = Type(built[2], NonNullable);
- Type newRefNullArray = Type(built[2], Nullable);
- Type newRttArray = Type(Rtt(0, built[2]));
-
- assert(refSig != newRefSig);
- assert(refStruct != newRefStruct);
- assert(refArray != newRefArray);
- assert(refNullArray != newRefNullArray);
- assert(rttArray != newRttArray);
-
- std::cout << "After building types:\n";
- std::cout << "(ref $sig) => " << newRefSig << "\n";
- std::cout << "(ref $struct) => " << newRefStruct << "\n";
- std::cout << "(ref $array) => " << newRefArray << "\n";
- std::cout << "(ref null $array) => " << newRefNullArray << "\n";
- std::cout << "(rtt 0 $array) => " << newRttArray << "\n\n";
-}
-
// Check that the builder works when there are duplicate definitions
void test_canonicalization() {
std::cout << ";; Test canonicalization\n";
@@ -496,7 +432,6 @@ int main() {
// Run the tests twice to ensure things still work when the global stores are
// already populated.
for (size_t i = 0; i < 2; ++i) {
- test_builder();
test_canonicalization();
test_basic();
test_recursive();
diff --git a/test/example/type-builder.txt b/test/example/type-builder.txt
index a219816e6..df67b599d 100644
--- a/test/example/type-builder.txt
+++ b/test/example/type-builder.txt
@@ -1,25 +1,3 @@
-;; Test TypeBuilder
-Before setting heap types:
-(ref $sig) => [T](ref [T](func))
-(ref $struct) => [T](ref [T](func))
-(ref $array) => [T](ref [T](func))
-(ref null $array) => [T](ref null [T](func))
-(rtt 0 $array) => [T](rtt 0 [T](func))
-
-After setting heap types:
-(ref $sig) => [T](ref [T](func (param [T](ref [T](struct (field [T](ref null [T](array (mut externref))) (mut [T](rtt 0 [T](array (mut externref)))))))) (result [T](ref [T](array (mut externref))) i32)))
-(ref $struct) => [T](ref [T](struct (field [T](ref null [T](array (mut externref))) (mut [T](rtt 0 [T](array (mut externref)))))))
-(ref $array) => [T](ref [T](array (mut externref)))
-(ref null $array) => [T](ref null [T](array (mut externref)))
-(rtt 0 $array) => [T](rtt 0 [T](array (mut externref)))
-
-After building types:
-(ref $sig) => (ref (func (param (ref (struct (field (ref null (array (mut externref))) (mut (rtt 0 (array (mut externref)))))))) (result (ref (array (mut externref))) i32)))
-(ref $struct) => (ref (struct (field (ref null (array (mut externref))) (mut (rtt 0 (array (mut externref)))))))
-(ref $array) => (ref (array (mut externref)))
-(ref null $array) => (ref null (array (mut externref)))
-(rtt 0 $array) => (rtt 0 (array (mut externref)))
-
;; Test canonicalization
;; Test basic
;; Test recursive types
@@ -48,28 +26,6 @@ After building types:
(func (param anyref) (result (ref null ...1)))
;; Test LUBs
-;; Test TypeBuilder
-Before setting heap types:
-(ref $sig) => [T](ref [T](func))
-(ref $struct) => [T](ref [T](func))
-(ref $array) => [T](ref [T](func))
-(ref null $array) => [T](ref null [T](func))
-(rtt 0 $array) => [T](rtt 0 [T](func))
-
-After setting heap types:
-(ref $sig) => [T](ref [T](func (param [T](ref [T](struct (field [T](ref null [T](array (mut externref))) (mut [T](rtt 0 [T](array (mut externref)))))))) (result [T](ref [T](array (mut externref))) i32)))
-(ref $struct) => [T](ref [T](struct (field [T](ref null [T](array (mut externref))) (mut [T](rtt 0 [T](array (mut externref)))))))
-(ref $array) => [T](ref [T](array (mut externref)))
-(ref null $array) => [T](ref null [T](array (mut externref)))
-(rtt 0 $array) => [T](rtt 0 [T](array (mut externref)))
-
-After building types:
-(ref $sig) => (ref (func (param (ref (struct (field (ref null (array (mut externref))) (mut (rtt 0 (array (mut externref)))))))) (result (ref (array (mut externref))) i32)))
-(ref $struct) => (ref (struct (field (ref null (array (mut externref))) (mut (rtt 0 (array (mut externref)))))))
-(ref $array) => (ref (array (mut externref)))
-(ref null $array) => (ref null (array (mut externref)))
-(rtt 0 $array) => (rtt 0 (array (mut externref)))
-
;; Test canonicalization
;; Test basic
;; Test recursive types
diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt
new file mode 100644
index 000000000..c58827a21
--- /dev/null
+++ b/test/gtest/CMakeLists.txt
@@ -0,0 +1,8 @@
+include_directories(../../third_party/googletest/googletest/include)
+
+set(unittest_SOURCES
+ type-builder.cpp
+)
+
+binaryen_add_executable(binaryen-unittests "${unittest_SOURCES}")
+target_link_libraries(binaryen-unittests gtest gtest_main)
diff --git a/test/gtest/type-builder.cpp b/test/gtest/type-builder.cpp
new file mode 100644
index 000000000..37e47c148
--- /dev/null
+++ b/test/gtest/type-builder.cpp
@@ -0,0 +1,64 @@
+#include <gtest/gtest.h>
+#include <wasm-type.h>
+
+using namespace wasm;
+
+TEST(TypeBuilder, Growth) {
+ TypeBuilder builder;
+ EXPECT_EQ(builder.size(), size_t{0});
+ builder.grow(3);
+ EXPECT_EQ(builder.size(), size_t{3});
+}
+
+TEST(TypeBuilder, Basics) {
+ // (type $sig (func (param (ref $struct)) (result (ref $array) i32)))
+ // (type $struct (struct (field (ref null $array) (mut rtt 0 $array))))
+ // (type $array (array (mut externref)))
+
+ TypeBuilder builder(3);
+ ASSERT_EQ(builder.size(), size_t{3});
+
+ Type refSig = builder.getTempRefType(builder[0], NonNullable);
+ Type refStruct = builder.getTempRefType(builder[1], NonNullable);
+ Type refArray = builder.getTempRefType(builder[2], NonNullable);
+ Type refNullArray = builder.getTempRefType(builder[2], Nullable);
+ Type rttArray = builder.getTempRttType(Rtt(0, builder[2]));
+ Type refNullExt(HeapType::ext, Nullable);
+
+ Signature sig(refStruct, builder.getTempTupleType({refArray, Type::i32}));
+ Struct struct_({Field(refNullArray, Immutable), Field(rttArray, Mutable)});
+ Array array(Field(refNullExt, Mutable));
+
+ builder[0] = sig;
+ builder[1] = struct_;
+ builder[2] = array;
+
+ std::vector<HeapType> built = builder.build();
+ ASSERT_EQ(built.size(), size_t{3});
+
+ // The built types should have the correct kinds.
+ ASSERT_TRUE(built[0].isSignature());
+ ASSERT_TRUE(built[1].isStruct());
+ ASSERT_TRUE(built[2].isArray());
+
+ // The built types should have the correct structure.
+ Type newRefSig = Type(built[0], NonNullable);
+ Type newRefStruct = Type(built[1], NonNullable);
+ Type newRefArray = Type(built[2], NonNullable);
+ Type newRefNullArray = Type(built[2], Nullable);
+ Type newRttArray = Type(Rtt(0, built[2]));
+
+ EXPECT_EQ(built[0].getSignature(),
+ Signature(newRefStruct, {newRefArray, Type::i32}));
+ EXPECT_EQ(
+ built[1].getStruct(),
+ Struct({Field(newRefNullArray, Immutable), Field(newRttArray, Mutable)}));
+ EXPECT_EQ(built[2].getArray(), Array(Field(refNullExt, Mutable)));
+
+ // The built types should be different from the temporary types.
+ EXPECT_NE(newRefSig, refSig);
+ EXPECT_NE(newRefStruct, refStruct);
+ EXPECT_NE(newRefArray, refArray);
+ EXPECT_NE(newRefNullArray, refNullArray);
+ EXPECT_NE(newRttArray, rttArray);
+}