BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Macros | Functions
unittest.h File Reference

A unit test facility. More...

#include <bsp430/core.h>
#include <string.h>
#include <inttypes.h>

Go to the source code of this file.

Macros

#define configBSP430_UNITTEST   0
 
#define BSP430_UNITTEST   (configBSP430_UNITTEST - 0)
 
#define BSP430_UNITTEST_LED_FAILED   BSP430_LED_RED
 
#define BSP430_UNITTEST_LED_PASSED   BSP430_LED_GREEN
 
#define configBSP430_UNITTEST_FAILFAST   0
 
#define BSP430_UNITTEST_FAIL(msg_)
 
#define BSP430_UNITTEST_ASSERT_TRUE(expr_)
 
#define BSP430_UNITTEST_ASSERT_FALSE(expr_)
 
#define BSP430_UNITTEST_ASSERT_EQUAL(v1_, v2_)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_ASCIIZ(v1_, v2_)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_, v2_, type_, pri_)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTd(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,int,"%" PRId16)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTu(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned int,"%" PRIu16)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTx(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned int,"%#" PRIx16)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTld(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,long,"%" PRId32)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTlu(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned long,"%" PRIu32)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTlx(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned long, "%#" PRIx32)
 
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTp(v1_, v2_)   BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,const void *,"%p")
 

Functions

void vBSP430unittestInitialize (void)
 
void vBSP430unittestResult_ (int line, int passp, const char *format,...)
 
void vBSP430unittestFinalize (void)
 

Detailed Description

A unit test facility.

This abstracts the unit-test specific portions of test430, since BSP430 itself replaces the platform support portions of that package.

It is not a full-featured xUnit style infrastructure, as there is no framework concept of test suites or setup/teardown functionality to restore a known configuration. Basically all it supports is verifying intermediate values and displaying unexpected results for further analysis.

Note
This facility depends on BSP430_CONSOLE and BSP430_LED. The corresponding modules must be linked into any application that uses it.
Warning
All macros potentially involve console output and are subject to the material in Interrupts and Console Operations.
Homepage
http://github.com/pabigot/bsp430

Macro Definition Documentation

#define BSP430_UNITTEST   (configBSP430_UNITTEST - 0)

Indicate that the unit test interface is available on the platform. This is normally set by <bsp430/utility/unittest.h> when configBSP430_UNITTEST is true.

C Preprocessor Only:
This macro may have a value that restricts its use to C preprocessor conditional directives.
Dependency:
configBSP430_UNITTEST
Defaulted:
The value here is superseded by previously encountered definitions.
#define BSP430_UNITTEST_ASSERT_EQUAL (   v1_,
  v2_ 
)
Value:
do { \
static const char * expr_str_ = #v1_ "==" #v2_; \
vBSP430unittestResult_(__LINE__, (v1_) == (v2_), expr_str_); \
} while (0)
void vBSP430unittestResult_(int line, int passp, const char *format,...)

Validate that the expressions are equal

#define BSP430_UNITTEST_ASSERT_EQUAL_ASCIIZ (   v1_,
  v2_ 
)
Value:
do { \
static const char * expr_str_ = #v1_ "==" #v2_; \
const char * v1v_ = (v1_); \
const char * v2v_ = (v2_); \
vBSP430unittestResult_(__LINE__, 0 == strcmp(v1v_, v2v_), expr_str_); \
} while (0)
void vBSP430unittestResult_(int line, int passp, const char *format,...)

Validate that the ASCIIZ contents are equal

#define BSP430_UNITTEST_ASSERT_EQUAL_FMT_ (   v1_,
  v2_,
  type_,
  pri_ 
)
Value:
do { \
static const char * expr_str_ = #v1_ "==" #v2_; \
type_ v1v_ = (v1_); \
type_ v2v_ = (v2_); \
if (v1v_ == v2v_) { \
vBSP430unittestResult_(__LINE__, 1, "%s", expr_str_); \
} else { \
vBSP430unittestResult_(__LINE__, 0, "%s: " pri_ " != " pri_, expr_str_, v1v_, v2v_); \
} \
} while (0)
void vBSP430unittestResult_(int line, int passp, const char *format,...)

Diagnose equality failures.

Parameters
v1_an expression of some type
v2_an expression of the same type as v1_
type_a type suitable for holding the values of v1_ and v2_ for equality comparison purposes
pri_a print(3) format specifier, inclusive of the leading %, to be used to format the passed values of v1_ and v2_ in the case where the test fails
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTd (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,int,"%" PRId16)

Validate that the expressions are equal.

If they are not, emit a diagnostic showing both values formatted as signed 16-bit integers.

Examples:
utility/unittest/main.c.
#define BSP430_UNITTEST_ASSERT_EQUAL_FMTld (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,long,"%" PRId32)

Validate that the expressions are equal.

If they are not, emit a diagnostic showing both values formatted as signed 32-bit decimal integers.

