1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17#include <gtest/gtest.h>
18
19#include <platform/sysinfo.h>
20#include <platform/checked_snprintf.h>
21
22char cpu_count_env_var[256];
23
24TEST(GetAvailableCpu, NoVariable) {
25    EXPECT_NE(0u, cb::get_available_cpu_count());
26}
27
28TEST(GetAvailableCpu, CorrectVariable_ExactNumber) {
29    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
30                     "COUCHBASE_CPU_COUNT=10000");
31    putenv(cpu_count_env_var);
32    EXPECT_EQ(10000u, cb::get_available_cpu_count());
33}
34
35TEST(GetAvailableCpu, CorrectVariable_LeadingSpace) {
36    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
37                     "COUCHBASE_CPU_COUNT= 9999");
38    putenv(cpu_count_env_var);
39    EXPECT_EQ(9999u, cb::get_available_cpu_count());
40}
41
42TEST(GetAvailableCpu, CorrectVariable_TrailingSpace) {
43    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
44                     "COUCHBASE_CPU_COUNT=9998 ");
45    putenv(cpu_count_env_var);
46    EXPECT_EQ(9998u, cb::get_available_cpu_count());
47}
48
49TEST(GetAvailableCpu, CorrectVariable_LeadingTab) {
50    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
51                     "COUCHBASE_CPU_COUNT=\t9997");
52    putenv(cpu_count_env_var);
53    EXPECT_EQ(9997u, cb::get_available_cpu_count());
54}
55
56TEST(GetAvailableCpu, CorrectVariable_TrailigTab) {
57    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
58                     "COUCHBASE_CPU_COUNT=9996\t");
59    putenv(cpu_count_env_var);
60    EXPECT_EQ(9996u, cb::get_available_cpu_count());
61}
62
63TEST(GetAvailableCpu, InvalidValue) {
64    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
65                     "COUCHBASE_CPU_COUNT=1a");
66    putenv(cpu_count_env_var);
67    EXPECT_THROW(cb::get_available_cpu_count(), std::logic_error);
68
69    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
70                     "COUCHBASE_CPU_COUNT=1 a");
71    putenv(cpu_count_env_var);
72    EXPECT_THROW(cb::get_available_cpu_count(), std::logic_error);
73
74    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
75                     "COUCHBASE_CPU_COUNT=a1");
76    putenv(cpu_count_env_var);
77    EXPECT_THROW(cb::get_available_cpu_count(), std::logic_error);
78
79    checked_snprintf(cpu_count_env_var, sizeof(cpu_count_env_var),
80                     "COUCHBASE_CPU_COUNT=a 1");
81    putenv(cpu_count_env_var);
82    EXPECT_THROW(cb::get_available_cpu_count(), std::logic_error);
83}
84
85TEST(GetCpuCount, get_cpu_count) {
86    auto count = cb::get_cpu_count();
87    EXPECT_NE(0u, count);
88    std::cout << "get_cpu_count:" << count << std::endl;
89}
90
91// This test may never fail, but we at least expect the following to never
92// happen
93TEST(GetCpuCount, get_cpu_index) {
94    EXPECT_LE(cb::get_cpu_index(), cb::get_cpu_count());
95}
96