1//===-- Unittests for sched_get_priority_{min,max} ------------------------===//
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 "src/errno/libc_errno.h"
10#include "src/sched/sched_get_priority_max.h"
11#include "src/sched/sched_get_priority_min.h"
12#include "test/UnitTest/Test.h"
13
14#include <sched.h>
15
16TEST(LlvmLibcSchedGetPriorityTest, HandleBadPolicyTest) {
17
18 // Test arbitrary values for which there is no policy.
19 {
20 int policy = -1;
21 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
22 ASSERT_EQ(max_priority, -1);
23 ASSERT_ERRNO_EQ(EINVAL);
24 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
25 ASSERT_EQ(min_priority, -1);
26 ASSERT_ERRNO_EQ(EINVAL);
27 }
28
29 {
30 int policy = 30;
31 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
32 ASSERT_EQ(max_priority, -1);
33 ASSERT_ERRNO_EQ(EINVAL);
34 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
35 ASSERT_EQ(min_priority, -1);
36 ASSERT_ERRNO_EQ(EINVAL);
37 }
38
39 {
40 int policy = 80;
41 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
42 ASSERT_EQ(max_priority, -1);
43 ASSERT_ERRNO_EQ(EINVAL);
44 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
45 ASSERT_EQ(min_priority, -1);
46 ASSERT_ERRNO_EQ(EINVAL);
47 }
48
49 {
50 int policy = 110;
51 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
52 ASSERT_EQ(max_priority, -1);
53 ASSERT_ERRNO_EQ(EINVAL);
54 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
55 ASSERT_EQ(min_priority, -1);
56 ASSERT_ERRNO_EQ(EINVAL);
57 }
58}
59
60TEST(LlvmLibcSchedGetPriorityTest, SmokeTest) {
61 LIBC_NAMESPACE::libc_errno = 0;
62
63 // We Test:
64 // SCHED_OTHER, SCHED_FIFO, SCHED_RR
65 // Linux specific test could also include:
66 // SCHED_BATCH, SCHED_ISO, SCHED_IDLE, SCHED_DEADLINE
67 {
68 int policy = SCHED_OTHER;
69 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
70 ASSERT_GE(max_priority, 0);
71 ASSERT_ERRNO_SUCCESS();
72 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
73 ASSERT_GE(min_priority, 0);
74 ASSERT_ERRNO_SUCCESS();
75
76 ASSERT_LE(max_priority, 99);
77 ASSERT_GE(min_priority, 0);
78 ASSERT_GE(max_priority, min_priority);
79 }
80
81 {
82 int policy = SCHED_FIFO;
83 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
84 ASSERT_GE(max_priority, 0);
85 ASSERT_ERRNO_SUCCESS();
86 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
87 ASSERT_GE(min_priority, 0);
88 ASSERT_ERRNO_SUCCESS();
89
90 ASSERT_LE(max_priority, 99);
91 ASSERT_GE(min_priority, 0);
92 ASSERT_GT(max_priority, min_priority);
93 }
94
95 {
96 int policy = SCHED_RR;
97 int max_priority = LIBC_NAMESPACE::sched_get_priority_max(policy);
98 ASSERT_GE(max_priority, 0);
99 ASSERT_ERRNO_SUCCESS();
100 int min_priority = LIBC_NAMESPACE::sched_get_priority_min(policy);
101 ASSERT_GE(min_priority, 0);
102 ASSERT_ERRNO_SUCCESS();
103
104 ASSERT_LE(max_priority, 99);
105 ASSERT_GE(min_priority, 0);
106 ASSERT_GT(max_priority, min_priority);
107 }
108}
109

source code of libc/test/src/sched/get_priority_test.cpp