From 24774096801d2a574d62266c448883dee3f5aa63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 24 Jul 2022 16:40:58 +0200 Subject: [PATCH] Tests: Add pthread scheduler priority tests --- Tests/LibC/CMakeLists.txt | 1 + Tests/LibC/TestPThreadPriority.cpp | 68 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 Tests/LibC/TestPThreadPriority.cpp diff --git a/Tests/LibC/CMakeLists.txt b/Tests/LibC/CMakeLists.txt index b82b6121580..cd9ac9ebfee 100644 --- a/Tests/LibC/CMakeLists.txt +++ b/Tests/LibC/CMakeLists.txt @@ -17,6 +17,7 @@ set(TEST_SOURCES TestMkDir.cpp TestPthreadCancel.cpp TestPthreadCleanup.cpp + TestPThreadPriority.cpp TestPthreadSpinLocks.cpp TestPthreadRWLocks.cpp TestQsort.cpp diff --git a/Tests/LibC/TestPThreadPriority.cpp b/Tests/LibC/TestPThreadPriority.cpp new file mode 100644 index 00000000000..36447dab487 --- /dev/null +++ b/Tests/LibC/TestPThreadPriority.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022, kleines Filmröllchen + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +#define TEST_CASE_IN_PTHREAD(x) \ + static void* __TESTCASE_FUNC(x##__inner)(void*); \ + TEST_CASE(x) \ + { \ + pthread_t thread; \ + pthread_create(&thread, nullptr, __TESTCASE_FUNC(x##__inner), nullptr); \ + pthread_join(thread, nullptr); \ + } \ + static void* __TESTCASE_FUNC(x##__inner)(void*) + +TEST_CASE_IN_PTHREAD(basic_priority) +{ + auto min_priority = sched_get_priority_min(0); + auto max_priority = sched_get_priority_max(0); + sched_param const min_priority_parameter { .sched_priority = min_priority }; + sched_param const max_priority_parameter { .sched_priority = max_priority }; + + auto rc = pthread_setschedparam(0, 0, &min_priority_parameter); + EXPECT_EQ(rc, 0); + sched_param output_parameter; + rc = pthread_getschedparam(0, 0, &output_parameter); + EXPECT_EQ(rc, 0); + EXPECT_EQ(output_parameter.sched_priority, min_priority); + + rc = pthread_setschedparam(0, 0, &max_priority_parameter); + EXPECT_EQ(rc, 0); + rc = pthread_getschedparam(0, 0, &output_parameter); + EXPECT_EQ(rc, 0); + EXPECT_EQ(output_parameter.sched_priority, max_priority); + + rc = pthread_setschedparam(0, 0, &max_priority_parameter); + EXPECT_EQ(rc, 0); + return 0; +} + +TEST_CASE_IN_PTHREAD(invalid_arguments) +{ + auto min_priority = sched_get_priority_min(0); + auto max_priority = sched_get_priority_max(0); + sched_param const under_priority_parameter { .sched_priority = min_priority - 1 }; + sched_param const over_priority_parameter { .sched_priority = max_priority + 1 }; + sched_param const min_priority_parameter { .sched_priority = min_priority }; + + // Set too high or too low priorities. + auto rc = pthread_setschedparam(0, 0, &over_priority_parameter); + EXPECT_EQ(rc, EINVAL); + rc = pthread_setschedparam(0, 0, &under_priority_parameter); + EXPECT_EQ(rc, EINVAL); + + // Get and set a thread that doesn't exist. + rc = pthread_setschedparam(-42069, 0, &min_priority_parameter); + EXPECT_EQ(rc, ESRCH); + sched_param output_parameter; + rc = pthread_getschedparam(-42069, 0, &output_parameter); + EXPECT_EQ(rc, ESRCH); + return 0; +}