Echo Writes Code

test_option.cpp

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
#include "crucible/core.hpp"
#include "crucible/test.hpp"

namespace test_option
{
	CRUCIBLE_TEST_SCENARIO(option_states)
	{
		crucible::option<int> op { crucible::make_some(5) };
		CRUCIBLE_ASSERT(op.is_some());
		CRUCIBLE_ASSERT_NOT(op.is_none());
		CRUCIBLE_ASSERT_EQ(op.value(), 5);

		op = crucible::make_some(10);
		CRUCIBLE_ASSERT(op.is_some());
		CRUCIBLE_ASSERT_NOT(op.is_none());
		CRUCIBLE_ASSERT_EQ(op.value(), 10);

		op = crucible::make_none();
		CRUCIBLE_ASSERT_NOT(op.is_some());
		CRUCIBLE_ASSERT(op.is_none());
	}

	CRUCIBLE_TEST_SCENARIO(option_references)
	{
		{
			int x { 5 };
			crucible::option op { crucible::make_some(crucible::mutable_borrow(x)) };
			CRUCIBLE_ASSERT_EQ(op.value().get(), 5);
			op.value().get() = 15;
			CRUCIBLE_ASSERT_EQ(x, 15);
		}

		{
			int const x { 10 };
			crucible::option op { crucible::make_some(crucible::immutable_borrow(x)) };
			CRUCIBLE_ASSERT_EQ(op.value().get(), 10);

			int y { 15 };
			op = crucible::make_some(crucible::immutable_borrow(y));
			CRUCIBLE_ASSERT_EQ(op.value().get(), 15);
		}
	}
}

CRUCIBLE_TEST_MAIN