1 #include <gtest/gtest.h>
2 #include <thread>
3 #include <threads.h>
4
5 using namespace testing::ext;
6
7 class ThreadThrdTest : public testing::Test {
SetUp()8 void SetUp() override {}
TearDown()9 void TearDown() override {}
10 };
11
ExitArg(void * arg)12 static int ExitArg(void* arg)
13 {
14 thrd_exit(static_cast<int>(reinterpret_cast<uintptr_t>(arg)));
15 }
16
ReturnArg(void * arg)17 static int ReturnArg(void* arg)
18 {
19 return static_cast<int>(reinterpret_cast<uintptr_t>(arg));
20 }
21
22 /**
23 * @tc.name: thrd_create_001
24 * @tc.desc: Determine if the thread function creation was successful, and use thrd_join The join function saves the
25 * thread return value to a variable, and finally determines that the variable is equal to the expected value.
26 * @tc.type: FUNC
27 * */
28 HWTEST_F(ThreadThrdTest, thrd_create_001, TestSize.Level1)
29 {
30 thrd_t thrd;
31 int realResult;
32 EXPECT_EQ(thrd_success, thrd_create(&thrd, ReturnArg, reinterpret_cast<void*>(1)));
33 EXPECT_EQ(thrd_success, thrd_join(thrd, &realResult));
34 EXPECT_EQ(1, realResult);
35 }
36
37 /**
38 * @tc.name: thrd_create_002
39 * @tc.desc: It determines that the thread function was successfully created, and it determines that the thrd_join
40 * The join function successfully saved the return value of the thread function to a null pointer
41 * @tc.type: FUNC
42 * */
43 HWTEST_F(ThreadThrdTest, thrd_create_002, TestSize.Level1)
44 {
45 thrd_t thrd;
46 EXPECT_EQ(thrd_success, thrd_create(&thrd, ReturnArg, reinterpret_cast<void*>(1)));
47 EXPECT_EQ(thrd_success, thrd_join(thrd, nullptr));
48 }
49
50 /**
51 * @tc.name: thrd_create_003
52 * @tc.desc: Determine whether the thread creation function call was successful, and determine thrd_ The detach function
53 * call was successful
54 * @tc.type: FUNC
55 * */
56 HWTEST_F(ThreadThrdTest, thrd_create_003, TestSize.Level1)
57 {
58 thrd_t thread;
59
60 EXPECT_EQ(thrd_success, thrd_create(&thread, ExitArg, reinterpret_cast<void*>(1)));
61 EXPECT_EQ(thrd_success, thrd_detach(thread));
62 }
63
64 /**
65 * @tc.name: thrd_current_001
66 * @tc.desc: In multithreading, determine if the current identifiers of the child thread and the main thread are not
67 * equal.
68 * 2. Determine if the identifiers of the same child thread are equal
69 * @tc.type: FUNC
70 * */
71 HWTEST_F(ThreadThrdTest, thrd_current_001, TestSize.Level1)
72 {
73 thrd_t thread1 = thrd_current();
74 thrd_t thread2 = thrd_current();
75 EXPECT_TRUE(thrd_equal(thread1, thread2));
76 }
77
78 /**
79 * @tc.name: thrd_equal_001
80 * @tc.desc: Determine if the identifiers of the same child thread are equal
81 * @tc.type: FUNC
82 * */
83 HWTEST_F(ThreadThrdTest, thrd_equal_001, TestSize.Level1)
84 {
85 thrd_t thread1 = thrd_current();
__anon3cafc67d0102null86 std::thread([&thread1] {
87 thrd_t thread2 = thrd_current();
88 EXPECT_FALSE(thrd_equal(thread1, thread2));
89 thrd_t thread3 = thrd_current();
90 EXPECT_TRUE(thrd_equal(thread2, thread3));
91 }).join();
92 }
93
94 /**
95 * @tc.name: thrd_exit_001
96 * @tc.desc: Call thrd in the main thread_ When using the exit function,
97 * the program can exit normally after the last thread terminates, and the exit status code is EXIT_ SUCCESS
98 * @tc.type: FUNC
99 * */
100 HWTEST_F(ThreadThrdTest, thrd_exit_001, TestSize.Level1)
101 {
102 EXPECT_EXIT(thrd_exit(12), ::testing::ExitedWithCode(EXIT_SUCCESS), "");
103 }
104
105 /**
106 * @tc.name: thrd_sleep_001
107 * @tc.desc: Set a negative sleep time to determine if the return is invalidTime
108 * @tc.type: FUNC
109 * */
110 HWTEST_F(ThreadThrdTest, thrd_sleep_001, TestSize.Level1)
111 {
112 timespec invalidTime = { .tv_sec = -1 };
113 EXPECT_EQ(-2, thrd_sleep(&invalidTime, nullptr));
114 }