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 
32 using namespace ns3;
33 
34 /*************************************************************************/
38 {
39 public:
45  CommandLineTestCaseBase (std::string description);
48 
55  void Parse (CommandLine &cmd, int n, ...);
56 
58  static int m_count;
59 };
60 
62 
64  : TestCase (description)
65 {
66 }
67 
68 void
70 {
71  std::stringstream ss;
72  ss << GetParent ()->GetName () << "-testcase-" << m_count << "-" << GetName ();
73  ++m_count;
74 
75  int argc = n + 1; // test name will go in argv[0], other n to follow
76  char ** argv = new char* [argc + 1]; // extra entry for final null
77  argv[argc] = 0;
78 
79  argv[0] = new char [strlen (ss.str ().c_str ()) + 1];
80  strcpy (argv[0], ss.str ().c_str ());
81 
82  va_list ap;
83  va_start (ap, n);
84  for (int i = 1; i < argc; ++i)
85  {
86  char *arg = va_arg (ap, char *);
87  argv[i] = new char [strlen (arg) + 1];
88  strcpy (argv[i], arg);
89  }
90  va_end (ap);
91 
92  cmd.Parse (argc, argv);
93 
94  // Clean up all the new's
95  for (int i = 0; i < argc; ++i)
96  {
97  delete [] argv[i];
98  }
99  delete [] argv;
100 }
101 
102 /*************************************************************************/
106 {
107 public:
111 private:
112  virtual void DoRun (void);
114 };
115 
117  : CommandLineTestCaseBase ("boolean")
118 {
119 }
120 
121 void
123 {
125  bool myBool = true;
126  bool myDefaultFalseBool = false;
127 
128  cmd.AddValue ("my-bool", "help", myBool);
129  cmd.AddValue ("my-false-bool", "help", myDefaultFalseBool);
130 
131  Parse (cmd, 1, "--my-bool=0");
132  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false");
133 
134  Parse (cmd, 1, "--my-bool=1");
135  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given integer argument");
136 
137  Parse (cmd, 1, "--my-bool");
138  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly toggle a default true boolean value to false, given no argument");
139 
140  Parse (cmd, 1, "--my-false-bool");
141  NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool, true, "Command parser did not correctly toggle a default false boolean value to true, given no argument");
142 
143  Parse (cmd, 1, "--my-bool=t");
144  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given 't' argument");
145 
146  Parse (cmd, 1, "--my-bool=true");
147  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given \"true\" argument");
148 }
149 
150 /*************************************************************************/
154 {
155 public:
157  virtual ~CommandLineIntTestCase () {}
159 private:
160  virtual void DoRun (void);
162 };
163 
165  : CommandLineTestCaseBase ("int")
166 {
167 }
168 
169 void
171 {
173  bool myBool = true;
174  int32_t myInt32 = 10;
175 
176  cmd.AddValue ("my-bool", "help", myBool);
177  cmd.AddValue ("my-int32", "help", myInt32);
178 
179  Parse (cmd, 2, "--my-bool=0", "--my-int32=-3");
180  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false");
181  NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "Command parser did not correctly set an integer value to -3");
182 
183  Parse (cmd, 2, "--my-bool=1", "--my-int32=+2");
184  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true");
185  NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "Command parser did not correctly set an integer value to +2");
186 }
187 
188 /*************************************************************************/
192 {
193 public:
197 private:
198  virtual void DoRun (void);
200 };
201 
203  : CommandLineTestCaseBase ("unsigned-int")
204 {
205 }
206 
207 void
209 {
211  bool myBool = true;
212  uint32_t myUint32 = 10;
213 
214  cmd.AddValue ("my-bool", "help", myBool);
215  cmd.AddValue ("my-uint32", "help", myUint32);
216 
217  Parse (cmd, 2, "--my-bool=0", "--my-uint32=9");
218 
219  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to true");
220  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9");
221 }
222 
223 /*************************************************************************/
227 {
228 public:
232 private:
233  virtual void DoRun (void);
235 };
236 
238  : CommandLineTestCaseBase ("string")
239 {
240 }
241 
242 void
244 {
246  uint32_t myUint32 = 10;
247  std::string myStr = "MyStr";
248 
249  cmd.AddValue ("my-uint32", "help", myUint32);
250  cmd.AddValue ("my-str", "help", myStr);
251 
252  Parse (cmd, 2, "--my-uint32=9", "--my-str=XX");
253 
254  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9");
255  NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "Command parser did not correctly set an string value to \"XX\"");
256 }
257 
258 /*************************************************************************/
262 {
263 public:
265 };
266 
268  : TestSuite ("command-line", UNIT)
269 {
270  AddTestCase (new CommandLineBooleanTestCase, TestCase::QUICK);
271  AddTestCase (new CommandLineIntTestCase, TestCase::QUICK);
272  AddTestCase (new CommandLineUnsignedIntTestCase, TestCase::QUICK);
273  AddTestCase (new CommandLineStringTestCase, TestCase::QUICK);
274 }
275 
The Test Suite that glues all of the Test Cases together.
static CommandLineTestSuite CommandLineTestSuite
Test instance.
A suite of tests to run.
Definition: test.h:1333
virtual ~CommandLineIntTestCase()
Destructor.
encapsulates test code
Definition: test.h:1147
virtual void DoRun(void)
Run the test.
tuple cmd
Definition: second.py:35
virtual ~CommandLineStringTestCase()
Destructor.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
A test base class that drives Command Line parsing.
#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:161
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
Parse command-line arguments.
Definition: command-line.h:205
virtual void DoRun(void)
Run the test.
virtual void DoRun(void)
Run the test.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Test unsigned int Command Line processing.
TestCase * GetParent() const
Get the parent of this TestCsse.
Definition: test.cc:375
CommandLineTestCaseBase(std::string description)
Constructor.
Test boolean Command Line processing.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
virtual void DoRun(void)
Run the test.
void Parse(CommandLine &cmd, int n,...)
Excercise the CommandLine with the provided arguments.
Test int Command Line processing.
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string GetName(void) const
Definition: test.cc:369
virtual ~CommandLineTestCaseBase()
Destructor.
static int m_count
Test iteration counter to give each test a unique name.
Test string Command Line processing.
virtual ~CommandLineBooleanTestCase()
Destructor.