3 #include "../src/commandlineflags.h"
4 #include "../src/internal_macros.h"
5 #include "gmock/gmock.h"
6 #include "gtest/gtest.h"
11 #if defined(BENCHMARK_OS_WINDOWS)
12 int setenv(const char* name
, const char* value
, int overwrite
) {
14 // NOTE: getenv_s is far superior but not available under mingw.
15 char* env_value
= getenv(name
);
16 if (env_value
== nullptr) {
20 return _putenv_s(name
, value
);
23 int unsetenv(const char* name
) { return _putenv_s(name
, ""); }
25 #endif // BENCHMARK_OS_WINDOWS
27 TEST(BoolFromEnv
, Default
) {
28 ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
29 EXPECT_EQ(BoolFromEnv("not_in_env", true), true);
32 TEST(BoolFromEnv
, False
) {
33 ASSERT_EQ(setenv("IN_ENV", "0", 1), 0);
34 EXPECT_EQ(BoolFromEnv("in_env", true), false);
37 ASSERT_EQ(setenv("IN_ENV", "N", 1), 0);
38 EXPECT_EQ(BoolFromEnv("in_env", true), false);
41 ASSERT_EQ(setenv("IN_ENV", "n", 1), 0);
42 EXPECT_EQ(BoolFromEnv("in_env", true), false);
45 ASSERT_EQ(setenv("IN_ENV", "NO", 1), 0);
46 EXPECT_EQ(BoolFromEnv("in_env", true), false);
49 ASSERT_EQ(setenv("IN_ENV", "No", 1), 0);
50 EXPECT_EQ(BoolFromEnv("in_env", true), false);
53 ASSERT_EQ(setenv("IN_ENV", "no", 1), 0);
54 EXPECT_EQ(BoolFromEnv("in_env", true), false);
57 ASSERT_EQ(setenv("IN_ENV", "F", 1), 0);
58 EXPECT_EQ(BoolFromEnv("in_env", true), false);
61 ASSERT_EQ(setenv("IN_ENV", "f", 1), 0);
62 EXPECT_EQ(BoolFromEnv("in_env", true), false);
65 ASSERT_EQ(setenv("IN_ENV", "FALSE", 1), 0);
66 EXPECT_EQ(BoolFromEnv("in_env", true), false);
69 ASSERT_EQ(setenv("IN_ENV", "False", 1), 0);
70 EXPECT_EQ(BoolFromEnv("in_env", true), false);
73 ASSERT_EQ(setenv("IN_ENV", "false", 1), 0);
74 EXPECT_EQ(BoolFromEnv("in_env", true), false);
77 ASSERT_EQ(setenv("IN_ENV", "OFF", 1), 0);
78 EXPECT_EQ(BoolFromEnv("in_env", true), false);
81 ASSERT_EQ(setenv("IN_ENV", "Off", 1), 0);
82 EXPECT_EQ(BoolFromEnv("in_env", true), false);
85 ASSERT_EQ(setenv("IN_ENV", "off", 1), 0);
86 EXPECT_EQ(BoolFromEnv("in_env", true), false);
90 TEST(BoolFromEnv
, True
) {
91 ASSERT_EQ(setenv("IN_ENV", "1", 1), 0);
92 EXPECT_EQ(BoolFromEnv("in_env", false), true);
95 ASSERT_EQ(setenv("IN_ENV", "Y", 1), 0);
96 EXPECT_EQ(BoolFromEnv("in_env", false), true);
99 ASSERT_EQ(setenv("IN_ENV", "y", 1), 0);
100 EXPECT_EQ(BoolFromEnv("in_env", false), true);
103 ASSERT_EQ(setenv("IN_ENV", "YES", 1), 0);
104 EXPECT_EQ(BoolFromEnv("in_env", false), true);
107 ASSERT_EQ(setenv("IN_ENV", "Yes", 1), 0);
108 EXPECT_EQ(BoolFromEnv("in_env", false), true);
111 ASSERT_EQ(setenv("IN_ENV", "yes", 1), 0);
112 EXPECT_EQ(BoolFromEnv("in_env", false), true);
115 ASSERT_EQ(setenv("IN_ENV", "T", 1), 0);
116 EXPECT_EQ(BoolFromEnv("in_env", false), true);
119 ASSERT_EQ(setenv("IN_ENV", "t", 1), 0);
120 EXPECT_EQ(BoolFromEnv("in_env", false), true);
123 ASSERT_EQ(setenv("IN_ENV", "TRUE", 1), 0);
124 EXPECT_EQ(BoolFromEnv("in_env", false), true);
127 ASSERT_EQ(setenv("IN_ENV", "True", 1), 0);
128 EXPECT_EQ(BoolFromEnv("in_env", false), true);
131 ASSERT_EQ(setenv("IN_ENV", "true", 1), 0);
132 EXPECT_EQ(BoolFromEnv("in_env", false), true);
135 ASSERT_EQ(setenv("IN_ENV", "ON", 1), 0);
136 EXPECT_EQ(BoolFromEnv("in_env", false), true);
139 ASSERT_EQ(setenv("IN_ENV", "On", 1), 0);
140 EXPECT_EQ(BoolFromEnv("in_env", false), true);
143 ASSERT_EQ(setenv("IN_ENV", "on", 1), 0);
144 EXPECT_EQ(BoolFromEnv("in_env", false), true);
147 #ifndef BENCHMARK_OS_WINDOWS
148 ASSERT_EQ(setenv("IN_ENV", "", 1), 0);
149 EXPECT_EQ(BoolFromEnv("in_env", false), true);
154 TEST(Int32FromEnv
, NotInEnv
) {
155 ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
156 EXPECT_EQ(Int32FromEnv("not_in_env", 42), 42);
159 TEST(Int32FromEnv
, InvalidInteger
) {
160 ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
161 EXPECT_EQ(Int32FromEnv("in_env", 42), 42);
165 TEST(Int32FromEnv
, ValidInteger
) {
166 ASSERT_EQ(setenv("IN_ENV", "42", 1), 0);
167 EXPECT_EQ(Int32FromEnv("in_env", 64), 42);
171 TEST(DoubleFromEnv
, NotInEnv
) {
172 ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
173 EXPECT_EQ(DoubleFromEnv("not_in_env", 0.51), 0.51);
176 TEST(DoubleFromEnv
, InvalidReal
) {
177 ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
178 EXPECT_EQ(DoubleFromEnv("in_env", 0.51), 0.51);
182 TEST(DoubleFromEnv
, ValidReal
) {
183 ASSERT_EQ(setenv("IN_ENV", "0.51", 1), 0);
184 EXPECT_EQ(DoubleFromEnv("in_env", 0.71), 0.51);
188 TEST(StringFromEnv
, Default
) {
189 ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
190 EXPECT_STREQ(StringFromEnv("not_in_env", "foo"), "foo");
193 TEST(StringFromEnv
, Valid
) {
194 ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
195 EXPECT_STREQ(StringFromEnv("in_env", "bar"), "foo");
199 TEST(KvPairsFromEnv
, Default
) {
200 ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0);
201 EXPECT_THAT(KvPairsFromEnv("not_in_env", {{"foo", "bar"}}),
202 testing::ElementsAre(testing::Pair("foo", "bar")));
205 TEST(KvPairsFromEnv
, MalformedReturnsDefault
) {
206 ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0);
207 EXPECT_THAT(KvPairsFromEnv("in_env", {{"foo", "bar"}}),
208 testing::ElementsAre(testing::Pair("foo", "bar")));
212 TEST(KvPairsFromEnv
, Single
) {
213 ASSERT_EQ(setenv("IN_ENV", "foo=bar", 1), 0);
214 EXPECT_THAT(KvPairsFromEnv("in_env", {}),
215 testing::ElementsAre(testing::Pair("foo", "bar")));
219 TEST(KvPairsFromEnv
, Multiple
) {
220 ASSERT_EQ(setenv("IN_ENV", "foo=bar,baz=qux", 1), 0);
221 EXPECT_THAT(KvPairsFromEnv("in_env", {}),
222 testing::UnorderedElementsAre(testing::Pair("foo", "bar"),
223 testing::Pair("baz", "qux")));
228 } // namespace benchmark