From cb1a40859029f33184355475e51fec95afb79a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Sat, 22 Jul 2023 03:03:09 +0300 Subject: init Just some C wares; hmap, list, optional, unit tests. --- optional/optional-test.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 optional/optional-test.c (limited to 'optional/optional-test.c') diff --git a/optional/optional-test.c b/optional/optional-test.c new file mode 100644 index 0000000..88577d3 --- /dev/null +++ b/optional/optional-test.c @@ -0,0 +1,55 @@ +#include "optional.h" +#include "../utest/utest.h" + +#include + +OPTIONALDEC(int); +OPTIONALDEC(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'); + 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) -- cgit v1.2.3