diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -148,7 +148,11 @@ ${fq_deps_list} ) - target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest libc_test_utils) + if(LIBC_UNITTEST_TEST_MAIN) + target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest testfilter_test libc_test_utils) + else() + target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest LibcUnitTestMain libc_test_utils) + endif() add_custom_command( TARGET ${fq_target_name} diff --git a/libc/test/utils/CMakeLists.txt b/libc/test/utils/CMakeLists.txt --- a/libc/test/utils/CMakeLists.txt +++ b/libc/test/utils/CMakeLists.txt @@ -1,8 +1,11 @@ add_subdirectory(FPUtil) add_subdirectory(CPP) +add_subdirectory(UnitTest) if(NOT LLVM_LIBC_FULL_BUILD) return() endif() add_subdirectory(tools) + + diff --git a/libc/utils/UnitTest/CMakeLists.txt b/libc/utils/UnitTest/CMakeLists.txt --- a/libc/utils/UnitTest/CMakeLists.txt +++ b/libc/utils/UnitTest/CMakeLists.txt @@ -7,3 +7,17 @@ target_include_directories(LibcUnitTest PUBLIC ${LIBC_SOURCE_DIR}) add_dependencies(LibcUnitTest libc.utils.CPP.standalone_cpp) target_link_libraries(LibcUnitTest PUBLIC libc_test_utils) + +add_library( + LibcUnitTestMain + LibcTestMain.cpp +) + +add_header_library( + LibcUnitTestHdr + HDRS + Test.h + LibcTest.h + FuschiaTest.h + PlatformDefs.h +) diff --git a/libc/utils/UnitTest/LibcTest.h b/libc/utils/UnitTest/LibcTest.h --- a/libc/utils/UnitTest/LibcTest.h +++ b/libc/utils/UnitTest/LibcTest.h @@ -70,7 +70,7 @@ virtual void SetUp() {} virtual void TearDown() {} - static int runTests(); + static int runTests(const char *); protected: static void addTest(Test *T); diff --git a/libc/utils/UnitTest/LibcTest.cpp b/libc/utils/UnitTest/LibcTest.cpp --- a/libc/utils/UnitTest/LibcTest.cpp +++ b/libc/utils/UnitTest/LibcTest.cpp @@ -1,4 +1,4 @@ -//===-- Implementation of the base class for libc unittests ---------------===// +///===-- Implementation of the base class for libc unittests ---------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -143,14 +143,18 @@ End = T; } -int Test::runTests() { +int Test::runTests(const char *TestFilter) { int TestCount = 0; int FailCount = 0; - for (Test *T = Start; T != nullptr; T = T->Next, ++TestCount) { + for (Test *T = Start; T != nullptr; T = T->Next) { const char *TestName = T->getName(); + std::string StrTestName(TestName); constexpr auto GREEN = "\033[32m"; constexpr auto RED = "\033[31m"; constexpr auto RESET = "\033[0m"; + if ((TestFilter != NULL) && (StrTestName != TestFilter)) { + continue; + } std::cout << GREEN << "[ RUN ] " << RESET << TestName << '\n'; RunContext Ctx; T->SetUp(); @@ -167,11 +171,20 @@ std::cout << GREEN << "[ OK ] " << RESET << TestName << '\n'; break; } + ++TestCount; } - std::cout << "Ran " << TestCount << " tests. " - << " PASS: " << TestCount - FailCount << ' ' - << " FAIL: " << FailCount << '\n'; + if (TestCount > 0) { + std::cout << "Ran " << TestCount << " tests. " + << " PASS: " << TestCount - FailCount << ' ' + << " FAIL: " << FailCount << '\n'; + } else { + std::cout << "No tests run.\n"; + if (TestFilter) { + std::cout << "No matching test for " << TestFilter << '\n'; + } + return 1; + } return FailCount > 0 ? 1 : 0; } @@ -346,8 +359,15 @@ return false; } +static const char *getTestFilter(int argc, char *argv[]) { + return argc > 1 ? argv[1] : NULL; +} + #endif // ENABLE_SUBPROCESS_TESTS } // namespace testing } // namespace __llvm_libc -int main() { return __llvm_libc::testing::Test::runTests(); } +int main(int argc, char *argv[]) { + const char *TestFilter = __llvm_libc::testing::getTestFilter(argc, argv); + return __llvm_libc::testing::Test::runTests(TestFilter); +}