mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 03:57:43 +00:00
Tests: Establish root Tests directory, move Userland/Tests there
With the goal of centralizing all tests in the system, this is a first step to establish a Tests sub-tree. It will contain all of the unit tests and test harnesses for the various components in the system.
This commit is contained in:
parent
6e641fadfa
commit
fd0dbd1ebf
49 changed files with 1 additions and 1 deletions
130
Tests/LibPthread/TestLibPthreadSpinLocks.cpp
Normal file
130
Tests/LibPthread/TestLibPthreadSpinLocks.cpp
Normal file
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibPthread/pthread.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
#include <unistd.h>
|
||||
|
||||
TEST_CASE(spin_init_process_scope)
|
||||
{
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_init(&lock, PTHREAD_SCOPE_PROCESS);
|
||||
EXPECT_EQ(0, result);
|
||||
result = pthread_spin_destroy(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
|
||||
{
|
||||
pthread_spinlock_t garbage_lock { 0x1337 };
|
||||
auto result = pthread_spin_init(&garbage_lock, PTHREAD_SCOPE_PROCESS);
|
||||
EXPECT_EQ(0, result);
|
||||
result = pthread_spin_destroy(&garbage_lock);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(spin_init_system_scope)
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_init(&lock, PTHREAD_SCOPE_SYSTEM);
|
||||
EXPECT_EQ(0, result);
|
||||
pthread_spinlock_t garbage_lock { 0x99999 };
|
||||
result = pthread_spin_init(&garbage_lock, PTHREAD_SCOPE_PROCESS);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
|
||||
TEST_CASE(spin_lock)
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_lock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
|
||||
// We should detect that this thread already holds this lock.
|
||||
result = pthread_spin_lock(&lock);
|
||||
EXPECT_EQ(EDEADLK, result);
|
||||
}
|
||||
|
||||
TEST_CASE(spin_try_lock)
|
||||
{
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_trylock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
|
||||
result = pthread_spin_unlock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_trylock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
|
||||
// We should detect that this thread already holds the lock.
|
||||
result = pthread_spin_trylock(&lock);
|
||||
EXPECT_EQ(EBUSY, result);
|
||||
}
|
||||
}
|
||||
|
||||
static void lock_from_different_thread(pthread_spinlock_t* lock)
|
||||
{
|
||||
pthread_t thread_id {};
|
||||
auto result = pthread_create(
|
||||
&thread_id, nullptr, [](void* param) -> void* {
|
||||
auto lock = (pthread_spinlock_t*)param;
|
||||
pthread_spin_lock(lock);
|
||||
return nullptr;
|
||||
},
|
||||
lock);
|
||||
EXPECT_EQ(0, result);
|
||||
|
||||
result = pthread_join(thread_id, nullptr);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
|
||||
TEST_CASE(spin_unlock)
|
||||
{
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_lock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
|
||||
result = pthread_spin_unlock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
lock_from_different_thread(&lock);
|
||||
|
||||
auto result = pthread_spin_unlock(&lock);
|
||||
EXPECT_EQ(EPERM, result);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(spin_destroy)
|
||||
{
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
auto result = pthread_spin_lock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
|
||||
result = pthread_spin_destroy(&lock);
|
||||
EXPECT_EQ(EBUSY, result);
|
||||
|
||||
result = pthread_spin_unlock(&lock);
|
||||
EXPECT_EQ(0, result);
|
||||
}
|
||||
|
||||
{
|
||||
pthread_spinlock_t lock {};
|
||||
lock_from_different_thread(&lock);
|
||||
|
||||
auto result = pthread_spin_destroy(&lock);
|
||||
EXPECT_EQ(EBUSY, result);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue