Ocean
Ocean::Test::Validation Class Reference

This class implements a helper class to validate tests. More...

Public Member Functions

 Validation ()=default
 Default constructor, by default the verified has succeeded. More...
 
 Validation (RandomGenerator &randomGenerator)
 Creates a new validation object associated with a random generator, by default the verified has succeeded. More...
 
 ~Validation ()
 Destructs this validation object. More...
 
void expectTrue (const bool value)
 Informs this validation object that a value is expected to be True. More...
 
void expectTrue (const bool value, const char *file, const int line)
 Informs this validation object that a value is expected to be True. More...
 
void expectFalse (const bool value)
 Informs this validation object that a value is expected to be False. More...
 
void expectFalse (const bool value, const char *file, const int line)
 Informs this validation object that a value is expected to be False. More...
 
template<typename T >
void expectEqual (const T &value0, const T &value1)
 Informs this validation object that a value is expected to be equal to another value. More...
 
template<typename T >
void expectEqual (const T &value0, const T &value1, const char *file, const int line)
 Informs this validation object that a value is expected to be equal to another value. More...
 
template<typename T >
void expectLess (const T &value0, const T &value1)
 Informs this validation object that a value is expected to be less than another value. More...
 
template<typename T >
void expectLess (const T &value0, const T &value1, const char *file, const int line)
 Informs this validation object that a value is expected to be less than another value. More...
 
template<typename T >
void expectLessEqual (const T &value0, const T &value1)
 Informs this validation object that a value is expected to be less than or equal to another value. More...
 
template<typename T >
void expectLessEqual (const T &value0, const T &value1, const char *file, const int line)
 Informs this validation object that a value is expected to be less than or equal to another value. More...
 
template<typename T >
void expectGreater (const T &value0, const T &value1)
 Informs this validation object that a value is expected to be greater than another value. More...
 
template<typename T >
void expectGreater (const T &value0, const T &value1, const char *file, const int line)
 Informs this validation object that a value is expected to be greater than another value. More...
 
template<typename T >
void expectGreaterEqual (const T &value0, const T &value1)
 Informs this validation object that a value is expected to be greater than or equal to another value. More...
 
template<typename T >
void expectGreaterEqual (const T &value0, const T &value1, const char *file, const int line)
 Informs this validation object that a value is expected to be greater than or equal to another value. More...
 
template<typename T >
void expectInsideRange (const T &lower, const T &value, const T &upper)
 Informs this validation object that a value is expected to be inside a range. More...
 
template<typename T >
void expectInsideRange (const T &lower, const T &value, const T &upper, const char *file, const int line)
 Informs this validation object that a value is expected to be inside a range. More...
 
void setFailed ()
 Explicitly sets the validation to be failed. More...
 
void setFailed (const char *file, const int line)
 Explicitly sets the validation to be failed. More...
 
bool succeeded () const
 Returns if this validation has succeeded. More...
 
std::string randomGeneratorOutput () const
 Returns a string containing the random generator's initial seed, if any. More...
 

Protected Member Functions

 Validation (const Validation &)=delete
 Disabled copy constructor. More...
 
void setSucceededFalse ()
 Sets the succeeded state to false. More...
 

Protected Attributes

bool succeeded_ = true
 True, if the validation has succeeded; False, if the validation has failed. More...
 
RandomGeneratorrandomGenerator_ = nullptr
 Optional random generator object which will be used during validation. More...
 
bool succeededChecked_ = false
 True, if the success state of this validation has been checked. More...
 

Detailed Description

This class implements a helper class to validate tests.

The following example shows how to use Validation and calling the functions directly.
In case of an error, no further information will be provided:

bool testFunction()
{
Log::info() << "Running a test ...";
Validation validation;
validation.expectTrue(4 + 4 == 8);
validation.expectFalse(4 + 4 == 7);
validation.expectEqual(4 + 4, 8);
if (4 + 4 == 7)
{
validation.setFailed();
}
Log::info() << "Validation: " << validation;
return validation.succeeded();
}
static MessageObject info()
Returns the message for information messages.
Definition: Messenger.h:1064
Validation()=default
Default constructor, by default the verified has succeeded.

The following example shows how to use Validation and calling the functions directly but also providing FILE and LINE macro parameters.
In case of an error, additional in information about the location will be provided:

