The Sparta Modeling Framework
Loading...
Searching...
No Matches
SpartaTester.hpp File Reference

File that defines the SpartaTester class and testing Macros. More...

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <inttypes.h>
#include <stdexcept>
#include <string>
#include <set>
#include <boost/algorithm/string/predicate.hpp>
#include "sparta/utils/Colors.hpp"
#include "simdb/utils/MathUtils.hpp"

Go to the source code of this file.

Classes

class  sparta::SpartaTester
 A simple testing class. More...
 

Namespaces

namespace  sparta
 Macros for handling exponential backoff.
 

Macros

#define TEST_INIT
 Initialized the test. Should be placed OUTSIDE of a code block SOMEWHERE in the test source.
 
#define EXPECT_REACHED()   sparta::SpartaTester::getInstance()->reachedMethod(__FUNCTION__)
 Add this method to be checked against whether or not it was called at least once. This macro can be placed anywhere inside the function expected to be called.
 
#define ENSURE_ALL_REACHED(x)   sparta::SpartaTester::getInstance()->expectAllReached(x, __LINE__, __FUNCTION__)
 make sure that the same number of methods were reached as were expected by this test.
 
#define EXPECT_TRUE(x)   sparta::SpartaTester::getInstance()->expect((x), #x, __LINE__, __FILE__)
 Determine if the block x evaluates to true.
 
#define EXPECT_EQUAL(x, y)   sparta::SpartaTester::getInstance()->expectEqual((x), (y), true, #x, __LINE__, __FILE__)
 Determine if the block x is equal (using operator= on x) to y.
 
#define EXPECT_NOTEQUAL(x, y)   sparta::SpartaTester::getInstance()->expectEqual((x), (y), false, #x, __LINE__, __FILE__)
 Determine if the block x is not equal (using operator= on x) to y.
 
#define EXPECT_WITHIN_TOLERANCE(x, y, tol)
 Determine if the block x is equal (using operator= on x) to y within a specified tolerance.
 
#define EXPECT_WITHIN_EPSILON(x, y)
 Determine if the block x is equal (using operator= on x) to y within 2x the machine epsilon, as determined by std::numeric_limits<T>::epsilon()
 
#define EXPECT_FALSE(x)   sparta::SpartaTester::getInstance()->expect(!(x), #x, __LINE__, __FILE__)
 Determine if the block x evaluates to false.
 
#define EXPECT_THROW(x)
 Determine if the block x correctly throws an exception.
 
#define EXPECT_THROW_MSG_SHORT(x, expected_msg)
 Determine if the block x correctly throws an exception with the given message (no line number/file)
 
#define EXPECT_THROW_MSG_LONG(x, expected_msg)
 Determine if the block x correctly throws an exception with the given message on the file and line number.
 
#define EXPECT_THROW_MSG_CONTAINS(x, expected_msg)
 
#define EXPECT_NOTHROW(x)
 Determine if the block x throws an exception incorrectly.
 
#define EXPECT_FILES_EQUAL(a, b)
 Determine if the block a and b contain the same exact data with the exception of lines beginning with '#'.
 
#define EXPECT_FILES_NOTEQUAL(a, b)
 Determine if the block a and b contain different data with the exception of lines beginning with '#'.
 
#define ERROR_CODE   sparta::SpartaTester::getErrorCode()
 The number of errors found in the testing.
 
#define REPORT_ERROR
 Prints the error code with a nice pretty message.
 

Detailed Description

File that defines the SpartaTester class and testing Macros.

Definition in file SpartaTester.hpp.

Macro Definition Documentation

◆ ENSURE_ALL_REACHED

#define ENSURE_ALL_REACHED (   x)    sparta::SpartaTester::getInstance()->expectAllReached(x, __LINE__, __FUNCTION__)

make sure that the same number of methods were reached as were expected by this test.

Parameters
xthe number of unique methods that you expect to be reached at least once. x should equal the number of times EXPECT_REACHED() is placed throughout the test.

Definition at line 429 of file SpartaTester.hpp.

◆ ERROR_CODE

#define ERROR_CODE   sparta::SpartaTester::getErrorCode()

