Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_meta.d

raw
1 @safe unittest
2 {
3 import std.meta;
4
5 import std.meta;
6 alias TL = AliasSeq!(int, double);
7
8 int foo(TL td) // same as int foo(int, double);
9 {
10 return td[0] + cast(int) td[1];
11 }
12 }
13
14 @safe unittest
15 {
16 import std.meta;
17
18 alias TL = AliasSeq!(int, double);
19
20 alias Types = AliasSeq!(TL, char);
21 static assert(is(Types == AliasSeq!(int, double, char)));
22 }
23
24 @safe unittest
25 {
26 import std.meta;
27
28 // Creates a compile-time sequence of function call expressions
29 // that each call `func` with the next variadic template argument
30 template Map(alias func, args...)
31 {
32 auto ref lazyItem() {return func(args[0]);}
33
34 static if (args.length == 1)
35 {
36 alias Map = lazyItem;
37 }
38 else
39 {
40 // recurse
41 alias Map = AliasSeq!(lazyItem, Map!(func, args[1 .. $]));
42 }
43 }
44
45 static void test(int a, int b)
46 {
47 assert(a == 4);
48 assert(b == 16);
49 }
50
51 static int a = 2;
52 static int b = 4;
53
54 test(Map!(i => i ^^ 2, a, b));
55 assert(a == 2);
56 assert(b == 4);
57
58 test(Map!((ref i) => i *= i, a, b));
59 assert(a == 4);
60 assert(b == 16);
61
62 static void testRef(ref int a, ref int b)
63 {
64 assert(a++ == 16);
65 assert(b++ == 256);
66 }
67
68 testRef(Map!(function ref(ref i) => i *= i, a, b));
69 assert(a == 17);
70 assert(b == 257);
71 }
72
73 @safe unittest
74 {
75 import std.meta;
76
77 // Without Alias this would fail if Args[0] was e.g. a value and
78 // some logic would be needed to detect when to use enum instead
79 alias Head(Args...) = Alias!(Args[0]);
80 alias Tail(Args...) = Args[1 .. $];
81
82 alias Blah = AliasSeq!(3, int, "hello");
83 static assert(Head!Blah == 3);
84 static assert(is(Head!(Tail!Blah) == int));
85 static assert((Tail!Blah)[1] == "hello");
86 }
87
88 @safe unittest
89 {
90 import std.meta;
91
92 alias a = Alias!(123);
93 static assert(a == 123);
94
95 enum abc = 1;
96 alias b = Alias!(abc);
97 static assert(b == 1);
98
99 alias c = Alias!(3 + 4);
100 static assert(c == 7);
101
102 alias concat = (s0, s1) => s0 ~ s1;
103 alias d = Alias!(concat("Hello", " World!"));
104 static assert(d == "Hello World!");
105
106 alias e = Alias!(int);
107 static assert(is(e == int));
108
109 alias f = Alias!(AliasSeq!(int));
110 static assert(!is(typeof(f[0]))); //not an AliasSeq
111 static assert(is(f == int));
112
113 auto g = 6;
114 alias h = Alias!g;
115 ++h;
116 assert(g == 7);
117 }
118
119 @safe unittest
120 {
121 import std.meta;
122
123 import std.stdio;
124
125 void foo()
126 {
127 writefln("The index of long is %s",
128 staticIndexOf!(long, AliasSeq!(int, long, double)));
129 // prints: The index of long is 1
130 }
131 }
132
133 @safe unittest
134 {
135 import std.meta;
136
137 alias Types = AliasSeq!(int, long, double, char);
138 alias TL = Erase!(long, Types);
139 static assert(is(TL == AliasSeq!(int, double, char)));
140 }
141
142 @safe unittest
143 {
144 import std.meta;
145
146 alias Types = AliasSeq!(int, long, long, int);
147 static assert(is(EraseAll!(long, Types) == AliasSeq!(int, int)));
148 }
149
150 @safe unittest
151 {
152 import std.meta;
153
154 alias Types = AliasSeq!(int, long, long, int, float);
155
156 alias TL = NoDuplicates!(Types);
157 static assert(is(TL == AliasSeq!(int, long, float)));
158 }
159
160 @safe unittest
161 {
162 import std.meta;
163
164 alias Types = AliasSeq!(int, long, long, int, float);
165
166 alias TL = Replace!(long, char, Types);
167 static assert(is(TL == AliasSeq!(int, char, long, int, float)));
168 }
169
170 @safe unittest
171 {
172 import std.meta;
173
174 alias Types = AliasSeq!(int, long, long, int, float);
175
176 alias TL = ReplaceAll!(long, char, Types);
177 static assert(is(TL == AliasSeq!(int, char, char, int, float)));
178 }
179
180 @safe unittest
181 {
182 import std.meta;
183
184 alias Types = AliasSeq!(int, long, long, int, float, byte, ubyte, short, ushort, uint);
185
186 alias TL = Reverse!(Types);
187 static assert(is(TL == AliasSeq!(uint, ushort, short, ubyte, byte, float, int, long, long, int)));
188 }
189
190 @safe unittest
191 {
192 import std.meta;
193
194 class A { }
195 class B : A { }
196 class C : B { }
197 alias Types = AliasSeq!(A, C, B);
198
199 MostDerived!(Object, Types) x; // x is declared as type C
200 static assert(is(typeof(x) == C));
201 }
202
203 @safe unittest
204 {
205 import std.meta;
206
207 class A { }
208 class B : A { }
209 class C : B { }
210 alias Types = AliasSeq!(A, C, B);
211
212 alias TL = DerivedToFront!(Types);
213 static assert(is(TL == AliasSeq!(C, B, A)));
214
215 alias TL2 = DerivedToFront!(A, A, A, B, B, B, C, C, C);
216 static assert(is(TL2 == AliasSeq!(C, C, C, B, B, B, A, A, A)));
217 }
218
219 @safe unittest
220 {
221 import std.meta;
222
223 import std.traits : Unqual;
224 alias TL = staticMap!(Unqual, int, const int, immutable int, uint, ubyte, byte, short, ushort);
225 static assert(is(TL == AliasSeq!(int, int, int, uint, ubyte, byte, short, ushort)));
226 }
227
228 @safe unittest
229 {
230 import std.meta;
231
232 import std.traits : isIntegral;
233
234 static assert(!allSatisfy!(isIntegral, int, double));
235 static assert( allSatisfy!(isIntegral, int, long));
236 }
237
238 @safe unittest
239 {
240 import std.meta;
241
242 import std.traits : isIntegral;
243
244 static assert(!anySatisfy!(isIntegral, string, double));
245 static assert( anySatisfy!(isIntegral, int, double));
246 }
247
248 @safe unittest
249 {
250 import std.meta;
251
252 import std.traits : isNarrowString, isUnsigned;
253
254 alias Types1 = AliasSeq!(string, wstring, dchar[], char[], dstring, int);
255 alias TL1 = Filter!(isNarrowString, Types1);
256 static assert(is(TL1 == AliasSeq!(string, wstring, char[])));
257
258 alias Types2 = AliasSeq!(int, byte, ubyte, dstring, dchar, uint, ulong);
259 alias TL2 = Filter!(isUnsigned, Types2);
260 static assert(is(TL2 == AliasSeq!(ubyte, uint, ulong)));
261 }
262
263 @safe unittest
264 {
265 import std.meta;
266
267 import std.traits : isPointer;
268
269 alias isNoPointer = templateNot!isPointer;
270 static assert(!isNoPointer!(int*));
271 static assert(allSatisfy!(isNoPointer, string, char, float));
272 }
273
274 @safe unittest
275 {
276 import std.meta;
277
278 import std.traits : isNumeric, isUnsigned;
279
280 alias storesNegativeNumbers = templateAnd!(isNumeric, templateNot!isUnsigned);
281 static assert(storesNegativeNumbers!int);
282 static assert(!storesNegativeNumbers!string && !storesNegativeNumbers!uint);
283
284 // An empty sequence of predicates always yields true.
285 alias alwaysTrue = templateAnd!();
286 static assert(alwaysTrue!int);
287 }
288
289 @safe unittest
290 {
291 import std.meta;
292
293 import std.traits : isPointer, isUnsigned;
294
295 alias isPtrOrUnsigned = templateOr!(isPointer, isUnsigned);
296 static assert( isPtrOrUnsigned!uint && isPtrOrUnsigned!(short*));
297 static assert(!isPtrOrUnsigned!int && !isPtrOrUnsigned!(string));
298
299 // An empty sequence of predicates never yields true.
300 alias alwaysFalse = templateOr!();
301 static assert(!alwaysFalse!int);
302 }
303
304 @safe unittest
305 {
306 import std.meta;
307
308 import std.algorithm.iteration : map;
309 import std.algorithm.sorting : sort;
310 import std.string : capitalize;
311
312 struct S
313 {
314 int a;
315 int c;
316 int b;
317 }
318
319 alias capMembers = aliasSeqOf!([__traits(allMembers, S)].sort().map!capitalize());
320 static assert(capMembers[0] == "A");
321 static assert(capMembers[1] == "B");
322 static assert(capMembers[2] == "C");
323 }
324
325 @safe unittest
326 {
327 import std.meta;
328
329 static immutable REF = [0, 1, 2, 3];
330 foreach (I, V; aliasSeqOf!([0, 1, 2, 3]))
331 {
332 static assert(V == I);
333 static assert(V == REF[I]);
334 }
335 }
336
337 @safe unittest
338 {
339 import std.meta;
340
341 // enum bool isImplicitlyConvertible(From, To)
342 import std.traits : isImplicitlyConvertible;
343
344 static assert(allSatisfy!(
345 ApplyLeft!(isImplicitlyConvertible, ubyte),
346 short, ushort, int, uint, long, ulong));
347
348 static assert(is(Filter!(ApplyRight!(isImplicitlyConvertible, short),
349 ubyte, string, short, float, int) == AliasSeq!(ubyte, short)));
350 }
351
352 @safe unittest
353 {
354 import std.meta;
355
356 import std.traits : hasMember, ifTestable;
357
358 struct T1
359 {
360 bool foo;
361 }
362
363 struct T2
364 {
365 struct Test
366 {
367 bool opCast(T : bool)() { return true; }
368 }
369
370 Test foo;
371 }
372
373 static assert(allSatisfy!(ApplyRight!(hasMember, "foo"), T1, T2));
374 static assert(allSatisfy!(ApplyRight!(ifTestable, a => a.foo), T1, T2));
375 }
376
377 @safe unittest
378 {
379 import std.meta;
380
381 import std.traits : Largest;
382
383 alias Types = AliasSeq!(byte, short, int, long);
384
385 static assert(is(staticMap!(ApplyLeft!(Largest, short), Types) ==
386 AliasSeq!(short, short, int, long)));
387 static assert(is(staticMap!(ApplyLeft!(Largest, int), Types) ==
388 AliasSeq!(int, int, int, long)));
389 }
390
391 @safe unittest
392 {
393 import std.meta;
394
395 import std.traits : FunctionAttribute, SetFunctionAttributes;
396
397 static void foo() @system;
398 static int bar(int) @system;
399
400 alias SafeFunctions = AliasSeq!(
401 void function() @safe,
402 int function(int) @safe);
403
404 static assert(is(staticMap!(ApplyRight!(
405 SetFunctionAttributes, "D", FunctionAttribute.safe),
406 typeof(&foo), typeof(&bar)) == SafeFunctions));
407 }
408
409 @safe unittest
410 {
411 import std.meta;
412
413 alias ImInt0 = Repeat!(0, int);
414 static assert(is(ImInt0 == AliasSeq!()));
415
416 alias ImInt1 = Repeat!(1, immutable(int));
417 static assert(is(ImInt1 == AliasSeq!(immutable(int))));
418
419 alias Real3 = Repeat!(3, real);
420 static assert(is(Real3 == AliasSeq!(real, real, real)));
421
422 alias Real12 = Repeat!(4, Real3);
423 static assert(is(Real12 == AliasSeq!(real, real, real, real, real, real,
424 real, real, real, real, real, real)));
425
426 alias Composite = AliasSeq!(uint, int);
427 alias Composite2 = Repeat!(2, Composite);
428 static assert(is(Composite2 == AliasSeq!(uint, int, uint, int)));
429
430 alias ImInt10 = Repeat!(10, int);
431 static assert(is(ImInt10 == AliasSeq!(int, int, int, int, int, int, int, int, int, int)));
432
433 alias Big = Repeat!(1_000_000, int);
434 }
435
436 @safe unittest
437 {
438 import std.meta;
439
440 auto staticArray(T, size_t n)(Repeat!(n, T) elems)
441 {
442 T[n] a = [elems];
443 return a;
444 }
445
446 auto a = staticArray!(long, 3)(3, 1, 4);
447 assert(is(typeof(a) == long[3]));
448 assert(a == [3, 1, 4]);
449 }
450
451 @safe unittest
452 {
453 import std.meta;
454
455 alias Nums = AliasSeq!(7, 2, 3, 23);
456 enum Comp(int N1, int N2) = N1 < N2;
457 static assert(AliasSeq!(2, 3, 7, 23) == staticSort!(Comp, Nums));
458 }
459
460 @safe unittest
461 {
462 import std.meta;
463
464 alias Types = AliasSeq!(uint, short, ubyte, long, ulong);
465 enum Comp(T1, T2) = __traits(isUnsigned, T2) - __traits(isUnsigned, T1);
466 static assert(is(AliasSeq!(uint, ubyte, ulong, short, long) == staticSort!(Comp,
467 Types)));
468 }
469
470 @safe unittest
471 {
472 import std.meta;
473
474 enum Comp(int N1, int N2) = N1 < N2;
475 static assert( staticIsSorted!(Comp, 2, 2));
476 static assert( staticIsSorted!(Comp, 2, 3, 7, 23));
477 static assert(!staticIsSorted!(Comp, 7, 2, 3, 23));
478 }
479
480 @safe unittest
481 {
482 import std.meta;
483
484 enum Comp(T1, T2) = __traits(isUnsigned, T2) - __traits(isUnsigned, T1);
485 static assert( staticIsSorted!(Comp, uint, ubyte, ulong, short, long));
486 static assert(!staticIsSorted!(Comp, uint, short, ubyte, long, ulong));
487 }
488
489 @safe unittest
490 {
491 import std.meta;
492
493 static assert(is(Stride!(1, short, int, long) == AliasSeq!(short, int, long)));
494 static assert(is(Stride!(2, short, int, long) == AliasSeq!(short, long)));
495 static assert(is(Stride!(-1, short, int, long) == AliasSeq!(long, int, short)));
496 static assert(is(Stride!(-2, short, int, long) == AliasSeq!(long, short)));
497
498 alias attribs = AliasSeq!(short, int, long, ushort, uint, ulong);
499 static assert(is(Stride!(3, attribs) == AliasSeq!(short, ushort)));
500 static assert(is(Stride!(3, attribs[1 .. $]) == AliasSeq!(int, uint)));
501 static assert(is(Stride!(-3, attribs) == AliasSeq!(ulong, long)));
502 }
503
504 @safe unittest
505 {
506 import std.meta;
507
508 // ApplyRight combined with Instantiate can be used to apply various
509 // templates to the same parameters.
510 import std.string : leftJustify, center, rightJustify;
511 alias functions = staticMap!(ApplyRight!(Instantiate, string),
512 leftJustify, center, rightJustify);
513 string result = "";
514 static foreach (f; functions)
515 {
516 {
517 auto x = &f; // not a template, but a function instantiation
518 result ~= x("hello", 7);
519 result ~= ";";
520 }
521 }
522
523 assert(result == "hello ; hello ; hello;");
524 }
525