Add some <sched.h> tests.
I was intending to change our behavior to match POSIX, but reality convinced
me otherwise.
Also add missing argument names to the header file on the assumption they'll
be shown in Studio one day.
Bug: http://b/26203902
Test: ran tests
Change-Id: I2aaea48a88d408a32925033fc8f17448fb63252e
diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp
index a4cffc0..e70528e 100644
--- a/tests/sched_test.cpp
+++ b/tests/sched_test.cpp
@@ -270,3 +270,34 @@
CPU_FREE(set1);
CPU_FREE(set2);
}
+
+TEST(sched, sched_get_priority_min_sched_get_priority_max) {
+ EXPECT_LE(sched_get_priority_min(SCHED_BATCH), sched_get_priority_max(SCHED_BATCH));
+ EXPECT_LE(sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO));
+ EXPECT_LE(sched_get_priority_min(SCHED_IDLE), sched_get_priority_max(SCHED_IDLE));
+ EXPECT_LE(sched_get_priority_min(SCHED_OTHER), sched_get_priority_max(SCHED_OTHER));
+ EXPECT_LE(sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR));
+}
+
+TEST(sched, sched_getscheduler_sched_setscheduler) {
+ // POSIX: "If pid is zero, the scheduling policy shall be returned for the
+ // calling process".
+ ASSERT_EQ(sched_getscheduler(getpid()), sched_getscheduler(0));
+
+ const int original_policy = sched_getscheduler(getpid());
+ sched_param p = {};
+ p.sched_priority = sched_get_priority_min(original_policy);
+ errno = 0;
+ ASSERT_EQ(-1, sched_setscheduler(getpid(), INT_MAX, &p));
+ ASSERT_EQ(EINVAL, errno);
+
+ ASSERT_EQ(0, sched_getparam(getpid(), &p));
+ ASSERT_EQ(original_policy, sched_setscheduler(getpid(), SCHED_BATCH, &p));
+ // POSIX says this should return the previous policy (here SCHED_BATCH),
+ // but the Linux system call doesn't, and the glibc wrapper doesn't correct
+ // this (the "returns 0" behavior is even documented on the man page in
+ // the BUGS section). This was our historical behavior too, so in the
+ // absence of reasons to break compatibility with ourselves and glibc, we
+ // don't behave as POSIX specifies. http://b/26203902.
+ ASSERT_EQ(0, sched_setscheduler(getpid(), original_policy, &p));
+}