/* taptap.h -- simplistic Test Anything Protocol generator. */ #ifndef HEADER_TAPTAP_H #define HEADER_TAPTAP_H #define TAPTAP_VERSION "0.2" #include #include static int _taptap_counter = 0; static int _taptap_fail_counter = 0; static int _taptap_planned = -1; #define plan(n) do { _taptap_planned = n; printf ("1..%d\n", n); } while(0) #define ok(expr, ...) \ do { \ _taptap_counter++; \ if (expr) \ { \ if (#__VA_ARGS__[0]) \ printf ("ok %-8d - %s\n", _taptap_counter, "" __VA_ARGS__); \ else \ printf ("ok %d\n", _taptap_counter); \ } \ else \ { \ _taptap_fail_counter++; \ if (#__VA_ARGS__[0]) \ printf ("not ok %-4d - %s:%d: %s failed - %s\n", \ _taptap_counter, __FILE__, __LINE__, # expr, \ "" __VA_ARGS__); \ else \ printf ("not ok %-4d - %s:%d: %s failed\n", \ _taptap_counter, __FILE__, __LINE__, # expr); \ } \ } while(0) #define is(got, expected, ...) \ do { \ long _got = (got); \ long _expected = (expected); \ _taptap_counter++; \ if (_expected == _got) \ { \ if (#__VA_ARGS__[0]) \ printf ("ok %-8d - %s\n", _taptap_counter, "" __VA_ARGS__); \ else \ printf ("ok %d\n", _taptap_counter); \ } \ else \ { \ _taptap_fail_counter++; \ if (#__VA_ARGS__[0]) \ printf ("not ok %-4d - %s:%d: %s(%ld, %p) != %s - %s\n", \ _taptap_counter, __FILE__, __LINE__, \ # got, _got, (void*) _got, # expected, \ "" __VA_ARGS__); \ else \ printf ("not ok %-4d - %s:%d: %s(%ld, %p) != %s\n", \ _taptap_counter, __FILE__, __LINE__, \ # got, _got, (void*) _got, # expected); \ } \ } while(0) #define fail(...) \ do { \ _taptap_counter++; \ _taptap_fail_counter++; \ if (#__VA_ARGS__[0]) \ printf ("not ok %-4d - %s:%d - %s\n", \ _taptap_counter, __FILE__, __LINE__, "" __VA_ARGS__); \ else \ printf ("not ok %-4d - %s:%d\n", \ _taptap_counter, \ __FILE__, __LINE__); \ } while(0) #define pass(...) do { ok(1, __VA_ARGS__); } while(0) static inline int taptap_summary() { if (_taptap_fail_counter == 0) printf ("# All %d tests passed.\n", _taptap_counter); else printf ("# %d of %d tests FAILED. %02.2f%% passed.\n", _taptap_fail_counter, _taptap_counter, (0.0 + _taptap_counter - _taptap_fail_counter) / _taptap_counter*100); if (_taptap_counter != _taptap_planned && _taptap_planned > 0) printf ("# You planned %d tests but ran %d.\n", _taptap_planned, _taptap_counter); return _taptap_fail_counter == 0 ? 0 : 1; } #define bail_out(reason) \ do { \ printf ("Bail out! %s\n", reason); \ taptap_summary(); \ exit (255); \ } while(0) #endif /* !defined HEADER_TAPTAP_H */