8#include "ns3/command-line.h"
10#include "ns3/global-value.h"
12#include "ns3/string.h"
13#include "ns3/system-path.h"
15#include "ns3/type-id.h"
85 char** argv =
new char*[argc + 1];
88 argv[0] =
new char[strlen(ss.str().c_str()) + 1];
89 strcpy(argv[0], ss.str().c_str());
93 for (
int i = 1; i < argc; ++i)
95 char* arg = va_arg(ap,
char*);
96 argv[i] =
new char[strlen(arg) + 1];
101 cmd.Parse(argc, argv);
104 for (
int i = 0; i < argc; ++i)
128 void DoRun()
override;
141 bool myDefaultFalseBool =
false;
143 cmd.AddValue(
"my-bool",
"help", myBool);
144 cmd.AddValue(
"my-false-bool",
"help", myDefaultFalseBool);
146 Parse(cmd, 1,
"--my-bool=0");
149 "CommandLine did not correctly set a boolean value to false, given 0");
151 Parse(cmd, 1,
"--my-bool=1");
154 "CommandLine did not correctly set a boolean value to true, given 1");
156 Parse(cmd, 1,
"--my-bool");
159 "CommandLine did not correctly toggle a default true boolean value to "
160 "false, given no argument");
162 Parse(cmd, 1,
"--my-false-bool");
165 "CommandLine did not correctly toggle a default false boolean value to "
166 "true, given no argument");
168 Parse(cmd, 1,
"--my-bool=t");
172 "CommandLine did not correctly set a boolean value to true, given 't' argument");
174 Parse(cmd, 1,
"--my-bool=true");
178 "CommandLine did not correctly set a boolean value to true, given \"true\" argument");
198 void DoRun()
override;
210 uint8_t myUint8 = 10;
212 cmd.AddValue(
"my-uint8",
"help", myUint8);
214 Parse(cmd, 1,
"--my-uint8=1");
217 "CommandLine did not correctly set a uint8_t value to 1, given 1");
237 void DoRun()
override;
252 cmd.AddValue(
"my-bool",
"help", myBool);
253 cmd.AddValue(
"my-int32",
"help", myInt32);
255 Parse(cmd, 2,
"--my-bool=0",
"--my-int32=-3");
258 "CommandLine did not correctly set a boolean value to false");
261 Parse(cmd, 2,
"--my-bool=1",
"--my-int32=+2");
264 "CommandLine did not correctly set a boolean value to true");
285 void DoRun()
override;
300 cmd.AddValue(
"my-bool",
"help", myBool);
301 cmd.AddValue(
"my-uint32",
"help", myUint32);
303 Parse(cmd, 2,
"--my-bool=0",
"--my-uint32=9");
307 "CommandLine did not correctly set a boolean value to false");
310 "CommandLine did not correctly set an unsigned integer value to 9");
330 void DoRun()
override;
343 std::string myStr =
"MyStr";
345 cmd.AddValue(
"my-uint32",
"help", myUint32);
346 cmd.AddValue(
"my-str",
"help", myStr);
348 Parse(cmd, 2,
"--my-uint32=9",
"--my-str=XX");
352 "CommandLine did not correctly set an unsigned integer value to 9");
355 "CommandLine did not correctly set a string value to \"XX\"");
375 void DoRun()
override;
389 cmd.AddValue(
"my-uint32",
"help", myUint32);
391 Parse(cmd, 2,
"--my-uint32=1",
"--my-uint32=2");
395 "CommandLine did not correctly set an unsigned integer value to 2");
415 void DoRun()
override;
429 cmd.AddValue(
"my-uint32",
"help", myUint32);
431 Parse(cmd, 2,
"quack",
"--my-uint32=5");
435 "CommandLine did not correctly set an unsigned integer value to 5");
455 void DoRun()
override;
469 std::string myStr =
"MyStr";
471 cmd.AddNonOption(
"my-bool",
"help", myBool);
472 cmd.AddNonOption(
"my-int",
"help", myInt);
473 cmd.AddNonOption(
"my-str",
"help", myStr);
475 Parse(cmd, 2,
"true",
"5");
480 "CommandLine did not correctly set an integer non-option value to 5");
483 Parse(cmd, 5,
"false",
"6",
"newValue",
"extraVal1",
"extraVal2");
488 "CommandLine did not correctly set an integer non-option value to 5");
493 "CommandLine did not parse the correct number of extra non-options.");
496 "CommandLine did not correctly get one extra non-option");
499 "CommandLine did not correctly get two extra non-option");
519 void DoRun()
override;
531 constexpr int CHARBUF_SIZE = 10;
532 char charbuf[CHARBUF_SIZE] =
"charstar";
535 cmd.AddValue(
"charbuf",
"a char* buffer", charbuf, CHARBUF_SIZE);
536 Parse(cmd, 1,
"--charbuf=deadbeef");
538 std::string value{charbuf};
Parse command-line arguments.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
TestCase * GetParent() const
Get the parent of this TestCase.
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
std::string GetName() const
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Test boolean Command Line processing.
CommandLineBooleanTestCase()
Constructor.
void DoRun() override
Run the test.
~CommandLineBooleanTestCase() override
Destructor.
Test char* buffer argument.
~CommandLineCharStarTestCase() override
Destructor.
void DoRun() override
Run the test.
CommandLineCharStarTestCase()
Constructor.
Test int Command Line processing.
~CommandLineIntTestCase() override
Destructor.
void DoRun() override
Run the test.
CommandLineIntTestCase()
Constructor.
Test ignoring invalid arguments.
void DoRun() override
Run the test.
~CommandLineInvalidTestCase() override
Destructor.
CommandLineInvalidTestCase()
Constructor.
Test non-option arguments.
CommandLineNonOptionTestCase()
Constructor.
void DoRun() override
Run the test.
~CommandLineNonOptionTestCase() override
Destructor.
Test order of argument parsing.
CommandLineOrderTestCase()
Constructor.
~CommandLineOrderTestCase() override
Destructor.
void DoRun() override
Run the test.
Test string Command Line processing.
void DoRun() override
Run the test.
~CommandLineStringTestCase() override
Destructor.
CommandLineStringTestCase()
Constructor.
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.
~CommandLineTestCaseBase() override
Destructor.
The Test Suite that glues all of the Test Cases together.
CommandLineTestSuite()
Constructor.
Test uint8_t Command Line processing.
CommandLineUint8tTestCase()
Constructor.
~CommandLineUint8tTestCase() override
Destructor.
void DoRun() override
Run the test.
Test unsigned int Command Line processing.
void DoRun() override
Run the test.
~CommandLineUnsignedIntTestCase() override
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.
Namespace for test files, TestCases and TestSuites.
Every class exported by the ns3 library is enclosed in the ns3 namespace.