[libc-commits] [libc] d5475af - [libc][NFC] Convert threads unittests in to integration tests.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Thu Jun 23 13:32:53 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-06-23T20:32:33Z
New Revision: d5475af2f7616a66d3c79218c72b6336b7e1f483

URL: https://github.com/llvm/llvm-project/commit/d5475af2f7616a66d3c79218c72b6336b7e1f483
DIFF: https://github.com/llvm/llvm-project/commit/d5475af2f7616a66d3c79218c72b6336b7e1f483.diff

LOG: [libc][NFC] Convert threads unittests in to integration tests.

This is mostly a mechanical change. In a future pass, all tests from
pthread which create threads will also be converted to integration tests.

Some of thread related features are tightly coupled with the loader. So,
they can only be tested with the in-house loader. Hence, going forward, all
tests which create threads will have to be integration tests.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D128381

Added: 
    libc/test/integration/src/threads/CMakeLists.txt
    libc/test/integration/src/threads/call_once_test.cpp
    libc/test/integration/src/threads/cnd_test.cpp
    libc/test/integration/src/threads/mtx_test.cpp
    libc/test/integration/src/threads/thrd_test.cpp

Modified: 
    libc/test/integration/CMakeLists.txt
    libc/test/integration/src/CMakeLists.txt
    libc/test/src/CMakeLists.txt
    libc/utils/IntegrationTest/test.h

Removed: 
    libc/test/src/threads/CMakeLists.txt
    libc/test/src/threads/call_once_test.cpp
    libc/test/src/threads/cnd_test.cpp
    libc/test/src/threads/mtx_test.cpp
    libc/test/src/threads/thrd_test.cpp


################################################################################
diff  --git a/libc/test/integration/CMakeLists.txt b/libc/test/integration/CMakeLists.txt
index 393084d122aad..675f4c47c878a 100644
--- 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
index 641755c0f39e8..659e4b5bea710 100644
--- 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
similarity index 74%
rename from libc/test/src/threads/CMakeLists.txt
rename to libc/test/integration/src/threads/CMakeLists.txt
index 6ac093cb7205c..05acc7f1da456 100644
--- 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 @@ add_libc_unittest(
     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
similarity index 92%
rename from libc/test/src/threads/call_once_test.cpp
rename to libc/test/integration/src/threads/call_once_test.cpp
index 6090e63435af4..cf853372f3a93 100644
--- 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 <threads.h>
 
 static constexpr unsigned int NUM_THREADS = 5;
 static __llvm_libc::cpp::Atomic<unsigned int> thread_count;
@@ -32,7 +34,7 @@ static int func(void *) {
   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 @@ static int once_func_caller(void *) {
 // 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 @@ TEST(LlvmLibcCallOnceTest, TestSynchronization) {
 
   __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
similarity index 93%
rename from libc/test/src/threads/cnd_test.cpp
rename to libc/test/integration/src/threads/cnd_test.cpp
index 5d69142312bdc..605bc72dd40c3 100644
--- 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 <threads.h>
 
 namespace wait_notify_broadcast_test {
 
@@ -54,7 +56,7 @@ int broadcast_thread_func(void *) {
   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 @@ int waiter_thread_func(void *unused) {
   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 @@ TEST(LlvmLibcCndVarTest, SingleWaiterTest) {
 }
 
 } // 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
similarity index 94%
rename from libc/test/src/threads/mtx_test.cpp
rename to libc/test/integration/src/threads/mtx_test.cpp
index 3abe77e0eab72..2d927cdf3d59d 100644
--- 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 <threads.h>
 
 constexpr int START = 0;
 constexpr int MAX = 10000;
@@ -36,7 +38,7 @@ int counter(void *arg) {
   return 0;
 }
 
-TEST(LlvmLibcMutexTest, RelayCounter) {
+void relay_counter() {
   ASSERT_EQ(__llvm_libc::mtx_init(&mutex, mtx_plain),
             static_cast<int>(thrd_success));
 
@@ -82,7 +84,7 @@ int stepper(void *arg) {
   return 0;
 }
 
-TEST(LlvmLibcMutexTest, WaitAndStep) {
+void wait_and_step() {
   ASSERT_EQ(__llvm_libc::mtx_init(&start_lock, mtx_plain),
             static_cast<int>(thrd_success));
   ASSERT_EQ(__llvm_libc::mtx_init(&step_lock, mtx_plain),
@@ -156,7 +158,7 @@ int waiter_func(void *) {
   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 @@ TEST(LlvmLibcMutexTest, MultipleWaiters) {
   __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
similarity index 86%
rename from libc/test/src/threads/thrd_test.cpp
rename to libc/test/integration/src/threads/thrd_test.cpp
index ef4b258076747..69df030278894 100644
--- 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 <threads.h>
 
 static constexpr int thread_count = 1000;
 static int counter = 0;
@@ -18,7 +20,7 @@ static int thread_func(void *) {
   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 @@ TEST(LlvmLibcThreadTest, CreateAndJoin) {
 
 static int return_arg(void *arg) { return *reinterpret_cast<int *>(arg); }
 
-TEST(LlvmLibcThreadTest, SpawnAndJoin) {
+void spawn_and_join() {
   thrd_t thread_list[thread_count];
   int args[thread_count];
 
@@ -50,3 +52,8 @@ TEST(LlvmLibcThreadTest, SpawnAndJoin) {
     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
index 2895eba829dbe..3d96014182fff 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -50,7 +50,6 @@ endif()
 # 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
index 97fa10e0524b9..556265052dfad 100644
--- 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


        


More information about the libc-commits mailing list