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,
117 :
TestCase(
"environment-variable-cache")
134 const char* envCstr = std::getenv(
m_variable.c_str());
154 << where <<
" variable: '" << envValue <<
"', expect[" << expect.size() <<
"]"
155 <<
", dict[" << dict.size() <<
"]\n";
160 for (
const auto& kv : expect)
162 std::cout <<
" [" << i++ <<
"] '" << kv.first <<
"'\t'" << kv.second <<
"'";
164 auto loc = dict.find(kv.first);
165 bool found = loc != dict.end();
166 std::cout << (found ?
"\tfound" :
"\tNOT FOUND");
171 bool match = kv.second == loc->second;
174 std::cout <<
", match";
178 std::cout <<
", NO MATCH: '" << loc->second <<
"'";
189 for (
const auto& kv : dict)
191 bool found = expect.find(kv.first) != expect.end();
194 std::cout << (
first ?
"Unexpected keys:" :
"");
196 std::cout <<
" [" << i <<
"] '" << kv.first <<
"'\t'" << kv.second <<
"'"
197 <<
" unexpected key, value\n";
205 const std::string& envValue,
209 Check(where, envValue, expect);
218 where <<
": key '" << key <<
"' " << (expect.first ?
"not " :
"")
219 <<
"found unexpectedly");
222 where <<
": incorrect value for key '" << key <<
"'");
227 const std::string& envValue,
229 const std::string& key,
241 Check(
"unset",
"", {});
254 "not|the|right=value",
255 {{
"not",
""}, {
"the",
""}, {
"right",
"value"}},
260 SetCheckAndGet(
"key-only",
"key", {{
"key",
""}},
"key", {
true,
""});
263 SetCheckAndGet(
"front-|",
"|key", {{
"key",
""}},
"key", {
true,
""});
264 SetCheckAndGet(
"back-|",
"key|", {{
"key",
""}},
"key", {
true,
""});
267 SetCheckAndGet(
"front-||",
"||key", {{
"key",
""}},
"key", {
true,
""});
268 SetCheckAndGet(
"back-||",
"key||", {{
"key",
""}},
"key", {
true,
""});
271 SetCheckAndGet(
"two keys",
"key1|key2", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
272 CheckGet(
"two keys",
"key2", {
true,
""});
276 SetCheckAndGet(
"||two keys",
"||key1|key2", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
277 CheckGet(
"||two keys",
"key2", {
true,
""});
278 SetCheckAndGet(
"two keys||",
"key1|key2||", {{
"key1",
""}, {
"key2",
""}},
"key1", {
true,
""});
279 CheckGet(
"two keys||",
"key2", {
true,
""});
282 SetCheckAndGet(
"key-val",
"key=value", {{
"key",
"value"}},
"key", {
true,
"value"});
286 "key1|key2=value|key3|key4=value",
287 {{
"key1",
""}, {
"key2",
"value"}, {
"key3",
""}, {
"key4",
"value"}},
290 CheckGet(
"mixed",
"key2", {
true,
"value"});
291 CheckGet(
"mixed",
"key3", {
true,
""});
292 CheckGet(
"mixed",
"key4", {
true,
"value"});
298 SetCheckAndGet(
"key==",
"key==", {{
"key",
"="}},
"key", {
true,
"="});
301 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.
TestCase(const TestCase &)=delete
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new 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.
Namespace for test files, TestCases and TestSuites.
Every class exported by the ns3 library is enclosed in the ns3 namespace.