diff --git a/libc/test/integration/CMakeLists.txt b/libc/test/integration/CMakeLists.txt --- a/libc/test/integration/CMakeLists.txt +++ b/libc/test/integration/CMakeLists.txt @@ -1,5 +1,10 @@ add_custom_target(libc-integration-tests) +function(add_libc_integration_test_suite name) + add_custom_target(${name}) + add_dependencies(libc-integration-tests ${name}) +endfunction() + add_library( libc_integration_test_dummy STATIC diff --git a/libc/test/integration/src/CMakeLists.txt b/libc/test/integration/src/CMakeLists.txt --- a/libc/test/integration/src/CMakeLists.txt +++ b/libc/test/integration/src/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(stdlib) +add_subdirectory(threads) diff --git a/libc/test/src/threads/CMakeLists.txt b/libc/test/integration/src/threads/CMakeLists.txt rename from libc/test/src/threads/CMakeLists.txt rename to libc/test/integration/src/threads/CMakeLists.txt --- a/libc/test/src/threads/CMakeLists.txt +++ b/libc/test/integration/src/threads/CMakeLists.txt @@ -1,29 +1,32 @@ -add_libc_testsuite(libc_threads_unittests) +add_libc_integration_test_suite(libc-threads-integration-tests) -add_libc_unittest( - call_once_test +add_integration_test( + mtx_test SUITE - libc_threads_unittests + libc-threads-integration-tests SRCS - call_once_test.cpp + mtx_test.cpp + LOADER + libc.loader.linux.crt1 DEPENDS libc.include.threads - libc.src.threads.call_once + libc.src.errno.errno libc.src.threads.mtx_destroy libc.src.threads.mtx_init libc.src.threads.mtx_lock libc.src.threads.mtx_unlock libc.src.threads.thrd_create libc.src.threads.thrd_join - libc.src.__support.CPP.atomic ) -add_libc_unittest( +add_integration_test( thrd_test SUITE - libc_threads_unittests + libc-threads-integration-tests SRCS thrd_test.cpp + LOADER + libc.loader.linux.crt1 DEPENDS libc.include.threads libc.src.errno.errno @@ -31,29 +34,34 @@ libc.src.threads.thrd_join ) -add_libc_unittest( - mtx_test +add_integration_test( + call_once_test SUITE - libc_threads_unittests + libc-threads-integration-tests SRCS - mtx_test.cpp + call_once_test.cpp + LOADER + libc.loader.linux.crt1 DEPENDS libc.include.threads - libc.src.errno.errno + libc.src.threads.call_once libc.src.threads.mtx_destroy libc.src.threads.mtx_init libc.src.threads.mtx_lock libc.src.threads.mtx_unlock libc.src.threads.thrd_create libc.src.threads.thrd_join + libc.src.__support.CPP.atomic ) -add_libc_unittest( +add_integration_test( cnd_test SUITE - libc_threads_unittests + libc-threads-integration-tests SRCS cnd_test.cpp + LOADER + libc.loader.linux.crt1 DEPENDS libc.include.threads libc.src.threads.cnd_init diff --git a/libc/test/src/threads/call_once_test.cpp b/libc/test/integration/src/threads/call_once_test.cpp rename from libc/test/src/threads/call_once_test.cpp rename to libc/test/integration/src/threads/call_once_test.cpp --- a/libc/test/src/threads/call_once_test.cpp +++ b/libc/test/integration/src/threads/call_once_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for call_once -------------------------------------------===// +//===-- Tests for call_once -----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,6 @@ // //===----------------------------------------------------------------------===// -#include "include/threads.h" #include "src/__support/CPP/atomic.h" #include "src/threads/call_once.h" #include "src/threads/mtx_destroy.h" @@ -15,7 +14,10 @@ #include "src/threads/mtx_unlock.h" #include "src/threads/thrd_create.h" #include "src/threads/thrd_join.h" -#include "utils/UnitTest/Test.h" + +#include "utils/IntegrationTest/test.h" + +#include static constexpr unsigned int NUM_THREADS = 5; static __llvm_libc::cpp::Atomic thread_count; @@ -32,7 +34,7 @@ return 0; } -TEST(LlvmLibcCallOnceTest, CallFrom5Threads) { +void call_from_5_threads() { // Ensure the call count and thread count are 0 to begin with. call_count = 0; thread_count = 0; @@ -73,7 +75,7 @@ // Test the synchronization aspect of the call_once function. // This is not a fool proof test, but something which might be // useful when we add a flakiness detection scheme to UnitTest. -TEST(LlvmLibcCallOnceTest, TestSynchronization) { +void test_synchronization() { start_count = 0; done_count = 0; @@ -111,3 +113,9 @@ __llvm_libc::mtx_destroy(&once_func_blocker); } + +int main() { + call_from_5_threads(); + test_synchronization(); + return 0; +} diff --git a/libc/test/src/threads/cnd_test.cpp b/libc/test/integration/src/threads/cnd_test.cpp rename from libc/test/src/threads/cnd_test.cpp rename to libc/test/integration/src/threads/cnd_test.cpp --- a/libc/test/src/threads/cnd_test.cpp +++ b/libc/test/integration/src/threads/cnd_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for condition variable broadcast fucntionality ----------===// +//===-- Tests for standard condition variables ----------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,6 @@ // //===----------------------------------------------------------------------===// -#include "include/threads.h" #include "src/__support/CPP/atomic.h" #include "src/threads/cnd_broadcast.h" #include "src/threads/cnd_destroy.h" @@ -19,7 +18,10 @@ #include "src/threads/mtx_unlock.h" #include "src/threads/thrd_create.h" #include "src/threads/thrd_join.h" -#include "utils/UnitTest/Test.h" + +#include "utils/IntegrationTest/test.h" + +#include namespace wait_notify_broadcast_test { @@ -54,7 +56,7 @@ return 0; } -TEST(LlvmLibcCndVarTest, WaitNotifyBroadcastTest) { +void wait_notify_broadcast_test() { __llvm_libc::cnd_init(&broadcast_cnd); __llvm_libc::cnd_init(&threads_ready_cnd); __llvm_libc::mtx_init(&broadcast_mtx, mtx_plain); @@ -111,7 +113,7 @@ return 0x600D; } -TEST(LlvmLibcCndVarTest, SingleWaiterTest) { +void single_waiter_test() { ASSERT_EQ(__llvm_libc::mtx_init(&waiter_mtx, mtx_plain), int(thrd_success)); ASSERT_EQ(__llvm_libc::mtx_init(&main_thread_mtx, mtx_plain), int(thrd_success)); @@ -142,3 +144,9 @@ } } // namespace single_waiter_test + +int main() { + wait_notify_broadcast_test::wait_notify_broadcast_test(); + single_waiter_test::single_waiter_test(); + return 0; +} diff --git a/libc/test/src/threads/mtx_test.cpp b/libc/test/integration/src/threads/mtx_test.cpp rename from libc/test/src/threads/mtx_test.cpp rename to libc/test/integration/src/threads/mtx_test.cpp --- a/libc/test/src/threads/mtx_test.cpp +++ b/libc/test/integration/src/threads/mtx_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for mtx_t -----------------------------------------------===// +//===-- Tests for mtx_t operations ----------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,14 +6,16 @@ // //===----------------------------------------------------------------------===// -#include "include/threads.h" #include "src/threads/mtx_destroy.h" #include "src/threads/mtx_init.h" #include "src/threads/mtx_lock.h" #include "src/threads/mtx_unlock.h" #include "src/threads/thrd_create.h" #include "src/threads/thrd_join.h" -#include "utils/UnitTest/Test.h" + +#include "utils/IntegrationTest/test.h" + +#include constexpr int START = 0; constexpr int MAX = 10000; @@ -36,7 +38,7 @@ return 0; } -TEST(LlvmLibcMutexTest, RelayCounter) { +void relay_counter() { ASSERT_EQ(__llvm_libc::mtx_init(&mutex, mtx_plain), static_cast(thrd_success)); @@ -82,7 +84,7 @@ return 0; } -TEST(LlvmLibcMutexTest, WaitAndStep) { +void wait_and_step() { ASSERT_EQ(__llvm_libc::mtx_init(&start_lock, mtx_plain), static_cast(thrd_success)); ASSERT_EQ(__llvm_libc::mtx_init(&step_lock, mtx_plain), @@ -156,7 +158,7 @@ return 0; } -TEST(LlvmLibcMutexTest, MultipleWaiters) { +void multiple_waiters() { __llvm_libc::mtx_init(&multiple_waiter_lock, mtx_plain); __llvm_libc::mtx_init(&counter_lock, mtx_plain); @@ -189,3 +191,10 @@ __llvm_libc::mtx_destroy(&multiple_waiter_lock); __llvm_libc::mtx_destroy(&counter_lock); } + +int main() { + relay_counter(); + wait_and_step(); + multiple_waiters(); + return 0; +} diff --git a/libc/test/src/threads/thrd_test.cpp b/libc/test/integration/src/threads/thrd_test.cpp rename from libc/test/src/threads/thrd_test.cpp rename to libc/test/integration/src/threads/thrd_test.cpp --- a/libc/test/src/threads/thrd_test.cpp +++ b/libc/test/integration/src/threads/thrd_test.cpp @@ -1,4 +1,4 @@ -//===-- Unittests for thrd_t ----------------------------------------------===// +//===-- Tests for thrd_t creation and joining -----------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,10 +6,12 @@ // //===----------------------------------------------------------------------===// -#include "include/threads.h" #include "src/threads/thrd_create.h" #include "src/threads/thrd_join.h" -#include "utils/UnitTest/Test.h" + +#include "utils/IntegrationTest/test.h" + +#include static constexpr int thread_count = 1000; static int counter = 0; @@ -18,7 +20,7 @@ return 0; } -TEST(LlvmLibcThreadTest, CreateAndJoin) { +void create_and_join() { for (counter = 0; counter <= thread_count;) { thrd_t thread; int old_counter_val = counter; @@ -33,7 +35,7 @@ static int return_arg(void *arg) { return *reinterpret_cast(arg); } -TEST(LlvmLibcThreadTest, SpawnAndJoin) { +void spawn_and_join() { thrd_t thread_list[thread_count]; int args[thread_count]; @@ -50,3 +52,8 @@ ASSERT_EQ(retval, i); } } + +int main() { + create_and_join(); + spawn_and_join(); +} diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt --- a/libc/test/src/CMakeLists.txt +++ b/libc/test/src/CMakeLists.txt @@ -50,7 +50,6 @@ # add_subdirectory(assert) # add_subdirectory(signal) add_subdirectory(stdio) -add_subdirectory(threads) add_subdirectory(time) if(${LIBC_TARGET_OS} STREQUAL "linux") diff --git a/libc/utils/IntegrationTest/test.h b/libc/utils/IntegrationTest/test.h --- a/libc/utils/IntegrationTest/test.h +++ b/libc/utils/IntegrationTest/test.h @@ -13,7 +13,7 @@ #include "src/__support/OSUtil/quick_exit.h" #define __AS_STRING(val) #val -#define __CHECK(file, line, val, should_exit) \ +#define __CHECK_TRUE(file, line, val, should_exit) \ if (!(val)) { \ __llvm_libc::write_to_stderr(file ":" __AS_STRING( \ line) ": Expected '" #val "' to be true, but is false\n"); \ @@ -21,17 +21,41 @@ __llvm_libc::quick_exit(127); \ } -#define __CHECK_NE(file, line, val, should_exit) \ - if ((val)) { \ +#define __CHECK_FALSE(file, line, val, should_exit) \ + if (val) { \ __llvm_libc::write_to_stderr(file ":" __AS_STRING( \ line) ": Expected '" #val "' to be false, but is true\n"); \ if (should_exit) \ __llvm_libc::quick_exit(127); \ } -#define EXPECT_TRUE(val) __CHECK(__FILE__, __LINE__, val, false) -#define ASSERT_TRUE(val) __CHECK(__FILE__, __LINE__, val, true) -#define EXPECT_FALSE(val) __CHECK_NE(__FILE__, __LINE__, val, false) -#define ASSERT_FALSE(val) __CHECK_NE(__FILE__, __LINE__, val, true) +#define __CHECK_EQ(file, line, val1, val2, should_exit) \ + if ((val1) != (val2)) { \ + __llvm_libc::write_to_stderr(file ":" __AS_STRING( \ + line) ": Expected '" #val1 "' to be equal to '" #val2 "'\n"); \ + if (should_exit) \ + __llvm_libc::quick_exit(127); \ + } + +#define __CHECK_NE(file, line, val1, val2, should_exit) \ + if ((val1) == (val2)) { \ + __llvm_libc::write_to_stderr(file ":" __AS_STRING( \ + line) ": Expected '" #val1 "' to not be equal to '" #val2 "'\n"); \ + if (should_exit) \ + __llvm_libc::quick_exit(127); \ + } + +#define EXPECT_TRUE(val) __CHECK_TRUE(__FILE__, __LINE__, val, false) +#define ASSERT_TRUE(val) __CHECK_TRUE(__FILE__, __LINE__, val, true) +#define EXPECT_FALSE(val) __CHECK_FALSE(__FILE__, __LINE__, val, false) +#define ASSERT_FALSE(val) __CHECK_FALSE(__FILE__, __LINE__, val, true) +#define EXPECT_EQ(val1, val2) \ + __CHECK_EQ(__FILE__, __LINE__, (val1), (val2), false) +#define ASSERT_EQ(val1, val2) \ + __CHECK_EQ(__FILE__, __LINE__, (val1), (val2), true) +#define EXPECT_NE(val1, val2) \ + __CHECK_NE(__FILE__, __LINE__, (val1), (val2), false) +#define ASSERT_NE(val1, val2) \ + __CHECK_NE(__FILE__, __LINE__, (val1), (val2), true) #endif // LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H