A Discrete-Event Network Simulator
API
command-line-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/command-line.h"
21 #include "ns3/log.h"
22 #include "ns3/config.h"
23 #include "ns3/global-value.h"
24 #include "ns3/system-path.h"
25 #include "ns3/type-id.h"
26 #include "ns3/test.h"
27 #include "ns3/string.h"
28 #include <cstdlib>
29 #include <cstdarg>
30 #include <sstream>
31 
45 namespace ns3 {
46 
47 namespace tests {
48 
49 
55 {
56 public:
62  CommandLineTestCaseBase (std::string description);
65  {}
66 
73  void Parse (CommandLine &cmd, int n, ...);
74 
76  static int m_count;
77 };
78 
80 
82  : TestCase (description)
83 {}
84 
85 void
87 {
88  std::stringstream ss;
89  ss << GetParent ()->GetName () << "-testcase-" << m_count << "-" << GetName ();
90  ++m_count;
91 
92  int argc = n + 1; // test name will go in argv[0], other n to follow
93  char ** argv = new char* [argc + 1]; // extra entry for final null
94  argv[argc] = 0;
95 
96  argv[0] = new char [strlen (ss.str ().c_str ()) + 1];
97  strcpy (argv[0], ss.str ().c_str ());
98 
99  va_list ap;
100  va_start (ap, n);
101  for (int i = 1; i < argc; ++i)
102  {
103  char *arg = va_arg (ap, char *);
104  argv[i] = new char [strlen (arg) + 1];
105  strcpy (argv[i], arg);
106  }
107  va_end (ap);
108 
109  cmd.Parse (argc, argv);
110 
111  // Clean up all the new's
112  for (int i = 0; i < argc; ++i)
113  {
114  delete [] argv[i];
115  }
116  delete [] argv;
117 }
118 
124 {
125 public:
130  {}
131 
132 private:
134  virtual void DoRun (void);
135 
136 };
137 
139  : CommandLineTestCaseBase ("boolean")
140 {}
141 
142 void
144 {
146  bool myBool = true;
147  bool myDefaultFalseBool = false;
148 
149  cmd.AddValue ("my-bool", "help", myBool);
150  cmd.AddValue ("my-false-bool", "help", myDefaultFalseBool);
151 
152  Parse (cmd, 1, "--my-bool=0");
153  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean value to false, given 0");
154 
155  Parse (cmd, 1, "--my-bool=1");
156  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true, given 1");
157 
158  Parse (cmd, 1, "--my-bool");
159  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly toggle a default true boolean value to false, given no argument");
160 
161  Parse (cmd, 1, "--my-false-bool");
162  NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool, true, "CommandLine did not correctly toggle a default false boolean value to true, given no argument");
163 
164  Parse (cmd, 1, "--my-bool=t");
165  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true, given 't' argument");
166 
167  Parse (cmd, 1, "--my-bool=true");
168  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true, given \"true\" argument");
169 }
170 
176 {
177 public:
182  {}
183 
184 private:
186  virtual void DoRun (void);
187 
188 };
189 
191  : CommandLineTestCaseBase ("uint8_t")
192 {}
193 
194 void
196 {
198  uint8_t myUint8 = 10;
199 
200  cmd.AddValue ("my-uint8", "help", myUint8);
201 
202  Parse (cmd, 1, "--my-uint8=1");
203  NS_TEST_ASSERT_MSG_EQ (myUint8, 1, "CommandLine did not correctly set a uint8_t value to 1, given 1");
204 }
205 
211 {
212 public:
217  {}
218 
219 private:
221  virtual void DoRun (void);
222 
223 };
224 
226  : CommandLineTestCaseBase ("int")
227 {}
228 
229 void
231 {
233  bool myBool = true;
234  int32_t myInt32 = 10;
235 
236  cmd.AddValue ("my-bool", "help", myBool);
237  cmd.AddValue ("my-int32", "help", myInt32);
238 
239  Parse (cmd, 2, "--my-bool=0", "--my-int32=-3");
240  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean value to false");
241  NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "CommandLine did not correctly set an integer value to -3");
242 
243  Parse (cmd, 2, "--my-bool=1", "--my-int32=+2");
244  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true");
245  NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "CommandLine did not correctly set an integer value to +2");
246 }
247 
253 {
254 public:
259  {}
260 
261 private:
263  virtual void DoRun (void);
264 
265 };
266 
268  : CommandLineTestCaseBase ("unsigned-int")
269 {}
270 
271 void
273 {
275  bool myBool = true;
276  uint32_t myUint32 = 10;
277 
278  cmd.AddValue ("my-bool", "help", myBool);
279  cmd.AddValue ("my-uint32", "help", myUint32);
280 
281  Parse (cmd, 2, "--my-bool=0", "--my-uint32=9");
282 
283  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean value to false");
284  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "CommandLine did not correctly set an unsigned integer value to 9");
285 }
286 
292 {
293 public:
298  {}
299 
300 private:
302  virtual void DoRun (void);
303 
304 };
305 
307  : CommandLineTestCaseBase ("string")
308 {}
309 
310 void
312 {
314  uint32_t myUint32 = 10;
315  std::string myStr = "MyStr";
316 
317  cmd.AddValue ("my-uint32", "help", myUint32);
318  cmd.AddValue ("my-str", "help", myStr);
319 
320  Parse (cmd, 2, "--my-uint32=9", "--my-str=XX");
321 
322  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "CommandLine did not correctly set an unsigned integer value to 9");
323  NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "CommandLine did not correctly set a string value to \"XX\"");
324 }
325 
331 {
332 public:
337  {}
338 
339 private:
341  virtual void DoRun (void);
342 
343 };
344 
346  : CommandLineTestCaseBase ("order")
347 {}
348 
349 void
351 {
353  uint32_t myUint32 = 0;
354 
355  cmd.AddValue ("my-uint32", "help", myUint32);
356 
357  Parse (cmd, 2, "--my-uint32=1", "--my-uint32=2");
358 
359  NS_TEST_ASSERT_MSG_EQ (myUint32, 2, "CommandLine did not correctly set an unsigned integer value to 2");
360 }
361 
367 {
368 public:
373  {}
374 
375 private:
377  virtual void DoRun (void);
378 
379 };
380 
382  : CommandLineTestCaseBase ("invalid")
383 {}
384 
385 void
387 {
389  uint32_t myUint32 = 0;
390 
391  cmd.AddValue ("my-uint32", "help", myUint32);
392 
393  Parse (cmd, 2, "quack", "--my-uint32=5");
394 
395  NS_TEST_ASSERT_MSG_EQ (myUint32, 5, "CommandLine did not correctly set an unsigned integer value to 5");
396 }
397 
403 {
404 public:
409  {}
410 
411 private:
413  virtual void DoRun (void);
414 
415 };
416 
418  : CommandLineTestCaseBase ("nonoption")
419 {}
420 
421 void
423 {
425  bool myBool = false;
426  int32_t myInt = 1;
427  std::string myStr = "MyStr";
428 
429  cmd.AddNonOption ("my-bool", "help", myBool);
430  cmd.AddNonOption ("my-int", "help", myInt);
431  cmd.AddNonOption ("my-str", "help", myStr);
432 
433  Parse (cmd, 2, "true", "5");
434 
435  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean non-option");
436  NS_TEST_ASSERT_MSG_EQ (myInt, 5, "CommandLine did not correctly set an integer non-option value to 5");
437  NS_TEST_ASSERT_MSG_EQ (myStr, "MyStr", "CommandLine did not leave a non-option unmodified.");
438 
439  Parse (cmd, 5, "false", "6", "newValue", "extraVal1", "extraVal2");
440 
441  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean non-option");
442  NS_TEST_ASSERT_MSG_EQ (myInt
443  , 6, "CommandLine did not correctly set an integer non-option value to 5");
444  NS_TEST_ASSERT_MSG_EQ (myStr, "newValue", "CommandLine did not leave a non-option unmodified.");
445 
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");
449 }
450 
456 {
457 public:
460 };
461 
463  : TestSuite ("command-line")
464 {
473 }
474 
480 
481 
482 } // namespace tests
483 
484 } // namespace ns3
485 
Parse command-line arguments.
Definition: command-line.h:229
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
TestCase * GetParent() const
Get the parent of this TestCsse.
Definition: test.cc:376
std::string GetName(void) const
Definition: test.cc:370
A suite of tests to run.
Definition: test.h:1188
Test boolean Command Line processing.
Test int Command Line processing.
virtual void DoRun(void)
Run the test.
virtual void DoRun(void)
Run the test.
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:141
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35