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)
134 virtual void DoRun (
void);
147 bool myDefaultFalseBool =
false;
149 cmd.AddValue (
"my-bool",
"help", myBool);
150 cmd.AddValue (
"my-false-bool",
"help", myDefaultFalseBool);
153 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"CommandLine did not correctly set a boolean value to false, given 0");
156 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"CommandLine did not correctly set a boolean value to true, given 1");
159 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"CommandLine did not correctly toggle a default true boolean value to false, given no argument");
162 NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool,
true,
"CommandLine did not correctly toggle a default false boolean value to true, given no argument");
165 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"CommandLine did not correctly set a boolean value to true, given 't' argument");
168 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"CommandLine did not correctly set a boolean value to true, given \"true\" argument");
186 virtual void DoRun (
void);
198 uint8_t myUint8 = 10;
200 cmd.AddValue (
"my-uint8",
"help", myUint8);
203 NS_TEST_ASSERT_MSG_EQ (myUint8, 1,
"CommandLine did not correctly set a uint8_t value to 1, given 1");
221 virtual void DoRun (
void);
234 int32_t myInt32 = 10;
236 cmd.AddValue (
"my-bool",
"help", myBool);
237 cmd.AddValue (
"my-int32",
"help", myInt32);
239 Parse (
cmd, 2,
"--my-bool=0",
"--my-int32=-3");
243 Parse (
cmd, 2,
"--my-bool=1",
"--my-int32=+2");
263 virtual void DoRun (
void);
276 uint32_t myUint32 = 10;
278 cmd.AddValue (
"my-bool",
"help", myBool);
279 cmd.AddValue (
"my-uint32",
"help", myUint32);
281 Parse (
cmd, 2,
"--my-bool=0",
"--my-uint32=9");
284 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"CommandLine did not correctly set an unsigned integer value to 9");
302 virtual void DoRun (
void);
314 uint32_t myUint32 = 10;
315 std::string myStr =
"MyStr";
317 cmd.AddValue (
"my-uint32",
"help", myUint32);
318 cmd.AddValue (
"my-str",
"help", myStr);
320 Parse (
cmd, 2,
"--my-uint32=9",
"--my-str=XX");
322 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"CommandLine did not correctly set an unsigned integer value to 9");
341 virtual void DoRun (
void);
353 uint32_t myUint32 = 0;
355 cmd.AddValue (
"my-uint32",
"help", myUint32);
357 Parse (
cmd, 2,
"--my-uint32=1",
"--my-uint32=2");
359 NS_TEST_ASSERT_MSG_EQ (myUint32, 2,
"CommandLine did not correctly set an unsigned integer value to 2");
377 virtual void DoRun (
void);
389 uint32_t myUint32 = 0;
391 cmd.AddValue (
"my-uint32",
"help", myUint32);
393 Parse (
cmd, 2,
"quack",
"--my-uint32=5");
395 NS_TEST_ASSERT_MSG_EQ (myUint32, 5,
"CommandLine did not correctly set an unsigned integer value to 5");
413 virtual void DoRun (
void);
427 std::string myStr =
"MyStr";
429 cmd.AddNonOption (
"my-bool",
"help", myBool);
430 cmd.AddNonOption (
"my-int",
"help", myInt);
431 cmd.AddNonOption (
"my-str",
"help", myStr);
436 NS_TEST_ASSERT_MSG_EQ (myInt, 5,
"CommandLine did not correctly set an integer non-option value to 5");
439 Parse (
cmd, 5,
"false",
"6",
"newValue",
"extraVal1",
"extraVal2");
443 , 6,
"CommandLine did not correctly set an integer non-option value to 5");
446 NS_TEST_ASSERT_MSG_EQ (
cmd.GetNExtraNonOptions (), 2,
"CommandLine did not parse the correct number of extra non-options.");
447 NS_TEST_ASSERT_MSG_EQ (
cmd.GetExtraNonOption (0),
"extraVal1",
"CommandLine did not correctly get one extra non-option");
448 NS_TEST_ASSERT_MSG_EQ (
cmd.GetExtraNonOption (1),
"extraVal2",
"CommandLine did not correctly get two extra non-option");
Parse command-line arguments.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
TestCase * GetParent() const
Get the parent of this TestCsse.
std::string GetName(void) const
Test boolean Command Line processing.
CommandLineBooleanTestCase()
Constructor.
virtual ~CommandLineBooleanTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
Test int Command Line processing.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineIntTestCase()
Destructor.
CommandLineIntTestCase()
Constructor.
Test ignoring invalid arguments.
virtual ~CommandLineInvalidTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
CommandLineInvalidTestCase()
Constructor.
Test non-option arguments.
CommandLineNonOptionTestCase()
Constructor.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineNonOptionTestCase()
Destructor.
Test order of argument parsing.
CommandLineOrderTestCase()
Constructor.
virtual ~CommandLineOrderTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
Test string Command Line processing.
virtual ~CommandLineStringTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
CommandLineStringTestCase()
Constructor.
A test base class that drives Command Line parsing.
CommandLineTestCaseBase(std::string description)
Constructor.
void Parse(CommandLine &cmd, int n,...)
Exercise the CommandLine with the provided arguments.
static int m_count
Test iteration counter to give each test a unique name.
virtual ~CommandLineTestCaseBase()
Destructor.
The Test Suite that glues all of the Test Cases together.
CommandLineTestSuite()
Constructor.
Test uint8_t Command Line processing.
CommandLineUint8tTestCase()
Constructor.
virtual ~CommandLineUint8tTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
Test unsigned int Command Line processing.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
CommandLineUnsignedIntTestCase()
Constructor.
static CommandLineTestSuite g_commandLineTestSuite
CommandLineTestSuite instance variable.
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.