A Discrete-Event Network Simulator
API
Testing

Tools to define and execute unit tests. More...

+ Collaboration diagram for Testing:

Modules

 Testing Implementation
 Internal implementation of the Testing system.
 

Files

file  example-as-test.cc
 Implementation of classes ns3::ExampleAsTestSuite and ns3::ExampleTestCase.
 
file  example-as-test.h
 Enable examples to be run as meaningful tests.
 
file  sample-test-suite.cc
 Example use of TestSuite.
 
file  test.cc
 ns3::TestCase, ns3::TestSuite, ns3::TestRunner implementations,
 
file  test.h
 ns3::TestCase, ns3::TestSuite, ns3::TestRunner declarations, and NS_TEST_ASSERT macro definitions.
 

Classes

class  ns3::ExampleAsTestCase
 Execute an example program as a test, by comparing the output to a reference file. More...
 
class  ns3::ExampleAsTestSuite
 Execute an example program as a test suite. More...
 
class  ns3::TestCase
 encapsulates test code More...
 
class  ns3::TestSuite
 A suite of tests to run. More...
 
class  ns3::TestVectors< T >
 A simple way to store test vectors (for stimulus or from responses) More...
 

Macros

#define ASSERT_ON_FAILURE
 Check if we should assert on errors, and do so. More...
 
#define CONTINUE_ON_FAILURE
 If we shouldn't continue on errors, return. More...
 
#define CONTINUE_ON_FAILURE_RETURNS_BOOL
 If we shouldn't continue on errors, return test status. More...
 
#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. More...
 
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg)
 Test that an actual and expected (limit) value are equal and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
 Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL(actual, limit, tol, msg)
 Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
 Test that an actual value is greater than a limit and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_GT_OR_EQ(actual, limit, msg)
 Test that an actual value is greater than or equal to a limit and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
 Test that an actual value is less than a limit and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_LT_OR_EQ(actual, limit, msg)
 Test that an actual value is less than or equal to a limit and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
 Test that an actual and expected (limit) value are not equal and report and abort if not. More...
 
#define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL(actual, limit, msg)
 Test that an actual and expected (limit) value are not equal and report and abort if not. More...
 
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
 Test that an actual and expected (limit) value are equal and report if not. More...
 
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg)
 Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if not. More...
 
#define NS_TEST_EXPECT_MSG_GT(actual, limit, msg)
 Test that an actual value is greater than a limit and report if not. More...
 
#define NS_TEST_EXPECT_MSG_GT_OR_EQ(actual, limit, msg)
 Test that an actual value is greater than or equal to limit and report if not. More...
 
#define NS_TEST_EXPECT_MSG_LT(actual, limit, msg)
 Test that an actual value is less than a limit and report if not. More...
 
#define NS_TEST_EXPECT_MSG_LT_OR_EQ(actual, limit, msg)
 Test that an actual value is less than or equal to a limit and report if not. More...
 
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
 Test that an actual and expected (limit) value are not equal and report if not. More...
 

Functions

bool ns3::TestDoubleIsEqual (const double a, const double b, const double epsilon=std::numeric_limits< double >::epsilon())
 Compare two double precision floating point numbers and declare them equal if they are within some epsilon of each other. More...
 

Detailed Description

Tools to define and execute unit tests.

This module lists the normal Testing API. Most of these macros forward to the implementation macros in testingimpl. You should generally use these macros only.

Macro Definition Documentation

◆ ASSERT_ON_FAILURE

#define ASSERT_ON_FAILURE
Value:
do { \
if (MustAssertOnFailure ()) \
{ \
*(volatile int *)0 = 0; \
} \
} while (false)

Check if we should assert on errors, and do so.

Definition at line 74 of file test.h.

◆ CONTINUE_ON_FAILURE

#define CONTINUE_ON_FAILURE
Value:
do { \
if (!MustContinueOnFailure ()) \
{ \
return; \
} \
} while (false)

If we shouldn't continue on errors, return.

Definition at line 86 of file test.h.

◆ CONTINUE_ON_FAILURE_RETURNS_BOOL

#define CONTINUE_ON_FAILURE_RETURNS_BOOL
Value:
do { \
if (!MustContinueOnFailure ()) \
{ \
return IsStatusFailure (); \
} \
} while (false)

