this repo has no description
1#include "fmt.h"
2
3#include <stdbool.h>
4#include <string.h>
5
6#define ASSERT(cond) assertTrue(cond, #cond)
7
8#define TEST(expected, fmt, ...) \
9 do { \
10 str_size result = format(fmt, __VA_ARGS__); \
11 ASSERT(result.length == strlen(expected)); \
12 ASSERT(result.str != NULL); \
13 ASSERT(strncmp(expected, result.str, strlen(expected)) == 0); \
14 str_size_free(result); \
15 } while (0)
16
17void assertTrue(bool cond, const char *cond_str) {
18 if (!cond) {
19 fprintf(stderr, "Assertion failure: %s is not true!\n", cond_str);
20 abort();
21 }
22}
23
24int main() {
25 do {
26 str_size result = format("hello");
27 ASSERT(result.length == 5);
28 ASSERT(result.str != NULL);
29 ASSERT(strncmp("hello", result.str, 5) == 0);
30 str_size_free(result);
31 } while (0);
32
33 TEST("hello 42", "hello %d", 42);
34 TEST("x hello 42", "%s hello %d", "x", 42);
35}