102 lines
2.5 KiB
C
102 lines
2.5 KiB
C
|
|
#ifndef TEST_FRAMEWORK_H
|
||
|
|
#define TEST_FRAMEWORK_H
|
||
|
|
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <assert.h>
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
int passed;
|
||
|
|
int failed;
|
||
|
|
int total;
|
||
|
|
const char *current_test;
|
||
|
|
} test_state_t;
|
||
|
|
|
||
|
|
static test_state_t test_state = {0, 0, 0, NULL};
|
||
|
|
|
||
|
|
#define TEST_ASSERT(condition, message) \
|
||
|
|
do { \
|
||
|
|
if (!(condition)) { \
|
||
|
|
fprintf(stderr, " ✗ FAIL: %s\n", message); \
|
||
|
|
test_state.failed++; \
|
||
|
|
} else { \
|
||
|
|
test_state.passed++; \
|
||
|
|
} \
|
||
|
|
test_state.total++; \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define ASSERT_EQ(a, b) \
|
||
|
|
do { \
|
||
|
|
if ((a) != (b)) { \
|
||
|
|
fprintf(stderr, " ✗ FAIL: %ld != %ld\n", (long)(a), (long)(b)); \
|
||
|
|
test_state.failed++; \
|
||
|
|
} else { \
|
||
|
|
test_state.passed++; \
|
||
|
|
} \
|
||
|
|
test_state.total++; \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define ASSERT_EQ_STR(a, b) \
|
||
|
|
do { \
|
||
|
|
if (strcmp((a), (b)) != 0) { \
|
||
|
|
fprintf(stderr, " ✗ FAIL: '%s' != '%s'\n", (a), (b)); \
|
||
|
|
test_state.failed++; \
|
||
|
|
} else { \
|
||
|
|
test_state.passed++; \
|
||
|
|
} \
|
||
|
|
test_state.total++; \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define ASSERT_NULL(ptr) \
|
||
|
|
do { \
|
||
|
|
if ((ptr) != NULL) { \
|
||
|
|
fprintf(stderr, " ✗ FAIL: pointer is not NULL\n"); \
|
||
|
|
test_state.failed++; \
|
||
|
|
} else { \
|
||
|
|
test_state.passed++; \
|
||
|
|
} \
|
||
|
|
test_state.total++; \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define ASSERT_NOT_NULL(ptr) \
|
||
|
|
do { \
|
||
|
|
if ((ptr) == NULL) { \
|
||
|
|
fprintf(stderr, " ✗ FAIL: pointer is NULL\n"); \
|
||
|
|
test_state.failed++; \
|
||
|
|
} else { \
|
||
|
|
test_state.passed++; \
|
||
|
|
} \
|
||
|
|
test_state.total++; \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define TEST_BEGIN(name) \
|
||
|
|
do { \
|
||
|
|
test_state.current_test = (name); \
|
||
|
|
printf("TEST: %s\n", (name)); \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define TEST_END \
|
||
|
|
do { \
|
||
|
|
printf("\n"); \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#define TEST_SUMMARY \
|
||
|
|
do { \
|
||
|
|
printf("\n========================================\n"); \
|
||
|
|
printf("Test Summary:\n"); \
|
||
|
|
printf(" Passed: %d\n", test_state.passed); \
|
||
|
|
printf(" Failed: %d\n", test_state.failed); \
|
||
|
|
printf(" Total: %d\n", test_state.total); \
|
||
|
|
printf("========================================\n"); \
|
||
|
|
if (test_state.failed > 0) { \
|
||
|
|
printf("❌ %d test(s) failed\n", test_state.failed); \
|
||
|
|
return 1; \
|
||
|
|
} else { \
|
||
|
|
printf("✓ All tests passed\n"); \
|
||
|
|
return 0; \
|
||
|
|
} \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#endif
|