1//===-- Unittests for pthread_condattr_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 "include/llvm-libc-macros/time-macros.h" // CLOCK_REALTIME, CLOCK_MONOTONIC
11#include "src/pthread/pthread_condattr_destroy.h"
12#include "src/pthread/pthread_condattr_getclock.h"
13#include "src/pthread/pthread_condattr_getpshared.h"
14#include "src/pthread/pthread_condattr_init.h"
15#include "src/pthread/pthread_condattr_setclock.h"
16#include "src/pthread/pthread_condattr_setpshared.h"
17#include "test/UnitTest/Test.h"
18
19// TODO: https://github.com/llvm/llvm-project/issues/88997
20#include <pthread.h> // PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
21
22TEST(LlvmLibcPThreadCondAttrTest, InitAndDestroy) {
23 pthread_condattr_t cond;
24 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
25 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
26}
27
28TEST(LlvmLibcPThreadCondAttrTest, GetDefaultValues) {
29 pthread_condattr_t cond;
30
31 // Invalid clock id.
32 clockid_t clock = 7;
33 // Invalid value.
34 int pshared = 42;
35
36 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
37 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getclock(&cond, &clock), 0);
38 ASSERT_EQ(clock, CLOCK_REALTIME);
39 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getpshared(&cond, &pshared), 0);
40 ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
41 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
42}
43
44TEST(LlvmLibcPThreadCondAttrTest, SetGoodValues) {
45 pthread_condattr_t cond;
46
47 // Invalid clock id.
48 clockid_t clock = 7;
49 // Invalid value.
50 int pshared = 42;
51
52 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
53 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setclock(&cond, CLOCK_MONOTONIC),
54 0);
55 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getclock(&cond, &clock), 0);
56 ASSERT_EQ(clock, CLOCK_MONOTONIC);
57 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setpshared(&cond,
58 PTHREAD_PROCESS_SHARED),
59 0);
60 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getpshared(&cond, &pshared), 0);
61 ASSERT_EQ(pshared, PTHREAD_PROCESS_SHARED);
62 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
63}
64
65TEST(LlvmLibcPThreadCondAttrTest, SetBadValues) {
66 pthread_condattr_t cond;
67
68 // Invalid clock id.
69 clockid_t clock = 7;
70 // Invalid value.
71 int pshared = 42;
72
73 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
74 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setclock(&cond, clock), EINVAL);
75 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getclock(&cond, &clock), 0);
76 ASSERT_EQ(clock, CLOCK_REALTIME);
77 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setpshared(&cond, pshared),
78 EINVAL);
79 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getpshared(&cond, &pshared), 0);
80 ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
81 ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
82}
83

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