A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
command-line-test-suite.cc
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 // ===========================================================================
33 // A test base class that drives Command Line parsing
34 // ===========================================================================
36 {
37 public:
38  CommandLineTestCaseBase (std::string description);
39  virtual ~CommandLineTestCaseBase () {}
40 
41  void Parse (const CommandLine &cmd, int n, ...);
42 };
43 
44 CommandLineTestCaseBase::CommandLineTestCaseBase (std::string description)
45  : TestCase (description)
46 {
47 }
48 
49 void
50 CommandLineTestCaseBase::Parse (const CommandLine &cmd, int n, ...)
51 {
52  char **args = new char* [n+1];
53  args[0] = (char *) "Test";
54  va_list ap;
55  va_start (ap, n);
56  int i = 0;
57  while (i < n)
58  {
59  char *arg = va_arg (ap, char *);
60  args[i+1] = arg;
61  i++;
62  }
63  int argc = n + 1;
64  cmd.Parse (argc, args);
65  delete [] args;
66 }
67 
68 // ===========================================================================
69 // Test boolean Command Line processing
70 // ===========================================================================
72 {
73 public:
75  virtual ~CommandLineBooleanTestCase () {}
76 
77 private:
78  virtual void DoRun (void);
79 
80 };
81 
82 CommandLineBooleanTestCase::CommandLineBooleanTestCase ()
83  : CommandLineTestCaseBase ("Check boolean arguments")
84 {
85 }
86 
87 void
89 {
90  CommandLine cmd;
91  bool myBool = true;
92 
93  cmd.AddValue ("my-bool", "help", myBool);
94 
95  Parse (cmd, 1, "--my-bool=0");
96  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false");
97 
98  Parse (cmd, 1, "--my-bool=1");
99  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true");
100 }
101 
102 // ===========================================================================
103 // Test int Command Line processing
104 // ===========================================================================
106 {
107 public:
109  virtual ~CommandLineIntTestCase () {}
110 
111 private:
112  virtual void DoRun (void);
113 
114 };
115 
116 CommandLineIntTestCase::CommandLineIntTestCase ()
117  : CommandLineTestCaseBase ("Check int arguments")
118 {
119 }
120 
121 void
123 {
124  CommandLine cmd;
125  bool myBool = true;
126  int32_t myInt32 = 10;
127 
128  cmd.AddValue ("my-bool", "help", myBool);
129  cmd.AddValue ("my-int32", "help", myInt32);
130 
131  Parse (cmd, 2, "--my-bool=0", "--my-int32=-3");
132  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to false");
133  NS_TEST_ASSERT_MSG_EQ (myInt32, -3, "Command parser did not correctly set an integer value to -3");
134 
135  Parse (cmd, 2, "--my-bool=1", "--my-int32=+2");
136  NS_TEST_ASSERT_MSG_EQ (myBool, true, "Command parser did not correctly set a boolean value to true");
137  NS_TEST_ASSERT_MSG_EQ (myInt32, +2, "Command parser did not correctly set an integer value to +2");
138 }
139 
140 // ===========================================================================
141 // Test unsigned int Command Line processing
142 // ===========================================================================
144 {
145 public:
147  virtual ~CommandLineUnsignedIntTestCase () {}
148 
149 private:
150  virtual void DoRun (void);
151 
152 };
153 
154 CommandLineUnsignedIntTestCase::CommandLineUnsignedIntTestCase ()
155  : CommandLineTestCaseBase ("Check unsigned int arguments")
156 {
157 }
158 
159 void
161 {
162  CommandLine cmd;
163  bool myBool = true;
164  uint32_t myUint32 = 10;
165 
166  cmd.AddValue ("my-bool", "help", myBool);
167  cmd.AddValue ("my-uint32", "help", myUint32);
168 
169  Parse (cmd, 2, "--my-bool=0", "--my-uint32=9");
170 
171  NS_TEST_ASSERT_MSG_EQ (myBool, false, "Command parser did not correctly set a boolean value to true");
172  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9");
173 }
174 
175 // ===========================================================================
176 // Test string Command Line processing
177 // ===========================================================================
179 {
180 public:
182  virtual ~CommandLineStringTestCase () {}
183 
184 private:
185  virtual void DoRun (void);
186 
187 };
188 
189 CommandLineStringTestCase::CommandLineStringTestCase ()
190  : CommandLineTestCaseBase ("Check unsigned int arguments")
191 {
192 }
193 
194 void
196 {
197  CommandLine cmd;
198  uint32_t myUint32 = 10;
199  std::string myStr = "MyStr";
200 
201  cmd.AddValue ("my-uint32", "help", myUint32);
202  cmd.AddValue ("my-str", "help", myStr);
203 
204  Parse (cmd, 2, "--my-uint32=9", "--my-str=XX");
205 
206  NS_TEST_ASSERT_MSG_EQ (myUint32, 9, "Command parser did not correctly set an unsigned integer value to 9");
207  NS_TEST_ASSERT_MSG_EQ (myStr, "XX", "Command parser did not correctly set an string value to \"XX\"");
208 }
209 
210 // ===========================================================================
211 // The Test Suite that glues all of the Test Cases together.
212 // ===========================================================================
214 {
215 public:
217 };
218 
219 CommandLineTestSuite::CommandLineTestSuite ()
220  : TestSuite ("command-line", UNIT)
221 {
222  AddTestCase (new CommandLineBooleanTestCase, TestCase::QUICK);
223  AddTestCase (new CommandLineIntTestCase, TestCase::QUICK);
224  AddTestCase (new CommandLineUnsignedIntTestCase, TestCase::QUICK);
225  AddTestCase (new CommandLineStringTestCase, TestCase::QUICK);
226 }
227 
A suite of tests to run.
Definition: test.h:962
encapsulates test code
Definition: test.h:834
virtual void DoRun(void)
Implementation to actually run this test case.
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
virtual void DoRun(void)
Implementation to actually run this test case.
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84