If we shouldn't continue on errors, return test status.

Definition at line 98 of file test.h.

◆ NS_TEST_ASSERT_MSG_EQ

#define NS_TEST_ASSERT_MSG_EQ (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) == (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) == " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that an actual and expected (limit) value are equal and report and abort if not.

Check to see if the expected (limit) value is equal to the actual value found in a test case. If the two values are equal nothing happens, but if the comparison fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream, for example:

NS_TEST_ASSERT_MSG_EQ (result, true,
"cannot open file " << filename << " in test");
#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:141

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]msgMessage that is output if the test does not pass.
Warning
Do not use this macro if you are comparing floating point numbers (float or double) as it is unlikely to do what you expect. Use NS_TEST_ASSERT_MSG_EQ_TOL instead.

Definition at line 141 of file test.h.

◆ NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL

#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) == (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) == " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE_RETURNS_BOOL; \
} \
} while (false)

Test that an actual and expected (limit) value are equal and report and abort if not.

Check to see if the expected (limit) value is equal to the actual value found in a test case. If the two values are equal nothing happens, but if the comparison fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream, for example:

"cannot open file " << filename << " in test");
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:192

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]msgMessage that is output if the test does not pass.
Warning
Do not use this macro if you are comparing floating point numbers (float or double) as it is unlikely to do what you expect. Use NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_TOL instead.

This function returns a Boolean value.

Definition at line 192 of file test.h.

◆ NS_TEST_ASSERT_MSG_EQ_TOL

#define NS_TEST_ASSERT_MSG_EQ_TOL (   actual,
  limit,
  tol,
  msg 
)
Value:
do { \
if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit << " +- " << tol; \
std::ostringstream condStream; \
condStream << #actual << " (actual) < " << #limit \
<< " (limit) + " << #tol << " (tol) && " \
<< #actual << " (actual) > " << #limit \
<< " (limit) - " << #tol << " (tol)"; \
ReportTestFailure (condStream.str (), actualStream.str (), \
limitStream.str (), msgStream.str (), \
__FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and abort if not.

Check to see if the expected (limit) value is equal to the actual value found in a test case to some tolerance. This is not the same thing as asking if two floating point are equal to within some epsilon, but is useful for that case. This assertion is geared toward more of a measurement problem. Consider measuring a physical rod of some kind that you have ordered. You need to determine if it is "good." You want to measure the rod to an arbitrary precision of sixteen significant figures, you will measure the rod to determine if its length is within the tolerances you provided. For example, 12.00 inches plus or minus .005 inch may be just fine.

In ns-3, you might want to measure a signal to noise ratio and check to see if the answer is what you expect. If you naively measure (double)1128.93 and compare this number with a constant 1128.93 you are almost certainly going to have your test fail because of floating point rounding errors. We provide a floating point comparison function ns3::TestDoubleIsEqual() but you will probably quickly find that is not what you want either. It may turn out to be the case that when you measured an SNR that printed as 1128.93, what was actually measured was something more like 1128.9287653857625442 for example. Given that the double epsilon is on the order of 0.0000000000000009, you would need to provide sixteen significant figures of expected value for this kind of test to pass even with a typical test for floating point "approximate equality." That is clearly not required or desired. You really want to be able to provide 1128.93 along with a tolerance just like you provided 12 inches +- 0.005 inch above.

This assertion is designed for real measurements by taking into account measurement tolerances. By doing so it also automatically compensates for floating point rounding errors. If you really want to check floating point equality down to the numeric_limits<double>::epsilon () range, consider using ns3::TestDoubleIsEqual().

Note
Mixing signed and unsigned types can lead to misleading results.

The message is interpreted as a stream, for example:

NS_TEST_ASSERT_MSG_EQ_TOL (snr, 1128.93, 0.005,
"wrong snr (" << snr << ") in test");
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:323

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]tolTolerance of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 323 of file test.h.

◆ NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL

#define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (   actual,
  limit,
  tol,
  msg 
)
Value:
do { \
if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit << " +- " << tol; \
std::ostringstream condStream; \
condStream << #actual << " (actual) < " << #limit \
<< " (limit) + " << #tol << " (tol) && " \
<< #actual << " (actual) > " << #limit \
<< " (limit) - " << #tol << " (tol)"; \
ReportTestFailure (condStream.str (), actualStream.str (), \
limitStream.str (), msgStream.str (), \
__FILE__, __LINE__); \
CONTINUE_ON_FAILURE_RETURNS_BOOL; \
} \
} while (false)

Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and abort if not.

