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");
static int m_count
Test iteration counter to give each test a unique name.
Test non-option arguments.
virtual ~CommandLineIntTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineStringTestCase()
Destructor.
CommandLineUint8tTestCase()
Constructor.
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.
CommandLineNonOptionTestCase()
Constructor.
Test string Command Line processing.
void Parse(CommandLine &cmd, int n,...)
Exercise the CommandLine with the provided arguments.
Parse command-line arguments.
Test unsigned int Command Line processing.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineUint8tTestCase()
Destructor.
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.
virtual void DoRun(void)
Run the test.
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.
Test uint8_t Command Line processing.
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
virtual void DoRun(void)
Run the test.
std::string GetName(void) const
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.
Test ignoring invalid arguments.
virtual ~CommandLineTestCaseBase()
Destructor.
CommandLineStringTestCase()
Constructor.
virtual void DoRun(void)
Run the test.
virtual ~CommandLineBooleanTestCase()
Destructor.
virtual ~CommandLineNonOptionTestCase()
Destructor.