bool testFunction()
{
Log::info() << "Running a test ...";
Validation validation;
validation.expectTrue(4 + 4 == 8, __FILE__, __LINE__);
validation.expectFalse(4 + 4 == 7, __FILE__, __LINE__);
validation.expectEqual(4 + 4, 8, __FILE__, __LINE__);
if (4 + 4 == 7)
{
validation.setFailed(__FILE__, __LINE__);
}
Log::info() << "Validation: " << validation;
return validation.succeeded();
}

The following example shows how to use Validation while not calling the object's functions directly but using the corresponding macros.
In case of an error, additional in information about the location will be provided:

bool testFunction()
{
Log::info() << "Running a test ...";
Validation validation;
OCEAN_EXPECT_TRUE(validation, 4 + 4 == 8);
OCEAN_EXPECT_FALSE(validation, 4 + 4 == 7);
OCEAN_EXPECT_EQUAL(validation, 4 + 4, 8);
if (4 + 4 == 7)
{
OCEAN_SET_FAILED(validation);
}
Log::info() << "Validation: " << validation;
return validation.succeeded();
}
See also
PrecisionValidation.

Constructor & Destructor Documentation

◆ Validation() [1/3]

Ocean::Test::Validation::Validation ( )
default

Default constructor, by default the verified has succeeded.

◆ Validation() [2/3]

Ocean::Test::Validation::Validation ( RandomGenerator randomGenerator)
inlineexplicit

Creates a new validation object associated with a random generator, by default the verified has succeeded.

Parameters
randomGeneratorThe random generator which will be used during verification

◆ ~Validation()

Ocean::Test::Validation::~Validation ( )
inline

Destructs this validation object.

◆ Validation() [3/3]

Ocean::Test::Validation::Validation ( const Validation )
protecteddelete

Disabled copy constructor.

Member Function Documentation

◆ expectEqual() [1/2]

template<typename T >
void Ocean::Test::Validation::expectEqual ( const T &  value0,
const T &  value1 
)
inline

Informs this validation object that a value is expected to be equal to another value.

In case the both values are not identical, this validation object will not succeed.

Parameters
value0The first value to compare
value1The second value to compare
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectEqual() [2/2]

