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"
93 char ** argv =
new char* [argc + 1];
96 argv[0] =
new char [strlen (ss.str ().c_str ()) + 1];
97 strcpy (argv[0], ss.str ().c_str ());
101 for (
int i = 1; i < argc; ++i)
103 char *arg = va_arg (ap,
char *);
104 argv[i] =
new char [strlen (arg) + 1];
105 strcpy (argv[i], arg);
109 cmd.
Parse (argc, argv);
112 for (
int i = 0; i < argc; ++i)
130 virtual void DoRun (
void);
144 bool myDefaultFalseBool =
false;
146 cmd.
AddValue (
"my-bool",
"help", myBool);
147 cmd.
AddValue (
"my-false-bool",
"help", myDefaultFalseBool);
149 Parse (cmd, 1,
"--my-bool=0");
150 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly set a boolean value to false");
152 Parse (cmd, 1,
"--my-bool=1");
153 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true, given integer argument");
155 Parse (cmd, 1,
"--my-bool");
156 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly toggle a default true boolean value to false, given no argument");
158 Parse (cmd, 1,
"--my-false-bool");
159 NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool,
true,
"Command parser did not correctly toggle a default false boolean value to true, given no argument");
161 Parse (cmd, 1,
"--my-bool=t");
162 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true, given 't' argument");
164 Parse (cmd, 1,
"--my-bool=true");
165 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true, given \"true\" argument");
179 virtual void DoRun (
void);
193 int32_t myInt32 = 10;
195 cmd.
AddValue (
"my-bool",
"help", myBool);
196 cmd.
AddValue (
"my-int32",
"help", myInt32);
198 Parse (cmd, 2,
"--my-bool=0",
"--my-int32=-3");
199 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly set a boolean value to false");
202 Parse (cmd, 2,
"--my-bool=1",
"--my-int32=+2");
203 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"Command parser did not correctly set a boolean value to true");
218 virtual void DoRun (
void);
232 uint32_t myUint32 = 10;
234 cmd.
AddValue (
"my-bool",
"help", myBool);
235 cmd.
AddValue (
"my-uint32",
"help", myUint32);
237 Parse (cmd, 2,
"--my-bool=0",
"--my-uint32=9");
239 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"Command parser did not correctly set a boolean value to true");
240 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"Command parser did not correctly set an unsigned integer value to 9");
254 virtual void DoRun (
void);
267 uint32_t myUint32 = 10;
268 std::string myStr =
"MyStr";
270 cmd.
AddValue (
"my-uint32",
"help", myUint32);
271 cmd.
AddValue (
"my-str",
"help", myStr);
273 Parse (cmd, 2,
"--my-uint32=9",
"--my-str=XX");
275 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"Command parser did not correctly set an unsigned integer value to 9");
276 NS_TEST_ASSERT_MSG_EQ (myStr,
"XX",
"Command parser did not correctly set an string value to \"XX\"");
290 virtual void DoRun (
void);
303 uint32_t myUint32 = 0;
305 cmd.
AddValue (
"my-uint32",
"help", myUint32);
307 Parse (cmd, 2,
"--my-uint32=1",
"--my-uint32=2");
309 NS_TEST_ASSERT_MSG_EQ (myUint32, 2,
"Command parser did not correctly set an unsigned integer value to 2");
323 virtual void DoRun (
void);
336 uint32_t myUint32 = 0;
338 cmd.
AddValue (
"my-uint32",
"help", myUint32);
340 Parse (cmd, 2,
"quack",
"--my-uint32=5");
342 NS_TEST_ASSERT_MSG_EQ (myUint32, 5,
"Command parser did not correctly set an unsigned integer value to 5");
static int m_count
Test iteration counter to give each test a unique name.
virtual ~CommandLineIntTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineStringTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
CommandLineUnsignedIntTestCase()
Constructor.
virtual ~CommandLineInvalidTestCase()
Destructor.
Test boolean Command Line processing.
CommandLineIntTestCase()
Constructor.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
CommandLineInvalidTestCase()
Constructor.
#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.
CommandLineOrderTestCase()
Constructor.
virtual ~CommandLineOrderTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
Test string Command Line processing.
void Parse(CommandLine &cmd, int n,...)
Excercise the CommandLine with the provided arguments.
Parse command-line arguments.
Test unsigned int Command Line processing.
virtual void DoRun(void)
Run the test.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Run the test.
Test order of argument parsing.
TestCase * GetParent() const
Get the parent of this TestCsse.
Test int Command Line processing.
CommandLineBooleanTestCase()
Constructor.
A test base class that drives Command Line parsing.
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
static CommandLineTestSuite g_commandLineTestSuite
CommandLineTestSuite instance variable.
CommandLineTestCaseBase(std::string description)
Constructor.
CommandLineTestSuite()
Constructor.
The Test Suite that glues all of the Test Cases together.
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string GetName(void) const
Test ignoring invalid arguments.
virtual ~CommandLineTestCaseBase()
Destructor.
CommandLineStringTestCase()
Constructor.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineBooleanTestCase()
Destructor.