20 #include "ns3/environment-variable.h"
58 void DoRun()
override;
71 void SetVariable(
const std::string& where,
const std::string& value);
86 void Check(
const std::string& where,
const std::string& envValue,
KeyValueStore expect);
113 const std::string& envValue,
115 const std::string& key,
127 :
TestCase(
"environment-variable-cache")
144 const char* envCstr = std::getenv(
m_variable.c_str());
164 << where <<
" variable: '" << envValue <<
"', expect[" << expect.size() <<
"]"
165 <<
", dict[" << dict.size() <<
"]\n";
170 for (
const auto& kv : expect)
172 std::cout <<
" [" << i++ <<
"] '" << kv.first <<
"'\t'" << kv.second <<
"'";
174 auto loc = dict.find(kv.first);
175 bool found = loc != dict.end();
176 std::cout << (found ?
"\tfound" :
"\tNOT FOUND");
181 bool match = kv.second == loc->second;
184 std::cout <<
", match";
188 std::cout <<
", NO MATCH: '" << loc->second <<
"'";
199 for (
const auto& kv : dict)
201 bool found = expect.find(kv.first) != expect.end();
204 std::cout << (
first ?
"Unexpected keys:" :
"");
206 std::cout <<
" [" << i <<
"] '" << kv.first <<
"'\t'" << kv.second <<
"'"
207 <<
" unexpected key, value\n";
215 const std::string& envValue,
219 Check(where, envValue, expect);
228 where <<
": key '" << key <<
"' " << (expect.first ?
"not " :
"")
229 <<
"found unexpectedly");
232 where <<
": incorrect value for key '" << key <<
"'");
237 const std::string& envValue,
239 const std::string& key,
251 Check(
"unset",
"", {});
264 "not|the|right=value",
265 {{
"not",
""}, {
"the",
""}, {
"right",
"value"}},
270 SetCheckAndGet(
"key-only",
"key", {{
"key",
""}},
"key", {
true,
""});
273 SetCheckAndGet(
"front-|",
"|key", {{
"key",
""}},
"key", {
true,
""});
274 SetCheckAndGet(
"back-|",
"key|", {{
"key",
""}},
"key", {
true,
""});
277 SetCheckAndGet(
"front-||",
"||key", {{
"key",
""}},
"key", {
true,
""});
278 SetCheckAndGet(
"back-||",
"key||", {{
"key",
""}},
"key", {
true,
""});
281 SetCheckAndGet(
"two keys",
"key1|key2", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
282 CheckGet(
"two keys",
"key2", {
true,
""});
286 SetCheckAndGet(
"||two keys",
"||key1|key2", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
287 CheckGet(
"||two keys",
"key2", {
true,
""});
288 SetCheckAndGet(
"two keys||",
"key1|key2||", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
289 CheckGet(
"two keys||",
"key2", {
true,
""});
292 SetCheckAndGet(
"key-val",
"key=value", {{
"key",
"value"}},
"key", {
true,
"value"});
296 "key1|key2=value|key3|key4=value",
297 {{
"key1",
""}, {
"key2",
"value"}, {
"key3",
""}, {
"key4",
"value"}},
300 CheckGet(
"mixed",
"key2", {
true,
"value"});
301 CheckGet(
"mixed",
"key3", {
true,
""});
302 CheckGet(
"mixed",
"key4", {
true,
"value"});
308 SetCheckAndGet(
"key==",
"key==", {{
"key",
"="}},
"key", {
true,
"="});
311 std::cout << std::endl;
std::unordered_map< std::string, std::string > KeyValueStore
Key, value store type.
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
static bool Unset(const std::string &variable)
Unset an environment variable.
std::pair< bool, std::string > KeyFoundType
Result of a key lookup.
static void Clear()
Clear the instance, forcing all new lookups.
static bool Set(const std::string &variable, const std::string &value)
Set an environment variable.
static std::shared_ptr< Dictionary > GetDictionary(const std::string &envvar, const std::string &delim=";")
Get the dictionary for a particular environment variable.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
EnvironmentVariable tests.
EnvVarTestCase()
Constructor.
void SetVariable(const std::string &where, const std::string &value)
Set the test environment variable.
void CheckGet(const std::string &where, const std::string &key, KeyFoundType expect)
Check the result from a Get.
const std::string m_delimiter
Always use a non-default delimiter.
const std::string m_variable
Test environment variable name.
void Check(const std::string &where, const std::string &envValue, KeyValueStore expect)
Read envValue and check that it contains only the key,value pairs from expect.
void SetAndCheck(const std::string &where, const std::string &envValue, KeyValueStore expect)
Set and Check the variable.
void DoRun() override
Run the tests.
EnvironmentVariable::Dictionary::KeyValueStore KeyValueStore
The key,value store.
EnvironmentVariable::KeyFoundType KeyFoundType
The return type from EnvironmentVariable::Get()
void UnsetVariable(const std::string &where)
Unset the test environment variable.
void SetCheckAndGet(const std::string &where, const std::string &envValue, KeyValueStore expectDict, const std::string &key, KeyFoundType expectValue)
Set, Check, and Get a variable.
~EnvVarTestCase() override
Destructor.
Environment variable handling test suite.
EnvironmentVariableTestSuite()
static EnvironmentVariableTestSuite g_EnvironmentVariableTestSuite
Static variable for test initialization.
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Every class exported by the ns3 library is enclosed in the ns3 namespace.