aboutsummaryrefslogtreecommitdiff
path: root/optional/optional-test.c
blob: 73597e5d9f0833a8b9e7dd2699c59cab374b3803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "optional.h"
#include "../utest/utest.h"

#include <stdio.h>
#include <string.h>

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)