aboutsummaryrefslogtreecommitdiff
path: root/optional/optional-test.c
blob: 88577d37258bd646ea8698633d281febf5a84633 (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
#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)