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
test.h
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2009 University of Washington
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
19
#ifndef NS3_TEST_H
20
#define NS3_TEST_H
21
22
#include <iostream>
23
#include <fstream>
24
#include <sstream>
25
#include <string>
26
#include <vector>
27
#include <list>
28
#include <limits>
29
#include <stdint.h>
30
31
#include "
system-wall-clock-ms.h
"
32
#include "
deprecated.h
"
33
34
//
35
// Note on below macros:
36
//
37
// When multiple statements are used in a macro, they should be bound together
38
// in a loop syntactically, so the macro can appear safely inside if clauses
39
// or other places that expect a single statement or a statement block. The
40
// "strange" do while construct is a generally expected best practice for
41
// defining a robust macro.
42
//
43
49
#define ASSERT_ON_FAILURE \
50
do { \
51
if (MustAssertOnFailure ()) \
52
{ \
53
*(volatile int *)0 = 0; \
54
} \
55
} while (false)
56
62
#define CONTINUE_ON_FAILURE \
63
do { \
64
if (!MustContinueOnFailure ()) \
65
{ \
66
return; \
67
} \
68
} while (false)
69
75
#define CONTINUE_ON_FAILURE_RETURNS_BOOL \
76
do { \
77
if (!MustContinueOnFailure ()) \
78
{ \
79
return IsStatusFailure (); \
80
} \
81
} while (false)
82
83
84
85
// ===========================================================================
86
// Test for equality (generic version)
87
// ===========================================================================
88
92
#define NS_TEST_ASSERT_MSG_EQ_INTERNAL(actual, limit, msg, file, line) \
93
do { \
94
if (!((actual) == (limit))) \
95
{ \
96
ASSERT_ON_FAILURE; \
97
std::ostringstream msgStream; \
98
msgStream << msg; \
99
std::ostringstream actualStream; \
100
actualStream << actual; \
101
std::ostringstream limitStream; \
102
limitStream << limit; \
103
ReportTestFailure (std::string (#actual) + " (actual) == " + \
104
std::string (#limit) + " (limit)", \
105
actualStream.str (), limitStream.str (), \
106
msgStream.str (), file, line); \
107
CONTINUE_ON_FAILURE; \
108
} \
109
} while (false)
110
137
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg) \
138
NS_TEST_ASSERT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
139
143
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL(actual, limit, msg, file, line) \
144
do { \
145
if (!((actual) == (limit))) \
146
{ \
147
ASSERT_ON_FAILURE; \
148
std::ostringstream msgStream; \
149
msgStream << msg; \
150
std::ostringstream actualStream; \
151
actualStream << actual; \
152
std::ostringstream limitStream; \
153
limitStream << limit; \
154
ReportTestFailure (std::string (#actual) + " (actual) == " + \
155
std::string (#limit) + " (limit)", \
156
actualStream.str (), limitStream.str (), \
157
msgStream.str (), file, line); \
158
CONTINUE_ON_FAILURE_RETURNS_BOOL; \
159
} \
160
} while (false)
161
191
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg) \
192
NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
193
200
#define NS_TEST_EXPECT_MSG_EQ_INTERNAL(actual, limit, msg, file, line) \
201
do { \
202
if (!((actual) == (limit))) \
203
{ \
204
ASSERT_ON_FAILURE; \
205
std::ostringstream msgStream; \
206
msgStream << msg; \
207
std::ostringstream actualStream; \
208
actualStream << actual; \
209
std::ostringstream limitStream; \
210
limitStream << limit; \
211
ReportTestFailure (std::string (#actual) + " (actual) == " + \
212
std::string (#limit) + " (limit)", \
213
actualStream.str (), limitStream.str (), \
214
msgStream.str (), file, line); \
215
} \
216
} while (false)
217
244
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg) \
245
NS_TEST_EXPECT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
246
247
// ===========================================================================
248
// Test for equality with a provided tolerance (use for floating point
249
// comparisons -- both float and double)
250
// ===========================================================================
251
255
#define NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line) \
256
do { \
257
if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
258
{ \
259
ASSERT_ON_FAILURE; \
260
std::ostringstream msgStream; \
261
msgStream << msg; \
262
std::ostringstream actualStream; \
263
actualStream << actual; \
264
std::ostringstream limitStream; \
265
limitStream << limit << " +- " << tol; \
266
std::ostringstream condStream; \
267
condStream << #actual << " (actual) < " << #limit \
268
<< " (limit) + " << #tol << " (tol) && " \
269
<< #actual << " (actual) > " << #limit \
270
<< " (limit) - " << #tol << " (tol)"; \
271
ReportTestFailure (condStream.str (), actualStream.str (), \
272
limitStream.str (), msgStream.str (), \
273
file, line); \
274
CONTINUE_ON_FAILURE; \
275
} \
276
} while (false)
277
326
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg) \
327
NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
328
332
#define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL(actual, limit, tol, msg, file, line) \
333
do { \
334
if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
335
{ \
336
ASSERT_ON_FAILURE; \
337
std::ostringstream msgStream; \
338
msgStream << msg; \
339
std::ostringstream actualStream; \
340
actualStream << actual; \
341
std::ostringstream limitStream; \
342
limitStream << limit << " +- " << tol; \
343
std::ostringstream condStream; \
344
condStream << #actual << " (actual) < " << #limit \
345
<< " (limit) + " << #tol << " (tol) && " \
346
<< #actual << " (actual) > " << #limit \
347
<< " (limit) - " << #tol << " (tol)"; \
348
ReportTestFailure (condStream.str (), actualStream.str (), \
349
limitStream.str (), msgStream.str (), \
350
file, line); \
351
CONTINUE_ON_FAILURE_RETURNS_BOOL; \
352
} \
353
} while (false)
354
406
#define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL(actual, limit, tol, msg) \
407
NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
408
415
#define NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line) \
416
do { \
417
if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
418
{ \
419
ASSERT_ON_FAILURE; \
420
std::ostringstream msgStream; \
421
msgStream << msg; \
422
std::ostringstream actualStream; \
423
actualStream << actual; \
424
std::ostringstream limitStream; \
425
limitStream << limit << " +- " << tol; \
426
std::ostringstream condStream; \
427
condStream << #actual << " (actual) < " << #limit \
428
<< " (limit) + " << #tol << " (tol) && " \
429
<< #actual << " (actual) > " << #limit \
430
<< " (limit) - " << #tol << " (tol)"; \
431
ReportTestFailure (condStream.str (), actualStream.str (), \
432
limitStream.str (), msgStream.str (), \
433
file, line); \
434
} \
435
} while (false)
436
485
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg) \
486
NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
487
488
// ===========================================================================
489
// Test for inequality
490
// ===========================================================================
491
495
#define NS_TEST_ASSERT_MSG_NE_INTERNAL(actual, limit, msg, file, line) \
496
do { \
497
if (!((actual) != (limit))) \
498
{ \
499
ASSERT_ON_FAILURE; \
500
std::ostringstream msgStream; \
501
msgStream << msg; \
502
std::ostringstream actualStream; \
503
actualStream << actual; \
504
std::ostringstream limitStream; \
505
limitStream << limit; \
506
ReportTestFailure (std::string (#actual) + " (actual) != " + \
507
std::string (#limit) + " (limit)", \
508
actualStream.str (), limitStream.str (), \
509
msgStream.str (), file, line); \
510
CONTINUE_ON_FAILURE; \
511
} \
512
} while (false)
513
539
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg) \
540
NS_TEST_ASSERT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
541
545
#define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL(actual, limit, msg, file, line) \
546
do { \
547
if (!((actual) != (limit))) \
548
{ \
549
ASSERT_ON_FAILURE; \
550
std::ostringstream msgStream; \
551
msgStream << msg; \
552
std::ostringstream actualStream; \
553
actualStream << actual; \
554
std::ostringstream limitStream; \
555
limitStream << limit; \
556
ReportTestFailure (std::string (#actual) + " (actual) != " + \
557
std::string (#limit) + " (limit)", \
558
actualStream.str (), limitStream.str (), \
559
msgStream.str (), file, line); \
560
CONTINUE_ON_FAILURE_RETURNS_BOOL; \
561
} \
562
} while (false)
563
592
#define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL(actual, limit, msg) \
593
NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
594
601
#define NS_TEST_EXPECT_MSG_NE_INTERNAL(actual, limit, msg, file, line) \
602
do { \
603
if (!((actual) != (limit))) \
604
{ \
605
ASSERT_ON_FAILURE; \
606
std::ostringstream msgStream; \
607
msgStream << msg; \
608
std::ostringstream actualStream; \
609
actualStream << actual; \
610
std::ostringstream limitStream; \
611
limitStream << limit; \
612
ReportTestFailure (std::string (#actual) + " (actual) != " + \
613
std::string (#limit) + " (limit)", \
614
actualStream.str (), limitStream.str (), \
615
msgStream.str (), file, line); \
616
} \
617
} while (false)
618
644
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg) \
645
NS_TEST_EXPECT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
646
647
// ===========================================================================
648
// Test for less than relation
649
// ===========================================================================
650
654
#define NS_TEST_ASSERT_MSG_LT_INTERNAL(actual, limit, msg, file, line) \
655
do { \
656
if (!((actual) < (limit))) \
657
{ \
658
ASSERT_ON_FAILURE; \
659
std::ostringstream msgStream; \
660
msgStream << msg; \
661
std::ostringstream actualStream; \
662
actualStream << actual; \
663
std::ostringstream limitStream; \
664
limitStream << limit; \
665
ReportTestFailure (std::string (#actual) + " (actual) < " + \
666
std::string (#limit) + " (limit)", \
667
actualStream.str (), limitStream.str (), \
668
msgStream.str (), file, line); \
669
CONTINUE_ON_FAILURE; \
670
} \
671
} while (false)
672
688
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg) \
689
NS_TEST_ASSERT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
690
697
#define NS_TEST_EXPECT_MSG_LT_INTERNAL(actual, limit, msg, file, line) \
698
do { \
699
if (!((actual) < (limit))) \
700
{ \
701
ASSERT_ON_FAILURE; \
702
std::ostringstream msgStream; \
703
msgStream << msg; \
704
std::ostringstream actualStream; \
705
actualStream << actual; \
706
std::ostringstream limitStream; \
707
limitStream << limit; \
708
ReportTestFailure (std::string (#actual) + " (actual) < " + \
709
std::string (#limit) + " (limit)", \
710
actualStream.str (), limitStream.str (), \
711
msgStream.str (), file, line); \
712
} \
713
} while (false)
714
729
#define NS_TEST_EXPECT_MSG_LT(actual, limit, msg) \
730
NS_TEST_EXPECT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
731
732
// ===========================================================================
733
// Test for greater than relation
734
// ===========================================================================
735
739
#define NS_TEST_ASSERT_MSG_GT_INTERNAL(actual, limit, msg, file, line) \
740
do { \
741
if (!((actual) > (limit))) \
742
{ \
743
ASSERT_ON_FAILURE; \
744
std::ostringstream msgStream; \
745
msgStream << msg; \
746
std::ostringstream actualStream; \
747
actualStream << actual; \
748
std::ostringstream limitStream; \
749
limitStream << limit; \
750
ReportTestFailure (std::string (#actual) + " (actual) > " + \
751
std::string (#limit) + " (limit)", \
752
actualStream.str (), limitStream.str (), \
753
msgStream.str (), file, line); \
754
CONTINUE_ON_FAILURE; \
755
} \
756
} while (false)
757
773
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg) \
774
NS_TEST_ASSERT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
775
782
#define NS_TEST_EXPECT_MSG_GT_INTERNAL(actual, limit, msg, file, line) \
783
do { \
784
if (!((actual) > (limit))) \
785
{ \
786
ASSERT_ON_FAILURE; \
787
std::ostringstream msgStream; \
788
msgStream << msg; \
789
std::ostringstream actualStream; \
790
actualStream << actual; \
791
std::ostringstream limitStream; \
792
limitStream << limit; \
793
ReportTestFailure (std::string (#actual) + " (actual) > " + \
794
std::string (#limit) + " (limit)", \
795
actualStream.str (), limitStream.str (), \
796
msgStream.str (), file, line); \
797
} \
798
} while (false)
799
814
#define NS_TEST_EXPECT_MSG_GT(actual, limit, msg) \
815
NS_TEST_EXPECT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
816
817
namespace
ns3 {
818
837
bool
TestDoubleIsEqual
(
const
double
a,
const
double
b,
838
const
double
epsilon = std::numeric_limits<double>::epsilon ());
839
840
class
TestRunnerImpl;
841
849
class
TestCase
850
{
851
public
:
856
enum
TestDuration
{
857
QUICK
= 1,
858
EXTENSIVE
= 2,
859
TAKES_FOREVER
= 3
860
};
861
865
virtual
~TestCase
();
866
867
protected
:
871
TestCase
(std::string name);
872
881
void
AddTestCase
(
TestCase
*testCase)
NS_DEPRECATED
;
882
889
void
AddTestCase
(
TestCase
*testCase,
enum
TestDuration
duration);
890
903
void
SetDataDir
(std::string directory);
904
909
bool
GetErrorStatus
(
void
) const
NS_DEPRECATED
;
913
bool
IsStatusFailure
(
void
) const;
917
bool
IsStatusSuccess
(
void
) const;
918
922
std::
string
GetName
(
void
) const;
923
924
// The methods below are used only by test macros and should not
925
// be used by normal users.
926
932
void
ReportTestFailure
(std::
string
cond, std::
string
actual,
933
std::
string
limit, std::
string
message,
934
std::
string
file, int32_t line);
940
bool
MustAssertOnFailure
(
void
) const;
946
bool
MustContinueOnFailure
(
void
) const;
952
std::
string
CreateDataDirFilename
(std::
string
filename);
960
std::
string
CreateTempDirFilename
(std::
string
filename);
961
private
:
962
friend class
TestRunnerImpl
;
963
970
virtual
void
DoSetup
(
void
);
971
977
virtual
void
DoRun
(
void
) = 0;
978
984
virtual
void
DoTeardown
(
void
);
985
986
// forbid copying objects
991
TestCase
(
TestCase
& tc);
996
TestCase
& operator= (
TestCase
& tc);
997
998
// methods called by TestRunnerImpl
1003
void
Run
(TestRunnerImpl *runner);
1008
bool
IsFailed
(
void
) const;
1009
1010
// Forward declaration is enough, since we only include a pointer here
1011
struct
Result
;
1012
1013
TestCase
*
m_parent
;
1014
std::vector<
TestCase
*>
m_children
;
1015
std::
string
m_dataDir
;
1016
TestRunnerImpl *
m_runner
;
1017
struct Result *
m_result
;
1018
std::
string
m_name
;
1019
enum
TestDuration
m_duration
;
1020
};
1021
1025
class
TestSuite
: public
TestCase
1026
{
1027
public
:
1032
enum
Type
{
1033
ALL = 0,
1034
BVT = 1,
1035
UNIT
,
1036
SYSTEM
,
1037
EXAMPLE
,
1038
PERFORMANCE
1039
};
1040
1047
TestSuite
(std::string name, Type type = UNIT);
1048
1054
TestSuite::Type
GetTestType (
void
);
1055
1056
private
:
1057
virtual
void
DoRun
(
void
);
1058
1059
1060
TestSuite::Type
m_type
;
1061
};
1062
1066
class
TestRunner
1067
{
1068
public
:
1076
static
int
Run
(
int
argc,
char
*argv[]);
1077
};
1078
1082
template
<
typename
T>
1083
class
TestVectors
1084
{
1085
public
:
1089
TestVectors
();
1093
virtual
~
TestVectors
();
1094
1098
void
Reserve (uint32_t reserve);
1099
1104
uint32_t Add (T vector);
1105
1109
uint32_t GetN (
void
)
const
;
1115
T Get (uint32_t i)
const
;
1116
1117
private
:
1121
TestVectors
(
const
TestVectors
& tv);
1125
TestVectors
&
operator=
(
const
TestVectors
& tv);
1129
bool
operator==
(
const
TestVectors
& tv)
const
;
1130
1131
typedef
std::vector<T>
TestVector
;
1132
TestVector
m_vectors
;
1133
};
1134
1135
template
<
typename
T>
1136
TestVectors<T>::TestVectors
()
1137
: m_vectors ()
1138
{
1139
}
1140
1141
template
<
typename
T>
1142
void
1143
TestVectors<T>::Reserve
(uint32_t reserve)
1144
{
1145
m_vectors.reserve (reserve);
1146
}
1147
1148
template
<
typename
T>
1149
TestVectors<T>::~TestVectors
()
1150
{
1151
}
1152
1153
template
<
typename
T>
1154
uint32_t
1155
TestVectors<T>::Add
(T vector)
1156
{
1157
uint32_t index = m_vectors.size ();
1158
m_vectors.push_back (vector);
1159
return
index;
1160
}
1161
1162
template
<
typename
T>
1163
uint32_t
1164
TestVectors<T>::GetN
(
void
)
const
1165
{
1166
return
m_vectors.size ();
1167
}
1168
1169
template
<
typename
T>
1170
T
1171
TestVectors<T>::Get
(uint32_t i)
const
1172
{
1173
NS_ABORT_MSG_UNLESS
(m_vectors.size () > i,
"TestVectors::Get(): Bad index"
);
1174
return
m_vectors[i];
1175
}
1176
1177
}
// namespace ns3
1178
1179
#endif
/* NS3_TEST_H */
src
core
model
test.h
Generated on Fri Aug 30 2013 01:42:47 for ns-3 by
1.8.1.2