This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathoptional_test.d
More file actions
97 lines (87 loc) · 2.49 KB
/
Copy pathoptional_test.d
File metadata and controls
97 lines (87 loc) · 2.49 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import core.stdcpp.optional;
unittest
{
optional!int o1;
optional!int o2 = nullopt;
auto o3 = optional!int(in_place, 10);
assert(!o1 && o2.has_value == false && o3 && o3.value == 10);
o1 = 20;
assert(o1 && o1.value == 20);
o1 = nullopt;
assert(!o1);
int temp = 30;
assert(o1.value_or(temp) == 30);
optional!(void*) o4;
auto o5 = optional!(void*)(in_place, cast(void*)0x1234);
assert(!o4 && o5 && o5.value == cast(void*)0x1234);
o4 = o5;
o5 = null;
assert(o5.value == null);
o5 = o4;
o5.reset();
assert(!o5);
{
optional!Complex o6;
auto o7 = optional!Complex(in_place, Complex(20));
assert(!o6 && o7 && o7.value.buffer[0] == 20 && o7.value.buffer[$-1] == 20);
optional!Complex o8 = o6;
assert(!o8);
optional!Complex o9 = o7;
assert(o9 && o9.value.buffer[0] == 20 && o9.value.buffer[$-1] == 20);
o9 = o6;
assert(!o9);
o6 = o7;
assert(o6 && o6.value.buffer[0] == 20 && o6.value.buffer[$-1] == 20);
o7.reset();
assert(!o7);
assert(callC_val(false, o1, o1, o5, o5, o7, o7) == 0);
assert(callC_val(true, o3, o3, o4, o4, o6, o6) == 0);
}
assert(opt_refCount == 0);
}
extern(C++):
__gshared int opt_refCount = 0;
struct Complex
{
bool valid = false;
int[16] buffer;
this(int val)
{
valid = true;
buffer[] = val;
++opt_refCount;
}
this(ref inout(Complex) rhs) inout
{
valid = rhs.valid;
if (rhs.valid)
{
buffer[] = rhs.buffer[]; ++opt_refCount;
}
}
~this()
{
if (valid)
--opt_refCount;
}
}
int callC_val(bool, optional!int, ref const(optional!int), optional!(void*), ref const(optional!(void*)), optional!Complex, ref const(optional!Complex));
int fromC_val(bool set, optional!int a1, ref const(optional!int) a2,
optional!(void*) a3, ref const(optional!(void*)) a4,
optional!Complex a5, ref const(optional!Complex) a6)
{
if (set)
{
assert(a1 && a1.value == 10);
assert(a2 && a2.value == 10);
assert(a3 && a3.value == cast(void*)0x1234);
assert(a4 && a4.value == cast(void*)0x1234);
assert(a5 && a5.value.buffer[0] == 20 && a5.value.buffer[$-1] == 20);
assert(a6 && a6.value.buffer[0] == 20 && a6.value.buffer[$-1] == 20);
}
else
{
assert(!a1 && !a2 && !a3 && !a4 && !a5 && !a6);
}
return 0;
}