2019-04-17 16:08:52 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include "../src/commandlineflags.h"
|
|
|
|
#include "../src/internal_macros.h"
|
2021-05-04 13:36:11 +00:00
|
|
|
#include "gmock/gmock.h"
|
2019-04-17 16:08:52 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2021-05-04 13:36:11 +00:00
|
|
|
int unsetenv(const char* name) { return _putenv_s(name, ""); }
|
2019-04-17 16:08:52 +00:00
|
|
|
|
|
|
|
#endif // BENCHMARK_OS_WINDOWS
|
|
|
|
|
|
|
|
TEST(BoolFromEnv, Default) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("not_in_env", true), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BoolFromEnv, False) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "0", 1), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "N", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "n", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "NO", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "No", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "no", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "F", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "f", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "FALSE", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "False", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "false", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "OFF", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "Off", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "off", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", true), false);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-04-17 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BoolFromEnv, True) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "1", 1), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-04-17 16:08:52 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "Y", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "y", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "YES", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "Yes", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "yes", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "T", 1), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "t", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "TRUE", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "True", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "true", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "ON", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "On", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "on", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
|
|
|
|
#ifndef BENCHMARK_OS_WINDOWS
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(BoolFromEnv("in_env", false), true);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
#endif
|
2019-04-17 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Int32FromEnv, NotInEnv) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(Int32FromEnv("not_in_env", 42), 42);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Int32FromEnv, InvalidInteger) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(Int32FromEnv("in_env", 42), 42);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-04-17 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Int32FromEnv, ValidInteger) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "42", 1), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_EQ(Int32FromEnv("in_env", 64), 42);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-04-17 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:08 +00:00
|
|
|
TEST(DoubleFromEnv, NotInEnv) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(DoubleFromEnv("not_in_env", 0.51), 0.51);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DoubleFromEnv, InvalidReal) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(DoubleFromEnv("in_env", 0.51), 0.51);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DoubleFromEnv, ValidReal) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "0.51", 1), 0);
|
2019-10-23 08:07:08 +00:00
|
|
|
EXPECT_EQ(DoubleFromEnv("in_env", 0.71), 0.51);
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-10-23 08:07:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 16:08:52 +00:00
|
|
|
TEST(StringFromEnv, Default) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_STREQ(StringFromEnv("not_in_env", "foo"), "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringFromEnv, Valid) {
|
2020-08-18 09:02:20 +00:00
|
|
|
ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
|
2019-04-17 16:08:52 +00:00
|
|
|
EXPECT_STREQ(StringFromEnv("in_env", "bar"), "foo");
|
2020-08-18 09:02:20 +00:00
|
|
|
unsetenv("IN_ENV");
|
2019-04-17 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 13:36:11 +00:00
|
|
|
TEST(KvPairsFromEnv, Default) {
|
|
|
|
ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
|
|
|
|
EXPECT_THAT(KvPairsFromEnv("not_in_env", {{"foo", "bar"}}),
|
|
|
|
testing::ElementsAre(testing::Pair("foo", "bar")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(KvPairsFromEnv, MalformedReturnsDefault) {
|
|
|
|
ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
|
|
|
|
EXPECT_THAT(KvPairsFromEnv("in_env", {{"foo", "bar"}}),
|
|
|
|
testing::ElementsAre(testing::Pair("foo", "bar")));
|
|
|
|
unsetenv("IN_ENV");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(KvPairsFromEnv, Single) {
|
|
|
|
ASSERT_EQ(setenv("IN_ENV", "foo=bar", 1), 0);
|
|
|
|
EXPECT_THAT(KvPairsFromEnv("in_env", {}),
|
|
|
|
testing::ElementsAre(testing::Pair("foo", "bar")));
|
|
|
|
unsetenv("IN_ENV");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(KvPairsFromEnv, Multiple) {
|
|
|
|
ASSERT_EQ(setenv("IN_ENV", "foo=bar,baz=qux", 1), 0);
|
|
|
|
EXPECT_THAT(KvPairsFromEnv("in_env", {}),
|
|
|
|
testing::UnorderedElementsAre(testing::Pair("foo", "bar"),
|
|
|
|
testing::Pair("baz", "qux")));
|
|
|
|
unsetenv("IN_ENV");
|
|
|
|
}
|
|
|
|
|
2019-04-17 16:08:52 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace benchmark
|