A Discrete-Event Network Simulator Home Tutorials  ▼ Docs    ▼ Develop ▼ 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  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::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)   NS_TEST_ASSERT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_GT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_LT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_GT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_LT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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)   NS_TEST_EXPECT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)
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

 #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 77 of file test.h.

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

If we shouldn't continue on errors, return.

Definition at line 89 of file test.h.

 #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 101 of file test.h.

 #define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL ( actual, limit, msg ) NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the expected value of the test. [in] msg Message 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 227 of file test.h.

Referenced by UanTest::DoPhyTests(), and Ns2MobilityHelperTest::WriteTrace().

 #define NS_TEST_ASSERT_MSG_EQ_TOL ( actual, limit, tol, msg ) NS_TEST_ASSERT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)

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

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");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the expected value of the test. [in] tol Tolerance of the test. [in] msg Message that is output if the test does not pass.

Definition at line 380 of file test.h.

 #define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL ( actual, limit, tol, msg ) NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)

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

The message is interpreted as a stream, for example:

"wrong snr (" << snr << ") in test");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the expected value of the test. [in] tol Tolerance of the test. [in] msg Message that is output if the test does not pass.

This function returns a Boolean value.

Definition at line 473 of file test.h.

 #define NS_TEST_ASSERT_MSG_GT ( actual, limit, msg ) NS_TEST_ASSERT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 997 of file test.h.

 #define NS_TEST_ASSERT_MSG_GT_OR_EQ ( actual, limit, msg ) NS_TEST_ASSERT_MSG_GT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 1018 of file test.h.

 #define NS_TEST_ASSERT_MSG_LT ( actual, limit, msg ) NS_TEST_ASSERT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 811 of file test.h.

 #define NS_TEST_ASSERT_MSG_LT_OR_EQ ( actual, limit, msg ) NS_TEST_ASSERT_MSG_LT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 832 of file test.h.

 #define NS_TEST_ASSERT_MSG_NE ( actual, limit, msg ) NS_TEST_ASSERT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the value that actual is tested against. [in] msg Message 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_ASSERT_MSG_FLNE instead.

Definition at line 624 of file test.h.

 #define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL ( actual, limit, msg ) NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the expected value of the test. [in] msg Message 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_ASSERT_MSG_FLNE instead.

This function returns a Boolean value.

Definition at line 682 of file test.h.

Referenced by Ns2MobilityHelperTest::CheckInitialPositions().

 #define NS_TEST_EXPECT_MSG_EQ_TOL ( actual, limit, tol, msg ) NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (actual, limit, tol, msg, __FILE__, __LINE__)

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

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");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the expected value of the test. [in] tol Tolerance of the test. [in] msg Message that is output if the test does not pass.

Definition at line 565 of file test.h.

 #define NS_TEST_EXPECT_MSG_GT ( actual, limit, msg ) NS_TEST_EXPECT_MSG_GT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 1090 of file test.h.

 #define NS_TEST_EXPECT_MSG_GT_OR_EQ ( actual, limit, msg ) NS_TEST_EXPECT_MSG_GT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 1111 of file test.h.

 #define NS_TEST_EXPECT_MSG_LT ( actual, limit, msg ) NS_TEST_EXPECT_MSG_LT_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 903 of file test.h.

 #define NS_TEST_EXPECT_MSG_LT_OR_EQ ( actual, limit, msg ) NS_TEST_EXPECT_MSG_LT_OR_EQ_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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] actual Expression for the actual value found during the test. [in] limit Expression for the limit value of the test. [in] msg Message that is output if the test does not pass.

Definition at line 924 of file test.h.

 #define NS_TEST_EXPECT_MSG_NE ( actual, limit, msg ) NS_TEST_EXPECT_MSG_NE_INTERNAL (actual, limit, msg, __FILE__, __LINE__)

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");

is legal.

Parameters
 [in] actual Expression for the actual value found during the test. [in] limit Expression for the value that actual is tested against. [in] msg Message 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 739 of file test.h.

## Function Documentation

 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] a The first of double precision floating point numbers to compare [in] b The second of double precision floating point numbers to compare [in] epsilon The 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 max, and NS_LOG_FUNCTION.