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);
150 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"CommandLine did not correctly set a boolean value to false, given 0");
153 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"CommandLine did not correctly set a boolean value to true, given 1");
156 NS_TEST_ASSERT_MSG_EQ (myBool,
false,
"CommandLine did not correctly toggle a default true boolean value to false, given no argument");
159 NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool,
true,
"CommandLine did not correctly toggle a default false boolean value to true, given no argument");
162 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"CommandLine did not correctly set a boolean value to true, given 't' argument");
165 NS_TEST_ASSERT_MSG_EQ (myBool,
true,
"CommandLine 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");
202 Parse (
cmd, 2,
"--my-bool=1",
"--my-int32=+2");
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");
240 NS_TEST_ASSERT_MSG_EQ (myUint32, 9,
"CommandLine 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,
"CommandLine did not correctly set an unsigned integer value to 9");
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,
"CommandLine 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,
"CommandLine did not correctly set an unsigned integer value to 5");
356 virtual void DoRun (
void);
371 std::string myStr =
"MyStr";
373 cmd.AddNonOption (
"my-bool",
"help", myBool);
374 cmd.AddNonOption (
"my-int",
"help", myInt);
375 cmd.AddNonOption (
"my-str",
"help", myStr);
380 NS_TEST_ASSERT_MSG_EQ (myInt, 5,
"CommandLine did not correctly set an integer non-option value to 5");
383 Parse (
cmd, 5,
"false",
"6",
"newValue",
"extraVal1",
"extraVal2");
387 , 6,
"CommandLine did not correctly set an integer non-option value to 5");
390 NS_TEST_ASSERT_MSG_EQ (
cmd.GetNExtraNonOptions (), 2,
"CommandLine did not parse the correct number of extra non-options.");
391 NS_TEST_ASSERT_MSG_EQ (
cmd.GetExtraNonOption (0),
"extraVal1",
"CommandLine did not correctly get one extra non-option");
392 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.
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.
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.
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
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.