Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_variant.d

raw
1 @system unittest
2 {
3 import std.variant;
4
5 Variant a; // Must assign before use, otherwise exception ensues
6 // Initialize with an integer; make the type int
7 Variant b = 42;
8 assert(b.type == typeid(int));
9 // Peek at the value
10 assert(b.peek!(int) !is null && *b.peek!(int) == 42);
11 // Automatically convert per language rules
12 auto x = b.get!(real);
13
14 // Assign any other type, including other variants
15 a = b;
16 a = 3.14;
17 assert(a.type == typeid(double));
18 // Implicit conversions work just as with built-in types
19 assert(a < b);
20 // Check for convertibility
21 assert(!a.convertsTo!(int)); // double not convertible to int
22 // Strings and all other arrays are supported
23 a = "now I'm a string";
24 assert(a == "now I'm a string");
25
26 // can also assign arrays
27 a = new int[42];
28 assert(a.length == 42);
29 a[5] = 7;
30 assert(a[5] == 7);
31
32 // Can also assign class values
33 class Foo {}
34 auto foo = new Foo;
35 a = foo;
36 assert(*a.peek!(Foo) == foo); // and full type information is preserved
37 }
38
39 @safe unittest
40 {
41 import std.variant;
42
43 struct Cat { int a, b, c; }
44
45 align(1) struct S
46 {
47 long l;
48 ubyte b;
49 }
50
51 align(1) struct T
52 {
53 ubyte b;
54 long l;
55 }
56
57 static assert(maxSize!(int, long) == 8);
58 static assert(maxSize!(bool, byte) == 1);
59 static assert(maxSize!(bool, Cat) == 12);
60 static assert(maxSize!(char) == 1);
61 static assert(maxSize!(char, short, ubyte) == 2);
62 static assert(maxSize!(char, long, ubyte) == 8);
63 import std.algorithm.comparison : max;
64 static assert(maxSize!(long, S) == max(long.sizeof, S.sizeof));
65 static assert(maxSize!(S, T) == max(S.sizeof, T.sizeof));
66 static assert(maxSize!(int, ubyte[7]) == 7);
67 static assert(maxSize!(int, ubyte[3]) == 4);
68 static assert(maxSize!(int, int, ubyte[3]) == 4);
69 static assert(maxSize!(void, int, ubyte[3]) == 4);
70 static assert(maxSize!(void) == 1);
71 }
72
73 @system unittest
74 {
75 import std.variant;
76
77 alias Var = VariantN!(maxSize!(int, double, string));
78
79 Var a; // Must assign before use, otherwise exception ensues
80 // Initialize with an integer; make the type int
81 Var b = 42;
82 assert(b.type == typeid(int));
83 // Peek at the value
84 assert(b.peek!(int) !is null && *b.peek!(int) == 42);
85 // Automatically convert per language rules
86 auto x = b.get!(real);
87
88 // Assign any other type, including other variants
89 a = b;
90 a = 3.14;
91 assert(a.type == typeid(double));
92 // Implicit conversions work just as with built-in types
93 assert(a < b);
94 // Check for convertibility
95 assert(!a.convertsTo!(int)); // double not convertible to int
96 // Strings and all other arrays are supported
97 a = "now I'm a string";
98 assert(a == "now I'm a string");
99 }
100
101 @system unittest
102 {
103 import std.variant;
104
105 alias Var = VariantN!(maxSize!(int[]));
106
107 Var a = new int[42];
108 assert(a.length == 42);
109 a[5] = 7;
110 assert(a[5] == 7);
111 }
112
113 @system unittest
114 {
115 import std.variant;
116
117 alias Var = VariantN!(maxSize!(int*)); // classes are pointers
118 Var a;
119
120 class Foo {}
121 auto foo = new Foo;
122 a = foo;
123 assert(*a.peek!(Foo) == foo); // and full type information is preserved
124 }
125
126 @system unittest
127 {
128 import std.variant;
129
130 auto v = Algebraic!(int, double, string)(5);
131 assert(v.peek!(int));
132 v = 3.14;
133 assert(v.peek!(double));
134 // auto x = v.peek!(long); // won't compile, type long not allowed
135 // v = '1'; // won't compile, type char not allowed
136 }
137
138 @system unittest
139 {
140 import std.variant;
141
142 import std.typecons : Tuple, tuple;
143
144 // A tree is either a leaf or a branch of two other trees
145 alias Tree(Leaf) = Algebraic!(Leaf, Tuple!(This*, This*));
146 Tree!int tree = tuple(new Tree!int(42), new Tree!int(43));
147 Tree!int* right = tree.get!1[1];
148 assert(*right == 43);
149
150 // An object is a double, a string, or a hash of objects
151 alias Obj = Algebraic!(double, string, This[string]);
152 Obj obj = "hello";
153 assert(obj.get!1 == "hello");
154 obj = 42.0;
155 assert(obj.get!0 == 42);
156 obj = ["customer": Obj("John"), "paid": Obj(23.95)];
157 assert(obj.get!2["customer"] == "John");
158 }
159
160 @system unittest
161 {
162 import std.variant;
163
164 Variant a; // Must assign before use, otherwise exception ensues
165 // Initialize with an integer; make the type int
166 Variant b = 42;
167 assert(b.type == typeid(int));
168 // Peek at the value
169 assert(b.peek!(int) !is null && *b.peek!(int) == 42);
170 // Automatically convert per language rules
171 auto x = b.get!(real);
172
173 // Assign any other type, including other variants
174 a = b;
175 a = 3.14;
176 assert(a.type == typeid(double));
177 // Implicit conversions work just as with built-in types
178 assert(a < b);
179 // Check for convertibility
180 assert(!a.convertsTo!(int)); // double not convertible to int
181 // Strings and all other arrays are supported
182 a = "now I'm a string";
183 assert(a == "now I'm a string");
184 }
185
186 @system unittest
187 {
188 import std.variant;
189
190 Variant a = new int[42];
191 assert(a.length == 42);
192 a[5] = 7;
193 assert(a[5] == 7);
194 }
195
196 @system unittest
197 {
198 import std.variant;
199
200 Variant a;
201
202 class Foo {}
203 auto foo = new Foo;
204 a = foo;
205 assert(*a.peek!(Foo) == foo); // and full type information is preserved
206 }
207
208 @system unittest
209 {
210 import std.variant;
211
212 auto a = variantArray(1, 3.14, "Hi!");
213 assert(a[1] == 3.14);
214 auto b = Variant(a); // variant array as variant
215 assert(b[1] == 3.14);
216 }
217
218 @system unittest
219 {
220 import std.variant;
221
222 import std.exception : assertThrown;
223
224 Variant v;
225
226 // uninitialized use
227 assertThrown!VariantException(v + 1);
228 assertThrown!VariantException(v.length);
229
230 // .get with an incompatible target type
231 assertThrown!VariantException(Variant("a").get!int);
232
233 // comparison between incompatible types
234 assertThrown!VariantException(Variant(3) < Variant("a"));
235 }
236
237 @system unittest
238 {
239 import std.variant;
240
241 Algebraic!(int, string) variant;
242
243 variant = 10;
244 assert(variant.visit!((string s) => cast(int) s.length,
245 (int i) => i)()
246 == 10);
247 variant = "string";
248 assert(variant.visit!((int i) => i,
249 (string s) => cast(int) s.length)()
250 == 6);
251
252 // Error function usage
253 Algebraic!(int, string) emptyVar;
254 auto rslt = emptyVar.visit!((string s) => cast(int) s.length,
255 (int i) => i,
256 () => -1)();
257 assert(rslt == -1);
258
259 // Generic function usage
260 Algebraic!(int, float, real) number = 2;
261 assert(number.visit!(x => x += 1) == 3);
262
263 // Generic function for int/float with separate behavior for string
264 Algebraic!(int, float, string) something = 2;
265 assert(something.visit!((string s) => s.length, x => x) == 2); // generic
266 something = "asdf";
267 assert(something.visit!((string s) => s.length, x => x) == 4); // string
268
269 // Generic handler and empty handler
270 Algebraic!(int, float, real) empty2;
271 assert(empty2.visit!(x => x + 1, () => -1) == -1);
272 }
273
274 @system unittest
275 {
276 import std.variant;
277
278 Algebraic!(int, string) variant;
279
280 variant = 10;
281 auto which = -1;
282 variant.tryVisit!((int i) { which = 0; })();
283 assert(which == 0);
284
285 // Error function usage
286 variant = "test";
287 variant.tryVisit!((int i) { which = 0; },
288 () { which = -100; })();
289 assert(which == -100);
290 }
291