diff options
Diffstat (limited to 'tests/unit/test_framework.h')
| -rw-r--r-- | tests/unit/test_framework.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/unit/test_framework.h b/tests/unit/test_framework.h new file mode 100644 index 0000000..6eb3a10 --- /dev/null +++ b/tests/unit/test_framework.h | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #ifndef TEST_FRAMEWORK_H | ||
| 2 | #define TEST_FRAMEWORK_H | ||
| 3 | |||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | static int g_tests_passed = 0; | ||
| 9 | static int g_tests_failed = 0; | ||
| 10 | |||
| 11 | #define ASSERT(cond, msg) do { \ | ||
| 12 | if (cond) { \ | ||
| 13 | printf(" PASS: %s\n", msg); \ | ||
| 14 | g_tests_passed++; \ | ||
| 15 | } else { \ | ||
| 16 | printf(" FAIL: %s (at %s:%d)\n", msg, __FILE__, __LINE__); \ | ||
| 17 | g_tests_failed++; \ | ||
| 18 | } \ | ||
| 19 | } while(0) | ||
| 20 | |||
| 21 | #define ASSERT_EQ_INT(expected, actual, msg) do { \ | ||
| 22 | int _e = (expected), _a = (actual); \ | ||
| 23 | if (_e == _a) { \ | ||
| 24 | printf(" PASS: %s (got %d)\n", msg, _a); \ | ||
| 25 | g_tests_passed++; \ | ||
| 26 | } else { \ | ||
| 27 | printf(" FAIL: %s (expected %d, got %d) at %s:%d\n", msg, _e, _a, __FILE__, __LINE__); \ | ||
| 28 | g_tests_failed++; \ | ||
| 29 | } \ | ||
| 30 | } while(0) | ||
| 31 | |||
| 32 | #define ASSERT_EQ_STR(expected, actual, msg) do { \ | ||
| 33 | const char *_e = (expected), *_a = (actual); \ | ||
| 34 | if (_e && _a && strcmp(_e, _a) == 0) { \ | ||
| 35 | printf(" PASS: %s (got \"%s\")\n", msg, _a); \ | ||
| 36 | g_tests_passed++; \ | ||
| 37 | } else { \ | ||
| 38 | printf(" FAIL: %s (expected \"%s\", got \"%s\") at %s:%d\n", msg, _e ? _e : "(null)", _a ? _a : "(null)", __FILE__, __LINE__); \ | ||
| 39 | g_tests_failed++; \ | ||
| 40 | } \ | ||
| 41 | } while(0) | ||
| 42 | |||
| 43 | #define ASSERT_MEM_EQ(expected, actual, len, msg) do { \ | ||
| 44 | const uint8_t *_e = (const uint8_t *)(expected), *_a = (const uint8_t *)(actual); \ | ||
| 45 | size_t _l = (len); \ | ||
| 46 | if (_e && _a && memcmp(_e, _a, _l) == 0) { \ | ||
| 47 | printf(" PASS: %s (%zu bytes match)\n", msg, _l); \ | ||
| 48 | g_tests_passed++; \ | ||
| 49 | } else { \ | ||
| 50 | printf(" FAIL: %s (%zu bytes mismatch) at %s:%d\n", msg, _l, __FILE__, __LINE__); \ | ||
| 51 | g_tests_failed++; \ | ||
| 52 | } \ | ||
| 53 | } while(0) | ||
| 54 | |||
| 55 | #define TEST_SUMMARY() do { \ | ||
| 56 | printf("\n=== Results: %d passed, %d failed ===\n", g_tests_passed, g_tests_failed); \ | ||
| 57 | return g_tests_failed > 0 ? 1 : 0; \ | ||
| 58 | } while(0) | ||
| 59 | |||
| 60 | #endif | ||