Skip to content

Commit 1d41de8

Browse files
authored
Add command line flags tests (#793)
Increase coverage
1 parent 415835e commit 1d41de8

4 files changed

Lines changed: 83 additions & 6 deletions

File tree

src/commandlineflags.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <limits>
2222

2323
namespace benchmark {
24+
namespace {
25+
2426
// Parses 'str' for a 32-bit signed integer. If successful, writes
2527
// the result to *value and returns true; otherwise leaves *value
2628
// unchanged and returns false.
@@ -88,6 +90,8 @@ static std::string FlagToEnvVar(const char* flag) {
8890
return "BENCHMARK_" + env_var;
8991
}
9092

93+
} // namespace
94+
9195
// Reads and returns the Boolean environment variable corresponding to
9296
// the given flag; if it's not set, returns default_value.
9397
//

src/commandlineflags.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@
2323
std::string FLAG(name) = (default_val)
2424

2525
namespace benchmark {
26-
// Parses 'str' for a 32-bit signed integer. If successful, writes the result
27-
// to *value and returns true; otherwise leaves *value unchanged and returns
28-
// false.
29-
bool ParseInt32(const std::string& src_text, const char* str, int32_t* value);
30-
3126
// Parses a bool/Int32/string from the environment variable
3227
// corresponding to the given Google Test flag.
3328
bool BoolFromEnv(const char* flag, bool default_val);
3429
int32_t Int32FromEnv(const char* flag, int32_t default_val);
35-
double DoubleFromEnv(const char* flag, double default_val);
3630
const char* StringFromEnv(const char* flag, const char* default_val);
3731

3832
// Parses a string for a bool flag, in the form of either

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ if (BENCHMARK_ENABLE_GTEST_TESTS)
195195

196196
add_gtest(benchmark_gtest)
197197
add_gtest(benchmark_name_gtest)
198+
add_gtest(commandlineflags_gtest)
198199
add_gtest(statistics_gtest)
199200
add_gtest(string_util_gtest)
200201
endif(BENCHMARK_ENABLE_GTEST_TESTS)

test/commandlineflags_gtest.cc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <cstdlib>
2+
3+
#include "../src/commandlineflags.h"
4+
#include "../src/internal_macros.h"
5+
#include "gtest/gtest.h"
6+
7+
namespace benchmark {
8+
namespace {
9+
10+
#if defined(BENCHMARK_OS_WINDOWS)
11+
int setenv(const char* name, const char* value, int overwrite) {
12+
if (!overwrite) {
13+
// NOTE: getenv_s is far superior but not available under mingw.
14+
char* env_value = getenv(name);
15+
if (env_value == nullptr) {
16+
return -1;
17+
}
18+
}
19+
return _putenv_s(name, value);
20+
}
21+
22+
int unsetenv(const char* name) {
23+
return _putenv_s(name, "");
24+
}
25+
26+
#endif // BENCHMARK_OS_WINDOWS
27+
28+
TEST(BoolFromEnv, Default) {
29+
ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0);
30+
EXPECT_EQ(BoolFromEnv("not_in_env", true), true);
31+
}
32+
33+
TEST(BoolFromEnv, False) {
34+
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "0", 1), 0);
35+
EXPECT_EQ(BoolFromEnv("in_env", true), false);
36+
unsetenv("BENCHMARK_IN_ENV");
37+
}
38+
39+
TEST(BoolFromEnv, True) {
40+
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "1", 1), 0);
41+
EXPECT_EQ(BoolFromEnv("in_env", false), true);
42+
unsetenv("BENCHMARK_IN_ENV");
43+
44+
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0);
45+
EXPECT_EQ(BoolFromEnv("in_env", false), true);
46+
unsetenv("BENCHMARK_IN_ENV");
47+
}
48+
49+
TEST(Int32FromEnv, NotInEnv) {
50+
ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0);
51+
EXPECT_EQ(Int32FromEnv("not_in_env", 42), 42);
52+
}
53+
54+
TEST(Int32FromEnv, InvalidInteger) {
55+
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0);
56+
EXPECT_EQ(Int32FromEnv("in_env", 42), 42);
57+
ASSERT_EQ(unsetenv("BENCHMARK_IN_ENV"), 0);
58+
}
59+
60+
TEST(Int32FromEnv, ValidInteger) {
61+
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "42", 1), 0);
62+
EXPECT_EQ(Int32FromEnv("in_env", 64), 42);
63+
unsetenv("BENCHMARK_IN_ENV");
64+
}
65+
66+
TEST(StringFromEnv, Default) {
67+
ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0);
68+
EXPECT_STREQ(StringFromEnv("not_in_env", "foo"), "foo");
69+
}
70+
71+
TEST(StringFromEnv, Valid) {
72+
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0);
73+
EXPECT_STREQ(StringFromEnv("in_env", "bar"), "foo");
74+
unsetenv("BENCHMARK_IN_ENV");
75+
}
76+
77+
} // namespace
78+
} // namespace benchmark

0 commit comments

Comments
 (0)