|
| 1 | +/* |
| 2 | + * Minimal Unit Test Framework for DWGSIM |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * #include "test_framework.h" |
| 6 | + * |
| 7 | + * TEST(test_name) { |
| 8 | + * ASSERT(condition); |
| 9 | + * ASSERT_EQ(expected, actual); |
| 10 | + * ASSERT_STR_EQ(expected, actual); |
| 11 | + * } |
| 12 | + * |
| 13 | + * int main(void) { |
| 14 | + * RUN_TEST(test_name); |
| 15 | + * TEST_SUMMARY(); |
| 16 | + * return test_failures; |
| 17 | + * } |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef TEST_FRAMEWORK_H |
| 21 | +#define TEST_FRAMEWORK_H |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <string.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <math.h> |
| 27 | + |
| 28 | +/* Global test counters */ |
| 29 | +static int test_count = 0; |
| 30 | +static int test_failures = 0; |
| 31 | +static int test_assertions = 0; |
| 32 | +static const char *current_test = NULL; |
| 33 | + |
| 34 | +/* Test function signature */ |
| 35 | +typedef void (*test_fn)(void); |
| 36 | + |
| 37 | +/* Define a test function */ |
| 38 | +#define TEST(name) static void name(void) |
| 39 | + |
| 40 | +/* Run a test */ |
| 41 | +#define RUN_TEST(name) do { \ |
| 42 | + current_test = #name; \ |
| 43 | + test_count++; \ |
| 44 | + int prev_failures = test_failures; \ |
| 45 | + name(); \ |
| 46 | + if (test_failures == prev_failures) { \ |
| 47 | + printf(" [PASS] %s\n", #name); \ |
| 48 | + } \ |
| 49 | +} while (0) |
| 50 | + |
| 51 | +/* Print test summary */ |
| 52 | +#define TEST_SUMMARY() do { \ |
| 53 | + printf("\n----------------------------------------\n"); \ |
| 54 | + printf("Tests: %d | Passed: %d | Failed: %d | Assertions: %d\n", \ |
| 55 | + test_count, test_count - test_failures, test_failures, test_assertions); \ |
| 56 | + printf("----------------------------------------\n"); \ |
| 57 | +} while (0) |
| 58 | + |
| 59 | +/* Basic assertion */ |
| 60 | +#define ASSERT(cond) do { \ |
| 61 | + test_assertions++; \ |
| 62 | + if (!(cond)) { \ |
| 63 | + printf(" [FAIL] %s\n", current_test); \ |
| 64 | + printf(" Assertion failed: %s\n", #cond); \ |
| 65 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 66 | + test_failures++; \ |
| 67 | + return; \ |
| 68 | + } \ |
| 69 | +} while (0) |
| 70 | + |
| 71 | +/* Assert with message */ |
| 72 | +#define ASSERT_MSG(cond, msg) do { \ |
| 73 | + test_assertions++; \ |
| 74 | + if (!(cond)) { \ |
| 75 | + printf(" [FAIL] %s\n", current_test); \ |
| 76 | + printf(" %s\n", msg); \ |
| 77 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 78 | + test_failures++; \ |
| 79 | + return; \ |
| 80 | + } \ |
| 81 | +} while (0) |
| 82 | + |
| 83 | +/* Assert equality for integers */ |
| 84 | +#define ASSERT_EQ(expected, actual) do { \ |
| 85 | + test_assertions++; \ |
| 86 | + long long _exp = (long long)(expected); \ |
| 87 | + long long _act = (long long)(actual); \ |
| 88 | + if (_exp != _act) { \ |
| 89 | + printf(" [FAIL] %s\n", current_test); \ |
| 90 | + printf(" Expected: %lld, Actual: %lld\n", _exp, _act); \ |
| 91 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 92 | + test_failures++; \ |
| 93 | + return; \ |
| 94 | + } \ |
| 95 | +} while (0) |
| 96 | + |
| 97 | +/* Assert inequality */ |
| 98 | +#define ASSERT_NE(not_expected, actual) do { \ |
| 99 | + test_assertions++; \ |
| 100 | + long long _nexp = (long long)(not_expected); \ |
| 101 | + long long _act = (long long)(actual); \ |
| 102 | + if (_nexp == _act) { \ |
| 103 | + printf(" [FAIL] %s\n", current_test); \ |
| 104 | + printf(" Expected NOT: %lld, but got: %lld\n", _nexp, _act); \ |
| 105 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 106 | + test_failures++; \ |
| 107 | + return; \ |
| 108 | + } \ |
| 109 | +} while (0) |
| 110 | + |
| 111 | +/* Assert string equality */ |
| 112 | +#define ASSERT_STR_EQ(expected, actual) do { \ |
| 113 | + test_assertions++; \ |
| 114 | + const char *_exp = (expected); \ |
| 115 | + const char *_act = (actual); \ |
| 116 | + if (_exp == NULL || _act == NULL || strcmp(_exp, _act) != 0) { \ |
| 117 | + printf(" [FAIL] %s\n", current_test); \ |
| 118 | + printf(" Expected: \"%s\", Actual: \"%s\"\n", \ |
| 119 | + _exp ? _exp : "(null)", _act ? _act : "(null)"); \ |
| 120 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 121 | + test_failures++; \ |
| 122 | + return; \ |
| 123 | + } \ |
| 124 | +} while (0) |
| 125 | + |
| 126 | +/* Assert floating point equality within epsilon */ |
| 127 | +#define ASSERT_FLOAT_EQ(expected, actual, epsilon) do { \ |
| 128 | + test_assertions++; \ |
| 129 | + double _exp = (double)(expected); \ |
| 130 | + double _act = (double)(actual); \ |
| 131 | + double _eps = (double)(epsilon); \ |
| 132 | + if (fabs(_exp - _act) > _eps) { \ |
| 133 | + printf(" [FAIL] %s\n", current_test); \ |
| 134 | + printf(" Expected: %g, Actual: %g (epsilon: %g)\n", _exp, _act, _eps); \ |
| 135 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 136 | + test_failures++; \ |
| 137 | + return; \ |
| 138 | + } \ |
| 139 | +} while (0) |
| 140 | + |
| 141 | +/* Assert pointer is not NULL */ |
| 142 | +#define ASSERT_NOT_NULL(ptr) do { \ |
| 143 | + test_assertions++; \ |
| 144 | + if ((ptr) == NULL) { \ |
| 145 | + printf(" [FAIL] %s\n", current_test); \ |
| 146 | + printf(" Expected non-NULL pointer\n"); \ |
| 147 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 148 | + test_failures++; \ |
| 149 | + return; \ |
| 150 | + } \ |
| 151 | +} while (0) |
| 152 | + |
| 153 | +/* Assert pointer is NULL */ |
| 154 | +#define ASSERT_NULL(ptr) do { \ |
| 155 | + test_assertions++; \ |
| 156 | + if ((ptr) != NULL) { \ |
| 157 | + printf(" [FAIL] %s\n", current_test); \ |
| 158 | + printf(" Expected NULL pointer\n"); \ |
| 159 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 160 | + test_failures++; \ |
| 161 | + return; \ |
| 162 | + } \ |
| 163 | +} while (0) |
| 164 | + |
| 165 | +/* Assert a < b */ |
| 166 | +#define ASSERT_LT(a, b) do { \ |
| 167 | + test_assertions++; \ |
| 168 | + long long _a = (long long)(a); \ |
| 169 | + long long _b = (long long)(b); \ |
| 170 | + if (!(_a < _b)) { \ |
| 171 | + printf(" [FAIL] %s\n", current_test); \ |
| 172 | + printf(" Expected %lld < %lld\n", _a, _b); \ |
| 173 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 174 | + test_failures++; \ |
| 175 | + return; \ |
| 176 | + } \ |
| 177 | +} while (0) |
| 178 | + |
| 179 | +/* Assert a <= b */ |
| 180 | +#define ASSERT_LE(a, b) do { \ |
| 181 | + test_assertions++; \ |
| 182 | + long long _a = (long long)(a); \ |
| 183 | + long long _b = (long long)(b); \ |
| 184 | + if (!(_a <= _b)) { \ |
| 185 | + printf(" [FAIL] %s\n", current_test); \ |
| 186 | + printf(" Expected %lld <= %lld\n", _a, _b); \ |
| 187 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 188 | + test_failures++; \ |
| 189 | + return; \ |
| 190 | + } \ |
| 191 | +} while (0) |
| 192 | + |
| 193 | +/* Assert a > b */ |
| 194 | +#define ASSERT_GT(a, b) do { \ |
| 195 | + test_assertions++; \ |
| 196 | + long long _a = (long long)(a); \ |
| 197 | + long long _b = (long long)(b); \ |
| 198 | + if (!(_a > _b)) { \ |
| 199 | + printf(" [FAIL] %s\n", current_test); \ |
| 200 | + printf(" Expected %lld > %lld\n", _a, _b); \ |
| 201 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 202 | + test_failures++; \ |
| 203 | + return; \ |
| 204 | + } \ |
| 205 | +} while (0) |
| 206 | + |
| 207 | +/* Assert a >= b */ |
| 208 | +#define ASSERT_GE(a, b) do { \ |
| 209 | + test_assertions++; \ |
| 210 | + long long _a = (long long)(a); \ |
| 211 | + long long _b = (long long)(b); \ |
| 212 | + if (!(_a >= _b)) { \ |
| 213 | + printf(" [FAIL] %s\n", current_test); \ |
| 214 | + printf(" Expected %lld >= %lld\n", _a, _b); \ |
| 215 | + printf(" at %s:%d\n", __FILE__, __LINE__); \ |
| 216 | + test_failures++; \ |
| 217 | + return; \ |
| 218 | + } \ |
| 219 | +} while (0) |
| 220 | + |
| 221 | +/* Test suite name header */ |
| 222 | +#define TEST_SUITE(name) printf("\n=== %s ===\n", name) |
| 223 | + |
| 224 | +#endif /* TEST_FRAMEWORK_H */ |
0 commit comments