-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gtest as a git submodule in third_party and integrate it into the build the same way WABT does. Adds a new executable, `binaryen-unittests`, to execute `gtest_main`. As a nontrivial example test, port one of the `TypeBuilder` tests from example/ to gtest/. Using gtest has a number of advantages over the current example tests: - Tests are compiled and linked at build time rather than runtime, surfacing errors earlier and speeding up test execution. - Tests are all built into a single binary, reducing overall link time and further reducing test overhead. - Tests are built from the same CMake project as the rest of Binaryen, so compiler settings (e.g. sanitizers) are applied uniformly rather than having to be separately set via the COMPILER_FLAGS environment variable. - Using the industry-standard gtest rather than our own script reduces our maintenance burden. Using gtest will lower the barrier to writing C++ tests and will hopefully lead to us having more proper unit tests.
- Loading branch information
Showing
12 changed files
with
139 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "third_party/googletest"] | ||
path = third_party/googletest | ||
url = https://github.com/google/googletest.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
if(BUILD_LLVM_DWARF) | ||
add_subdirectory(llvm-project) | ||
endif() | ||
|
||
include_directories( | ||
googletest/googletest | ||
googletest/googletest/include | ||
) | ||
|
||
add_library(gtest STATIC | ||
googletest/googletest/src/gtest-all.cc | ||
) | ||
|
||
add_library(gtest_main STATIC | ||
googletest/googletest/src/gtest_main.cc | ||
) |
Oops, something went wrong.