The number of errors found in the testing.

Definition at line 714 of file SpartaTester.hpp.

◆ EXPECT_EQUAL

#define EXPECT_EQUAL (   x,
 
)    sparta::SpartaTester::getInstance()->expectEqual((x), (y), true, #x, __LINE__, __FILE__)

Determine if the block x is equal (using operator= on x) to y.

Note
Types must match. Comparison is made at run-time.
Precondition
x must be printable to cout/cerr using the insertion operator

Value of x and y, if not equal, is printed in error message.

Example usage:

if(EXPECT_EQUAL((const char*)x, "12345")) {
std::cout << "Wadda know, it they were the same!" << std::endl;
}
else {
std::cerr << "Values differed" << std::endl;
}
#define EXPECT_EQUAL(x, y)
Determine if the block x is equal (using operator= on x) to y.

Definition at line 466 of file SpartaTester.hpp.

◆ EXPECT_FALSE

#define EXPECT_FALSE (   x)    sparta::SpartaTester::getInstance()->expect(!(x), #x, __LINE__, __FILE__)

Determine if the block x evaluates to false.

Example usage:

if(EXPECT_FALSE(false) == false) {
std::cout << "Wadda know, false IS false!" << std::endl;
}
else {
std::cerr << "False isn't false anymore?!? What's this world coming to?!" << std::endl;
}
#define EXPECT_FALSE(x)
Determine if the block x evaluates to false.

Definition at line 550 of file SpartaTester.hpp.

◆ EXPECT_FILES_EQUAL

#define EXPECT_FILES_EQUAL (   a,
 
)
Value:
{ \
sparta::SpartaTester::getInstance()->expectFilesEqual(a, b, true, __LINE__, __FILE__); \
}

Determine if the block a and b contain the same exact data with the exception of lines beginning with '#'.

Example usage:

EXPECT_FILES_EQUAL("a.out.golden", "b.out");
#define EXPECT_FILES_EQUAL(a, b)
Determine if the block a and b contain the same exact data with the exception of lines beginning with...

Definition at line 692 of file SpartaTester.hpp.

◆ EXPECT_FILES_NOTEQUAL

#define EXPECT_FILES_NOTEQUAL (   a,
 
)
Value:
{ \
sparta::SpartaTester::getInstance()->expectFilesEqual(a, b, false, __LINE__, __FILE__); \
}

Determine if the block a and b contain different data with the exception of lines beginning with '#'.

Example usage:

EXPECT_FILES_NOTEQUAL("a.out.golden", "b.out");
#define EXPECT_FILES_NOTEQUAL(a, b)
Determine if the block a and b contain different data with the exception of lines beginning with '#'.

Definition at line 706 of file SpartaTester.hpp.

◆ EXPECT_NOTEQUAL

#define EXPECT_NOTEQUAL (   x,
 
)    sparta::SpartaTester::getInstance()->expectEqual((x), (y), false, #x, __LINE__, __FILE__)

Determine if the block x is not equal (using operator= on x) to y.

Note
Types must match exactly. Comparison is made at run-time.
Precondition
x must be printable to cout/cerr using the insertion operator

Value of x and y, if equal, is printed in error message.

Example usage:

if(EXPECT_NOTEQUAL((const char*)x, "12345")) {
std::cout << "Wadda know, they were different" << std::endl;
}
else {
std::cerr << "Values were same" << std::endl;
}
#define EXPECT_NOTEQUAL(x, y)
Determine if the block x is not equal (using operator= on x) to y.

Definition at line 486 of file SpartaTester.hpp.

◆ EXPECT_NOTHROW

#define EXPECT_NOTHROW (   x)
Value:
{ \
bool did_it_throw = false; \
std::string exception_what; \
try { x; } \
catch(std::exception& e) \
{ did_it_throw = true; \
exception_what = e.what(); \
} \
catch(...) \
{ did_it_throw = true; } \
if(did_it_throw == true) { \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, exception_what.c_str()); \
} \
}

Determine if the block x throws an exception incorrectly.

Example usage:

