A Discrete-Event Network Simulator
API
ns3::ExampleAsTestCase Class Reference

Execute an example program as a test, by comparing the output to a reference file. More...

#include "example-as-test.h"

+ Inheritance diagram for ns3::ExampleAsTestCase:
+ Collaboration diagram for ns3::ExampleAsTestCase:

Public Member Functions

 ExampleAsTestCase (const std::string name, const std::string program, const std::string dataDir, const std::string args="", const bool shouldNotErr=true)
 Constructor. More...
 
 ~ExampleAsTestCase () override
 Destructor. More...
 
void DoRun () override
 Implementation to actually run this TestCase. More...
 
virtual std::string GetCommandTemplate () const
 Customization point for more complicated patterns to invoke the example program. More...
 
virtual std::string GetPostProcessingCommand () const
 Customization point for tests requiring post-processing of stdout. More...
 
- Public Member Functions inherited from ns3::TestCase
 TestCase (const TestCase &)=delete
 
virtual ~TestCase ()
 Destructor. More...
 
std::string GetName () const
 
TestCaseoperator= (const TestCase &)=delete
 

Protected Attributes

std::string m_args
 Any additional arguments to the program. More...
 
std::string m_dataDir
 The source directory for the test. More...
 
std::string m_program
 The program to run. More...
 
bool m_shouldNotErr
 Whether error return status is a test failure. More...
 

Additional Inherited Members

- Public Types inherited from ns3::TestCase
enum  TestDuration { QUICK = 1 , EXTENSIVE = 2 , TAKES_FOREVER = 3 }
 How long the test takes to execute. More...
 
- Protected Member Functions inherited from ns3::TestCase
 TestCase (std::string name)
 Constructor. More...
 
void AddTestCase (TestCase *testCase, TestDuration duration=QUICK)
 Add an individual child TestCase to this test suite. More...
 
TestCaseGetParent () const
 Get the parent of this TestCase. More...
 
bool IsStatusFailure () const
 Check if any tests failed. More...
 
bool IsStatusSuccess () const
 Check if all tests passed. More...
 
void SetDataDir (std::string directory)
 Set the data directory where reference trace files can be found. More...
 
void ReportTestFailure (std::string cond, std::string actual, std::string limit, std::string message, std::string file, int32_t line)
 Log the failure of this TestCase. More...
 
bool MustAssertOnFailure () const
 Check if this run should assert on failure. More...
 
bool MustContinueOnFailure () const
 Check if this run should continue on failure. More...
 
std::string CreateDataDirFilename (std::string filename)
 Construct the full path to a file in the data directory. More...
 
std::string CreateTempDirFilename (std::string filename)
 Construct the full path to a file in a temporary directory. More...
 

Detailed Description

Execute an example program as a test, by comparing the output to a reference file.

User can subclass and override the GetCommandTemplate() and GetPostProcessingCommand() methods if more complex example invocation patterns are required.

See also
examples-as-tests-test-suite.cc

Definition at line 48 of file example-as-test.h.

Constructor & Destructor Documentation

◆ ExampleAsTestCase()

ns3::ExampleAsTestCase::ExampleAsTestCase ( const std::string  name,
const std::string  program,
const std::string  dataDir,
const std::string  args = "",
const bool  shouldNotErr = true 
)

Constructor.

Parameters
[in]nameThe test case name, typically the program name and summary of the arguments, such as my-example-foo
[in]programThe actual example program names, such as my-example
[in]dataDirThe location of the reference file. This is normally provided by the symbol NS_TEST_SOURCEDIR in the module-examples-test-suite.cc file. The reference file should be named after the test case name, for example my-example-foo.log. If you use the --update argument to test.py or test-runner the reference file will be created with the correct name.
[in]argsAny additional arguments to the program.
[in]shouldNotErrWhether an error return status should be considered a test failure. This is useful when testing error detection which might return a non-zero status. The output (on std::cout and std::cerr) will be compared to the reference logs as normal.

◆ ~ExampleAsTestCase()

ns3::ExampleAsTestCase::~ExampleAsTestCase ( )
override

Destructor.

Member Function Documentation

◆ DoRun()

void ns3::ExampleAsTestCase::DoRun ( )
overridevirtual

Implementation to actually run this TestCase.

Subclasses should override this method to conduct their tests.

Implements ns3::TestCase.

◆ GetCommandTemplate()

virtual std::string ns3::ExampleAsTestCase::GetCommandTemplate ( ) const
virtual

Customization point for more complicated patterns to invoke the example program.

Returns
The string to be given to the ns3 --command-template= argument.

Reimplemented in MpiTestCase.

◆ GetPostProcessingCommand()

virtual std::string ns3::ExampleAsTestCase::GetPostProcessingCommand ( ) const
virtual

Customization point for tests requiring post-processing of stdout.

For example to sort return "| sort"

One common case is to mask memory addresses, which can change when things are built on different platforms, recompiled locally, or even from run to run. A simple post-processing filter could be

‘"| sed -E 's/0x[0-9a-fA-F]{8,}/0x-address/g’"`

Default is "", no additional processing.

Returns
The string of post-processing commands

Reimplemented in MpiTestCase, and ns3::tests::CommandLineExampleTestCase.

Member Data Documentation

◆ m_args

std::string ns3::ExampleAsTestCase::m_args
protected

Any additional arguments to the program.

Definition at line 113 of file example-as-test.h.

Referenced by MpiTestCase::GetCommandTemplate().

◆ m_dataDir

std::string ns3::ExampleAsTestCase::m_dataDir
protected

The source directory for the test.

Definition at line 112 of file example-as-test.h.

◆ m_program

std::string ns3::ExampleAsTestCase::m_program
protected

The program to run.

Definition at line 111 of file example-as-test.h.

◆ m_shouldNotErr

bool ns3::ExampleAsTestCase::m_shouldNotErr
protected

Whether error return status is a test failure.

Definition at line 114 of file example-as-test.h.


The documentation for this class was generated from the following file: