summaryrefslogtreecommitdiff
path: root/Tests/LibPthread/TestLibPthreadRWLocks.cpp
blob: 8c48aaa5565ae9aa8517ed06a63fa235a14f71e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (c) 2021, Rodrigo Tobar <rtobarc@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibPthread/pthread.h>
#include <LibTest/TestCase.h>

TEST_CASE(rwlock_init)
{
    pthread_rwlock_t lock;
    auto result = pthread_rwlock_init(&lock, nullptr);
    EXPECT_EQ(0, result);
}

TEST_CASE(rwlock_rdlock)
{
    pthread_rwlock_t lock;
    auto result = pthread_rwlock_init(&lock, nullptr);
    EXPECT_EQ(0, result);

    result = pthread_rwlock_rdlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);

    result = pthread_rwlock_rdlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_rdlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);
}

TEST_CASE(rwlock_wrlock)
{
    pthread_rwlock_t lock;
    auto result = pthread_rwlock_init(&lock, nullptr);
    EXPECT_EQ(0, result);

    result = pthread_rwlock_wrlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);
}

TEST_CASE(rwlock_rwr_sequence)
{
    pthread_rwlock_t lock;
    auto result = pthread_rwlock_init(&lock, nullptr);
    EXPECT_EQ(0, result);

    result = pthread_rwlock_rdlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);

    result = pthread_rwlock_wrlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);

    result = pthread_rwlock_rdlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);
}

TEST_CASE(rwlock_wrlock_init_in_once)
{
    static pthread_rwlock_t lock;
    static pthread_once_t once1 = PTHREAD_ONCE_INIT;
    static pthread_once_t once2 = PTHREAD_ONCE_INIT;
    static pthread_once_t once3 = PTHREAD_ONCE_INIT;
    pthread_once(&once1, []() {
        pthread_once(&once2, []() {
            pthread_once(&once3, []() {
                auto result = pthread_rwlock_init(&lock, nullptr);
                EXPECT_EQ(0, result);
            });
        });
    });
    auto result = pthread_rwlock_wrlock(&lock);
    EXPECT_EQ(0, result);
    result = pthread_rwlock_unlock(&lock);
    EXPECT_EQ(0, result);
}