Check to see if the expected (limit) value is equal to the actual value found in a test case to some tolerance. This is not the same thing as asking if two floating point are equal to within some epsilon, but is useful for that case. This assertion is geared toward more of a measurement problem. Consider measuring a physical rod of some kind that you have ordered. You need to determine if it is "good." You want to measure the rod to an arbitrary precision of sixteen significant figures, you will measure the rod to determine if its length is within the tolerances you provided. For example, 12.00 inches plus or minus .005 inch may be just fine.

In ns-3, you might want to measure a signal to noise ratio and check to see if the answer is what you expect. If you naively measure (double)1128.93 and compare this number with a constant 1128.93 you are almost certainly going to have your test fail because of floating point rounding errors. We provide a floating point comparison function ns3::TestDoubleIsEqual() but you will probably quickly find that is not what you want either. It may turn out to be the case that when you measured an SNR that printed as 1128.93, what was actually measured was something more like 1128.9287653857625442 for example. Given that the double epsilon is on the order of 0.0000000000000009, you would need to provide sixteen significant figures of expected value for this kind of test to pass even with a typical test for floating point "approximate equality." That is clearly not required or desired. You really want to be able to provide 1128.93 along with a tolerance just like you provided 12 inches +- 0.005 inch above.

This assertion is designed for real measurements by taking into account measurement tolerances. By doing so it also automatically compensates for floating point rounding errors. If you really want to check floating point equality down to the numeric_limits<double>::epsilon () range, consider using ns3::TestDoubleIsEqual().

Note
Mixing signed and unsigned types can lead to misleading results.

The message is interpreted as a stream, for example:

"wrong snr (" << snr << ") in test");
#define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:408

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]tolTolerance of the test.
[in]msgMessage that is output if the test does not pass.

This function returns a Boolean value.

Definition at line 408 of file test.h.

◆ NS_TEST_ASSERT_MSG_GT

#define NS_TEST_ASSERT_MSG_GT (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) > (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) > " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that an actual value is greater than a limit and report and abort if not.

Check to see if the actual value found in a test case is greater than the limit value. If the actual value is greater nothing happens, but if the check fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 825 of file test.h.

◆ NS_TEST_ASSERT_MSG_GT_OR_EQ

#define NS_TEST_ASSERT_MSG_GT_OR_EQ (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) >= (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) > " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that an actual value is greater than or equal to a limit and report and abort if not.

Check to see if the actual value found in a test case is greater than or equal to the limit value. If the actual value is greater nothing happens, but if the check fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 862 of file test.h.

◆ NS_TEST_ASSERT_MSG_LT

#define NS_TEST_ASSERT_MSG_LT (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) < (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) < " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that an actual value is less than a limit and report and abort if not.

Check to see if the actual value found in a test case is less than the limit value. If the actual value is lesser nothing happens, but if the check fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 675 of file test.h.

◆ NS_TEST_ASSERT_MSG_LT_OR_EQ

#define NS_TEST_ASSERT_MSG_LT_OR_EQ (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) <= (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) < " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that an actual value is less than or equal to a limit and report and abort if not.

Check to see if the actual value found in a test case is less than or equal to the limit value. If the actual value is lesser or equal nothing happens, but if the check fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 712 of file test.h.

◆ NS_TEST_ASSERT_MSG_NE

#define NS_TEST_ASSERT_MSG_NE (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) != (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) != " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE; \
} \
} while (false)

Test that an actual and expected (limit) value are not equal and report and abort if not.

Check to see if the expected (limit) value is not equal to the actual value found in a test case. If the two values are not equal nothing happens, but if the comparison fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream, for example:

NS_TEST_ASSERT_MSG_NE (result, false,
"cannot open file " << filename << " in test");
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition: test.h:542

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the value that actual is tested against.
[in]msgMessage that is output if the test does not pass.

Definition at line 542 of file test.h.

◆ NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL

#define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) != (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) != " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
CONTINUE_ON_FAILURE_RETURNS_BOOL; \
} \
} while (false)

Test that an actual and expected (limit) value are not equal and report and abort if not.