template<typename T >
void Ocean::Test::Validation::expectEqual ( const T &  value0,
const T &  value1,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be equal to another value.

In case the both values are not identical, this validation object will not succeed.
This function will also write a message to the error log.

Parameters
value0The first value to compare
value1The second value to compare
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectFalse() [1/2]

void Ocean::Test::Validation::expectFalse ( const bool  value)
inline

Informs this validation object that a value is expected to be False.

In case the value is True, this validation object will not succeed.

Parameters
valueThe value to be expected False
See also
succeeded().

◆ expectFalse() [2/2]

void Ocean::Test::Validation::expectFalse ( const bool  value,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be False.

In case the value is True, this validation object will not succeed.
This function will also write a message to the error log.

Parameters
valueThe value to be expected False
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().

◆ expectGreater() [1/2]

template<typename T >
void Ocean::Test::Validation::expectGreater ( const T &  value0,
const T &  value1 
)
inline

Informs this validation object that a value is expected to be greater than another value.

In case 'value0 > value1' is false, the validation object will not succeed.

Parameters
value0The first value to compare
value1The second value to compare
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectGreater() [2/2]

template<typename T >
void Ocean::Test::Validation::expectGreater ( const T &  value0,
const T &  value1,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be greater than another value.

In case 'value0 > value1' is false, the validation object will not succeed.
This function will also write a message to the error log.

Parameters
value0The first value to compare
value1The second value to compare
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectGreaterEqual() [1/2]

template<typename T >
void Ocean::Test::Validation::expectGreaterEqual ( const T &  value0,
const T &  value1 
)
inline

Informs this validation object that a value is expected to be greater than or equal to another value.

In case 'value0 >= value1' is false, the validation object will not succeed.

Parameters
value0The first value to compare
value1The second value to compare
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectGreaterEqual() [2/2]

template<typename T >
void Ocean::Test::Validation::expectGreaterEqual ( const T &  value0,
const T &  value1,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be greater than or equal to another value.

In case 'value0 >= value1' is false, the validation object will not succeed.
This function will also write a message to the error log.

Parameters
value0The first value to compare
value1The second value to compare
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectInsideRange() [1/2]

template<typename T >
void Ocean::Test::Validation::expectInsideRange ( const T &  lower,
const T &  value,
const T &  upper 
)
inline

Informs this validation object that a value is expected to be inside a range.

In case 'lower <= value && value <= upper' is false, the validation object will not succeed.

Parameters
lowerThe lower bound of the range (inclusive)
valueThe value to be checked
upperThe upper bound of the range (inclusive)
See also
succeeded().
Template Parameters
TThe data type of the value

◆ expectInsideRange() [2/2]

template<typename T >
void Ocean::Test::Validation::expectInsideRange ( const T &  lower,
const T &  value,
const T &  upper,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be inside a range.

In case 'lower <= value && value <= upper' is false, the validation object will not succeed. This function will also write a message to the error log.

Parameters
lowerThe lower bound of the range (inclusive)
valueThe value to be checked
upperThe upper bound of the range (inclusive)
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().
Template Parameters
TThe data type of the value

◆ expectLess() [1/2]

template<typename T >
void Ocean::Test::Validation::expectLess ( const T &  value0,
const T &  value1 
)
inline

Informs this validation object that a value is expected to be less than another value.

In case 'value0 < value1' is false, the validation object will not succeed.

Parameters
value0The first value to compare
value1The second value to compare
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectLess() [2/2]

template<typename T >
void Ocean::Test::Validation::expectLess ( const T &  value0,
const T &  value1,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be less than another value.

In case 'value0 < value1' is false, the validation object will not succeed.
This function will also write a message to the error log.

Parameters
value0The first value to compare
value1The second value to compare
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectLessEqual() [1/2]

template<typename T >
void Ocean::Test::Validation::expectLessEqual ( const T &  value0,
const T &  value1 
)
inline

Informs this validation object that a value is expected to be less than or equal to another value.

In case 'value0 <= value1' is false, the validation object will not succeed.

Parameters
value0The first value to compare
value1The second value to compare
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectLessEqual() [2/2]

template<typename T >
void Ocean::Test::Validation::expectLessEqual ( const T &  value0,
const T &  value1,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be less than or equal to another value.

In case 'value0 <= value1' is false, the validation object will not succeed.
This function will also write a message to the error log.

Parameters
value0The first value to compare
value1The second value to compare
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().
Template Parameters
TThe data type of both values

◆ expectTrue() [1/2]

void Ocean::Test::Validation::expectTrue ( const bool  value)
inline

Informs this validation object that a value is expected to be True.

In case the value is False, this validation object will not succeed.

Parameters
valueThe value to be expected True
See also
succeeded().

◆ expectTrue() [2/2]

void Ocean::Test::Validation::expectTrue ( const bool  value,
const char *  file,
const int  line 
)
inline

Informs this validation object that a value is expected to be True.

In case the value is False, this validation object will not succeed.
This function will also write a message to the error log.

Parameters
valueThe value to be expected True
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().

◆ randomGeneratorOutput()

std::string Ocean::Test::Validation::randomGeneratorOutput ( ) const
inline

Returns a string containing the random generator's initial seed, if any.

Returns
The string with initial seed test, empty if no random generator is associated with this validation object

◆ setFailed() [1/2]

void Ocean::Test::Validation::setFailed ( )
inline

Explicitly sets the validation to be failed.

See also
succeeded().

◆ setFailed() [2/2]

void Ocean::Test::Validation::setFailed ( const char *  file,
const int  line 
)
inline

Explicitly sets the validation to be failed.

This function will also write a message to the error log.

Parameters
fileThe source file in which the function call happens, e.g., FILE, must be valid
lineThe line in the source file in which the function call happens, e.g., LINE, must be valid
See also
succeeded().

◆ setSucceededFalse()

void Ocean::Test::Validation::setSucceededFalse ( )
inlineprotected

Sets the succeeded state to false.

◆ succeeded()

bool Ocean::Test::Validation::succeeded ( ) const
inline

Returns if this validation has succeeded.

Returns
True, if so; False, if the validation has failed

Field Documentation

◆ randomGenerator_

RandomGenerator* Ocean::Test::Validation::randomGenerator_ = nullptr
protected

Optional random generator object which will be used during validation.

◆ succeeded_

bool Ocean::Test::Validation::succeeded_ = true
protected

True, if the validation has succeeded; False, if the validation has failed.

◆ succeededChecked_

bool Ocean::Test::Validation::succeededChecked_ = false
mutableprotected

True, if the success state of this validation has been checked.


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