EXPECT_NOTHROW(throw 10);
#define EXPECT_NOTHROW(x)
Determine if the block x throws an exception incorrectly.

Definition at line 667 of file SpartaTester.hpp.

◆ EXPECT_REACHED

#define EXPECT_REACHED ( )    sparta::SpartaTester::getInstance()->reachedMethod(__FUNCTION__)

Add this method to be checked against whether or not it was called at least once. This macro can be placed anywhere inside the function expected to be called.

Definition at line 420 of file SpartaTester.hpp.

◆ EXPECT_THROW

#define EXPECT_THROW (   x)
Value:
{ \
bool did_it_throw = false; \
try {x;} \
catch(...) \
{ did_it_throw = true; } \
if(did_it_throw == false) { \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__); \
} \
}

Determine if the block x correctly throws an exception.

Example usage:

EXPECT_THROW(throw 10);
#define EXPECT_THROW(x)
Determine if the block x correctly throws an exception.

Definition at line 561 of file SpartaTester.hpp.

◆ EXPECT_THROW_MSG_CONTAINS

#define EXPECT_THROW_MSG_CONTAINS (   x,
  expected_msg 
)
Value:
{ \
bool did_it_throw = false; \
try { x; } \
{ did_it_throw = true; \
if (boost::algorithm::contains(ex.what(), expected_msg) != true) { \
std::cerr << "Expected msg: " << expected_msg << std::endl; \
std::cerr << "Actual msg: " << ex.what() << std::endl; \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, ex.what()); \
} \
} \
if(did_it_throw == false) { \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, "did not throw"); \
} \
}
Used to construct and throw a standard C++ exception. Inherits from std::exception.

Definition at line 639 of file SpartaTester.hpp.

◆ EXPECT_THROW_MSG_LONG

#define EXPECT_THROW_MSG_LONG (   x,
  expected_msg 
)
Value:
{ \
bool did_it_throw = false; \
try { x; } \
{ did_it_throw = true; \
if (strcmp(expected_msg, ex.what()) != 0) { \
std::cerr << "Expected msg: " << expected_msg << std::endl; \
std::cerr << "Actual msg: " << ex.what() << std::endl; \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, ex.what()); \
} \
} \
if(did_it_throw == false) { \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, "did not throw"); \
} \
}

Determine if the block x correctly throws an exception with the given message on the file and line number.

Example usage:

// The thrower:
void foo() {
throw sparta::SpartaException("Hello");
}
// The message:
// ex.what() -> "Hello: in file: 'blah.cpp', on line: 123"
EXPECT_THROW_MSG_LONG(ex.what(), "Hello: in file: 'blah.cpp', on line: 123");
#define EXPECT_THROW_MSG_LONG(x, expected_msg)
Determine if the block x correctly throws an exception with the given message on the file and line nu...

Definition at line 623 of file SpartaTester.hpp.

◆ EXPECT_THROW_MSG_SHORT

#define EXPECT_THROW_MSG_SHORT (   x,
  expected_msg 
)
Value:
{ \
bool did_it_throw = false; \
try { x; } \
{ did_it_throw = true; \
if (strcmp(expected_msg, ex.rawReason().c_str()) != 0) { \
std::cerr << "Expected msg: " << expected_msg << std::endl; \
std::cerr << "Actual msg: " << ex.what() << std::endl; \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, ex.what()); \
} \
} \
if(did_it_throw == false) { \
sparta::SpartaTester::getInstance()->throwTestFailed(#x, __LINE__, __FILE__, "did not throw"); \
} \
}

Determine if the block x correctly throws an exception with the given message (no line number/file)

Example usage:

// The thrower:
void foo() {
throw sparta::SpartaException("Hello");
}
// The message:
// ex.what() -> "Hello: in file: 'blah.cpp', on line: 123"
EXPECT_THROW_MSG_SHORT(ex.what(), "Hello");
#define EXPECT_THROW_MSG_SHORT(x, expected_msg)
Determine if the block x correctly throws an exception with the given message (no line number/file)

Definition at line 589 of file SpartaTester.hpp.

◆ EXPECT_TRUE