Check to see if the expected (limit) value is not equal to the actual value found in a test case. If the two values are equal nothing happens, but if the comparison fails, an error is reported in a consistent way and the execution of the current test case is aborted.

The message is interpreted as a stream, for example:

"cannot open file " << filename << " in test");
#define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition: test.h:589

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]msgMessage that is output if the test does not pass.

This function returns a Boolean value.

Definition at line 589 of file test.h.

◆ NS_TEST_EXPECT_MSG_EQ

#define NS_TEST_EXPECT_MSG_EQ (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) == (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) == " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
} \
} while (false)

Test that an actual and expected (limit) value are equal and report if not.

Check to see if the expected (lmit) value is equal to the actual value found in a test case. If the two values are equal nothing happens, but if the comparison fails, an error is reported in a consistent way. EXPECT* macros do not return if an error is detected.

The message is interpreted as a stream, for example:

NS_TEST_EXPECT_MSG_EQUAL (result, true,
"cannot open file " << filename << " in test");

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]msgMessage that is output if the test does not pass.
Warning
Do not use this macro if you are comparing floating point numbers (float or double) as it is unlikely to do what you expect. Use NS_TEST_EXPECT_MSG_EQ_TOL instead.

Definition at line 240 of file test.h.

◆ NS_TEST_EXPECT_MSG_EQ_TOL

#define NS_TEST_EXPECT_MSG_EQ_TOL (   actual,
  limit,
  tol,
  msg 
)
Value:
do { \
if ((actual) > (limit) + (tol) || (actual) < (limit) - (tol)) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit << " +- " << tol; \
std::ostringstream condStream; \
condStream << #actual << " (actual) < " << #limit \
<< " (limit) + " << #tol << " (tol) && " \
<< #actual << " (actual) > " << #limit \
<< " (limit) - " << #tol << " (tol)"; \
ReportTestFailure (condStream.str (), actualStream.str (), \
limitStream.str (), msgStream.str (), \
__FILE__, __LINE__); \
} \
} while (false)

Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if not.

Check to see if the expected (limit) value is equal to the actual value found in a test case to some tolerance. This is not the same thing as asking if two floating point are equal to within some epsilon, but is useful for that case. This assertion is geared toward more of a measurement problem. Consider measuring a physical rod of some kind that you have ordered. You need to determine if it is "good." You want to measure the rod to an arbitrary precision of sixteen significant figures, you will measure the rod to determine if its length is within the tolerances you provided. For example, 12.00 inches plus or minus .005 inch may be just fine.

In ns-3, you might want to measure a signal to noise ratio and check to see if the answer is what you expect. If you naively measure (double)1128.93 and compare this number with a constant 1128.93 you are almost certainly going to have your test fail because of floating point rounding errors. We provide a floating point comparison function ns3::TestDoubleIsEqual() but you will probably quickly find that is not what you want either. It may turn out to be the case that when you measured an SNR that printed as 1128.93, what was actually measured was something more like 1128.9287653857625442 for example. Given that the double epsilon is on the order of 0.0000000000000009, you would need to provide sixteen significant figures of expected value for this kind of test to pass even with a typical test for floating point "approximate equality." That is clearly not required or desired. You really want to be able to provide 1128.93 along with a tolerance just like you provided 12 inches +- 0.005 inch above.

This assertion is designed for real measurements by taking into account measurement tolerances. By doing so it also automatically compensates for floating point rounding errors. If you really want to check floating point equality down to the numeric_limits<double>::epsilon () range, consider using ns3::TestDoubleIsEqual().

Note
Mixing signed and unsigned types can lead to misleading results.

The message is interpreted as a stream, for example:

NS_TEST_EXPECT_MSG_EQ_TOL (snr, 1128.93, 0.005,
"wrong snr (" << snr << ") in test");
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if ...
Definition: test.h:491

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the expected value of the test.
[in]tolTolerance of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 491 of file test.h.

◆ NS_TEST_EXPECT_MSG_GT

#define NS_TEST_EXPECT_MSG_GT (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) > (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) > " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
} \
} while (false)

Test that an actual value is greater than a limit and report if not.

Check to see if the actual value found in a test case is greater than the limit value. If the actual value is greater nothing happens, but if the check fails, an error is reported in a consistent way. EXPECT* macros do not return if an error is detected.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 899 of file test.h.

