[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / pthread / pthread_attr_test.cpp
blobaf9f5d07f1155309c9ad1c6a14647e4b2a46fdbb
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(__llvm_libc::pthread_attr_init(&attr), 0);
30 int detachstate;
31 ASSERT_EQ(__llvm_libc::pthread_attr_getdetachstate(&attr, &detachstate), 0);
32 ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE));
34 size_t guardsize;
35 ASSERT_EQ(__llvm_libc::pthread_attr_getguardsize(&attr, &guardsize), 0);
36 ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
38 ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
41 TEST(LlvmLibcPThreadattrTest, SetAndGetDetachState) {
42 pthread_attr_t attr;
43 ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
45 int detachstate;
46 __llvm_libc::pthread_attr_getdetachstate(&attr, &detachstate);
47 ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE));
48 ASSERT_EQ(
49 __llvm_libc::pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED),
50 0);
51 ASSERT_EQ(__llvm_libc::pthread_attr_getdetachstate(&attr, &detachstate), 0);
52 ASSERT_EQ(detachstate, int(PTHREAD_CREATE_DETACHED));
54 ASSERT_EQ(__llvm_libc::pthread_attr_setdetachstate(&attr, 0xBAD), EINVAL);
56 ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
59 TEST(LlvmLibcPThreadattrTest, SetAndGetGuardSize) {
60 pthread_attr_t attr;
61 ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
63 size_t guardsize;
64 ASSERT_EQ(__llvm_libc::pthread_attr_getguardsize(&attr, &guardsize), 0);
65 ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
66 ASSERT_EQ(__llvm_libc::pthread_attr_setguardsize(&attr, 2 * EXEC_PAGESIZE),
67 0);
68 ASSERT_EQ(__llvm_libc::pthread_attr_getguardsize(&attr, &guardsize), 0);
69 ASSERT_EQ(guardsize, size_t(2 * EXEC_PAGESIZE));
71 ASSERT_EQ(__llvm_libc::pthread_attr_setguardsize(&attr, EXEC_PAGESIZE / 2),
72 EINVAL);
74 ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
77 TEST(LlvmLibcPThreadattrTest, SetAndGetStackSize) {
78 pthread_attr_t attr;
79 ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
81 size_t stacksize;
82 ASSERT_EQ(
83 __llvm_libc::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN << 2), 0);
84 ASSERT_EQ(__llvm_libc::pthread_attr_getstacksize(&attr, &stacksize), 0);
85 ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2));
87 ASSERT_EQ(
88 __llvm_libc::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN / 2),
89 EINVAL);
91 ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
94 TEST(LlvmLibcPThreadattrTest, SetAndGetStack) {
95 pthread_attr_t attr;
96 ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
98 void *stack;
99 size_t stacksize;
100 ASSERT_EQ(
101 __llvm_libc::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN << 2), 0);
102 ASSERT_EQ(__llvm_libc::pthread_attr_getstack(&attr, &stack, &stacksize), 0);
103 ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2));
104 ASSERT_EQ(reinterpret_cast<uintptr_t>(stack), uintptr_t(0));
106 ASSERT_EQ(__llvm_libc::pthread_attr_setstack(
107 &attr, reinterpret_cast<void *>(1), PTHREAD_STACK_MIN << 2),
108 EINVAL);
109 ASSERT_EQ(__llvm_libc::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN / 2),
110 EINVAL);
112 ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);