#define EXPECT_TRUE (   x)    sparta::SpartaTester::getInstance()->expect((x), #x, __LINE__, __FILE__)

Determine if the block x evaluates to true.

Example usage:

if(EXPECT_TRUE(true)) {
std::cout << "Wadda know, it was true!" << std::endl;
}
else {
std::cerr << "True isn't true anymore?!? What's this world coming to?!" << std::endl;
}
#define EXPECT_TRUE(x)
Determine if the block x evaluates to true.

Definition at line 446 of file SpartaTester.hpp.

◆ EXPECT_WITHIN_EPSILON

#define EXPECT_WITHIN_EPSILON (   x,
 
)
Value:
sparta::SpartaTester::getInstance()-> \
expectEqualWithinTolerance((x), (y), \
(std::numeric_limits<decltype(x)>::epsilon()), #x, __LINE__, __FILE__)

Determine if the block x is equal (using operator= on x) to y within 2x the machine epsilon, as determined by std::numeric_limits<T>::epsilon()

Note
Types must match exactly. Comparison is made at run-time.
Types compared cannot be integral.
Precondition
x must be printable to cout/cerr using the insertion operator

If x and y differ by strictly more than (>) the specified tolerance, the x and y values and the tolerance value will be printed in an error message.

Example usage:

std::cout << "The x and y values were within machine epsilon of each other" << std::endl;
}
else {
std::cerr << "The x and y values differed by more than machine epsilon" << std::endl;
}
#define EXPECT_WITHIN_EPSILON(x, y)
Determine if the block x is equal (using operator= on x) to y within 2x the machine epsilon,...

Definition at line 532 of file SpartaTester.hpp.

◆ EXPECT_WITHIN_TOLERANCE

#define EXPECT_WITHIN_TOLERANCE (   x,
  y,
  tol 
)
Value:
sparta::SpartaTester::getInstance()-> \
expectEqualWithinTolerance((x), (y), (tol), #x, __LINE__, __FILE__)

Determine if the block x is equal (using operator= on x) to y within a specified tolerance.

Note
Types must match exactly. Comparison is made at run-time.
Precondition
x must be printable to cout/cerr using the insertion operator

If x and y differ by more than (>) the specified tolerance, the x and y values and the tolerance value will be printed in an error message.

Example usage:

if(EXPECT_WITHIN_TOLERANCE(x, y, 0.02)) {
std::cout << "The x and y values were within 0.02 of each other" << std::endl;
}
else {
std::cerr << "The x and y values differed by more than 0.02" << std::endl;
}
#define EXPECT_WITHIN_TOLERANCE(x, y, tol)
Determine if the block x is equal (using operator= on x) to y within a specified tolerance.

Definition at line 508 of file SpartaTester.hpp.

◆ REPORT_ERROR

#define REPORT_ERROR
Value:
if(ERROR_CODE != 0) { \
std::cout << std::dec << "\n" << SPARTA_UNMANAGED_COLOR_BRIGHT_RED << ERROR_CODE \
<< " ERROR(S) found during test.\n" << SPARTA_UNMANAGED_COLOR_NORMAL << std::endl; \
} else { \
std::cout << std::dec << "\n" \
<< "TESTS PASSED -- No errors found during test.\n" << std::endl; \
}
#define SPARTA_UNMANAGED_COLOR_NORMAL
Definition Colors.hpp:18
#define ERROR_CODE
The number of errors found in the testing.

Prints the error code with a nice pretty message.

Note
This is separate from returning ERROR_CODE, which must be done after this macro. See the example for sparta::SpartaTester . The reason for this separation is so that errors can be reported before teardown, which could fail uncatchably (i.e. segfault) when there are caught errors earlier in the test (e.g. dangling pointers).

Example:

int main() }
// ...
performDangerousCleanup();
return ERROR_CODE;
}
#define REPORT_ERROR
Prints the error code with a nice pretty message.

Definition at line 737 of file SpartaTester.hpp.

◆ TEST_INIT

#define TEST_INIT

Initialized the test. Should be placed OUTSIDE of a code block SOMEWHERE in the test source.

Definition at line 412 of file SpartaTester.hpp.