19638da20SNoah Goldstein //===-- Unittests for __sched_cpu_count -----------------------------------===//
20b790afbSMichael Jones //
30b790afbSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b790afbSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
50b790afbSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b790afbSMichael Jones //
70b790afbSMichael Jones //===----------------------------------------------------------------------===//
80b790afbSMichael Jones
90b790afbSMichael Jones #include "src/__support/OSUtil/syscall.h"
10d49b993fSSiva Chandra Reddy #include "src/errno/libc_errno.h"
110b790afbSMichael Jones #include "src/sched/sched_getaffinity.h"
120b790afbSMichael Jones #include "src/sched/sched_getcpucount.h"
134f1fe19dSSiva Chandra Reddy #include "test/UnitTest/ErrnoSetterMatcher.h"
140b790afbSMichael Jones
150b790afbSMichael Jones #include <sched.h>
160b790afbSMichael Jones #include <sys/syscall.h>
170b790afbSMichael Jones
TEST(LlvmLibcSchedCpuCountTest,SmokeTest)189638da20SNoah Goldstein TEST(LlvmLibcSchedCpuCountTest, SmokeTest) {
190b790afbSMichael Jones cpu_set_t mask;
20*3eb1e6d8Smichaelrj-google LIBC_NAMESPACE::libc_errno = 0;
21b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
22b6bc9d72SGuillaume Chatelet pid_t tid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_gettid);
230b790afbSMichael Jones ASSERT_GT(tid, pid_t(0));
24b6bc9d72SGuillaume Chatelet ASSERT_THAT(LIBC_NAMESPACE::sched_getaffinity(tid, sizeof(cpu_set_t), &mask),
250b790afbSMichael Jones Succeeds(0));
260b790afbSMichael Jones
270b790afbSMichael Jones // CPU_COUNT is a macro, but it expands to an LLVM-libc internal function that
280b790afbSMichael Jones // needs to be in the appropriate namespace for the test.
29b6bc9d72SGuillaume Chatelet int num_cpus = LIBC_NAMESPACE::CPU_COUNT(&mask);
300b790afbSMichael Jones ASSERT_GT(num_cpus, 0);
310b790afbSMichael Jones ASSERT_LE(num_cpus, int(sizeof(cpu_set_t) * sizeof(unsigned long)));
320b790afbSMichael Jones }
33