Xmega Application Note


Test Suite Framework

Data Structures

struct  test_case
 A single test case. More...
struct  test_suite
 A test suite. More...

Defines

#define __progmem_arg
 Progmem-related defines.
#define ARRAY_LEN(a)   (sizeof(a) / sizeof((a)[0]))
#define dbg(__fmt_)   printf_P(PSTR(__fmt_))
 Wrappers for printing debug-information.
#define dbg_error(_x,...)   printf_P(PSTR(_x), __VA_ARGS__)
#define dbg_info(__fmt_,...)   printf_P(PSTR(__fmt_), __VA_ARGS__)
#define dbg_putchar(c)   putc(c, stdout)
#define dbg_vprintf_pgm(...)   vfprintf_P(stdout, __VA_ARGS__)
#define DECLARE_TEST_SUITE(_sym)   const struct test_suite _sym
#define DEFINE_TEST_ARRAY(_sym)   const struct test_case *const _sym[]
 Convenience macro for creating an array of test cases.
#define DEFINE_TEST_CASE(_sym, _setup, _run, _cleanup, _name)
 Convenience macro for creating a test case struct.
#define DEFINE_TEST_SUITE(_sym, _test_array, _name)
 Convenience macro for creating a test suite.
#define test_fail(test, result,...)
 Fail the test.
#define test_fail_unless(test, condition,...)
 Verify that condition is true.

Enumerations

enum  test_status { TEST_PASS = 0, TEST_FAILED = 1 }
 

Status codes returned by test cases and fixtures.

More...

Functions

static int test_call (void(*func)(const struct test_case *), const struct test_case *test)
static int test_case_run (const struct test_case *test)
static void * test_get_data (void)
 Get the private data pointer for the current test.
void test_priv_fail (const struct test_case *test, int result, const char *file, unsigned int line, const char __progmem_arg *fmt,...)
static void test_report_failure (const struct test_case *test, const char *stage, int result)
static void test_set_data (void *data)
 Set private data pointer for the current test.
int test_suite_run (const struct test_suite *suite)
 Run a test suite.

Variables

static jmp_buf test_failure_jmpbuf
void * test_priv_data
void * test_priv_data

Define Documentation

#define __progmem_arg

Progmem-related defines.

Not relevant until printf is implemented directly in asf and supports arguments stored in progmem.

Definition at line 87 of file suite.h.

#define ARRAY_LEN (  )     (sizeof(a) / sizeof((a)[0]))

Definition at line 171 of file suite.h.

#define dbg ( __fmt_   )     printf_P(PSTR(__fmt_))

Wrappers for printing debug-information.

The stream must be set up by the test-application somehow, for now uses printf.

Definition at line 58 of file suite.h.

Referenced by test_case_run().

#define dbg_error ( _x,
...   )     printf_P(PSTR(_x), __VA_ARGS__)

Definition at line 62 of file suite.h.

Referenced by test_report_failure().

#define dbg_info ( __fmt_,
...   )     printf_P(PSTR(__fmt_), __VA_ARGS__)

Definition at line 60 of file suite.h.

Referenced by test_case_run().

#define dbg_putchar (  )     putc(c, stdout)

Definition at line 64 of file suite.h.

#define dbg_vprintf_pgm ( ...   )     vfprintf_P(stdout, __VA_ARGS__)

Definition at line 66 of file suite.h.

#define DECLARE_TEST_SUITE ( _sym   )     const struct test_suite _sym

Definition at line 187 of file suite.h.

#define DEFINE_TEST_ARRAY ( _sym   )     const struct test_case *const _sym[]

Convenience macro for creating an array of test cases.

Parameters:
_sym Variable name of the resulting array

Definition at line 152 of file suite.h.

#define DEFINE_TEST_CASE ( _sym,
_setup,
_run,
_cleanup,
_name   ) 
Value:
static const char _test_str_##_sym[] = _name;             \
        static const struct test_case _sym = {                    \
                .setup                  = _setup,                                                     \
                .run            = _run,                               \
                .cleanup        = _cleanup,                           \
                .name           = _test_str_##_sym                    \
        }

Convenience macro for creating a test case struct.

Parameters:
_sym Variable name of the resulting struct
_setup Function which sets up a test case environment. Can be NULL.
_run Test function
_cleanup Function which cleans up what was set up. Can be NULL.
_name String describing the test case.

Definition at line 139 of file suite.h.

#define DEFINE_TEST_SUITE ( _sym,
_test_array,
_name   ) 
Value:
static const char _test_str_##_sym[] = _name;        \
        const struct test_suite _sym = {                     \
                .nr_tests       = ARRAY_LEN(_test_array),               \
                .tests          = _test_array,                          \
                .name           = _test_str_##_sym                     \
        }

Convenience macro for creating a test suite.

Parameters:
_sym Variable name of the resulting struct
_test_array Array of test cases, created with DEFINE_TEST_ARRAY()
_name String describing the test suite.

Definition at line 180 of file suite.h.

#define test_fail ( test,
result,
...   ) 
Value:
test_priv_fail(test, result, __FILE__, __LINE__,          \
                                __VA_ARGS__)

Fail the test.

