9#include "ns3/environment-variable.h"
47 void DoRun()
override;
60 void SetVariable(
const std::string& where,
const std::string& value);
75 void Check(
const std::string& where,
const std::string& envValue,
KeyValueStore expect);
102 const std::string& envValue,
104 const std::string& key,
116 :
TestCase(
"environment-variable-cache")
133 const char* envCstr = std::getenv(
m_variable.c_str());
153 << where <<
" variable: '" << envValue <<
"', expect[" << expect.size() <<
"]"
154 <<
", dict[" << dict.size() <<
"]\n";
159 for (
const auto& kv : expect)
161 std::cout <<
" [" << i++ <<
"] '" << kv.first <<
"'\t'" << kv.second <<
"'";
163 auto loc = dict.find(kv.first);
164 bool found = loc != dict.end();
165 std::cout << (found ?
"\tfound" :
"\tNOT FOUND");
170 bool match = kv.second == loc->second;
173 std::cout <<
", match";
177 std::cout <<
", NO MATCH: '" << loc->second <<
"'";
188 for (
const auto& kv : dict)
190 bool found = expect.find(kv.first) != expect.end();
193 std::cout << (
first ?
"Unexpected keys:" :
"");
195 std::cout <<
" [" << i <<
"] '" << kv.first <<
"'\t'" << kv.second <<
"'"
196 <<
" unexpected key, value\n";
204 const std::string& envValue,
208 Check(where, envValue, expect);
217 where <<
": key '" << key <<
"' " << (expect.first ?
"not " :
"")
218 <<
"found unexpectedly");
221 where <<
": incorrect value for key '" << key <<
"'");
226 const std::string& envValue,
228 const std::string& key,
240 Check(
"unset",
"", {});
253 "not|the|right=value",
254 {{
"not",
""}, {
"the",
""}, {
"right",
"value"}},
259 SetCheckAndGet(
"key-only",
"key", {{
"key",
""}},
"key", {
true,
""});
262 SetCheckAndGet(
"front-|",
"|key", {{
"key",
""}},
"key", {
true,
""});
263 SetCheckAndGet(
"back-|",
"key|", {{
"key",
""}},
"key", {
true,
""});
266 SetCheckAndGet(
"front-||",
"||key", {{
"key",
""}},
"key", {
true,
""});
267 SetCheckAndGet(
"back-||",
"key||", {{
"key",
""}},
"key", {
true,
""});
270 SetCheckAndGet(
"two keys",
"key1|key2", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
271 CheckGet(
"two keys",
"key2", {
true,
""});
275 SetCheckAndGet(
"||two keys",
"||key1|key2", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
276 CheckGet(
"||two keys",
"key2", {
true,
""});
277 SetCheckAndGet(
"two keys||",
"key1|key2||", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
278 CheckGet(
"two keys||",
"key2", {
true,
""});
281 SetCheckAndGet(
"key-val",
"key=value", {{
"key",
"value"}},
"key", {
true,
"value"});
285 "key1|key2=value|key3|key4=value",
286 {{
"key1",
""}, {
"key2",
"value"}, {
"key3",
""}, {
"key4",
"value"}},
289 CheckGet(
"mixed",
"key2", {
true,
"value"});
290 CheckGet(
"mixed",
"key3", {
true,
""});
291 CheckGet(
"mixed",
"key4", {
true,
"value"});
297 SetCheckAndGet(
"key==",
"key==", {{
"key",
"="}},
"key", {
true,
"="});
300 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, Duration duration=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.