Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / pthread / pthread_attr_test.cpp
blob1af585a003e3b0ab5cdd4247b4f5477fbbea275e
1 //===-- Unittests for pthread_attr_t --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/pthread/pthread_attr_destroy.h"
10 #include "src/pthread/pthread_attr_getdetachstate.h"
11 #include "src/pthread/pthread_attr_getguardsize.h"
12 #include "src/pthread/pthread_attr_getstack.h"
13 #include "src/pthread/pthread_attr_getstacksize.h"
14 #include "src/pthread/pthread_attr_init.h"
15 #include "src/pthread/pthread_attr_setdetachstate.h"
16 #include "src/pthread/pthread_attr_setguardsize.h"
17 #include "src/pthread/pthread_attr_setstack.h"
18 #include "src/pthread/pthread_attr_setstacksize.h"
20 #include "test/UnitTest/Test.h"
22 #include <errno.h>
23 #include <linux/param.h> // For EXEC_PAGESIZE.
24 #include <pthread.h>
26 TEST(LlvmLibcPThreadAttrTest, InitAndDestroy) {
27 pthread_attr_t attr;
28 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
30 int detachstate;
31 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr, &detachstate),
32 0);
33 ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE));
35 size_t guardsize;
36 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr, &guardsize), 0);
37 ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
39 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
42 TEST(LlvmLibcPThreadattrTest, SetAndGetDetachState) {
43 pthread_attr_t attr;
44 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
46 int detachstate;
47 LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr, &detachstate);
48 ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE));
49 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setdetachstate(
50 &attr, PTHREAD_CREATE_DETACHED),
51 0);
52 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr, &detachstate),
53 0);
54 ASSERT_EQ(detachstate, int(PTHREAD_CREATE_DETACHED));
56 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setdetachstate(&attr, 0xBAD), EINVAL);
58 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
61 TEST(LlvmLibcPThreadattrTest, SetAndGetGuardSize) {
62 pthread_attr_t attr;
63 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
65 size_t guardsize;
66 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr, &guardsize), 0);
67 ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
68 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setguardsize(&attr, 2 * EXEC_PAGESIZE),
69 0);
70 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr, &guardsize), 0);
71 ASSERT_EQ(guardsize, size_t(2 * EXEC_PAGESIZE));
73 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setguardsize(&attr, EXEC_PAGESIZE / 2),
74 EINVAL);
76 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
79 TEST(LlvmLibcPThreadattrTest, SetAndGetStackSize) {
80 pthread_attr_t attr;
81 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
83 size_t stacksize;
84 ASSERT_EQ(
85 LIBC_NAMESPACE::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN << 2),
86 0);
87 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstacksize(&attr, &stacksize), 0);
88 ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2));
90 ASSERT_EQ(
91 LIBC_NAMESPACE::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN / 2),
92 EINVAL);
94 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
97 TEST(LlvmLibcPThreadattrTest, SetAndGetStack) {
98 pthread_attr_t attr;
99 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
101 void *stack;
102 size_t stacksize;
103 ASSERT_EQ(
104 LIBC_NAMESPACE::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN << 2),
106 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstack(&attr, &stack, &stacksize),
108 ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2));
109 ASSERT_EQ(reinterpret_cast<uintptr_t>(stack), uintptr_t(0));
111 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setstack(
112 &attr, reinterpret_cast<void *>(1), PTHREAD_STACK_MIN << 2),
113 EINVAL);
114 ASSERT_EQ(
115 LIBC_NAMESPACE::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN / 2),
116 EINVAL);
118 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);