#define BSP430_UNITTEST_ASSERT_EQUAL_FMTlu (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned long,"%" PRIu32)

Validate that the expressions are equal.

If they are not, emit a diagnostic showing both values formatted as unsigned 32-bit decimal integers.

#define BSP430_UNITTEST_ASSERT_EQUAL_FMTlx (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned long, "%#" PRIx32)

Validate that the expressions are equal.

If they are not, emit a diagnostic showing both values formatted as unsigned 32-bit hexadecimal integers.

#define BSP430_UNITTEST_ASSERT_EQUAL_FMTp (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,const void *,"%p")

Validate that the pointer expressions are equal.

If they are not, emit a diagnostic showing both pointer values.

#define BSP430_UNITTEST_ASSERT_EQUAL_FMTu (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned int,"%" PRIu16)

Validate that the expressions are equal.

If they are not, emit a diagnostic showing both values formatted as unsigned 16-bit decimal integers.

#define BSP430_UNITTEST_ASSERT_EQUAL_FMTx (   v1_,
  v2_ 
)    BSP430_UNITTEST_ASSERT_EQUAL_FMT_(v1_,v2_,unsigned int,"%#" PRIx16)

Validate that the expressions are equal.

If they are not, emit a diagnostic showing both values formatted as unsigned 16-bit hexadecimal integers.

#define BSP430_UNITTEST_ASSERT_FALSE (   expr_)
Value:
do { \
static const char * expr_str_ = #expr_; \
vBSP430unittestResult_(__LINE__, !(expr_), "NOT %s", expr_str_); \
} while (0)
void vBSP430unittestResult_(int line, int passp, const char *format,...)

Validate that the expression is false

Examples:
utility/unittest/main.c.
#define BSP430_UNITTEST_ASSERT_TRUE (   expr_)
Value:
do { \
static const char * expr_str_ = #expr_; \
vBSP430unittestResult_(__LINE__, !!(expr_), expr_str_); \
} while (0)
void vBSP430unittestResult_(int line, int passp, const char *format,...)

Validate that the expression is true

Examples:
utility/unittest/main.c.
#define BSP430_UNITTEST_FAIL (   msg_)
Value:
do { \
vBSP430unittestResult_(__LINE__, 0, msg_); \
} while (0)
void vBSP430unittestResult_(int line, int passp, const char *format,...)

Diagnose an unconditional failure

Examples:
utility/unittest/main.c.
#define BSP430_UNITTEST_LED_FAILED   BSP430_LED_RED

The LED that is lit if the tests failed. This is normally the first red LED; if either the red or green LED is missing, it is the first available LED so that on a one-LED platform failure is indicated by a lit LED.

#define BSP430_UNITTEST_LED_PASSED   BSP430_LED_GREEN

The LED that is lit if the tests failed. This is normally the first green LED; if either the red or green LED is missing, it is the second available LED. On a one-LED platform, success is not visibly indicated.

#define configBSP430_UNITTEST   0

Define to a true value to enable the unit test infrastructure. In addition, the <bsp430/utility/unittest.h> header must be included, and the utility/unittest module linked in.

C Preprocessor Only:
This macro may have a value that restricts its use to C preprocessor conditional directives.
Affects:
BSP430_UNITTEST
Defaulted:
The value here is superseded by previously encountered definitions.
#define configBSP430_UNITTEST_FAILFAST   0

Define to a true value to bypass subsequent tests after a failure. In this case, the internals of the unittest framework immediately execute the finalization routine on detection of a failure.

By default this is defined to a false value, and the framework continues to run the remaining tests in the application.

C Preprocessor Only:
This macro may have a value that restricts its use to C preprocessor conditional directives.
Defaulted:
The value here is superseded by previously encountered definitions.

Function Documentation

void vBSP430unittestFinalize ( void  )

Finalize the unittest framework.

This displays the results of the tests and emits a distinguished sequence (...done) to the console for use in external monitor applications.

If tests pass, BSP430_UNITTEST_LED_PASSED is lit, and this function returns.

If at least one test failed, BSP430_UNITTEST_LED_FAILED flashes at 1Hz and this function does not return.

Warning
This function may enable interrupts and sleep if interrupt-driven console transmission is enabled. See Interrupts and Console Operations.
Examples:
utility/unittest/main.c.
void vBSP430unittestInitialize ( void  )

Initialize the unittest framework.

This clears the history of previous tests and emits a distinguished sequence (sync...) to the console for use in external monitor applications.

Examples:
utility/unittest/main.c.
void vBSP430unittestResult_ ( int  line,
int  passp,
const char *  format,
  ... 
)

Display a unit test result.

Warning
This function may enable interrupts and sleep if interrupt-driven console transmission is enabled. See Interrupts and Console Operations.
Parameters
linethe line number on which the test occurred.
passpa non-zero value if the test passed; a zero value indicates the test failed.
formata cprintf format statement for any diagnostic or status to be displayed for the test. A newline will be appended automatically.