From d3a1b908fd3b0c47da2c33c16d18ebeb00da3b97 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 4 Sep 2026 05:46:36 +0200 Subject: refactor: reorganize tests --- test/macro_demo.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/macro_demo.c (limited to 'test/macro_demo.c') diff --git a/test/macro_demo.c b/test/macro_demo.c new file mode 100644 index 0000000..072ec02 --- /dev/null +++ b/test/macro_demo.c @@ -0,0 +1,47 @@ +// a playground for the macro unwrapper. open this file in hsdbg's source view, +// then hover any highlighted macro for a preview and click it to step through +// the expansion one layer at a time in the macros panel. + +#include + +// object-like macros that lean on each other, so unrolling takes real layers: +// KIB -> (1 << 10), MIB -> (KIB * KIB) -> ((1 << 10) * (1 << 10)), and so on +#define KIB (1 << 10) +#define MIB (KIB * KIB) +#define GIB (MIB * KIB) + +// function-like macros, including one built out of another +#define SQUARE(x) ((x) * (x)) +#define CUBE(x) (SQUARE(x) * (x)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define CLAMP(v, lo, hi) MAX(lo, MIN(v, hi)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +// the classic stringize-through-indirection: STR sees its argument raw, XSTR +// expands it first. hover both to see "VERSION" vs "3" +#define VERSION 3 +#define STR(x) #x +#define XSTR(x) STR(x) + +// token pasting, direct and through a level of indirection +#define CAT(a, b) a ## b +#define XCAT(a, b) CAT(a, b) + +// a variadic logging macro +#define LOG(level, fmt, ...) fprintf(stderr, "[" level "] " fmt "\n", __VA_ARGS__) + +int main(void) +{ + int width = 5; + int height = 9; + + printf("a mebibyte is %d bytes\n", MIB); + printf("square %d, cube %d\n", SQUARE(width), CUBE(width)); + printf("clamped %d\n", CLAMP(height, 0, 8)); + printf("building version %s\n", XSTR(VERSION)); + + int XCAT(sum_, 1) = width + height; + LOG("info", "sum is %d", XCAT(sum_, 1)); + + return 0; +} -- cgit v1.3