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 
72  void Parse (CommandLine &cmd, int n, ...);
73 
75  static int m_count;
76 };
77 
79 
81  : TestCase (description)
82 {
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:
129 private:
130  virtual void DoRun (void);
132 };
133 
135  : CommandLineTestCaseBase ("boolean")
136 {
137 }
138 
139 void
141 {
143  bool myBool = true;
144  bool myDefaultFalseBool = false;
145 
146  cmd.AddValue ("my-bool", "help", myBool);
147  cmd.AddValue ("my-false-bool", "help", myDefaultFalseBool);
148 
149  Parse (cmd, 1, "--my-bool=0");
150  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean value to false, given 0");
151 
152  Parse (cmd, 1, "--my-bool=1");
153  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true, given 1");
154 
155  Parse (cmd, 1, "--my-bool");
156  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly toggle a default true boolean value to false, given no argument");
157 
158  Parse (cmd, 1, "--my-false-bool");
159  NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool, true, "CommandLine did not correctly toggle a default false boolean value to true, given no argument");
160 
161  Parse (cmd, 1, "--my-bool=t");
162  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true, given 't' argument");
163 
164  Parse (cmd, 1, "--my-bool=true");
165  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true, given \"true\" argument");
166 }
167 
173 {
174 public:
176  virtual ~CommandLineIntTestCase () {}
178 private:
179  virtual void DoRun (void);
181 };
182 
184  : CommandLineTestCaseBase ("int")
185 {
186 }
187 
188 void
190 {
192  bool myBool = true;
193  int32_t myInt32 = 10;
194 
195  cmd.AddValue ("my-bool", "help", myBool);
196  cmd.AddValue ("my-int32", "help", myInt32);
197 
198  Parse (cmd, 2, "--my-bool=0", "--my-int32=-3");
199  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean value to false");
200  NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "CommandLine did not correctly set an integer value to -3");
201 
202  Parse (cmd, 2, "--my-bool=1", "--my-int32=+2");
203  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean value to true");
204  NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "CommandLine did not correctly set an integer value to +2");
205 }
206 
212 {
213 public:
217 private:
218  virtual void DoRun (void);
220 };
221 
223  : CommandLineTestCaseBase ("unsigned-int")
224 {
225 }
226 
227 void
229 {
231  bool myBool = true;
232  uint32_t myUint32 = 10;
233 
234  cmd.AddValue ("my-bool", "help", myBool);
235  cmd.AddValue ("my-uint32", "help", myUint32);
236 
237  Parse (cmd, 2, "--my-bool=0", "--my-uint32=9");
238 
239  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean value to false");
240  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "CommandLine did not correctly set an unsigned integer value to 9");
241 }
242 
248 {
249 public:
253 private:
254  virtual void DoRun (void);
256 };
257 
259  : CommandLineTestCaseBase ("string")
260 {
261 }
262 
263 void
265 {
267  uint32_t myUint32 = 10;
268  std::string myStr = "MyStr";
269 
270  cmd.AddValue ("my-uint32", "help", myUint32);
271  cmd.AddValue ("my-str", "help", myStr);
272 
273  Parse (cmd, 2, "--my-uint32=9", "--my-str=XX");
274 
275  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "CommandLine did not correctly set an unsigned integer value to 9");
276  NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "CommandLine did not correctly set a string value to \"XX\"");
277 }
278 
284 {
285 public:
289 private:
290  virtual void DoRun (void);
292 };
293 
295  : CommandLineTestCaseBase ("order")
296 {
297 }
298 
299 void
301 {
303  uint32_t myUint32 = 0;
304 
305  cmd.AddValue ("my-uint32", "help", myUint32);
306 
307  Parse (cmd, 2, "--my-uint32=1", "--my-uint32=2");
308 
309  NS_TEST_ASSERT_MSG_EQ (myUint32, 2, "CommandLine did not correctly set an unsigned integer value to 2");
310 }
311 
317 {
318 public:
322 private:
323  virtual void DoRun (void);
325 };
326 
328  : CommandLineTestCaseBase ("invalid")
329 {
330 }
331 
332 void
334 {
336  uint32_t myUint32 = 0;
337 
338  cmd.AddValue ("my-uint32", "help", myUint32);
339 
340  Parse (cmd, 2, "quack", "--my-uint32=5");
341 
342  NS_TEST_ASSERT_MSG_EQ (myUint32, 5, "CommandLine did not correctly set an unsigned integer value to 5");
343 }
344 
350 {
351 public:
355 private:
356  virtual void DoRun (void);
358 };
359 
361  : CommandLineTestCaseBase ("nonoption")
362 {
363 }
364 
365 void
367 {
369  bool myBool = false;
370  int32_t myInt = 1;
371  std::string myStr = "MyStr";
372 
373  cmd.AddNonOption ("my-bool", "help", myBool);
374  cmd.AddNonOption ("my-int", "help", myInt);
375  cmd.AddNonOption ("my-str", "help", myStr);
376 
377  Parse (cmd, 2, "true", "5");
378 
379  NS_TEST_ASSERT_MSG_EQ (myBool, true, "CommandLine did not correctly set a boolean non-option");
380  NS_TEST_ASSERT_MSG_EQ (myInt, 5, "CommandLine did not correctly set an integer non-option value to 5");
381  NS_TEST_ASSERT_MSG_EQ (myStr, "MyStr", "CommandLine did not leave a non-option unmodified.");
382 
383  Parse (cmd, 5, "false", "6", "newValue", "extraVal1", "extraVal2");
384 
385  NS_TEST_ASSERT_MSG_EQ (myBool, false, "CommandLine did not correctly set a boolean non-option");
386  NS_TEST_ASSERT_MSG_EQ (myInt
387 , 6, "CommandLine did not correctly set an integer non-option value to 5");
388  NS_TEST_ASSERT_MSG_EQ (myStr, "newValue", "CommandLine did not leave a non-option unmodified.");
389 
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");
393 }
394 
400 {
401 public:
403 };
404 
406  : TestSuite ("command-line")
407 {
415 }
416 
422 
423 
424  } // namespace tests
425 
426 } // namespace ns3
427 
static int m_count
Test iteration counter to give each test a unique name.
virtual void DoRun(void)
Run the test.
A suite of tests to run.
Definition: test.h:1342
cmd
Definition: second.py:35
Test boolean Command Line processing.
encapsulates test code
Definition: test.h:1155
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#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:168
virtual void DoRun(void)
Run the test.
Test string Command Line processing.
void Parse(CommandLine &cmd, int n,...)
Exercise the CommandLine with the provided arguments.
Parse command-line arguments.
Definition: command-line.h:213
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.
TestCase * GetParent() const
Get the parent of this TestCsse.
Definition: test.cc:376
Test int Command Line processing.
A test base class that drives Command Line parsing.
std::string GetName(void) const
Definition: test.cc:370
static CommandLineTestSuite g_commandLineTestSuite
CommandLineTestSuite instance variable.
CommandLineTestCaseBase(std::string description)
Constructor.
The Test Suite that glues all of the Test Cases together.
virtual void DoRun(void)
Run the test.