1 | #include <cstdlib> |
2 | |
3 | #include "../src/commandlineflags.h" |
4 | #include "../src/internal_macros.h" |
5 | #include "gmock/gmock.h" |
6 | #include "gtest/gtest.h" |
7 | |
8 | namespace benchmark { |
9 | namespace { |
10 | |
11 | #if defined(BENCHMARK_OS_WINDOWS) |
12 | int setenv(const char* name, const char* value, int overwrite) { |
13 | if (!overwrite) { |
14 | // NOTE: getenv_s is far superior but not available under mingw. |
15 | char* env_value = getenv(name); |
16 | if (env_value == nullptr) { |
17 | return -1; |
18 | } |
19 | } |
20 | return _putenv_s(name, value); |
21 | } |
22 | |
23 | int unsetenv(const char* name) { return _putenv_s(name, "" ); } |
24 | |
25 | #endif // BENCHMARK_OS_WINDOWS |
26 | |
27 | TEST(BoolFromEnv, Default) { |
28 | ASSERT_EQ(unsetenv("NOT_IN_ENV" ), 0); |
29 | EXPECT_EQ(BoolFromEnv("not_in_env" , true), true); |
30 | } |
31 | |
32 | TEST(BoolFromEnv, False) { |
33 | ASSERT_EQ(setenv("IN_ENV" , "0" , 1), 0); |
34 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
35 | unsetenv("IN_ENV" ); |
36 | |
37 | ASSERT_EQ(setenv("IN_ENV" , "N" , 1), 0); |
38 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
39 | unsetenv("IN_ENV" ); |
40 | |
41 | ASSERT_EQ(setenv("IN_ENV" , "n" , 1), 0); |
42 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
43 | unsetenv("IN_ENV" ); |
44 | |
45 | ASSERT_EQ(setenv("IN_ENV" , "NO" , 1), 0); |
46 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
47 | unsetenv("IN_ENV" ); |
48 | |
49 | ASSERT_EQ(setenv("IN_ENV" , "No" , 1), 0); |
50 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
51 | unsetenv("IN_ENV" ); |
52 | |
53 | ASSERT_EQ(setenv("IN_ENV" , "no" , 1), 0); |
54 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
55 | unsetenv("IN_ENV" ); |
56 | |
57 | ASSERT_EQ(setenv("IN_ENV" , "F" , 1), 0); |
58 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
59 | unsetenv("IN_ENV" ); |
60 | |
61 | ASSERT_EQ(setenv("IN_ENV" , "f" , 1), 0); |
62 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
63 | unsetenv("IN_ENV" ); |
64 | |
65 | ASSERT_EQ(setenv("IN_ENV" , "FALSE" , 1), 0); |
66 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
67 | unsetenv("IN_ENV" ); |
68 | |
69 | ASSERT_EQ(setenv("IN_ENV" , "False" , 1), 0); |
70 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
71 | unsetenv("IN_ENV" ); |
72 | |
73 | ASSERT_EQ(setenv("IN_ENV" , "false" , 1), 0); |
74 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
75 | unsetenv("IN_ENV" ); |
76 | |
77 | ASSERT_EQ(setenv("IN_ENV" , "OFF" , 1), 0); |
78 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
79 | unsetenv("IN_ENV" ); |
80 | |
81 | ASSERT_EQ(setenv("IN_ENV" , "Off" , 1), 0); |
82 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
83 | unsetenv("IN_ENV" ); |
84 | |
85 | ASSERT_EQ(setenv("IN_ENV" , "off" , 1), 0); |
86 | EXPECT_EQ(BoolFromEnv("in_env" , true), false); |
87 | unsetenv("IN_ENV" ); |
88 | } |
89 | |
90 | TEST(BoolFromEnv, True) { |
91 | ASSERT_EQ(setenv("IN_ENV" , "1" , 1), 0); |
92 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
93 | unsetenv("IN_ENV" ); |
94 | |
95 | ASSERT_EQ(setenv("IN_ENV" , "Y" , 1), 0); |
96 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
97 | unsetenv("IN_ENV" ); |
98 | |
99 | ASSERT_EQ(setenv("IN_ENV" , "y" , 1), 0); |
100 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
101 | unsetenv("IN_ENV" ); |
102 | |
103 | ASSERT_EQ(setenv("IN_ENV" , "YES" , 1), 0); |
104 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
105 | unsetenv("IN_ENV" ); |
106 | |
107 | ASSERT_EQ(setenv("IN_ENV" , "Yes" , 1), 0); |
108 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
109 | unsetenv("IN_ENV" ); |
110 | |
111 | ASSERT_EQ(setenv("IN_ENV" , "yes" , 1), 0); |
112 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
113 | unsetenv("IN_ENV" ); |
114 | |
115 | ASSERT_EQ(setenv("IN_ENV" , "T" , 1), 0); |
116 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
117 | unsetenv("IN_ENV" ); |
118 | |
119 | ASSERT_EQ(setenv("IN_ENV" , "t" , 1), 0); |
120 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
121 | unsetenv("IN_ENV" ); |
122 | |
123 | ASSERT_EQ(setenv("IN_ENV" , "TRUE" , 1), 0); |
124 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
125 | unsetenv("IN_ENV" ); |
126 | |
127 | ASSERT_EQ(setenv("IN_ENV" , "True" , 1), 0); |
128 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
129 | unsetenv("IN_ENV" ); |
130 | |
131 | ASSERT_EQ(setenv("IN_ENV" , "true" , 1), 0); |
132 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
133 | unsetenv("IN_ENV" ); |
134 | |
135 | ASSERT_EQ(setenv("IN_ENV" , "ON" , 1), 0); |
136 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
137 | unsetenv("IN_ENV" ); |
138 | |
139 | ASSERT_EQ(setenv("IN_ENV" , "On" , 1), 0); |
140 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
141 | unsetenv("IN_ENV" ); |
142 | |
143 | ASSERT_EQ(setenv("IN_ENV" , "on" , 1), 0); |
144 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
145 | unsetenv("IN_ENV" ); |
146 | |
147 | #ifndef BENCHMARK_OS_WINDOWS |
148 | ASSERT_EQ(setenv("IN_ENV" , "" , 1), 0); |
149 | EXPECT_EQ(BoolFromEnv("in_env" , false), true); |
150 | unsetenv("IN_ENV" ); |
151 | #endif |
152 | } |
153 | |
154 | TEST(Int32FromEnv, NotInEnv) { |
155 | ASSERT_EQ(unsetenv("NOT_IN_ENV" ), 0); |
156 | EXPECT_EQ(Int32FromEnv("not_in_env" , 42), 42); |
157 | } |
158 | |
159 | TEST(Int32FromEnv, InvalidInteger) { |
160 | ASSERT_EQ(setenv("IN_ENV" , "foo" , 1), 0); |
161 | EXPECT_EQ(Int32FromEnv("in_env" , 42), 42); |
162 | unsetenv("IN_ENV" ); |
163 | } |
164 | |
165 | TEST(Int32FromEnv, ValidInteger) { |
166 | ASSERT_EQ(setenv("IN_ENV" , "42" , 1), 0); |
167 | EXPECT_EQ(Int32FromEnv("in_env" , 64), 42); |
168 | unsetenv("IN_ENV" ); |
169 | } |
170 | |
171 | TEST(DoubleFromEnv, NotInEnv) { |
172 | ASSERT_EQ(unsetenv("NOT_IN_ENV" ), 0); |
173 | EXPECT_EQ(DoubleFromEnv("not_in_env" , 0.51), 0.51); |
174 | } |
175 | |
176 | TEST(DoubleFromEnv, InvalidReal) { |
177 | ASSERT_EQ(setenv("IN_ENV" , "foo" , 1), 0); |
178 | EXPECT_EQ(DoubleFromEnv("in_env" , 0.51), 0.51); |
179 | unsetenv("IN_ENV" ); |
180 | } |
181 | |
182 | TEST(DoubleFromEnv, ValidReal) { |
183 | ASSERT_EQ(setenv("IN_ENV" , "0.51" , 1), 0); |
184 | EXPECT_EQ(DoubleFromEnv("in_env" , 0.71), 0.51); |
185 | unsetenv("IN_ENV" ); |
186 | } |
187 | |
188 | TEST(StringFromEnv, Default) { |
189 | ASSERT_EQ(unsetenv("NOT_IN_ENV" ), 0); |
190 | EXPECT_STREQ(StringFromEnv("not_in_env" , "foo" ), "foo" ); |
191 | } |
192 | |
193 | TEST(StringFromEnv, Valid) { |
194 | ASSERT_EQ(setenv("IN_ENV" , "foo" , 1), 0); |
195 | EXPECT_STREQ(StringFromEnv("in_env" , "bar" ), "foo" ); |
196 | unsetenv("IN_ENV" ); |
197 | } |
198 | |
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" ))); |
203 | } |
204 | |
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" ))); |
209 | unsetenv("IN_ENV" ); |
210 | } |
211 | |
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" ))); |
216 | unsetenv("IN_ENV" ); |
217 | } |
218 | |
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" ))); |
224 | unsetenv("IN_ENV" ); |
225 | } |
226 | |
227 | } // namespace |
228 | } // namespace benchmark |
229 | |