1//===-- Unittests for pthread_mutexattr_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//===----------------------------------------------------------------------===//
8
9#include "src/pthread/pthread_mutexattr_destroy.h"
10#include "src/pthread/pthread_mutexattr_getpshared.h"
11#include "src/pthread/pthread_mutexattr_getrobust.h"
12#include "src/pthread/pthread_mutexattr_gettype.h"
13#include "src/pthread/pthread_mutexattr_init.h"
14#include "src/pthread/pthread_mutexattr_setpshared.h"
15#include "src/pthread/pthread_mutexattr_setrobust.h"
16#include "src/pthread/pthread_mutexattr_settype.h"
17#include "test/UnitTest/Test.h"
18
19#include <errno.h>
20#include <pthread.h>
21
22TEST(LlvmLibcPThreadMutexAttrTest, InitAndDestroy) {
23 pthread_mutexattr_t attr;
24 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr), 0);
25 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_destroy(&attr), 0);
26}
27
28TEST(LlvmLibcPThreadMutexAttrTest, SetAndGetType) {
29 int type;
30 pthread_mutexattr_t attr;
31 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr), 0);
32 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_gettype(&attr, &type), 0);
33 ASSERT_EQ(type, int(PTHREAD_MUTEX_DEFAULT));
34
35 ASSERT_EQ(
36 LIBC_NAMESPACE::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE),
37 0);
38 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_gettype(&attr, &type), 0);
39 ASSERT_EQ(type, int(PTHREAD_MUTEX_RECURSIVE));
40
41 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_settype(&attr,
42 PTHREAD_MUTEX_ERRORCHECK),
43 0);
44 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_gettype(&attr, &type), 0);
45 ASSERT_EQ(type, int(PTHREAD_MUTEX_ERRORCHECK));
46
47 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_settype(&attr, 0xBAD), EINVAL);
48}
49
50TEST(LlvmLibcPThreadMutexAttrTest, SetAndGetRobust) {
51 int robust;
52 pthread_mutexattr_t attr;
53 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr), 0);
54 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getrobust(&attr, &robust), 0);
55 ASSERT_EQ(robust, int(PTHREAD_MUTEX_STALLED));
56
57 ASSERT_EQ(
58 LIBC_NAMESPACE::pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST),
59 0);
60 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getrobust(&attr, &robust), 0);
61 ASSERT_EQ(robust, int(PTHREAD_MUTEX_ROBUST));
62
63 ASSERT_EQ(
64 LIBC_NAMESPACE::pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED),
65 0);
66 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getrobust(&attr, &robust), 0);
67 ASSERT_EQ(robust, int(PTHREAD_MUTEX_STALLED));
68
69 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setrobust(&attr, 0xBAD), EINVAL);
70}
71
72TEST(LlvmLibcPThreadMutexAttrTest, SetAndGetPShared) {
73 int pshared;
74 pthread_mutexattr_t attr;
75 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_init(&attr), 0);
76 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getpshared(&attr, &pshared), 0);
77 ASSERT_EQ(pshared, int(PTHREAD_PROCESS_PRIVATE));
78
79 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setpshared(
80 &attr, PTHREAD_PROCESS_SHARED),
81 0);
82 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getpshared(&attr, &pshared), 0);
83 ASSERT_EQ(pshared, int(PTHREAD_PROCESS_SHARED));
84
85 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setpshared(
86 &attr, PTHREAD_PROCESS_PRIVATE),
87 0);
88 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_getpshared(&attr, &pshared), 0);
89 ASSERT_EQ(pshared, int(PTHREAD_PROCESS_PRIVATE));
90
91 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutexattr_setpshared(&attr, 0xBAD), EINVAL);
92}
93

source code of libc/test/src/pthread/pthread_mutexattr_test.cpp