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 <stdlib.h>
28
#include <stdarg.h>
29
30
namespace
ns3 {
31
32
// ===========================================================================
33
// A test base class that drives Command Line parsing
34
// ===========================================================================
35
class
CommandLineTestCaseBase
:
public
TestCase
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
// ===========================================================================
71
class
CommandLineBooleanTestCase
:
public
CommandLineTestCaseBase
72
{
73
public
:
74
CommandLineBooleanTestCase
();
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
88
CommandLineBooleanTestCase::DoRun
(
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
// ===========================================================================
105
class
CommandLineIntTestCase
:
public
CommandLineTestCaseBase
106
{
107
public
:
108
CommandLineIntTestCase
();
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
122
CommandLineIntTestCase::DoRun
(
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
// ===========================================================================
143
class
CommandLineUnsignedIntTestCase
:
public
CommandLineTestCaseBase
144
{
145
public
:
146
CommandLineUnsignedIntTestCase
();
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
160
CommandLineUnsignedIntTestCase::DoRun
(
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
// ===========================================================================
178
class
CommandLineStringTestCase
:
public
CommandLineTestCaseBase
179
{
180
public
:
181
CommandLineStringTestCase
();
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
195
CommandLineStringTestCase::DoRun
(
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
// ===========================================================================
213
class
CommandLineTestSuite
:
public
TestSuite
214
{
215
public
:
216
CommandLineTestSuite
();
217
};
218
219
CommandLineTestSuite::CommandLineTestSuite
()
220
:
TestSuite
(
"command-line"
, UNIT)
221
{
222
AddTestCase
(
new
CommandLineBooleanTestCase
);
223
AddTestCase
(
new
CommandLineIntTestCase
);
224
AddTestCase
(
new
CommandLineUnsignedIntTestCase
);
225
AddTestCase
(
new
CommandLineStringTestCase
);
226
}
227
228
static
CommandLineTestSuite
CommandLineTestSuite
;
229
230
}
// namespace ns3
src
core
test
command-line-test-suite.cc
Generated on Tue Nov 13 2012 10:32:12 for ns-3 by
1.8.1.2