2012-08-18 16:07:05 +00:00
|
|
|
/* Note VERBOSE needs to be (un)defined before including this file */
|
|
|
|
|
|
|
|
#ifndef LIBTRANSMISSION_TEST_H
|
|
|
|
#define LIBTRANSMISSION_TEST_H 1
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-10-14 17:59:24 +00:00
|
|
|
#include "utils.h" /* tr_strcmp0() */
|
2012-08-18 16:07:05 +00:00
|
|
|
|
2012-10-14 17:59:24 +00:00
|
|
|
static int current_test = 0;
|
|
|
|
|
|
|
|
#define REPORT_TEST(test, result) \
|
|
|
|
fprintf( stderr, "%s %s:%d\n", result, __FILE__, __LINE__ )
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
should_print (bool pass)
|
|
|
|
{
|
|
|
|
if (!pass)
|
|
|
|
return true;
|
2012-08-18 16:07:05 +00:00
|
|
|
|
|
|
|
#ifdef VERBOSE
|
2012-10-14 17:59:24 +00:00
|
|
|
return true;
|
2012-08-18 16:07:05 +00:00
|
|
|
#else
|
2012-10-14 17:59:24 +00:00
|
|
|
return false;
|
2012-08-18 16:07:05 +00:00
|
|
|
#endif
|
2012-10-14 17:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
check_condition_impl (const char * file, int line, bool condition)
|
|
|
|
{
|
|
|
|
const bool pass = condition;
|
|
|
|
|
|
|
|
if (should_print (pass))
|
|
|
|
fprintf (stderr, "%s %s:%d\n", pass?"PASS":"FAIL", file, line);
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
check_streq_impl (const char * file, int line, const char * expected, const char * actual)
|
|
|
|
{
|
|
|
|
const bool pass = !tr_strcmp0 (expected, actual);
|
|
|
|
|
|
|
|
if (should_print (pass)) {
|
|
|
|
if (pass)
|
|
|
|
fprintf (stderr, "PASS %s:%d\n", file, line);
|
|
|
|
else
|
|
|
|
fprintf (stderr, "FAIL %s:%d, expected \"%s\", got \"%s\"\n", file, line, expected?expected:"(null)", actual?actual:"(null)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
check_eq_impl (const char * file, int line, int64_t expected, int64_t actual)
|
|
|
|
{
|
|
|
|
const bool pass = expected == actual;
|
|
|
|
|
|
|
|
if (should_print (pass)) {
|
|
|
|
if (pass)
|
|
|
|
fprintf (stderr, "PASS %s:%d\n", file, line);
|
|
|
|
else
|
|
|
|
fprintf (stderr, "FAIL %s:%d, expected \"%"PRId64"\", got \"%"PRId64"\"\n", file, line, expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define check(condition) \
|
|
|
|
do { \
|
|
|
|
++current_test; \
|
|
|
|
if (!check_condition_impl (__FILE__, __LINE__, (condition))) \
|
|
|
|
return current_test; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define check_streq(expected, actual) \
|
|
|
|
do { \
|
|
|
|
++current_test; \
|
|
|
|
if (!check_streq_impl (__FILE__, __LINE__, (expected), (actual))) \
|
|
|
|
return current_test; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define check_int_eq(expected, actual) \
|
|
|
|
do { \
|
|
|
|
++current_test; \
|
|
|
|
if (!check_eq_impl (__FILE__, __LINE__, (expected), (actual))) \
|
|
|
|
return current_test; \
|
|
|
|
} while (0)
|
2012-08-18 16:07:05 +00:00
|
|
|
|
2012-10-14 17:59:24 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2012-08-18 16:07:05 +00:00
|
|
|
|
|
|
|
typedef int (*testFunc)( void );
|
|
|
|
#define NUM_TESTS(tarray) ((int) (sizeof(tarray)/sizeof(tarray[0])))
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
runTests( const testFunc * const tests, int numTests )
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
|
2012-10-14 17:59:24 +00:00
|
|
|
(void) current_test; /* Use test even if we don't have any tests to run */
|
2012-08-18 16:07:05 +00:00
|
|
|
|
|
|
|
for( i = 0; i < numTests; i++ )
|
|
|
|
if( (ret = (*tests[i])()) )
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0; /* All tests passed */
|
|
|
|
}
|
|
|
|
|
2012-10-14 17:59:24 +00:00
|
|
|
#define MAIN_SINGLE_TEST(test) \
|
|
|
|
int main( void ) { \
|
|
|
|
const testFunc tests[] = { test }; \
|
|
|
|
return runTests(tests, 1); \
|
2012-08-18 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !LIBTRANSMISSION_TEST_H */
|