1 //===-- Unittests for pthread_mutexattr_t ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "hdr/errno_macros.h"
10 #include "src/pthread/pthread_mutexattr_destroy.h"
11 #include "src/pthread/pthread_mutexattr_getpshared.h"
12 #include "src/pthread/pthread_mutexattr_getrobust.h"
13 #include "src/pthread/pthread_mutexattr_gettype.h"
14 #include "src/pthread/pthread_mutexattr_init.h"
15 #include "src/pthread/pthread_mutexattr_setpshared.h"
16 #include "src/pthread/pthread_mutexattr_setrobust.h"
17 #include "src/pthread/pthread_mutexattr_settype.h"
18 #include "test/UnitTest/Test.h"
21 TEST(LlvmLibcPThreadMutexAttrTest
, InitAndDestroy
) {
22 pthread_mutexattr_t attr
;
23 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr
), 0);
24 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_destroy(&attr
), 0);
27 TEST(LlvmLibcPThreadMutexAttrTest
, SetAndGetType
) {
29 pthread_mutexattr_t attr
;
30 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr
), 0);
31 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_gettype(&attr
, &type
), 0);
32 ASSERT_EQ(type
, int(PTHREAD_MUTEX_DEFAULT
));
35 LIBC_NAMESPACE::pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
),
37 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_gettype(&attr
, &type
), 0);
38 ASSERT_EQ(type
, int(PTHREAD_MUTEX_RECURSIVE
));
40 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_settype(&attr
,
41 PTHREAD_MUTEX_ERRORCHECK
),
43 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_gettype(&attr
, &type
), 0);
44 ASSERT_EQ(type
, int(PTHREAD_MUTEX_ERRORCHECK
));
46 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_settype(&attr
, 0xBAD), EINVAL
);
49 TEST(LlvmLibcPThreadMutexAttrTest
, SetAndGetRobust
) {
51 pthread_mutexattr_t attr
;
52 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr
), 0);
53 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getrobust(&attr
, &robust
), 0);
54 ASSERT_EQ(robust
, int(PTHREAD_MUTEX_STALLED
));
57 LIBC_NAMESPACE::pthread_mutexattr_setrobust(&attr
, PTHREAD_MUTEX_ROBUST
),
59 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getrobust(&attr
, &robust
), 0);
60 ASSERT_EQ(robust
, int(PTHREAD_MUTEX_ROBUST
));
63 LIBC_NAMESPACE::pthread_mutexattr_setrobust(&attr
, PTHREAD_MUTEX_STALLED
),
65 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getrobust(&attr
, &robust
), 0);
66 ASSERT_EQ(robust
, int(PTHREAD_MUTEX_STALLED
));
68 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setrobust(&attr
, 0xBAD), EINVAL
);
71 TEST(LlvmLibcPThreadMutexAttrTest
, SetAndGetPShared
) {
73 pthread_mutexattr_t attr
;
74 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr
), 0);
75 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getpshared(&attr
, &pshared
), 0);
76 ASSERT_EQ(pshared
, int(PTHREAD_PROCESS_PRIVATE
));
78 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setpshared(
79 &attr
, PTHREAD_PROCESS_SHARED
),
81 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getpshared(&attr
, &pshared
), 0);
82 ASSERT_EQ(pshared
, int(PTHREAD_PROCESS_SHARED
));
84 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setpshared(
85 &attr
, PTHREAD_PROCESS_PRIVATE
),
87 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getpshared(&attr
, &pshared
), 0);
88 ASSERT_EQ(pshared
, int(PTHREAD_PROCESS_PRIVATE
));
90 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setpshared(&attr
, 0xBAD), EINVAL
);