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, "Command parser did not correctly set a boolean value to false");
151 
152  Parse (cmd, 1, "--my-bool=1");
153  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given integer argument");
154 
155  Parse (cmd, 1, "--my-bool");
156  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser 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, "Command parser 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, "Command parser 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, "Command parser 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, "Command parser did not correctly set a boolean value to false");
200  NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "Command parser 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, "Command parser did not correctly set a boolean value to true");
204  NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "Command parser 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, "Command parser did not correctly set a boolean value to true");
240  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser 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, "Command parser did not correctly set an unsigned integer value to 9");
276  NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "Command parser did not correctly set an 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, "Command parser 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, "Command parser did not correctly set an unsigned integer value to 5");
343 }
344 
350 {
351 public:
353 };
354 
356  : TestSuite ("command-line")
357 {
364 }
365 
371 
372 
373  } // namespace tests
374 
375 } // namespace ns3
376 
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
Test boolean Command Line processing.
encapsulates test code
Definition: test.h:1155
tuple cmd
Definition: second.py:35
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,...)
Excercise the CommandLine with the provided arguments.
Parse command-line arguments.
Definition: command-line.h:205
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.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static CommandLineTestSuite g_commandLineTestSuite
CommandLineTestSuite instance variable.
CommandLineTestCaseBase(std::string description)
Constructor.
The Test Suite that glues all of the Test Cases together.
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string GetName(void) const
Definition: test.cc:370
virtual void DoRun(void)
Run the test.