1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <unistd.h>
20
21 namespace {
22
CheckPageSize(int page_size)23 bool CheckPageSize(int page_size) {
24 return (page_size == 4 * 1024) || (page_size == 64 * 1024);
25 }
26
27 } // namespace
28
29 // Tests if sysconf(_SC_CLK_TCK) returns a reasonable value.
TEST(SysconfTest,TestClkTck)30 TEST(SysconfTest, TestClkTck) {
31 EXPECT_GE(sysconf(_SC_CLK_TCK), 0);
32 }
33
34 // Tests if sysconf(_SC_PAGESIZE) returns a reasonable value.
TEST(SysconfTest,TestPageSize)35 TEST(SysconfTest, TestPageSize) {
36 const int page_size = sysconf(_SC_PAGESIZE);
37 EXPECT_TRUE(CheckPageSize(page_size)) << page_size;
38 }
39
40 // Do the same with _SC_PAGE_SIZE, just in case.
TEST(SysconfTest,TestPage_Size)41 TEST(SysconfTest, TestPage_Size) {
42 const int page_size = sysconf(_SC_PAGE_SIZE);
43 EXPECT_TRUE(CheckPageSize(page_size)) << page_size;
44 }
45
46 // Tests if sysconf(_SC_NPROCESSORS_*) returns a positive value.
TEST(SysconfTest,TestNProcessors)47 TEST(SysconfTest, TestNProcessors) {
48 EXPECT_GE(sysconf(_SC_NPROCESSORS_CONF), 0);
49 EXPECT_GE(sysconf(_SC_NPROCESSORS_ONLN), 0);
50 // The number of online processors should be smaller than or equal to the number of processors
51 // physically available.
52 EXPECT_LE(sysconf(_SC_NPROCESSORS_ONLN), sysconf(_SC_NPROCESSORS_CONF));
53 }
54