Test Macros: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
 
(67 intermediate revisions by 5 users not shown)
Line 1: Line 1:
== Pass/Fail Macros ==
__NOTOC__
The Stride implementation of [http://en.wikipedia.org/wiki/Assertion_(software_development) assertions] are provided with a set of Test Macros (declared in srtest.h) available for use within test methods. The macros are optional - you are not required to use them in your '''Test Units'''. They provide shortcuts for testing assertions and automatic report annotation in the case of failures.


The STRIDE Test Unit implementation also provides a set of Test Macros (declared in srtest.h) available for use within test methods. The macros are optional - you are not required to use them in your test units. They provide shortcuts for testing assertions and automatic report annotation in the case of failures. 
'''Example Test Macros'''
<source  lang=cpp>
#include <mytest.h>


The macros can be used in C++ and C test unit code (Note that there is no C version of exceptions test).  
void MyTest::CheckFoo() {
    .. 
    srEXPECT_EQ(foo(2 + 2), 4);
}
void MyTest::CheckBoo() {
    ..
    srEXPECT_GT(boo(3 * 3), 7);
}
</source>


== General guidelines for all macros ==


'''srEXPECT_xx''' macros
== Guidelines ==
* set the current test case status to FAIL and produce an annotation in the report if the expectation fails. If the expectation succeeds, there is no action.


'''srASSERT_xx''' macros will set the current test case to fail (if it hasn’t already been set) and insert an annotation into the report if the assertion fails. In addition, a return from the current function will occur. srASSERT_xx macros can only be used in test functions which return void. If the assertion succeeds there is no action.  
There are three types of macros: '''EXPECT''', '''ASSERT''', and '''EXIT''' macros. They have slight but important differences in their behavior.


srLOG macro will add a new comment to the current test case and produce an annotation in the report with specified level of importance.
'''srEXPECT_''xx''('''''condition''''')'''
* If the condition <u>does not</u> match the expectation, this macro:
** sets the current test case status to FAIL
** adds an "error" annotation to the current test case's results report which includes the condition as well as file and line number
** does not alter code execution 


The report annotation produced by a failed macro always includes the source file and line along with details about the condition that failed and the failing values.
'''srASSERT_''xx''('''''condition''''')'''
* If the condition <u>does not</u> match the expectation, this macro:
** sets the current test case status to FAIL
** adds an "error" annotation to the current test case's results report which includes the condition as well as file and line number
** <u>immediately returns</u> from the current test function. The return specifies no value, therefore a function or method that uses <tt>srASSERT_xx</tt> <u>should be declared to return <tt>void</tt></u>. <i>In case when a test function is required to return other type then <tt>void</tt>, a simple trick could be applied - in <b>C++ test code</b> postpend any value of the required type using the <u>C/C++ <tt>comma</tt> operator</u> (e.g. <tt>srASSERT_TRUE(cond),1;</tt>); in <b>C only test code</b> postpend the value <u>space</u> separated (e.g. <tt>srASSERT_TRUE(cond) 1;</tt>).</i>


=== Boolean Macros ===
'''srEXIT_''xx''('''''condition''''')'''
The boolean macros take a single condition expression, ''cond'', that evaluates to an integral type or bool. The condition will be evaluated once. if ''cond'' evaluates to non-zero the assertion or expectation fails. When a failure occurs the report will be annotated.  
* The behavior and restrictions of the exit macros are identical to the '''ASSERT''' macros. '''EXIT''' macros differ only in that they cause the test unit to cease execution. That is, no subsequent test methods within the currently running test unit are executed once an EXIT macro has failed in its assertion. This macro is useful in test units that implement behavioral testing where each test methods depends on the successful completion of the preceding test method.
* If a teardown fixture is declared, and the srEXIT_xx() fires within a test method, the teardown method will be called.
* If srEXIT_xx() fires within a scl_test_cclass test unit, its de-init function will be called if declared.
 
 
Note that the supplemental information is put into the test report only if the macro fails. For example <tt>srEXPECT_EQ(a, 10)</tt>, if a equals 10, then no action is taken; if a is not equal to 10, then the normal srEXPECT actions are taken, and--in addition--the supplemental information is added to the failure record in the test report. The ''srEXIT_xx()'' macro does not support this feature.
 
The following sections document the several types of testing macros that are provided by the Stride Framework. For simplicity, we refer to all the macros using a '''''prefix''''' tag - when using the macros in test code, the '''''prefix''''' should be replaced by one of the following: '''srEXPECT''', '''srASSERT''', or '''srEXIT''', depending on how the test writer wants failures to be handled.
 
== Boolean Macros ==
The boolean macros take a single condition expression, ''cond'', that evaluates to an integral type or bool.  
 
The condition will be evaluated once. When a failure is detected, the report will be annotated.  


{| class="prettytable"
{| class="prettytable"
| colspan="3" | '''Boolean'''
| colspan="2" | '''Boolean'''


|-
! macro !! Pass if
|-
|-
| srEXPECT_TRUE(''cond'');
| '''''prefix'''''_TRUE(''cond'');
| srASSERT_TRUE(''cond'');
| ''cond'' is non-zero
| ''cond'' is true


|-
|-
| srEXPECT_FALSE(''cond'');
| '''''prefix'''''_FALSE(''cond'');
| srASSERT_FALSE(''cond'');
| ''cond'' is zero
| ''cond'' is false


|}
|}


=== Comparison Macros ===
==== Example ====
<source lang='c'>
int a = 5;
int b = 5;
srEXPECT_TRUE(a == b);
 
srEXPECT_FALSE(2 < 1);
 
srEXPECT_FALSE(a != b);
srEXPECT_TRUE(1 < 2);
srEXPECT_TRUE(1);
</source>
 
 
''' ''Hint:'' A simple way to add supplemental information to test failures'''
 
If the '''C++ compiler mode''' is enabled then the ''srEXPECT_xx()'' and ''srASSERT_xx()'' macros also support adding to a test's annotations using the '''<< operator'''. For example:


Comparison macros take two operands and compare them using the indicated operator. The comparison macros will work for primitive types as well as objects that have the corresponding comparison operator implemented.   
<source lang="cpp">
srEXPECT_TRUE(a == 10) << "My custom message" << " a equals: " << a;
</source>
 
 
== Comparison Macros ==
 
Comparison macros take two operands and compare them using the indicated operator.  
 
The comparison macros will work for scalar types as well as C++ objects that have the corresponding comparison operator implemented.   


{| class="prettytable"
{| class="prettytable"
| colspan="3" | '''Comparison'''
| colspan="2" | '''Comparison'''
|-
! macro !! Pass if


|-
|-
| srEXPECT_EQ(''val1'', ''val2'');
| '''''prefix'''''_EQ(''val1'', ''val2'');
| srASSERT_EQ(''val1'', ''val2'');
| ''val1'' == ''val2''
| ''val1'' == ''val2''


|-
|-
| srEXPECT_NE(''val1'', ''val2'');
| '''''prefix'''''_NE(''val1'', ''val2'');
| srASSERT_NE(''val1'', ''val2'');
| ''val1'' != ''val2''
| ''val1'' != ''val2''


|-
|-
| srEXPECT_LT(''val1'', ''val2'');
| '''''prefix'''''_LT(''val1'', ''val2'');
| srASSERT_LT(''val1'', ''val2'');
| ''val1''<nowiki> < </nowiki>''val2''
| ''val1''<nowiki> < </nowiki>''val2''


|-
|-
| srEXPECT_LE(''val1'', ''val2'');
| '''''prefix'''''_LE(''val1'', ''val2'');
| srASSERT_LE(''val1'', ''val2'');
| ''val1''<nowiki> <= </nowiki>''val2''
| ''val1''<nowiki> <= </nowiki>''val2''


|-
|-
| srEXPECT_GT(''val1'', ''val2'');
| '''''prefix'''''_GT(''val1'', ''val2'');
| srASSERT_GT(''val1'', ''val2'');
| ''val1'' > ''val2''
| ''val1'' > ''val2''


|-
|-
| srEXPECT_GE(''val1'', ''val2'');
| '''''prefix'''''_GE(''val1'', ''val2'');
| srASSERT_GE(''val1'', ''val2'');
| ''val1'' >= ''val2''
| ''val1'' >= ''val2''


|}
|}


=== C String Comparison Macros ===
==== Example ====
<source lang='c'>
int a = 5;
int b = 5;
 
srEXPECT_EQ( 4, 4 );
srEXPECT_NE( 6, 7 );
srEXPECT_EQ( a, b );
srEXPECT_GT( 2 , 1 );
srEXPECT_GE( 2 , 1 );
</source>
 


C String Comparison Macros are intended only for use with C-style zero terminated strings. The strings can be char or wchar_t based. In particular, these macros should not be used for object of one or other string class, since such classes have overloaded comparison operators. The standard comparison macros should be used instead.  
== C String Comparison Macros ==
 
C String Comparison Macros are intended only for use with C-style null terminated strings. The strings can be char or wchar_t based.  
 
Don't use these macros to compare C++ objects representing strings since such classes typically have overloaded comparison operators. The standard comparison macros should be used instead.  


* An empty string will appear in error message output as “”. A null string will appear as NULL with no surrounding quotes. Otherwise all output strings are quoted.  
* An empty string will appear in error message output as “”. A null string will appear as NULL with no surrounding quotes. Otherwise all output strings are quoted.  
Line 81: Line 146:


{| class="prettytable"
{| class="prettytable"
| colspan="3" | '''C-string comparison'''
| colspan="2" | '''C-string comparison'''
|-
! macro !! Pass if


|-
|-
| srEXPECT_STREQ(''str1'', ''str2'');
| '''''prefix'''''_STREQ(''str1'', ''str2'');
| srASSERT_STREQ(''str1'', ''str2'');
| ''str1'' and ''str2'' have the same content
| ''str1'' and ''str2'' have the same content


|-
|-
| srEXPECT_STRNE(''str1'', ''str2'');
| '''''prefix'''''_STRNE(''str1'', ''str2'');
| srASSERT_STRNE(''str1'', ''str2'');
| ''str1'' and ''str2'' have different content
| ''str1'' and ''str2'' have different content


|-
|-
| srEXPECT_STRCASEEQ(''str1'', ''str2'');
| '''''prefix'''''_STRCASEEQ(''str1'', ''str2'');
| srASSERT_STRCASEEQ(''str1'', ''str2'');
| ''str1'' and ''str2'' have the same content, ignoring case.
| ''str1'' and ''str2'' have the same content, ignoring case.


|-
|-
| srEXPECT_STRCASENE(''str1'', ''str2'');
| '''''prefix'''''_STRCASENE(''str1'', ''str2'');
| srASSERT_STRCASENE(''str1'', ''str2'');
| ''str1'' and ''str2'' have different content, ignoring case.
| ''str1'' and ''str2'' have different content, ignoring case.


|}
|}


=== Exception Macros ===
==== Example ====
Exception macros  are used to ensure that expected exceptions are thrown. They require exception support from the target compiler. If the target compiler does not have exception support the macros cannot be used and must be disabled.
<source lang='c'>
const char* s1 = "This String is unique";
const char* s2 = "This String has an equivalent";
const char* s2Twin = "This String has an equivalent";
const char* s2TwinNoCase = "this string has an equivalent";


{| class="prettytable"
srEXPECT_STREQ( s2, s2Twin);
| colspan="3" | '''Exceptions'''
srEXPECT_STREQ( s2, "This String has an equivalent");
srEXPECT_STRCASEEQ(s2, s2TwinNoCase);


|-
srEXPECT_STRNE(s1, s2); 
| srEXPECT_THROW(statement, ex_type);
srEXPECT_STRNE(s2, s2TwinNoCase);  
| srASSERT_THROW(statement, ex_type);
srEXPECT_STRCASENE(s1, s2);
| ''statement'' throws an exception of type ''ex_type''
</source>


|-
| srEXPECT_THROW_ANY(''statement'');
| srASSERT_THROW_ANY(''statement'');
| ''statement'' throws an exception (type not important)
|-
| srEXPECT_NO_THROW(''statement'');
| srASSERT_NO_THROW(''statement'');
| ''statement'' does not throw an exception


|}
== Predicate Macros ==
Predicate macros allow user control over the pass/fail decision making.


=== Predicate Macros ===
A predicate is a function returning bool that is implemented by the user. Up to four arguments can also passed to the predicate through the macro.
Predicate macros allow user control over the pass/fail decision making in a macro. A predicate is a function returning bool that is implemented by the user but passed to the macro. Other arguments for the predicate are also passed to the macro. The macros allow for predicate functions with up to four parameters.  


{| class="prettytable"
{| class="prettytable"
| colspan="3" | '''Predicates'''
| colspan="2" | '''Predicates'''
|-
! macro !! Pass if


|-
|-
| srEXPECT_PRED1(''pred'', ''val1'')
| '''''prefix'''''_PRED1(''pred'', ''val1'')
| srASSERT_PRED1(''pred'', ''val1'')
| ''pred''(''val1'') returns true
| ''pred''(''val1'') returns true


|-
|-
| srEXPECT_PRED2(''pred'', ''val1'', ''val2'')
| '''''prefix'''''_PRED2(''pred'', ''val1'', ''val2'')
| srASSERT_PRED2(''pred'', ''val1'', ''val2'')
| ''pred''(''val1'', ''val2'') returns true
| ''pred''(''val1'', ''val2'') returns true


Line 150: Line 209:


|}
|}
All predicate macros require a predicate function function which returns bool. The predicate macros allow functions with one to 4 parameters. Following are the report annotations resulting from expectation or assertion failures.


=== Floating Point Comparison Macros ===
==== Example ====
Floating point macros are for comparing equivalence (or near equivalence) of floating point numbers. These macros are necessary since because equivalence comparisons for floating point numbers will often fail due to round-off errors.  
<source lang='c'>
static int alwaysTrueOneArg(int i)
{
    return 1;
}
 
static int alwaysTrueTwoArgs(int i, int j)
{
    return 1;
}
 
static void Pass()
{
    // examples of passing expectations using predicates.
 
    srEXPECT_PRED1(alwaysTrueOneArg, 25);
    srEXPECT_PRED2(alwaysTrueTwoArgs, 100, 33);
}
</source>
 
 
== Floating Point Comparison Macros ==
Floating point macros are for comparing equivalence (or near equivalence) of floating point numbers.  
 
These macros are are very useful for dealing with floating point round-off effects.  


{| class="prettytable"
{| class="prettytable"
| colspan="3" | '''Floating Point comparison'''
| colspan="2" | '''Floating Point comparison'''
|-
! macro !! Pass if


|-
|-
| srEXPECT_NEAR(val1, val2, epsilon);
| '''''prefix'''''_NEAR(val1, val2, epsilon);
| srASSERT_NEAR(val1, val2, epsilon);
| The absolute value of the difference between val1 and val2 is less than or equal to epsilon.
| The absolute value of the difference between val1 and val2 is epsilon.
|}
|}


=== Dynamic Test Case Macros ===
==== Example ====
The macros presented so far are not capable of dealing with dynamic test cases. In order to handle dynamic test cases, each of the macros requires another parameter which is the test case to report against. Other than this, these macros provide exactly equivalent functionality to the non-dynamic peer. The dynamic macros are listed below. All require a test case, value of type srTestCaseHandle_t from srtest.h,  to be passed as the first parameter).
<source lang='c'>
float x =          2.00005f;
float nearX =      2.00006f;
float nearXEpsilon = .000019f;
 
double y =          1.2345;
double nearY =      1.23456;
double nearYEpsilon = .00019;
   
srEXPECT_NEAR(x, nearX, nearXEpsilon);
srEXPECT_NEAR(y, nearY, nearYEpsilon);
</source>
 
 
== Exception Macros ==
Exception macros are used to ensure that expected exceptions are thrown. They are applicable to <u>C++ code only</u>.
 
These macros require exception support from the target compiler. If the target compiler does not have exception support the macros cannot be used.


{| class="prettytable"
{| class="prettytable"
| '''''Nonfatal assertion'''''
| colspan="2" | '''Exceptions'''
| '''''Fatal Assertion'''''
|-
! macro !! Pass if


|-
|-
| colspan="2" | '''Boolean'''
| '''''prefix'''''_THROW(statement, ex_type);
| ''statement'' throws an exception of type ''ex_type''


|-
|-
| srEXPECT_TRUE_DYN(tc, ''cond'');
| '''''prefix'''''_THROW_ANY(''statement'');
| srASSERT_TRUE_DYN(tc, ''cond'');
| ''statement'' throws an exception (type not important)


|-
|-
| srEXPECT_FALSE_DYN(tc, ''cond'');
| '''''prefix'''''_NO_THROW(''statement'');
| srASSERT_FALSE_DYN(tc, ''cond'');
| ''statement'' does not throw an exception
|}
 
===== Example =====
<source lang='cpp'>
srEXPECT_THROW(throwStdException(), std::exception);
srEXPECT_THROW(throwInt(), int);
 
srEXPECT_THROW_ANY(throwStdException());
srEXPECT_THROW_ANY(throwInt());
 
srEXPECT_NO_THROW(doesntThrow());
</source>
 
 
== Dynamic Test Case Macros ==
The macros presented so far assume that their actions are directed at the currently in-scope test case. However, test cases can be created dynamically using Stride's [Runtime Test Services].
 
In order to handle dynamic test cases, each of the macros requires another parameter which specifies the test case to report against. Other than this, these macros provide exactly equivalent functionality to the non-dynamic peer. The dynamic macros are listed below. All require a test case, value of type '''srTestCaseHandle_t''' from srtest.h, to be passed as the first parameter).
 
{| class="prettytable"
! macro
|-
| colspan="1" | '''Boolean'''


|-
|-
| colspan="2" | '''Comparison'''
| '''''prefix'''''_TRUE_DYN(tc, ''cond'');


|-
|-
| srEXPECT_EQ_DYN(tc, ''val1'', ''val2'');
| '''''prefix'''''_FALSE_DYN(tc, ''cond'');
| srASSERT_EQ_DYN(tc, ''expect'', ''val'');


|-
|-
| srEXPECT_NE_DYN(tc, ''val1'', ''val2'');
| colspan="1" | '''Comparison'''
| srASSERT_NE_DYN(tc, ''val1'', ''val2'');


|-
|-
| srEXPECT_LT_DYN(tc, ''val1'', ''val2'');
| '''''prefix'''''_EQ_DYN(tc, ''val1'', ''val2'');
| srASSERT_LT_DYN(tc, ''val1'', ''val2'');


|-
|-
| srEXPECT_LE_DYN(tc, ''val1'', ''val2'');
| '''''prefix'''''_NE_DYN(tc, ''val1'', ''val2'');
| srASSERT_LE_DYN(tc, ''val1'', ''val2'');


|-
|-
| srEXPECT_GT_DYN(tc, ''val1'', ''val2'');
| '''''prefix'''''_LT_DYN(tc, ''val1'', ''val2'');
| srASSERT_GT_DYN(tc, ''val1'', ''val2'');


|-
|-
| srEXPECT_GE_DYN(tc, ''val1'', ''val2'');
| '''''prefix'''''_LE_DYN(tc, ''val1'', ''val2'');
| srASSERT_GE_DYN(tc, ''val1'', ''val2'');


|-
|-
| colspan="2" | '''C-string comparison'''
| '''''prefix'''''_GT_DYN(tc, ''val1'', ''val2'');


|-
|-
| srEXPECT_STREQ_DYN(tc, ''str1'', ''str2'');
| '''''prefix'''''_GE_DYN(tc, ''val1'', ''val2'');
| srASSERT_STREQ_DYN(tc, ''str1'', ''str2'');


|-
|-
| srEXPECT_STRNE_DYN(tc, ''str1'', ''str2'');
| colspan="1" | '''C-string comparison'''
| srASSERT_STRNE_DYN(tc, ''str1'', ''str2'');


|-
|-
| srEXPECT_STRCASEEQ_DYN(tc, ''str1'', ''str2'');
| '''''prefix'''''_STREQ_DYN(tc, ''str1'', ''str2'');
| srASSERT_STRCASEEQ_DYN(tc, ''str1'', ''str2'');


|-
|-
| srEXPECT_STRCASENE_DYN(tc, ''str1'', ''str2'');
| '''''prefix'''''_STRNE_DYN(tc, ''str1'', ''str2'');
| srASSERT_STRCASENE_DYN(tc, ''str1'', ''str2'');


|-
|-
| colspan="2" | '''Exceptions'''
| '''''prefix'''''_STRCASEEQ_DYN(tc, ''str1'', ''str2'');


|-
|-
| srEXPECT_THROW_DYN(statement, ex_type);
| '''''prefix'''''_STRCASENE_DYN(tc, ''str1'', ''str2'');
| srASSERT_THROW_DYN(tc, statement, ex_type);


|-
|-
| srEXPECT_THROW_ANY_DYN(tc, ''statement'');
| colspan="1" | '''Exceptions'''
| srASSERT_THROW_ANY_DYN(tc, ''statement'');


|-
|-
| srEXPECT_NO_THROW_DYN(tc, ''statement'');
| '''''prefix'''''_THROW_DYN(statement, ex_type);
| srASSERT_NO_THROW_DYN(tc, ''statement'');


|-
|-
| colspan="2" | '''Predicates'''
| '''''prefix'''''_THROW_ANY_DYN(tc, ''statement'');


|-
|-
| srEXPECT_PRED1_DYN(tc, ''pred'', ''val1'');
| '''''prefix'''''_NO_THROW_DYN(tc, ''statement'');
| srASSERT_PRED1_DYN(tc, ''pred'', ''val1'');


|-
|-
| srEXPECT_PRED2_DYN(tc, ''pred'', ''vall'', ''val2'');
| colspan="1" | '''Predicates'''
| srASSERT_PRED2_DYN(tc, ''pred'', ''vall'', ''val2'');


|-
|-
| (up to arity of 4)
| '''''prefix'''''_PRED1_DYN(tc, ''pred'', ''val1'');
|


|-
|-
| colspan="2" | '''Floating Point'''
| '''''prefix'''''_PRED2_DYN(tc, ''pred'', ''vall'', ''val2'');


|-
|-
| srEXPECT_NEAR_DYN(tc, ''val1'', ''val2'', ''epsilon'');
| (up to arity of 4)
| srASSERT_NEAR_DYN(tc, ''val1'', ''val2'', ''epsilon'');


|-
|-
| colspan="2" | '''Logging'''
| colspan="1" | '''Floating Point'''


|-
|-
| srLOG_DYN(tc, ''level'', ''message'');
| '''''prefix'''''_NEAR_DYN(tc, ''val1'', ''val2'', ''epsilon'');
|


|}
|}
=== Use operator << for report annotation (C++ tests only) ===
In C++ test code all macros support the adding to the report annotations using the << operator. For example:
<source lang="cpp">
srEXPECT_TRUE(a != b) << "My custom message";
</source>
As delivered, the macros will support stream input annotations for all numeric types, "C" string (char* or wchar_t*) and types allowing implicit cast to numeric type or "C" string. The user may overload the << operator in order to annotate reports using any custom type. An example is below.
The following will compile and execute successfully given that the << operator is overloaded as shown:
<source lang="cpp">
#include <srtest.h>
// MyCustomClass implementation
class MyCustomClass
{
public:
  MyCustomClass(int i) : m_int(i) {}
private:
  int m_int;
  friend stride::Message& operator<<(stride::Message& ss, const MyCustomClass& obj);
};
stride::Message& operator<<(stride::Message& ss, const MyCustomClass& obj)
{
  ss << obj.m_int;
  return ss;
}
void test()
{
    MyCustomClass custom(34);
    srEXPECT_FALSE(true) << custom;
}
</source>

Latest revision as of 22:25, 29 September 2015

The Stride implementation of assertions are provided with a set of Test Macros (declared in srtest.h) available for use within test methods. The macros are optional - you are not required to use them in your Test Units. They provide shortcuts for testing assertions and automatic report annotation in the case of failures.

Example Test Macros

#include <mytest.h>

void MyTest::CheckFoo() {
    ..  
    srEXPECT_EQ(foo(2 + 2), 4); 
}
void MyTest::CheckBoo() {
    ..
    srEXPECT_GT(boo(3 * 3), 7); 
}


Guidelines

There are three types of macros: EXPECT, ASSERT, and EXIT macros. They have slight but important differences in their behavior.

srEXPECT_xx(condition)

  • If the condition does not match the expectation, this macro:
    • sets the current test case status to FAIL
    • adds an "error" annotation to the current test case's results report which includes the condition as well as file and line number
    • does not alter code execution

srASSERT_xx(condition)

  • If the condition does not match the expectation, this macro:
    • sets the current test case status to FAIL
    • adds an "error" annotation to the current test case's results report which includes the condition as well as file and line number
    • immediately returns from the current test function. The return specifies no value, therefore a function or method that uses srASSERT_xx should be declared to return void. In case when a test function is required to return other type then void, a simple trick could be applied - in C++ test code postpend any value of the required type using the C/C++ comma operator (e.g. srASSERT_TRUE(cond),1;); in C only test code postpend the value space separated (e.g. srASSERT_TRUE(cond) 1;).

srEXIT_xx(condition)

  • The behavior and restrictions of the exit macros are identical to the ASSERT macros. EXIT macros differ only in that they cause the test unit to cease execution. That is, no subsequent test methods within the currently running test unit are executed once an EXIT macro has failed in its assertion. This macro is useful in test units that implement behavioral testing where each test methods depends on the successful completion of the preceding test method.
  • If a teardown fixture is declared, and the srEXIT_xx() fires within a test method, the teardown method will be called.
  • If srEXIT_xx() fires within a scl_test_cclass test unit, its de-init function will be called if declared.


Note that the supplemental information is put into the test report only if the macro fails. For example srEXPECT_EQ(a, 10), if a equals 10, then no action is taken; if a is not equal to 10, then the normal srEXPECT actions are taken, and--in addition--the supplemental information is added to the failure record in the test report. The srEXIT_xx() macro does not support this feature.

The following sections document the several types of testing macros that are provided by the Stride Framework. For simplicity, we refer to all the macros using a prefix tag - when using the macros in test code, the prefix should be replaced by one of the following: srEXPECT, srASSERT, or srEXIT, depending on how the test writer wants failures to be handled.

Boolean Macros

The boolean macros take a single condition expression, cond, that evaluates to an integral type or bool.

The condition will be evaluated once. When a failure is detected, the report will be annotated.

Boolean
macro Pass if
prefix_TRUE(cond); cond is non-zero
prefix_FALSE(cond); cond is zero

Example

int a = 5; 
int b = 5; 
srEXPECT_TRUE(a == b); 

srEXPECT_FALSE(2 < 1);

srEXPECT_FALSE(a != b);
srEXPECT_TRUE(1 < 2); 
srEXPECT_TRUE(1);


Hint: A simple way to add supplemental information to test failures

If the C++ compiler mode is enabled then the srEXPECT_xx() and srASSERT_xx() macros also support adding to a test's annotations using the << operator. For example:

srEXPECT_TRUE(a == 10) << "My custom message" << " a equals: " << a;


Comparison Macros

Comparison macros take two operands and compare them using the indicated operator.

The comparison macros will work for scalar types as well as C++ objects that have the corresponding comparison operator implemented.

Comparison
macro Pass if
prefix_EQ(val1, val2); val1 == val2
prefix_NE(val1, val2); val1 != val2
prefix_LT(val1, val2); val1 < val2
prefix_LE(val1, val2); val1 <= val2
prefix_GT(val1, val2); val1 > val2
prefix_GE(val1, val2); val1 >= val2

Example

int a = 5; 
int b = 5; 

srEXPECT_EQ( 4, 4 ); 
srEXPECT_NE( 6, 7 );
srEXPECT_EQ( a, b ); 
srEXPECT_GT( 2 , 1 );
srEXPECT_GE( 2 , 1 );


C String Comparison Macros

C String Comparison Macros are intended only for use with C-style null terminated strings. The strings can be char or wchar_t based.

Don't use these macros to compare C++ objects representing strings since such classes typically have overloaded comparison operators. The standard comparison macros should be used instead.

  • An empty string will appear in error message output as “”. A null string will appear as NULL with no surrounding quotes. Otherwise all output strings are quoted.
  • The type of str1 and str2 must be compatible with const char* or const wchar_t*.
C-string comparison
macro Pass if
prefix_STREQ(str1, str2); str1 and str2 have the same content
prefix_STRNE(str1, str2); str1 and str2 have different content
prefix_STRCASEEQ(str1, str2); str1 and str2 have the same content, ignoring case.
prefix_STRCASENE(str1, str2); str1 and str2 have different content, ignoring case.

Example

const char* s1 = "This String is unique"; 
const char* s2 = "This String has an equivalent"; 
const char* s2Twin = "This String has an equivalent"; 
const char* s2TwinNoCase = "this string has an equivalent"; 

srEXPECT_STREQ( s2, s2Twin); 
srEXPECT_STREQ( s2, "This String has an equivalent"); 
srEXPECT_STRCASEEQ(s2, s2TwinNoCase); 

srEXPECT_STRNE(s1, s2);  
srEXPECT_STRNE(s2, s2TwinNoCase); 
srEXPECT_STRCASENE(s1, s2);


Predicate Macros

Predicate macros allow user control over the pass/fail decision making.

A predicate is a function returning bool that is implemented by the user. Up to four arguments can also passed to the predicate through the macro.

Predicates
macro Pass if
prefix_PRED1(pred, val1) pred(val1) returns true
prefix_PRED2(pred, val1, val2) pred(val1, val2) returns true
…(up to arity of 4)

Example

static int alwaysTrueOneArg(int i)
{
    return 1; 
}

static int alwaysTrueTwoArgs(int i, int j)
{
    return 1;
}

static void Pass()
{
    // examples of passing expectations using predicates.

    srEXPECT_PRED1(alwaysTrueOneArg, 25); 
    srEXPECT_PRED2(alwaysTrueTwoArgs, 100, 33); 
}


Floating Point Comparison Macros

Floating point macros are for comparing equivalence (or near equivalence) of floating point numbers.

These macros are are very useful for dealing with floating point round-off effects.

Floating Point comparison
macro Pass if
prefix_NEAR(val1, val2, epsilon); The absolute value of the difference between val1 and val2 is less than or equal to epsilon.

Example

float x =           2.00005f;
float nearX =       2.00006f; 
float nearXEpsilon = .000019f;

double y =           1.2345; 
double nearY =       1.23456; 
double nearYEpsilon = .00019;
    
srEXPECT_NEAR(x, nearX, nearXEpsilon); 
srEXPECT_NEAR(y, nearY, nearYEpsilon);


Exception Macros

Exception macros are used to ensure that expected exceptions are thrown. They are applicable to C++ code only.

These macros require exception support from the target compiler. If the target compiler does not have exception support the macros cannot be used.

Exceptions
macro Pass if
prefix_THROW(statement, ex_type); statement throws an exception of type ex_type
prefix_THROW_ANY(statement); statement throws an exception (type not important)
prefix_NO_THROW(statement); statement does not throw an exception
Example
srEXPECT_THROW(throwStdException(), std::exception); 
srEXPECT_THROW(throwInt(), int);

srEXPECT_THROW_ANY(throwStdException()); 
srEXPECT_THROW_ANY(throwInt()); 

srEXPECT_NO_THROW(doesntThrow());


Dynamic Test Case Macros

The macros presented so far assume that their actions are directed at the currently in-scope test case. However, test cases can be created dynamically using Stride's [Runtime Test Services].

In order to handle dynamic test cases, each of the macros requires another parameter which specifies the test case to report against. Other than this, these macros provide exactly equivalent functionality to the non-dynamic peer. The dynamic macros are listed below. All require a test case, value of type srTestCaseHandle_t from srtest.h, to be passed as the first parameter).

macro
Boolean
prefix_TRUE_DYN(tc, cond);
prefix_FALSE_DYN(tc, cond);
Comparison
prefix_EQ_DYN(tc, val1, val2);
prefix_NE_DYN(tc, val1, val2);
prefix_LT_DYN(tc, val1, val2);
prefix_LE_DYN(tc, val1, val2);
prefix_GT_DYN(tc, val1, val2);
prefix_GE_DYN(tc, val1, val2);
C-string comparison
prefix_STREQ_DYN(tc, str1, str2);
prefix_STRNE_DYN(tc, str1, str2);
prefix_STRCASEEQ_DYN(tc, str1, str2);
prefix_STRCASENE_DYN(tc, str1, str2);
Exceptions
prefix_THROW_DYN(statement, ex_type);
prefix_THROW_ANY_DYN(tc, statement);
prefix_NO_THROW_DYN(tc, statement);
Predicates
prefix_PRED1_DYN(tc, pred, val1);
prefix_PRED2_DYN(tc, pred, vall, val2);
…(up to arity of 4)
Floating Point
prefix_NEAR_DYN(tc, val1, val2, epsilon);