Add command line flags tests (#793)

Increase coverage
This commit is contained in:
Dominic Hamon 2019-04-17 17:08:52 +01:00 committed by GitHub
parent 415835e03e
commit 1d41de8463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 6 deletions

View File

@ -21,6 +21,8 @@
#include <limits>
namespace benchmark {
namespace {
// Parses 'str' for a 32-bit signed integer. If successful, writes
// the result to *value and returns true; otherwise leaves *value
// unchanged and returns false.
@ -88,6 +90,8 @@ static std::string FlagToEnvVar(const char* flag) {
return "BENCHMARK_" + env_var;
}
} // namespace
// Reads and returns the Boolean environment variable corresponding to
// the given flag; if it's not set, returns default_value.
//

View File

@ -23,16 +23,10 @@
std::string FLAG(name) = (default_val)
namespace benchmark {
// Parses 'str' for a 32-bit signed integer. If successful, writes the result
// to *value and returns true; otherwise leaves *value unchanged and returns
// false.
bool ParseInt32(const std::string& src_text, const char* str, int32_t* value);
// Parses a bool/Int32/string from the environment variable
// corresponding to the given Google Test flag.
bool BoolFromEnv(const char* flag, bool default_val);
int32_t Int32FromEnv(const char* flag, int32_t default_val);
double DoubleFromEnv(const char* flag, double default_val);
const char* StringFromEnv(const char* flag, const char* default_val);
// Parses a string for a bool flag, in the form of either

View File

@ -195,6 +195,7 @@ if (BENCHMARK_ENABLE_GTEST_TESTS)
add_gtest(benchmark_gtest)
add_gtest(benchmark_name_gtest)
add_gtest(commandlineflags_gtest)
add_gtest(statistics_gtest)
add_gtest(string_util_gtest)
endif(BENCHMARK_ENABLE_GTEST_TESTS)

View File

@ -0,0 +1,78 @@
#include <cstdlib>
#include "../src/commandlineflags.h"
#include "../src/internal_macros.h"
#include "gtest/gtest.h"
namespace benchmark {
namespace {
#if defined(BENCHMARK_OS_WINDOWS)
int setenv(const char* name, const char* value, int overwrite) {
if (!overwrite) {
// NOTE: getenv_s is far superior but not available under mingw.
char* env_value = getenv(name);
if (env_value == nullptr) {
return -1;
}
}
return _putenv_s(name, value);
}
int unsetenv(const char* name) {
return _putenv_s(name, "");
}
#endif // BENCHMARK_OS_WINDOWS
TEST(BoolFromEnv, Default) {
ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0);
EXPECT_EQ(BoolFromEnv("not_in_env", true), true);
}
TEST(BoolFromEnv, False) {
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "0", 1), 0);
EXPECT_EQ(BoolFromEnv("in_env", true), false);
unsetenv("BENCHMARK_IN_ENV");
}
TEST(BoolFromEnv, True) {
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "1", 1), 0);
EXPECT_EQ(BoolFromEnv("in_env", false), true);
unsetenv("BENCHMARK_IN_ENV");
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0);
EXPECT_EQ(BoolFromEnv("in_env", false), true);
unsetenv("BENCHMARK_IN_ENV");
}
TEST(Int32FromEnv, NotInEnv) {
ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0);
EXPECT_EQ(Int32FromEnv("not_in_env", 42), 42);
}
TEST(Int32FromEnv, InvalidInteger) {
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0);
EXPECT_EQ(Int32FromEnv("in_env", 42), 42);
ASSERT_EQ(unsetenv("BENCHMARK_IN_ENV"), 0);
}
TEST(Int32FromEnv, ValidInteger) {
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "42", 1), 0);
EXPECT_EQ(Int32FromEnv("in_env", 64), 42);
unsetenv("BENCHMARK_IN_ENV");
}
TEST(StringFromEnv, Default) {
ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0);
EXPECT_STREQ(StringFromEnv("not_in_env", "foo"), "foo");
}
TEST(StringFromEnv, Valid) {
ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0);
EXPECT_STREQ(StringFromEnv("in_env", "bar"), "foo");
unsetenv("BENCHMARK_IN_ENV");
}
} // namespace
} // namespace benchmark