1//===-- Unittests for pthread_rwlockattr_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 "include/llvm-libc-macros/generic-error-number-macros.h" // EINVAL
10#include "src/pthread/pthread_rwlockattr_destroy.h"
11#include "src/pthread/pthread_rwlockattr_getpshared.h"
12#include "src/pthread/pthread_rwlockattr_init.h"
13#include "src/pthread/pthread_rwlockattr_setpshared.h"
14#include "test/UnitTest/Test.h"
15
16// TODO: https://github.com/llvm/llvm-project/issues/88997
17#include <pthread.h> // PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
18
19TEST(LlvmLibcPThreadRWLockAttrTest, InitAndDestroy) {
20 pthread_rwlockattr_t attr;
21 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
22 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
23}
24
25TEST(LlvmLibcPThreadRWLockAttrTest, GetDefaultValues) {
26 pthread_rwlockattr_t attr;
27
28 // Invalid value.
29 int pshared = 42;
30
31 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
32 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getpshared(&attr, &pshared), 0);
33 ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
34 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
35}
36
37TEST(LlvmLibcPThreadRWLockAttrTest, SetGoodValues) {
38 pthread_rwlockattr_t attr;
39
40 // Invalid value.
41 int pshared = 42;
42
43 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
44 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_setpshared(
45 &attr, PTHREAD_PROCESS_SHARED),
46 0);
47 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getpshared(&attr, &pshared), 0);
48 ASSERT_EQ(pshared, PTHREAD_PROCESS_SHARED);
49 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
50}
51
52TEST(LlvmLibcPThreadRWLockAttrTest, SetBadValues) {
53 pthread_rwlockattr_t attr;
54
55 // Invalid value.
56 int pshared = 42;
57
58 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
59 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_setpshared(&attr, pshared),
60 EINVAL);
61 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getpshared(&attr, &pshared), 0);
62 ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
63 ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
64}
65

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