Skip to content

[libc] fixup nascent pthread_condattr_test #89308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libc/test/src/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ add_libc_unittest(
SRCS
pthread_condattr_test.cpp
DEPENDS
libc.include.errno
libc.include.llvm-libc-macros.generic_error_number_macros
libc.include.llvm-libc-macros.time_macros
libc.include.pthread
libc.include.time
libc.src.pthread.pthread_condattr_destroy
libc.src.pthread.pthread_condattr_getclock
libc.src.pthread.pthread_condattr_getpshared
Expand Down
53 changes: 32 additions & 21 deletions libc/test/src/pthread/pthread_condattr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
//
//===----------------------------------------------------------------------===//

#include "include/llvm-libc-macros/generic-error-number-macros.h" // EINVAL
#include "include/llvm-libc-macros/time-macros.h" // CLOCK_REALTIME, CLOCK_MONOTONIC
#include "src/pthread/pthread_condattr_destroy.h"
#include "src/pthread/pthread_condattr_getclock.h"
#include "src/pthread/pthread_condattr_getpshared.h"
#include "src/pthread/pthread_condattr_init.h"
#include "src/pthread/pthread_condattr_setclock.h"
#include "src/pthread/pthread_condattr_setpshared.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <pthread.h>
#include <time.h>
// TODO: https://github.com/llvm/llvm-project/issues/88997
#include <pthread.h> // PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED

TEST(LlvmLibcPThreadCondAttrTest, InitAndDestroy) {
pthread_condattr_t cond;
ASSERT_EQ(pthread_condattr_init(&cond), 0);
ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
}

TEST(LlvmLibcPThreadCondAttrTest, GetDefaultValues) {
Expand All @@ -26,12 +33,12 @@ TEST(LlvmLibcPThreadCondAttrTest, GetDefaultValues) {
// Invalid value.
int pshared = 42;

ASSERT_EQ(pthread_condattr_init(&cond), 0);
ASSERT_EQ(pthread_condattr_getclock(&cond, &clock), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getclock(&cond, &clock), 0);
ASSERT_EQ(clock, CLOCK_REALTIME);
ASSERT_EQ(pthread_condattr_getpshared(&cond, &pshared), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getpshared(&cond, &pshared), 0);
ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
}

TEST(LlvmLibcPThreadCondAttrTest, SetGoodValues) {
Expand All @@ -42,14 +49,17 @@ TEST(LlvmLibcPThreadCondAttrTest, SetGoodValues) {
// Invalid value.
int pshared = 42;

ASSERT_EQ(pthread_condattr_init(&cond), 0);
ASSERT_EQ(pthread_condattr_setclock(&cond, CLOCK_MONOTONIC), 0);
ASSERT_EQ(pthread_condattr_getclock(&cond, &clock), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setclock(&cond, CLOCK_MONOTONIC),
0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getclock(&cond, &clock), 0);
ASSERT_EQ(clock, CLOCK_MONOTONIC);
ASSERT_EQ(pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED), 0);
ASSERT_EQ(pthread_condattr_getpshared(&cond, &pshared), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setpshared(&cond,
PTHREAD_PROCESS_SHARED),
0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getpshared(&cond, &pshared), 0);
ASSERT_EQ(pshared, PTHREAD_PROCESS_SHARED);
ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
}

TEST(LlvmLibcPThreadCondAttrTest, SetBadValues) {
Expand All @@ -60,12 +70,13 @@ TEST(LlvmLibcPThreadCondAttrTest, SetBadValues) {
// Invalid value.
int pshared = 42;

ASSERT_EQ(pthread_condattr_init(&cond), 0);
ASSERT_EQ(pthread_condattr_setclock(&cond, clock), EINVAL);
ASSERT_EQ(pthread_condattr_getclock(&cond, &clock), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_init(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setclock(&cond, clock), EINVAL);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getclock(&cond, &clock), 0);
ASSERT_EQ(clock, CLOCK_REALTIME);
ASSERT_EQ(pthread_condattr_setpshared(&cond, pshared), EINVAL);
ASSERT_EQ(pthread_condattr_getpshared(&cond, &pshared), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_setpshared(&cond, pshared),
EINVAL);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_getpshared(&cond, &pshared), 0);
ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_condattr_destroy(&cond), 0);
}
Loading