Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / test / commandlineflags_gtest.cc
blob8412008ffe3592a290c60175afcee6692b9ed55e
1 #include <cstdlib>
3 #include "../src/commandlineflags.h"
4 #include "../src/internal_macros.h"
5 #include "gmock/gmock.h"
6 #include "gtest/gtest.h"
8 namespace benchmark {
9 namespace {
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;
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);
35 unsetenv("IN_ENV");
37 ASSERT_EQ(setenv("IN_ENV", "N", 1), 0);
38 EXPECT_EQ(BoolFromEnv("in_env", true), false);
39 unsetenv("IN_ENV");
41 ASSERT_EQ(setenv("IN_ENV", "n", 1), 0);
42 EXPECT_EQ(BoolFromEnv("in_env", true), false);
43 unsetenv("IN_ENV");
45 ASSERT_EQ(setenv("IN_ENV", "NO", 1), 0);
46 EXPECT_EQ(BoolFromEnv("in_env", true), false);
47 unsetenv("IN_ENV");
49 ASSERT_EQ(setenv("IN_ENV", "No", 1), 0);
50 EXPECT_EQ(BoolFromEnv("in_env", true), false);
51 unsetenv("IN_ENV");
53 ASSERT_EQ(setenv("IN_ENV", "no", 1), 0);
54 EXPECT_EQ(BoolFromEnv("in_env", true), false);
55 unsetenv("IN_ENV");
57 ASSERT_EQ(setenv("IN_ENV", "F", 1), 0);
58 EXPECT_EQ(BoolFromEnv("in_env", true), false);
59 unsetenv("IN_ENV");
61 ASSERT_EQ(setenv("IN_ENV", "f", 1), 0);
62 EXPECT_EQ(BoolFromEnv("in_env", true), false);
63 unsetenv("IN_ENV");
65 ASSERT_EQ(setenv("IN_ENV", "FALSE", 1), 0);
66 EXPECT_EQ(BoolFromEnv("in_env", true), false);
67 unsetenv("IN_ENV");
69 ASSERT_EQ(setenv("IN_ENV", "False", 1), 0);
70 EXPECT_EQ(BoolFromEnv("in_env", true), false);
71 unsetenv("IN_ENV");
73 ASSERT_EQ(setenv("IN_ENV", "false", 1), 0);
74 EXPECT_EQ(BoolFromEnv("in_env", true), false);
75 unsetenv("IN_ENV");
77 ASSERT_EQ(setenv("IN_ENV", "OFF", 1), 0);
78 EXPECT_EQ(BoolFromEnv("in_env", true), false);
79 unsetenv("IN_ENV");
81 ASSERT_EQ(setenv("IN_ENV", "Off", 1), 0);
82 EXPECT_EQ(BoolFromEnv("in_env", true), false);
83 unsetenv("IN_ENV");
85 ASSERT_EQ(setenv("IN_ENV", "off", 1), 0);
86 EXPECT_EQ(BoolFromEnv("in_env", true), false);
87 unsetenv("IN_ENV");
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");
95 ASSERT_EQ(setenv("IN_ENV", "Y", 1), 0);
96 EXPECT_EQ(BoolFromEnv("in_env", false), true);
97 unsetenv("IN_ENV");
99 ASSERT_EQ(setenv("IN_ENV", "y", 1), 0);
100 EXPECT_EQ(BoolFromEnv("in_env", false), true);
101 unsetenv("IN_ENV");
103 ASSERT_EQ(setenv("IN_ENV", "YES", 1), 0);
104 EXPECT_EQ(BoolFromEnv("in_env", false), true);
105 unsetenv("IN_ENV");
107 ASSERT_EQ(setenv("IN_ENV", "Yes", 1), 0);
108 EXPECT_EQ(BoolFromEnv("in_env", false), true);
109 unsetenv("IN_ENV");
111 ASSERT_EQ(setenv("IN_ENV", "yes", 1), 0);
112 EXPECT_EQ(BoolFromEnv("in_env", false), true);
113 unsetenv("IN_ENV");
115 ASSERT_EQ(setenv("IN_ENV", "T", 1), 0);
116 EXPECT_EQ(BoolFromEnv("in_env", false), true);
117 unsetenv("IN_ENV");
119 ASSERT_EQ(setenv("IN_ENV", "t", 1), 0);
120 EXPECT_EQ(BoolFromEnv("in_env", false), true);
121 unsetenv("IN_ENV");
123 ASSERT_EQ(setenv("IN_ENV", "TRUE", 1), 0);
124 EXPECT_EQ(BoolFromEnv("in_env", false), true);
125 unsetenv("IN_ENV");
127 ASSERT_EQ(setenv("IN_ENV", "True", 1), 0);
128 EXPECT_EQ(BoolFromEnv("in_env", false), true);
129 unsetenv("IN_ENV");
131 ASSERT_EQ(setenv("IN_ENV", "true", 1), 0);
132 EXPECT_EQ(BoolFromEnv("in_env", false), true);
133 unsetenv("IN_ENV");
135 ASSERT_EQ(setenv("IN_ENV", "ON", 1), 0);
136 EXPECT_EQ(BoolFromEnv("in_env", false), true);
137 unsetenv("IN_ENV");
139 ASSERT_EQ(setenv("IN_ENV", "On", 1), 0);
140 EXPECT_EQ(BoolFromEnv("in_env", false), true);
141 unsetenv("IN_ENV");
143 ASSERT_EQ(setenv("IN_ENV", "on", 1), 0);
144 EXPECT_EQ(BoolFromEnv("in_env", false), true);
145 unsetenv("IN_ENV");
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
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);
162 unsetenv("IN_ENV");
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");
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);
179 unsetenv("IN_ENV");
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");
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");
196 unsetenv("IN_ENV");
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")));
209 unsetenv("IN_ENV");
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");
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");
227 } // namespace
228 } // namespace benchmark