mirror of
https://github.com/transmission/transmission
synced 2024-12-27 01:57:52 +00:00
steal some ideas about unit test error logging from Google Test
This commit is contained in:
parent
83d89cd33a
commit
a683ad49a8
1 changed files with 89 additions and 28 deletions
|
@ -6,33 +6,96 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static int test = 0;
|
||||
#include "transmission.h"
|
||||
#include "utils.h" /* tr_strcmp0() */
|
||||
|
||||
#define REPORT_TEST(test, res) \
|
||||
fprintf( stderr, "%s test #%d (%s, %d)\n", res, test, __FILE__, __LINE__ )
|
||||
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;
|
||||
|
||||
#ifdef VERBOSE
|
||||
#define check( A ) \
|
||||
do { \
|
||||
++test; \
|
||||
if( A ) \
|
||||
REPORT_TEST(test, "PASS"); \
|
||||
else { \
|
||||
REPORT_TEST(test, "FAIL"); \
|
||||
return test; \
|
||||
} \
|
||||
} while(0)
|
||||
return true;
|
||||
#else
|
||||
#define check( A ) \
|
||||
do { \
|
||||
++test; \
|
||||
if( !( A ) ){ \
|
||||
REPORT_TEST(test, "FAIL"); \
|
||||
return test; \
|
||||
} \
|
||||
} while(0)
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
||||
typedef int (*testFunc)( void );
|
||||
#define NUM_TESTS(tarray) ((int) (sizeof(tarray)/sizeof(tarray[0])))
|
||||
|
@ -42,7 +105,7 @@ runTests( const testFunc * const tests, int numTests )
|
|||
{
|
||||
int ret, i;
|
||||
|
||||
(void) test; /* Use test even if we don't have any tests to run */
|
||||
(void) current_test; /* Use test even if we don't have any tests to run */
|
||||
|
||||
for( i = 0; i < numTests; i++ )
|
||||
if( (ret = (*tests[i])()) )
|
||||
|
@ -51,12 +114,10 @@ runTests( const testFunc * const tests, int numTests )
|
|||
return 0; /* All tests passed */
|
||||
}
|
||||
|
||||
#define MAIN_SINGLE_TEST(test) \
|
||||
int main( void ) \
|
||||
{ \
|
||||
const testFunc tests[] = { test }; \
|
||||
\
|
||||
return runTests(tests, 1); \
|
||||
#define MAIN_SINGLE_TEST(test) \
|
||||
int main( void ) { \
|
||||
const testFunc tests[] = { test }; \
|
||||
return runTests(tests, 1); \
|
||||
}
|
||||
|
||||
#endif /* !LIBTRANSMISSION_TEST_H */
|
||||
|
|
Loading…
Reference in a new issue