Skip to content

Commit

Permalink
Use CTest for C++ tests (apache#8809)
Browse files Browse the repository at this point in the history
By using the `gtest_discover_tests` CMake macro the CPP and CRT tests can be configured to build binaries with a single test runner each. Once CTest has information about tests it can be used in IDE extensions such as [CMake Test Explorer](https://marketplace.visualstudio.com/items?itemName=fredericbonnet.cmake-test-adapter).

`ctest` can also run tests in parallel using the `-j` flag, which could be interesting in future.
  • Loading branch information
Mousius authored and ylc committed Jan 13, 2022
1 parent a106abc commit 6cdc2ff
Show file tree
Hide file tree
Showing 35 changed files with 42 additions and 273 deletions.
33 changes: 16 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ if(USE_PROFILER)
list(APPEND RUNTIME_SRCS ${RUNTIME_VM_PROFILER_SRCS})
endif(USE_PROFILER)

# Enable ctest if gtest is available
find_path(GTEST_INCLUDE_DIR gtest/gtest.h)
find_library(GTEST_LIB gtest "$ENV{GTEST_LIB}")
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
enable_testing()
include(CTest)
include(GoogleTest)
endif()

# Module rules
include(cmake/modules/VTA.cmake)
include(cmake/modules/StandaloneCrt.cmake)
Expand Down Expand Up @@ -579,26 +588,16 @@ if (HIDE_PRIVATE_SYMBOLS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_compile_definitions(tvm_allvisible PUBLIC DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
endif()

# Tests
set(TEST_EXECS "")
file(GLOB TEST_SRCS tests/cpp/*.cc)
find_path(GTEST_INCLUDE_DIR gtest/gtest.h)
find_library(GTEST_LIB gtest "$ENV{GTEST_LIB}")

# Create the `cpptest` target if we can find GTest. If not, we create dummy
# targets that give the user an informative error message.
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
foreach(__srcpath ${TEST_SRCS})
get_filename_component(__srcname ${__srcpath} NAME)
string(REPLACE ".cc" "" __execname ${__srcname})
add_executable(${__execname} ${__srcpath})
list(APPEND TEST_EXECS ${__execname})
target_include_directories(${__execname} SYSTEM PUBLIC ${GTEST_INCLUDE_DIR})
target_link_libraries(${__execname} PRIVATE ${TVM_TEST_LIBRARY_NAME} ${GTEST_LIB} pthread dl)
set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_ALL 1)
set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
endforeach()
add_custom_target(cpptest DEPENDS ${TEST_EXECS})
file(GLOB TEST_SRCS tests/cpp/*.cc)
add_executable(cpptest ${TEST_SRCS})
target_include_directories(cpptest SYSTEM PUBLIC ${GTEST_INCLUDE_DIR})
target_link_libraries(cpptest PRIVATE ${TVM_TEST_LIBRARY_NAME} ${GTEST_LIB} gtest_main pthread dl)
set_target_properties(cpptest PROPERTIES EXCLUDE_FROM_ALL 1)
set_target_properties(cpptest PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
gtest_discover_tests(cpptest)
elseif(NOT GTEST_INCLUDE_DIR)
add_custom_target(cpptest
COMMAND echo "Missing Google Test headers in include path"
Expand Down
24 changes: 7 additions & 17 deletions cmake/modules/StandaloneCrt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,16 @@ if(USE_MICRO)
PUBLIC_HEADER "${crt_headers}")
endforeach()

# Standalone CRT tests
file(GLOB TEST_SRCS ${CMAKE_SOURCE_DIR}/tests/crt/*_test.cc)
find_path(GTEST_INCLUDE_DIR gtest/gtest.h)
find_library(GTEST_LIB gtest "$ENV{GTEST_LIB}")

# Create the `crttest` target if we can find GTest. If not, we create dummy
# targets that give the user an informative error message.
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
foreach(__srcpath ${TEST_SRCS})
get_filename_component(__srcname ${__srcpath} NAME)
string(REPLACE ".cc" "" __execname ${__srcname})
add_executable(${__execname} ${__srcpath})
list(APPEND TEST_EXECS ${__execname})
target_include_directories(${__execname} PUBLIC ${GTEST_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro)
target_compile_options(${__execname} PRIVATE -pthread)
target_link_libraries(${__execname} ${cmake_crt_libraries} ${GTEST_LIB} pthread)
set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_ALL 1)
set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
endforeach()
add_custom_target(crttest DEPENDS ${TEST_EXECS})
file(GLOB TEST_SRCS ${CMAKE_SOURCE_DIR}/tests/crt/*_test.cc)
add_executable(crttest ${TEST_SRCS})
target_include_directories(crttest SYSTEM PUBLIC ${GTEST_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/standalone_crt/include ${CMAKE_SOURCE_DIR}/src/runtime/micro)
target_link_libraries(crttest PRIVATE ${cmake_crt_libraries} ${GTEST_LIB} gtest_main pthread dl)
set_target_properties(crttest PROPERTIES EXCLUDE_FROM_ALL 1)
set_target_properties(crttest PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
gtest_discover_tests(crttest)
elseif(NOT GTEST_INCLUDE_DIR)
add_custom_target(crttest
COMMAND echo "Missing Google Test headers in include path"
Expand Down
5 changes: 0 additions & 5 deletions tests/cpp/arith_simplify_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,3 @@ TEST(Simplify, Mod) {
auto es = ana.canonical_simplify(mod - x);
ICHECK(tvm::tir::is_zero(es));
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
8 changes: 1 addition & 7 deletions tests/cpp/attrs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,4 @@ TEST(Attrs, Basic) {
n->PrintDocString(os);
LOG(INFO) << "docstring\n" << os.str();
ICHECK(os.str().find("expr : PrimExpr, default=1") != std::string::npos);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
8 changes: 1 addition & 7 deletions tests/cpp/auto_scheduler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,4 @@ TEST(ComputeDAG, AccessAnalyzer) {
}
}
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
6 changes: 0 additions & 6 deletions tests/cpp/build_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,3 @@ TEST(BuildModule, Heterogeneous) {
ICHECK_LT(std::fabs(p_out[i] - (i + (i + 1.0) - (i - 1.0))), 1e-5);
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
8 changes: 1 addition & 7 deletions tests/cpp/container_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,4 @@ TEST(Optional, PackedCall) {
test_ffi(Optional<String>(s), static_cast<int>(kTVMObjectRValueRefArg));
test_ffi(s, static_cast<int>(kTVMObjectHandle));
test_ffi(String(s), static_cast<int>(kTVMObjectRValueRefArg));
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
8 changes: 1 addition & 7 deletions tests/cpp/dataflow_pattern_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,4 @@ TEST(DFPattern, HasShape) {
ICHECK(node);
ICHECK(node->pattern == a);
ICHECK(node->shape == shape);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
8 changes: 1 addition & 7 deletions tests/cpp/expr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,4 @@ TEST(ExprNodeRef, Basic) {
PrimExpr z = max(x + 1 + 2, 100);
const tir::MaxNode* op = z.as<tir::MaxNode>();
ICHECK(GetRef<ObjectRef>(op).same_as(z));
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
8 changes: 1 addition & 7 deletions tests/cpp/ir_functor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,4 @@ TEST(IRF, StmtMutator) {
ICHECK(new_block->writes[0]->region[0]->min.same_as(x));
ICHECK(new_block->match_buffers[0]->source->region[0]->min.same_as(x));
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
8 changes: 1 addition & 7 deletions tests/cpp/microtvm_runtime_standalone_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,4 @@ TEST(MicroStandaloneRuntime, BuildModule) {
}

#endif
#endif

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
#endif
8 changes: 1 addition & 7 deletions tests/cpp/object_protocol_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,4 @@ TEST(ObjectHierachy, Basic) {
ICHECK(refB.as<ObjA>() == nullptr);
ICHECK(refB.as<ObjAA>() == nullptr);
ICHECK(refB.as<ObjB>() != nullptr);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
8 changes: 1 addition & 7 deletions tests/cpp/packed_func_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,4 @@ TEST(TypedPackedFunc, RValue) {
// auto conversion.
tf(1, true);
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
6 changes: 0 additions & 6 deletions tests/cpp/parallel_for_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,3 @@ TEST(ParallelFor, Exception) {
}
ICHECK(exception);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
8 changes: 1 addition & 7 deletions tests/cpp/pattern_match_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,4 @@ TEST(Pattern, IntImm) {
ICHECK(!(v * c).Match(tx * ty));
// cannot match tx + 1 to v
ICHECK(!(v * c).Match((tx + 1) * 3));
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
6 changes: 0 additions & 6 deletions tests/cpp/profiling_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,3 @@ TEST(DefaultTimer, Basic) {
}
} // namespace runtime
} // namespace tvm

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/random_engine_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,4 @@ TEST(RandomEngine, Serialization) {

rand_state_b = rand_state_a;
for (int i = 0; i < 100000; i++) ICHECK_EQ(rng_a(), rng_b());
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
8 changes: 1 addition & 7 deletions tests/cpp/relay_build_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,4 @@ TEST(Relay, GetExprRefCount) {
ICHECK(ref_count[x.get()] == 2);
ICHECK(ref_count[y.get()] == 1);
ICHECK(ref_count[z.get()] == 1);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
6 changes: 0 additions & 6 deletions tests/cpp/relay_dismantler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,3 @@ TEST(Relay, TupleiGetItemSharedTuple) {
.as<CallNode>()
->args.size());
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/relay_pass_type_infer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,3 @@ TEST(Relay, SelfReference) {
auto expected = relay::FuncType(tvm::Array<relay::Type>{tensor_type}, tensor_type, {}, {});
ICHECK(tvm::StructuralEqual()(type_fx->checked_type(), expected));
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/relay_text_printer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,3 @@ TEST(Relay, LargeGraphPrint) {
};
ASSERT_EXIT((foo(), exit(0)), ::testing::ExitedWithCode(0), ".*");
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/relay_transform_sequential_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,3 @@ TEST(PassContextListConfigs, Basic) {
auto config = configs["relay.backend.use_auto_scheduler"];
ICHECK_EQ(config["type"], "IntImm");
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/support_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,3 @@ TEST(HashTests, HashStability) {

} // namespace test
} // namespace tvm

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/target_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,3 @@ TEST(TargetKindRegistryListTargetKinds, Basic) {
ICHECK_EQ(names.empty(), false);
ICHECK_EQ(std::count(std::begin(names), std::end(names), "llvm"), 1);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
8 changes: 1 addition & 7 deletions tests/cpp/tensor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,4 @@ TEST(Tensor, Reduce) {
auto C = te::compute(
{m, n}, [&](Var i, Var j) { return sum(max(1 + A[i][rv] + 1, B[j][rv]), {rv}); }, "C");
LOG(INFO) << C->op.as<te::ComputeOpNode>()->body;
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
6 changes: 0 additions & 6 deletions tests/cpp/texture_copy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,3 @@ TEST(TextureCopy, OverwritePoolSubview) {
}
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions tests/cpp/threading_backend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,3 @@ TEST(ThreadingBackend, TVMBackendParallelLaunchMultipleThreads) {
}
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
8 changes: 1 addition & 7 deletions tests/cpp/tir_analysis_side_effect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,4 @@ TEST(SimplePasses, SideEffect) {
ICHECK(tir::SideEffect(exp(tir::Cast(DataType::Float(32), i + 1))) == tir::CallEffectKind::kPure);
ICHECK(tir::SideEffect(tir::Call(DataType::Handle(), tir::builtin::tvm_storage_sync(), {})) ==
tir::CallEffectKind::kUpdateState);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
}
6 changes: 0 additions & 6 deletions tests/cpp/topi_ewise_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,3 @@ TEST(Tensor, Basic) {
}
} // namespace topi
} // namespace tvm

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}
Loading

0 comments on commit 6cdc2ff

Please sign in to comment.