◆ NS_TEST_EXPECT_MSG_GT_OR_EQ

#define NS_TEST_EXPECT_MSG_GT_OR_EQ (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) >= (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) > " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
} \
} while (false)

Test that an actual value is greater than or equal to limit and report if not.

Check to see if the actual value found in a test case is greater than or equal to the limit value. If the actual value is greater nothing happens, but if the check fails, an error is reported in a consistent way. EXPECT* macros do not return if an error is detected.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 935 of file test.h.

◆ NS_TEST_EXPECT_MSG_LT

#define NS_TEST_EXPECT_MSG_LT (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) < (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) < " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
} \
} while (false)

Test that an actual value is less than a limit and report if not.

Check to see if the actual value found in a test case is less than the limit value. If the actual value is lesser nothing happens, but if the check fails, an error is reported in a consistent way. EXPECT* macros do not return if an error is detected.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 748 of file test.h.

◆ NS_TEST_EXPECT_MSG_LT_OR_EQ

#define NS_TEST_EXPECT_MSG_LT_OR_EQ (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) <= (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) < " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
} \
} while (false)

Test that an actual value is less than or equal to a limit and report if not.

Check to see if the actual value found in a test case is less than or equal to the limit value. If the actual value is lesser or equal nothing happens, but if the check fails, an error is reported in a consistent way. EXPECT* macros do not return if an error is detected.

The message is interpreted as a stream.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the limit value of the test.
[in]msgMessage that is output if the test does not pass.

Definition at line 785 of file test.h.

◆ NS_TEST_EXPECT_MSG_NE

#define NS_TEST_EXPECT_MSG_NE (   actual,
  limit,
  msg 
)
Value:
do { \
if (!((actual) != (limit))) \
{ \
ASSERT_ON_FAILURE; \
std::ostringstream msgStream; \
msgStream << msg; \
std::ostringstream actualStream; \
actualStream << actual; \
std::ostringstream limitStream; \
limitStream << limit; \
ReportTestFailure (std::string (#actual) + " (actual) != " + \
std::string (#limit) + " (limit)", \
actualStream.str (), limitStream.str (), \
msgStream.str (), __FILE__, __LINE__); \
} \
} while (false)

Test that an actual and expected (limit) value are not equal and report if not.

Check to see if the expected (limit) value is not equal to the actual value found in a test case. If the two values are not equal nothing happens, but if the comparison fails, an error is reported in a consistent way. EXPECT* macros do not return if an error is detected.

The message is interpreted as a stream, for example:

NS_TEST_EXPECT_MSG_NE (result, false,
"cannot open file " << filename << " in test");
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Definition: test.h:636

is legal.

Parameters
[in]actualExpression for the actual value found during the test.
[in]limitExpression for the value that actual is tested against.
[in]msgMessage that is output if the test does not pass.
Warning
Do not use this macro if you are comparing floating point numbers (float or double). Use NS_TEST_EXPECT_MSG_FLNE instead.

Definition at line 636 of file test.h.

Function Documentation

◆ TestDoubleIsEqual()

bool ns3::TestDoubleIsEqual ( const double  a,
const double  b,
const double  epsilon = std::numeric_limits< double >::epsilon() 
)

Compare two double precision floating point numbers and declare them equal if they are within some epsilon of each other.

Approximate comparison of floating point numbers near equality is trickier than one may expect and is well-discussed in the literature. Basic strategies revolve around a suggestion by Knuth to compare the floating point numbers as binary integers, supplying a maximum difference between them . This max difference is specified in Units in the Last Place (ulps) or a floating point epsilon.

This routine is based on the GNU Scientific Library function gsl_fcmp.

Parameters
[in]aThe first of double precision floating point numbers to compare
[in]bThe second of double precision floating point numbers to compare
[in]epsilonThe tolerance to use in the comparison.
Returns
Returns true if the doubles are equal to a precision defined by epsilon

Definition at line 44 of file test.cc.

References epsilon, max, and NS_LOG_FUNCTION.

Referenced by WifiOfdmMaskSlopesTestCase::InterpolateAndAppendValues(), and TestInterBssConstantObssPdAlgo::NotifyPhyTxBegin().

+ Here is the caller graph for this function: