aboutsummaryrefslogtreecommitdiff
path: root/optional/optional-test.c
diff options
context:
space:
mode:
Diffstat (limited to 'optional/optional-test.c')
-rw-r--r--optional/optional-test.c55
1 files changed, 55 insertions, 0 deletions
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 <stdio.h>
+
+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)