A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
/*************************************************************************/
35
class
CommandLineTestCaseBase
:
public
TestCase
36
{
37
public
:
43
CommandLineTestCaseBase
(std::string description);
45
virtual
~CommandLineTestCaseBase
() {}
46
53
void
Parse (
CommandLine
&cmd,
int
n, ...);
54
};
55
56
CommandLineTestCaseBase::CommandLineTestCaseBase
(std::string description)
57
:
TestCase
(description)
58
{
59
}
60
61
void
62
CommandLineTestCaseBase::Parse
(
CommandLine
&cmd,
int
n, ...)
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
/*************************************************************************/
84
class
CommandLineBooleanTestCase
:
public
CommandLineTestCaseBase
85
{
86
public
:
87
CommandLineBooleanTestCase
();
88
virtual
~CommandLineBooleanTestCase
() {}
90
private
:
91
virtual
void
DoRun
(
void
);
93
};
94
95
CommandLineBooleanTestCase::CommandLineBooleanTestCase
()
96
:
CommandLineTestCaseBase
(
"Check boolean arguments"
)
97
{
98
}
99
100
void
101
CommandLineBooleanTestCase::DoRun
(
void
)
102
{
103
CommandLine
cmd;
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
/*************************************************************************/
132
class
CommandLineIntTestCase
:
public
CommandLineTestCaseBase
133
{
134
public
:
135
CommandLineIntTestCase
();
136
virtual
~CommandLineIntTestCase
() {}
138
private
:
139
virtual
void
DoRun
(
void
);
141
};
142
143
CommandLineIntTestCase::CommandLineIntTestCase
()
144
:
CommandLineTestCaseBase
(
"Check int arguments"
)
145
{
146
}
147
148
void
149
CommandLineIntTestCase::DoRun
(
void
)
150
{
151
CommandLine
cmd;
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
/*************************************************************************/
170
class
CommandLineUnsignedIntTestCase
:
public
CommandLineTestCaseBase
171
{
172
public
:
173
CommandLineUnsignedIntTestCase
();
174
virtual
~CommandLineUnsignedIntTestCase
() {}
176
private
:
177
virtual
void
DoRun
(
void
);
179
};
180
181
CommandLineUnsignedIntTestCase::CommandLineUnsignedIntTestCase
()
182
:
CommandLineTestCaseBase
(
"Check unsigned int arguments"
)
183
{
184
}
185
186
void
187
CommandLineUnsignedIntTestCase::DoRun
(
void
)
188
{
189
CommandLine
cmd;
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
/*************************************************************************/
205
class
CommandLineStringTestCase
:
public
CommandLineTestCaseBase
206
{
207
public
:
208
CommandLineStringTestCase
();
209
virtual
~CommandLineStringTestCase
() {}
211
private
:
212
virtual
void
DoRun
(
void
);
214
};
215
216
CommandLineStringTestCase::CommandLineStringTestCase
()
217
:
CommandLineTestCaseBase
(
"Check unsigned int arguments"
)
218
{
219
}
220
221
void
222
CommandLineStringTestCase::DoRun
(
void
)
223
{
224
CommandLine
cmd;
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
/*************************************************************************/
240
class
CommandLineTestSuite
:
public
TestSuite
241
{
242
public
:
243
CommandLineTestSuite
();
244
};
245
246
CommandLineTestSuite::CommandLineTestSuite
()
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
255
static
CommandLineTestSuite
CommandLineTestSuite
;
CommandLineTestSuite
The Test Suite that glues all of the Test Cases together.
Definition:
command-line-test-suite.cc:240
CommandLineStringTestCase::CommandLineStringTestCase
CommandLineStringTestCase()
Constructor.
Definition:
command-line-test-suite.cc:216
CommandLineTestSuite
static CommandLineTestSuite CommandLineTestSuite
Test instance.
Definition:
command-line-test-suite.cc:255
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1025
CommandLineIntTestCase::~CommandLineIntTestCase
virtual ~CommandLineIntTestCase()
Destructor.
Definition:
command-line-test-suite.cc:136
CommandLineTestSuite::CommandLineTestSuite
CommandLineTestSuite()
Constructor.
Definition:
command-line-test-suite.cc:246
ns3::TestCase
encapsulates test code
Definition:
test.h:849
CommandLineIntTestCase::DoRun
virtual void DoRun(void)
Run the test.
Definition:
command-line-test-suite.cc:149
CommandLineStringTestCase::~CommandLineStringTestCase
virtual ~CommandLineStringTestCase()
Destructor.
Definition:
command-line-test-suite.cc:209
CommandLineTestCaseBase
A test base class that drives Command Line parsing.
Definition:
command-line-test-suite.cc:35
CommandLineUnsignedIntTestCase::~CommandLineUnsignedIntTestCase
virtual ~CommandLineUnsignedIntTestCase()
Destructor.
Definition:
command-line-test-suite.cc:174
CommandLineBooleanTestCase::CommandLineBooleanTestCase
CommandLineBooleanTestCase()
Constructor.
Definition:
command-line-test-suite.cc:95
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:152
CommandLineIntTestCase::CommandLineIntTestCase
CommandLineIntTestCase()
Constructor.
Definition:
command-line-test-suite.cc:143
CommandLineStringTestCase::DoRun
virtual void DoRun(void)
Run the test.
Definition:
command-line-test-suite.cc:222
CommandLineBooleanTestCase::DoRun
virtual void DoRun(void)
Run the test.
Definition:
command-line-test-suite.cc:101
CommandLineUnsignedIntTestCase
Test unsigned int Command Line processing.
Definition:
command-line-test-suite.cc:170
CommandLineTestCaseBase::CommandLineTestCaseBase
CommandLineTestCaseBase(std::string description)
Constructor.
Definition:
command-line-test-suite.cc:56
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition:
test.cc:173
CommandLineBooleanTestCase
Test boolean Command Line processing.
Definition:
command-line-test-suite.cc:84
ns3::CommandLine::AddValue
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition:
command-line.h:408
CommandLineUnsignedIntTestCase::DoRun
virtual void DoRun(void)
Run the test.
Definition:
command-line-test-suite.cc:187
CommandLineTestCaseBase::Parse
void Parse(CommandLine &cmd, int n,...)
Excercise the CommandLine with the provided arguments.
Definition:
command-line-test-suite.cc:62
CommandLineIntTestCase
Test int Command Line processing.
Definition:
command-line-test-suite.cc:132
ns3::CommandLine::Parse
void Parse(int argc, char *argv[])
Parse the program arguments.
Definition:
command-line.cc:104
CommandLineTestCaseBase::~CommandLineTestCaseBase
virtual ~CommandLineTestCaseBase()
Destructor.
Definition:
command-line-test-suite.cc:45
CommandLineStringTestCase
Test string Command Line processing.
Definition:
command-line-test-suite.cc:205
NS_TEST_ASSERT_MSG_EQ
#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:137
CommandLineBooleanTestCase::~CommandLineBooleanTestCase
virtual ~CommandLineBooleanTestCase()
Destructor.
Definition:
command-line-test-suite.cc:88
CommandLineUnsignedIntTestCase::CommandLineUnsignedIntTestCase
CommandLineUnsignedIntTestCase()
Constructor.
Definition:
command-line-test-suite.cc:181
src
core
test
command-line-test-suite.cc
Generated on Sat Apr 19 2014 14:06:53 for ns-3 by
1.8.6