A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
command-line-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "ns3/command-line.h"
9#include "ns3/config.h"
10#include "ns3/global-value.h"
11#include "ns3/log.h"
12#include "ns3/string.h"
13#include "ns3/system-path.h"
14#include "ns3/test.h"
15#include "ns3/type-id.h"
16
17#include <cstdarg>
18#include <cstdlib>
19#include <sstream>
20
21/**
22 * \file
23 * \ingroup core-tests
24 * \ingroup commandline
25 * \ingroup commandline-tests
26 * CommandLine test suite.
27 */
28
29/**
30 * \ingroup core-tests
31 * \defgroup commandline-tests CommandLine test suite
32 */
33
34namespace ns3
35{
36
37namespace tests
38{
39
40/**
41 * \ingroup commandline-tests
42 * A test base class that drives Command Line parsing
43 */
45{
46 public:
47 /**
48 * Constructor
49 *
50 * \param description purpose of this TestCase
51 */
52 CommandLineTestCaseBase(std::string description);
53
54 /** Destructor */
56 {
57 }
58
59 /**
60 * Exercise the CommandLine with the provided arguments
61 *
62 * \param cmd the configured CommandLine
63 * \param n the number of arguments
64 */
65 void Parse(CommandLine& cmd, int n, ...);
66
67 /** Test iteration counter to give each test a unique name. */
68 static int m_count;
69};
70
72
74 : TestCase(description)
75{
76}
77
78void
80{
81 std::stringstream ss;
82 ss << GetParent()->GetName() << "-testcase-" << m_count << "-" << GetName();
83 ++m_count;
84
85 int argc = n + 1; // test name will go in argv[0], other n to follow
86 char** argv = new char*[argc + 1]; // extra entry for final null
87 argv[argc] = nullptr;
88
89 argv[0] = new char[strlen(ss.str().c_str()) + 1];
90 strcpy(argv[0], ss.str().c_str());
91
92 va_list ap;
93 va_start(ap, n);
94 for (int i = 1; i < argc; ++i)
95 {
96 char* arg = va_arg(ap, char*);
97 argv[i] = new char[strlen(arg) + 1];
98 strcpy(argv[i], arg);
99 }
100 va_end(ap);
101
102 cmd.Parse(argc, argv);
103
104 // Clean up all the new's
105 for (int i = 0; i < argc; ++i)
106 {
107 delete[] argv[i];
108 }
109 delete[] argv;
110}
111
112/**
113 * \ingroup commandline-tests
114 * Test boolean Command Line processing
115 */
117{
118 public:
119 /** Constructor */
121
122 /** Destructor */
124 {
125 }
126
127 private:
128 /** Run the test */
129 void DoRun() override;
130};
131
136
137void
139{
140 CommandLine cmd;
141 bool myBool = true;
142 bool myDefaultFalseBool = false;
143
144 cmd.AddValue("my-bool", "help", myBool);
145 cmd.AddValue("my-false-bool", "help", myDefaultFalseBool);
146
147 Parse(cmd, 1, "--my-bool=0");
149 false,
150 "CommandLine did not correctly set a boolean value to false, given 0");
151
152 Parse(cmd, 1, "--my-bool=1");
154 true,
155 "CommandLine did not correctly set a boolean value to true, given 1");
156
157 Parse(cmd, 1, "--my-bool");
159 false,
160 "CommandLine did not correctly toggle a default true boolean value to "
161 "false, given no argument");
162
163 Parse(cmd, 1, "--my-false-bool");
164 NS_TEST_ASSERT_MSG_EQ(myDefaultFalseBool,
165 true,
166 "CommandLine did not correctly toggle a default false boolean value to "
167 "true, given no argument");
168
169 Parse(cmd, 1, "--my-bool=t");
171 myBool,
172 true,
173 "CommandLine did not correctly set a boolean value to true, given 't' argument");
174
175 Parse(cmd, 1, "--my-bool=true");
177 myBool,
178 true,
179 "CommandLine did not correctly set a boolean value to true, given \"true\" argument");
180}
181
182/**
183 * \ingroup commandline-tests
184 * Test \c uint8_t Command Line processing
185 */
187{
188 public:
189 /** Constructor */
191
192 /** Destructor */
194 {
195 }
196
197 private:
198 /** Run the test */
199 void DoRun() override;
200};
201
206
207void
209{
210 CommandLine cmd;
211 uint8_t myUint8 = 10;
212
213 cmd.AddValue("my-uint8", "help", myUint8);
214
215 Parse(cmd, 1, "--my-uint8=1");
216 NS_TEST_ASSERT_MSG_EQ(myUint8,
217 1,
218 "CommandLine did not correctly set a uint8_t value to 1, given 1");
219}
220
221/**
222 * \ingroup commandline-tests
223 * Test int Command Line processing
224 */
226{
227 public:
228 /** Constructor */
230
231 /** Destructor */
233 {
234 }
235
236 private:
237 /** Run the test */
238 void DoRun() override;
239};
240
245
246void
248{
249 CommandLine cmd;
250 bool myBool = true;
251 int32_t myInt32 = 10;
252
253 cmd.AddValue("my-bool", "help", myBool);
254 cmd.AddValue("my-int32", "help", myInt32);
255
256 Parse(cmd, 2, "--my-bool=0", "--my-int32=-3");
258 false,
259 "CommandLine did not correctly set a boolean value to false");
260 NS_TEST_ASSERT_MSG_EQ(myInt32, -3, "CommandLine did not correctly set an integer value to -3");
261
262 Parse(cmd, 2, "--my-bool=1", "--my-int32=+2");
264 true,
265 "CommandLine did not correctly set a boolean value to true");
266 NS_TEST_ASSERT_MSG_EQ(myInt32, +2, "CommandLine did not correctly set an integer value to +2");
267}
268
269/**
270 * \ingroup commandline-tests
271 * Test unsigned int Command Line processing
272 */
274{
275 public:
276 /** Constructor */
278
279 /** Destructor */
281 {
282 }
283
284 private:
285 /** Run the test */
286 void DoRun() override;
287};
288
293
294void
296{
297 CommandLine cmd;
298 bool myBool = true;
299 uint32_t myUint32 = 10;
300
301 cmd.AddValue("my-bool", "help", myBool);
302 cmd.AddValue("my-uint32", "help", myUint32);
303
304 Parse(cmd, 2, "--my-bool=0", "--my-uint32=9");
305
307 false,
308 "CommandLine did not correctly set a boolean value to false");
309 NS_TEST_ASSERT_MSG_EQ(myUint32,
310 9,
311 "CommandLine did not correctly set an unsigned integer value to 9");
312}
313
314/**
315 * \ingroup commandline-tests
316 * Test string Command Line processing
317 */
319{
320 public:
321 /** Constructor */
323
324 /** Destructor */
326 {
327 }
328
329 private:
330 /** Run the test */
331 void DoRun() override;
332};
333
338
339void
341{
342 CommandLine cmd;
343 uint32_t myUint32 = 10;
344 std::string myStr = "MyStr";
345
346 cmd.AddValue("my-uint32", "help", myUint32);
347 cmd.AddValue("my-str", "help", myStr);
348
349 Parse(cmd, 2, "--my-uint32=9", "--my-str=XX");
350
351 NS_TEST_ASSERT_MSG_EQ(myUint32,
352 9,
353 "CommandLine did not correctly set an unsigned integer value to 9");
355 "XX",
356 "CommandLine did not correctly set a string value to \"XX\"");
357}
358
359/**
360 * \ingroup commandline-tests
361 * Test order of argument parsing
362 */
364{
365 public:
366 /** Constructor */
368
369 /** Destructor */
371 {
372 }
373
374 private:
375 /** Run the test */
376 void DoRun() override;
377};
378
383
384void
386{
387 CommandLine cmd;
388 uint32_t myUint32 = 0;
389
390 cmd.AddValue("my-uint32", "help", myUint32);
391
392 Parse(cmd, 2, "--my-uint32=1", "--my-uint32=2");
393
394 NS_TEST_ASSERT_MSG_EQ(myUint32,
395 2,
396 "CommandLine did not correctly set an unsigned integer value to 2");
397}
398
399/**
400 * \ingroup commandline-tests
401 * Test ignoring invalid arguments
402 */
404{
405 public:
406 /** Constructor */
408
409 /** Destructor */
411 {
412 }
413
414 private:
415 /** Run the test */
416 void DoRun() override;
417};
418
423
424void
426{
427 CommandLine cmd;
428 uint32_t myUint32 = 0;
429
430 cmd.AddValue("my-uint32", "help", myUint32);
431
432 Parse(cmd, 2, "quack", "--my-uint32=5");
433
434 NS_TEST_ASSERT_MSG_EQ(myUint32,
435 5,
436 "CommandLine did not correctly set an unsigned integer value to 5");
437}
438
439/**
440 * \ingroup commandline-tests
441 * Test non-option arguments
442 */
444{
445 public:
446 /** Constructor */
448
449 /** Destructor */
451 {
452 }
453
454 private:
455 /** Run the test */
456 void DoRun() override;
457};
458
463
464void
466{
467 CommandLine cmd;
468 bool myBool = false;
469 int32_t myInt = 1;
470 std::string myStr = "MyStr";
471
472 cmd.AddNonOption("my-bool", "help", myBool);
473 cmd.AddNonOption("my-int", "help", myInt);
474 cmd.AddNonOption("my-str", "help", myStr);
475
476 Parse(cmd, 2, "true", "5");
477
478 NS_TEST_ASSERT_MSG_EQ(myBool, true, "CommandLine did not correctly set a boolean non-option");
480 5,
481 "CommandLine did not correctly set an integer non-option value to 5");
482 NS_TEST_ASSERT_MSG_EQ(myStr, "MyStr", "CommandLine did not leave a non-option unmodified.");
483
484 Parse(cmd, 5, "false", "6", "newValue", "extraVal1", "extraVal2");
485
486 NS_TEST_ASSERT_MSG_EQ(myBool, false, "CommandLine did not correctly set a boolean non-option");
488 6,
489 "CommandLine did not correctly set an integer non-option value to 5");
490 NS_TEST_ASSERT_MSG_EQ(myStr, "newValue", "CommandLine did not leave a non-option unmodified.");
491
492 NS_TEST_ASSERT_MSG_EQ(cmd.GetNExtraNonOptions(),
493 2,
494 "CommandLine did not parse the correct number of extra non-options.");
495 NS_TEST_ASSERT_MSG_EQ(cmd.GetExtraNonOption(0),
496 "extraVal1",
497 "CommandLine did not correctly get one extra non-option");
498 NS_TEST_ASSERT_MSG_EQ(cmd.GetExtraNonOption(1),
499 "extraVal2",
500 "CommandLine did not correctly get two extra non-option");
501}
502
503/**
504 * \ingroup commandline-tests
505 * Test \c char* buffer argument
506 */
508{
509 public:
510 /** Constructor */
512
513 /** Destructor */
515 {
516 }
517
518 private:
519 /** Run the test */
520 void DoRun() override;
521};
522
527
528void
530{
531 // char* buffer option
532 constexpr int CHARBUF_SIZE = 10;
533 char charbuf[CHARBUF_SIZE] = "charstar";
534
535 CommandLine cmd;
536 cmd.AddValue("charbuf", "a char* buffer", charbuf, CHARBUF_SIZE);
537 Parse(cmd, 1, "--charbuf=deadbeef");
538
539 std::string value{charbuf};
540
541 NS_TEST_ASSERT_MSG_EQ(value, "deadbeef", "CommandLine did not correctly set a char* buffer");
542}
543
544/**
545 * \ingroup commandline-tests
546 * The Test Suite that glues all of the Test Cases together.
547 */
549{
550 public:
551 /** Constructor */
553};
554
568
569/**
570 * \ingroup commandline-tests
571 * CommandLineTestSuite instance variable.
572 */
574
575} // namespace tests
576
577} // namespace ns3
Parse command-line arguments.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
TestCase * GetParent() const
Get the parent of this TestCase.
Definition test.cc:374
std::string GetName() const
Definition test.cc:367
A suite of tests to run.
Definition test.h:1267
Test boolean Command Line processing.
Test int Command Line processing.
Test string Command Line processing.
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.
The Test Suite that glues all of the Test Cases together.
Test uint8_t Command Line processing.
Test unsigned int Command Line processing.
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.
Definition test.h:134
Every class exported by the ns3 library is enclosed in the ns3 namespace.