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/type-id.h"
25 #include "ns3/test.h"
26 #include "ns3/string.h"
27 #include <cstdlib>
28 #include <cstdarg>
29 
30 using namespace ns3;
31 
32 /*************************************************************************/
36 {
37 public:
43  CommandLineTestCaseBase (std::string description);
46 
53  void Parse (CommandLine &cmd, int n, ...);
54 };
55 
57  : TestCase (description)
58 {
59 }
60 
61 void
63 {
64  char **args = new char* [n+1];
65  args[0] = (char *) "Test";
66  va_list ap;
67  va_start (ap, n);
68  int i = 0;
69  while (i < n)
70  {
71  char *arg = va_arg (ap, char *);
72  args[i+1] = arg;
73  i++;
74  }
75  va_end (ap);
76  int argc = n + 1;
77  cmd.Parse (argc, args);
78  delete [] args;
79 }
80 
81 /*************************************************************************/
85 {
86 public:
90 private:
91  virtual void DoRun (void);
93 };
94 
96  : CommandLineTestCaseBase ("Check boolean arguments")
97 {
98 }
99 
100 void
102 {
104  bool myBool = true;
105  bool myDefaultFalseBool = false;
106 
107  cmd.AddValue ("my-bool", "help", myBool);
108  cmd.AddValue ("my-false-bool", "help", myDefaultFalseBool);
109 
110  Parse (cmd, 1, "--my-bool=0");
111  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false");
112 
113  Parse (cmd, 1, "--my-bool=1");
114  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given integer argument");
115 
116  Parse (cmd, 1, "--my-bool");
117  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly toggle a default true boolean value to false, given no argument");
118 
119  Parse (cmd, 1, "--my-false-bool");
120  NS_TEST_ASSERT_MSG_EQ (myDefaultFalseBool, true, "Command parser did not correctly toggle a default false boolean value to true, given no argument");
121 
122  Parse (cmd, 1, "--my-bool=t");
123  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given 't' argument");
124 
125  Parse (cmd, 1, "--my-bool=true");
126  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true, given \"true\" argument");
127 }
128 
129 /*************************************************************************/
133 {
134 public:
136  virtual ~CommandLineIntTestCase () {}
138 private:
139  virtual void DoRun (void);
141 };
142 
144  : CommandLineTestCaseBase ("Check int arguments")
145 {
146 }
147 
148 void
150 {
152  bool myBool = true;
153  int32_t myInt32 = 10;
154 
155  cmd.AddValue ("my-bool", "help", myBool);
156  cmd.AddValue ("my-int32", "help", myInt32);
157 
158  Parse (cmd, 2, "--my-bool=0", "--my-int32=-3");
159  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false");
160  NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "Command parser did not correctly set an integer value to -3");
161 
162  Parse (cmd, 2, "--my-bool=1", "--my-int32=+2");
163  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true");
164  NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "Command parser did not correctly set an integer value to +2");
165 }
166 
167 /*************************************************************************/
171 {
172 public:
176 private:
177  virtual void DoRun (void);
179 };
180 
182  : CommandLineTestCaseBase ("Check unsigned int arguments")
183 {
184 }
185 
186 void
188 {
190  bool myBool = true;
191  uint32_t myUint32 = 10;
192 
193  cmd.AddValue ("my-bool", "help", myBool);
194  cmd.AddValue ("my-uint32", "help", myUint32);
195 
196  Parse (cmd, 2, "--my-bool=0", "--my-uint32=9");
197 
198  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to true");
199  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9");
200 }
201 
202 /*************************************************************************/
206 {
207 public:
211 private:
212  virtual void DoRun (void);
214 };
215 
217  : CommandLineTestCaseBase ("Check unsigned int arguments")
218 {
219 }
220 
221 void
223 {
225  uint32_t myUint32 = 10;
226  std::string myStr = "MyStr";
227 
228  cmd.AddValue ("my-uint32", "help", myUint32);
229  cmd.AddValue ("my-str", "help", myStr);
230 
231  Parse (cmd, 2, "--my-uint32=9", "--my-str=XX");
232 
233  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9");
234  NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "Command parser did not correctly set an string value to \"XX\"");
235 }
236 
237 /*************************************************************************/
241 {
242 public:
244 };
245 
247  : TestSuite ("command-line", UNIT)
248 {
249  AddTestCase (new CommandLineBooleanTestCase, TestCase::QUICK);
250  AddTestCase (new CommandLineIntTestCase, TestCase::QUICK);
251  AddTestCase (new CommandLineUnsignedIntTestCase, TestCase::QUICK);
252  AddTestCase (new CommandLineStringTestCase, TestCase::QUICK);
253 }
254 
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:297
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:201
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.
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:491
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.
virtual ~CommandLineTestCaseBase()
Destructor.
Test string Command Line processing.
virtual ~CommandLineBooleanTestCase()
Destructor.