20 #include "ns3/command-line.h"
22 #include "ns3/config.h"
23 #include "ns3/global-value.h"
24 #include "ns3/system-path.h"
25 #include "ns3/type-id.h"
27 #include "ns3/string.h"
76 char ** argv =
new char* [argc + 1];
79 argv[0] =
new char [strlen (ss.str ().c_str ()) + 1];
80 strcpy (argv[0], ss.str ().c_str ());
84 for (
int i = 1; i < argc; ++i)
86 char *arg = va_arg (ap,
char *);
87 argv[i] =
new char [strlen (arg) + 1];
88 strcpy (argv[i], arg);
92 cmd.
Parse (argc, argv);
95 for (
int i = 0; i < argc; ++i)
112 virtual void DoRun (
void);
126 bool myDefaultFalseBool =
false;
128 cmd.
AddValue (
"my-bool",
"help", myBool);
129 cmd.
AddValue (
"my-false-bool",
"help", myDefaultFalseBool);
131 Parse (cmd, 1,
"--my-bool=0");
132 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly set a boolean value to false");
134 Parse (cmd, 1,
"--my-bool=1");
135 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true, given integer argument");
137 Parse (cmd, 1,
"--my-bool");
138 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly toggle a default true boolean value to false, given no argument");
140 Parse (cmd, 1,
"--my-false-bool");
141 NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool,
true,
"Command parser did not correctly toggle a default false boolean value to true, given no argument");
143 Parse (cmd, 1,
"--my-bool=t");
144 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true, given 't' argument");
146 Parse (cmd, 1,
"--my-bool=true");
147 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true, given \"true\" argument");
160 virtual void DoRun (
void);
174 int32_t myInt32 = 10;
176 cmd.
AddValue (
"my-bool",
"help", myBool);
177 cmd.
AddValue (
"my-int32",
"help", myInt32);
179 Parse (cmd, 2,
"--my-bool=0",
"--my-int32=-3");
180 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly set a boolean value to false");
183 Parse (cmd, 2,
"--my-bool=1",
"--my-int32=+2");
184 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true");
198 virtual void DoRun (
void);
212 uint32_t myUint32 = 10;
214 cmd.
AddValue (
"my-bool",
"help", myBool);
215 cmd.
AddValue (
"my-uint32",
"help", myUint32);
217 Parse (cmd, 2,
"--my-bool=0",
"--my-uint32=9");
219 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly set a boolean value to true");
220 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"Command parser did not correctly set an unsigned integer value to 9");
233 virtual void DoRun (
void);
246 uint32_t myUint32 = 10;
247 std::string myStr =
"MyStr";
249 cmd.
AddValue (
"my-uint32",
"help", myUint32);
250 cmd.
AddValue (
"my-str",
"help", myStr);
252 Parse (cmd, 2,
"--my-uint32=9",
"--my-str=XX");
254 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"Command parser did not correctly set an unsigned integer value to 9");
255 NS_TEST_ASSERT_MSG_EQ (myStr,
"XX",
"Command parser did not correctly set an string value to \"XX\"");
The Test Suite that glues all of the Test Cases together.
CommandLineStringTestCase()
Constructor.
static CommandLineTestSuite CommandLineTestSuite
Test instance.
virtual ~CommandLineIntTestCase()
Destructor.
CommandLineTestSuite()
Constructor.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineStringTestCase()
Destructor.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
A test base class that drives Command Line parsing.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
CommandLineBooleanTestCase()
Constructor.
Parse command-line arguments.
CommandLineIntTestCase()
Constructor.
virtual void DoRun(void)
Run the test.
virtual void DoRun(void)
Run the test.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Test unsigned int Command Line processing.
TestCase * GetParent() const
Get the parent of this TestCsse.
CommandLineTestCaseBase(std::string description)
Constructor.
Test boolean Command Line processing.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
virtual void DoRun(void)
Run the test.
void Parse(CommandLine &cmd, int n,...)
Excercise the CommandLine with the provided arguments.
Test int Command Line processing.
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string GetName(void) const
virtual ~CommandLineTestCaseBase()
Destructor.
static int m_count
Test iteration counter to give each test a unique name.
Test string Command Line processing.
virtual ~CommandLineBooleanTestCase()
Destructor.
CommandLineUnsignedIntTestCase()
Constructor.