Calling this function will cause the test to terminate with a failure. It will return directly to the test suite core, not to the caller.

Parameters:
test Test case which failed
result The result of the test (may not be 0)
... printf()-style format string and arguments

Definition at line 248 of file suite.h.

#define test_fail_unless ( test,
condition,
...   ) 
Value:
do {                                                            \
                if (!(condition))                                       \
                        test_fail(test, TEST_FAILED, __VA_ARGS__);      \
        } while (0)

Verify that condition is true.

If condition is false, fail the test with an error message indicating the condition which failed.

Parameters:
test The test case currently being run
condition Expression to be validated
... Format string and arguments

Definition at line 263 of file suite.h.


Enumeration Type Documentation

Status codes returned by test cases and fixtures.

Note that test cases and especially fixtures may return any of the status codes defined by status_code as well.

Enumerator:
TEST_PASS 

Test succeeded.

TEST_FAILED 

Test failed.

Definition at line 96 of file suite.h.

00096                  {
00097         TEST_PASS               = 0,            
00098         TEST_FAILED             = 1,            
00099 };


Function Documentation

static int test_call ( void(*)(const struct test_case *)  func,
const struct test_case test 
) [static]

For internal use only.

Call a test or fixture function

This function will initialize test_failure_jmpbuf and call func with test as the parameter.

Returns:
TEST_PASS if func was executed successfully, or the result value passed to test_fail() on failure.

Definition at line 76 of file suite.c.

References test_failure_jmpbuf, and TEST_PASS.

Referenced by test_case_run().

00078 {
00079         int     ret = 0;
00080 
00081         if (!func)
00082                 return TEST_PASS;
00083 
00084         /*
00085          * The first call to setjmp() always return 0. However, if the
00086          * call to func() below goes wrong, we'll return here again with
00087          * a nonzero value.
00088          */
00089         ret = setjmp(test_failure_jmpbuf);
00090         if (ret)
00091                 return ret;
00092 
00093         func(test);
00094 
00095         return TEST_PASS;
00096 }

static int test_case_run ( const struct test_case test  )  [static]

Definition at line 98 of file suite.c.

References test_case::cleanup, dbg, dbg_info, test_case::name, test_case::run, test_case::setup, test_call(), and test_report_failure().

00099 {
00100         int     result;
00101 
00102         dbg_info("Running test: %s\n", test->name);
00103         if (test->setup) {
00104                 int     ret;
00105                 dbg("Setting up fixture\n");
00106                 ret = test_call(test->setup, test);
00107                 if (ret) {
00108                         test_report_failure(test, "setup", ret);
00109                         result = ret;
00110                         goto out;
00111                 }
00112         }
00113 
00114         result = test_call(test->run, test);
00115         if (result)
00116                 test_report_failure(test, "test", result);
00117 
00118         if (test->cleanup) {
00119                 int     ret;
00120                 dbg("Cleaning up fixture\n");
00121                 ret = test_call(test->cleanup, test);
00122                 if (ret && !result) {
00123                         test_report_failure(test, "cleanup", ret);
00124                         result = ret;
00125                 }
00126         }
00127 
00128 out:
00129 
00130         return result;
00131 }

Here is the call graph for this function:

static void* test_get_data ( void   )  [inline, static]

Get the private data pointer for the current test.

Returns:
Pointer to arbitrary run-time data for the test currently being run, previously registered using test_set_data().

Definition at line 211 of file suite.h.

References test_priv_data.

00212 {
00213         return test_priv_data;
00214 }

void test_priv_fail ( const struct test_case test,
int  result,
const char *  file,
unsigned int  line,
const char __progmem_arg *  fmt,
  ... 
)
static void test_report_failure ( const struct test_case test,
const char *  stage,
int  result 
) [static]

Definition at line 59 of file suite.c.

References dbg_error, and test_case::name.

Referenced by test_case_run().

00061 {
00062         dbg_error("Test '%s' failed during '%s': %d\n",
00063                         test->name, stage, result);
00064 }

static void test_set_data ( void *  data  )  [inline, static]

Set private data pointer for the current test.

Parameters:
data Pointer to arbitrary run-time data for the test currently being run.

Definition at line 200 of file suite.h.

References test_priv_data.

00201 {
00202         test_priv_data = data;
00203 }

int test_suite_run ( const struct test_suite suite  ) 

Run a test suite.

Run all tests in suite, in the order in which they are found in the array.

Returns:
The number of tests that didn't pass.

Variable Documentation

jmp_buf test_failure_jmpbuf [static]

For internal use only.

Context saved before executing a test or fixture function. Used for doing non-local jumps from the test cases on failure.

Definition at line 57 of file suite.c.

Referenced by test_call().

For internal use only.

See also:
test_set_data(), test_get_data()

Definition at line 50 of file suite.c.

Referenced by test_get_data(), and test_set_data().

For internal use only.

See also:
test_set_data(), test_get_data()

Definition at line 50 of file suite.c.

Referenced by test_get_data(), and test_set_data().

@DOC_TITLE@
Generated on Fri Oct 22 12:15:26 2010 for AVR1300 Using the Xmega ADC by doxygen 1.6.3