#include "optional.h" #include "../utest/utest.h" #include #include DECL_OPTIONAL(int); DECL_OPTIONAL(char); DECL_NAMED_OPTIONAL(str, char *); struct my_struct { OPTIONAL(int) a; OPTIONAL(int) b; }; void optput(OPTIONAL(char) * dst, char src) { opt_set_some(*dst, src); } TEST_BEGIN(test_optional_example) { int a, b; struct my_struct my = { .a = OPTSOME(69), .b = OPTNONE, }; OPTIONAL(char) y = OPTNONE; OPTIONAL(char) z = OPTSOME('z'); OPTIONAL(str) s = OPTSOME("hello, world!"); expect(!strcmp(opt_default(s, "NULL!"), "hello, world!"), "expected to contain string \"hello, world!\""); float xy, xz; expect(opt_has(my.a), "expected to contain something"); expect(opt_unwrap(my.a, a), "expected to contain something"); asserteq(a, 69); expect(!opt_unwrap(my.b, b), "expected to contain nothing"); optput(&y, 'y'); opt_set_none(z); expect(opt_unwrap(y, xy), "expected to contain something"); asserteq(xy, 'y'); expect(!opt_unwrap(z, xz), "expected to contain nothing"); xy = opt_default(y, 'd'), xz = opt_default(z, 'd'); asserteq(xy, 'y'); asserteq(xz, 'd'); TEST_OUT } TEST_END RUN_TESTS(test_optional_example)