stdx.allocator.internal 0/4(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4830
4840
4850
4860
4870
4880
4890
4900
4910
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6110
6120
6130
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6470
6480
6490
6500
6510
6520
6530
6540
6550
6560
6570
6580
6590
6600
6610
6620
6630
6640
6650
6660
6670
6680
6690
6700
6710
6720
6730
6740
6750
6760
6770
6780
6790
6800
6810
6820
6830
6840
6850
6860
6870
6880
6890
6900
6910
6920
6930
6940
6950
6960
6970
6980
6990
7000
7010
7020
7030
7040
7050
7060
7070
7080
7090
7100
7110
7120
7130
7140
7150
7160
7170
7180
7190
7200
7210
7220
7230
7240
7250
7260
7270
7280
7290
7300
7310
7320
7330
7340
7350
7360
7370
7380
7390
7400
7410
7420
7430
7440
7450
7460
7470
7480
7490
7500
7510
7520
7530
7540
7550
7560
7570
7580
7590
7600
7610
7620
7630
7640
7650
7660
7670
7680
7690
7700
7710
7720
7730
7740
7750
7760
7770
7780
7790
7800
7810
7820
7830
7840
7850
7860
7870
7880
7890
7900
7910
7920
7930
7940
7950
7960
7970
7980
7990
8000
8010
8020
8030
8040
8050
8060
8070
8080
8090
8100
8110
8120
8130
8140
8150
8160
8170
8180
8190
8200
8210
8220
8230
8240
8250
8260
8270
8280
8290
8300
8310
8320
8330
8340
8350
8360
8370
8380
8390
8400
8410
8420
8430
8440
8450
8460
8470
8480
8490
8500
8510
8520
8530
8540
8550
8560
8570
8580
8590
8600
8610
8620
8630
8640
8650
8660
8670
8680
8690
8700
8710
8720
8730
8740
8750
8760
8770
8780
8790
8800
8810
8820
8830
8840
8850
8860
8870
8880
8890
8900
8910
8920
8930
8940
8950
8960
8970
8980
8990
9000
9010
9020
9030
9040
9050
9060
9070
9080
9090
9100
9110
9120
9130
9140
9150
9160
9170
9180
9190
9200
9210
9220
9230
9240
9250
9260
9270
9280
9290
9300
9310
9320
9330
9340
9350
9360
9370
9380
9390
9400
9410
9420
9430
9440
9450
9460
9470
9480
9490
9500
9510
9520
9530
9540
9550
9560
9570
9580
9590
9600
9610
9620
9630
9640
9650
9660
9670
9680
9690
9700
9710
9720
9730
9740
9750
9760
9770
9780
9790
9800
9810
9820
9830
9840
9850
9860
9870
9880
9890
9900
9910
9920
9930
9940
9950
9960
9970
9980
9990
10000
10010
10020
10030
10040
10050
10060
10070
10080
10090
10100
10110
10120
10130
10140
10150
10160
10170
10180
10190
10200
10210
10220
10230
10240
10250
10260
10270
10280
10290
10300
10310
10320
10330
10340
10350
10360
10370
10380
10390
10400
10410
10420
10430
10440
10450
10460
10470
10480
10490
10500
10510
10520
10530
10540
10550
10560
10570
10580
10590
10600
10610
10620
10630
10640
10650
10660
10670
10680
10690
10700
10710
10720
10730
10740
10750
10760
10770
10780
10790
10800
10810
10820
10830
10840
10850
10860
10870
10880
10890
10900
10910
10920
10930
10940
10950
10960
10970
10980
10990
11000
11010
11020
11030
11040
11050
11060
11070
11080
11090
11100
11110
11120
11130
11140
11150
11160
11170
11180
11190
11200
11210
11220
11230
11240
11250
11260
11270
11280
11290
11300
11310
11320
11330
11340
11350
11360
11370
11380
11390
11400
11410
11420
11430
11440
11450
11460
11470
11480
11490
11500
11510
11520
11530
11540
11550
11560
11570
11580
11590
11600
11610
11620
11630
11640
11650
11660
11670
11680
11690
11700
11710
11720
11730
11740
11750
11760
11770
11780
11790
11800
11810
11820
11830
11840
11850
11860
11870
11880
11890
11900
11910
11920
11930
11940
11950
11960
11970
11980
11990
12000
12010
12020
12030
12040
12050
12060
12070
12080
12090
12100
12110
12120
12130
12140
12150
12160
12170
12180
12190
12200
12210
12220
12230
12240
12250
12260
12270
12280
12290
12300
12310
12320
12330
12340
12350
12360
12370
12380
12390
12400
12410
12420
12430
12440
12450
12460
12470
12480
12490
12500
12510
12520
12530
12540
12550
12560
12570
12580
12590
12600
12610
12620
12630
12640
12650
12660
12670
12680
12690
12700
12710
12720
12730
12740
12750
12760
12770
12780
12790
12800
12810
12820
12830
12840
12850
12860
12870
12880
12890
12900
12910
12920
12930
12940
12950
12960
12970
12980
12990
13000
13010
13020
13030
13040
13050
13060
13070
13080
13090
13100
13110
13120
13130
13140
13150
13160
13170
13180
13190
13200
13210
13220
13230
13240
13250
13260
13270
13280
13290
13300
13310
13320
13330
13340
13350
13360
13370
13380
13390
13400
13410
13420
13430
13440
13450
13460
13470
13480
13490
13500
13510
13520
13530
13540
13550
13560
13570
13580
13590
13600
13610
13620
13630
13640
13650
13660
13670
13680
13690
13700
13710
13720
13730
13740
13750
13760
13770
13780
13790
13800
13810
13820
13830
13840
13850
13860
13870
13880
13890
13900
13910
13920
13930
13940
13950
13960
13970
13980
13990
14000
14010
14020
14030
14040
14050
14060
14070
14080
14090
14100
14110
14120
14130
14140
14150
14160
14170
14180
14190
14200
14210
14220
14230
14240
14250
14260
14270
14280
14290
14300
14310
14320
14330
14340
14350
14360
14370
14380
14390
14400
14410
14420
14430
14440
14450
14460
14470
14480
14490
14500
14510
14520
14530
14540
14550
14560
14570
14580
14590
14600
14610
14620
14630
14640
14650
14660
14670
14680
14690
14700
14710
14720
14730
14740
14750
14760
14770
14780
14790
14800
14810
// Private parts of Phobos module stdx.allocator.internal; import std.traits; /+ emplaceRef is a package function for phobos internal use. It works like emplace, but takes its argument by ref (as opposed to "by pointer"). This makes it easier to use, easier to be safe, and faster in a non-inline build. Furthermore, emplaceRef optionally takes a type paremeter, which specifies the type we want to build. This helps to build qualified objects on mutable buffer, without breaking the type system with unsafe casts. +/ package void emplaceRef(T, UT, Args...)(ref UT chunk, auto ref Args args) { static if (args.length == 0) { static assert(is(typeof({static T i;})), convFormat("Cannot emplace a %1$s because %1$s.this() is annotated with @disable.", T.stringof)); static if (is(T == class)) static assert(!isAbstractClass!T, T.stringof ~ " is abstract and it can't be emplaced"); emplaceInitializer(chunk); } else static if ( !is(T == struct) && Args.length == 1 /* primitives, enums, arrays */ || Args.length == 1 && is(typeof({T t = args[0];})) /* conversions */ || is(typeof(T(args))) /* general constructors */) { static struct S { T payload; this(ref Args x) { static if (Args.length == 1) static if (is(typeof(payload = x[0]))) payload = x[0]; else payload = T(x[0]); else payload = T(x); } } if (__ctfe) { static if (is(typeof(chunk = T(args)))) chunk = T(args); else static if (args.length == 1 && is(typeof(chunk = args[0]))) chunk = args[0]; else assert(0, "CTFE emplace doesn't support " ~ T.stringof ~ " from " ~ Args.stringof); } else { S* p = () @trusted { return cast(S*) &chunk; }(); emplaceInitializer(*p); p.__ctor(args); } } else static if (is(typeof(chunk.__ctor(args)))) { // This catches the rare case of local types that keep a frame pointer emplaceInitializer(chunk); chunk.__ctor(args); } else { //We can't emplace. Try to diagnose a disabled postblit. static assert(!(Args.length == 1 && is(Args[0] : T)), convFormat("Cannot emplace a %1$s because %1$s.this(this) is annotated with @disable.", T.stringof)); //We can't emplace. static assert(false, convFormat("%s cannot be emplaced from %s.", T.stringof, Args[].stringof)); } } // ditto package void emplaceRef(UT, Args...)(ref UT chunk, auto ref Args args) if (is(UT == Unqual!UT)) { emplaceRef!(UT, UT)(chunk, args); } //emplace helper functions private void emplaceInitializer(T)(ref T chunk) @trusted pure nothrow { static if (!hasElaborateAssign!T && isAssignable!T) chunk = T.init; else { import core.stdc.string : memcpy; static immutable T init = T.init; memcpy(&chunk, &init, T.sizeof); } } // emplace /** Given a pointer $(D chunk) to uninitialized memory (but already typed as $(D T)), constructs an object of non-$(D class) type $(D T) at that address. If `T` is a class, initializes the class reference to null. Returns: A pointer to the newly constructed object (which is the same as $(D chunk)). */ T* emplace(T)(T* chunk) @safe pure nothrow { emplaceRef!T(*chunk); return chunk; } /// @system unittest { static struct S { int i = 42; } S[2] s2 = void; emplace(&s2); assert(s2[0].i == 42 && s2[1].i == 42); } /// @system unittest { interface I {} class K : I {} K k = void; emplace(&k); assert(k is null); I i = void; emplace(&i); assert(i is null); } /** Given a pointer $(D chunk) to uninitialized memory (but already typed as a non-class type $(D T)), constructs an object of type $(D T) at that address from arguments $(D args). If `T` is a class, initializes the class reference to `args[0]`. This function can be $(D @trusted) if the corresponding constructor of $(D T) is $(D @safe). Returns: A pointer to the newly constructed object (which is the same as $(D chunk)). */ T* emplace(T, Args...)(T* chunk, auto ref Args args) if (is(T == struct) || Args.length == 1) { emplaceRef!T(*chunk, args); return chunk; } /// @system unittest { int a; int b = 42; assert(*emplace!int(&a, b) == 42); } @system unittest { shared int i; emplace(&i, 42); assert(i == 42); } private @nogc pure nothrow @safe void testEmplaceChunk(void[] chunk, size_t typeSize, size_t typeAlignment, string typeName) { assert(chunk.length >= typeSize, "emplace: Chunk size too small."); assert((cast(size_t) chunk.ptr) % typeAlignment == 0, "emplace: Chunk is not aligned."); } /** Given a raw memory area `chunk` (but already typed as a class type `T`), constructs an object of `class` type `T` at that address. The constructor is passed the arguments `Args`. If `T` is an inner class whose `outer` field can be used to access an instance of the enclosing class, then `Args` must not be empty, and the first member of it must be a valid initializer for that `outer` field. Correct initialization of this field is essential to access members of the outer class inside `T` methods. Note: This function is `@safe` if the corresponding constructor of `T` is `@safe`. Returns: The newly constructed object. */ T emplace(T, Args...)(T chunk, auto ref Args args) if (is(T == class)) { static assert(!isAbstractClass!T, T.stringof ~ " is abstract and it can't be emplaced"); // Initialize the object in its pre-ctor state enum classSize = __traits(classInstanceSize, T); (() @trusted => (cast(void*) chunk)[0 .. classSize] = typeid(T).initializer[])(); static if (isInnerClass!T) { static assert(Args.length > 0, "Initializing an inner class requires a pointer to the outer class"); static assert(is(Args[0] : typeof(T.outer)), "The first argument must be a pointer to the outer class"); chunk.outer = args[0]; alias args1 = args[1..$]; } else alias args1 = args; // Call the ctor if any static if (is(typeof(chunk.__ctor(args1)))) { // T defines a genuine constructor accepting args // Go the classic route: write .init first, then call ctor chunk.__ctor(args1); } else { static assert(args1.length == 0 && !is(typeof(&T.__ctor)), "Don't know how to initialize an object of type " ~ T.stringof ~ " with arguments " ~ typeof(args1).stringof); } return chunk; } /// @safe unittest { () @safe { class SafeClass { int x; @safe this(int x) { this.x = x; } } auto buf = new void[__traits(classInstanceSize, SafeClass)]; auto support = (() @trusted => cast(SafeClass)(buf.ptr))(); auto safeClass = emplace!SafeClass(support, 5); assert(safeClass.x == 5); class UnsafeClass { int x; @system this(int x) { this.x = x; } } auto buf2 = new void[__traits(classInstanceSize, UnsafeClass)]; auto support2 = (() @trusted => cast(UnsafeClass)(buf2.ptr))(); static assert(!__traits(compiles, emplace!UnsafeClass(support2, 5))); static assert(!__traits(compiles, emplace!UnsafeClass(buf2, 5))); }(); } @safe unittest { class Outer { int i = 3; class Inner { @safe auto getI() { return i; } } } auto outerBuf = new void[__traits(classInstanceSize, Outer)]; auto outerSupport = (() @trusted => cast(Outer)(outerBuf.ptr))(); auto innerBuf = new void[__traits(classInstanceSize, Outer.Inner)]; auto innerSupport = (() @trusted => cast(Outer.Inner)(innerBuf.ptr))(); auto inner = innerSupport.emplace!(Outer.Inner)(outerSupport.emplace!Outer); assert(inner.getI == 3); } /** Given a raw memory area `chunk`, constructs an object of `class` type `T` at that address. The constructor is passed the arguments `Args`. If `T` is an inner class whose `outer` field can be used to access an instance of the enclosing class, then `Args` must not be empty, and the first member of it must be a valid initializer for that `outer` field. Correct initialization of this field is essential to access members of the outer class inside `T` methods. Preconditions: `chunk` must be at least as large as `T` needs and should have an alignment multiple of `T`'s alignment. (The size of a `class` instance is obtained by using $(D __traits(classInstanceSize, T))). Note: This function can be `@trusted` if the corresponding constructor of `T` is `@safe`. Returns: The newly constructed object. */ T emplace(T, Args...)(void[] chunk, auto ref Args args) if (is(T == class)) { enum classSize = __traits(classInstanceSize, T); testEmplaceChunk(chunk, classSize, classInstanceAlignment!T, T.stringof); return emplace!T(cast(T)(chunk.ptr), args); } /// @system unittest { static class C { int i; this(int i){this.i = i;} } auto buf = new void[__traits(classInstanceSize, C)]; auto c = emplace!C(buf, 5); assert(c.i == 5); } @system unittest { class Outer { int i = 3; class Inner { auto getI() { return i; } } } auto outerBuf = new void[__traits(classInstanceSize, Outer)]; auto innerBuf = new void[__traits(classInstanceSize, Outer.Inner)]; auto inner = innerBuf.emplace!(Outer.Inner)(outerBuf.emplace!Outer); assert(inner.getI == 3); } @nogc pure nothrow @safe unittest { static if (__VERSION__ >= 2072) mixin(` int var = 6; align(__conv_EmplaceTestClass.alignof) ubyte[__traits(classInstanceSize, __conv_EmplaceTestClass)] buf; auto support = (() @trusted => cast(__conv_EmplaceTestClass)(buf.ptr))(); auto k = emplace!__conv_EmplaceTestClass(support, 5, var); assert(k.i == 5); assert(var == 7); `); } /** Given a raw memory area $(D chunk), constructs an object of non-$(D class) type $(D T) at that address. The constructor is passed the arguments $(D args), if any. Preconditions: $(D chunk) must be at least as large as $(D T) needs and should have an alignment multiple of $(D T)'s alignment. Note: This function can be $(D @trusted) if the corresponding constructor of $(D T) is $(D @safe). Returns: A pointer to the newly constructed object. */ T* emplace(T, Args...)(void[] chunk, auto ref Args args) if (!is(T == class)) { testEmplaceChunk(chunk, T.sizeof, T.alignof, T.stringof); emplaceRef!(T, Unqual!T)(*cast(Unqual!T*) chunk.ptr, args); return cast(T*) chunk.ptr; } /// @system unittest { struct S { int a, b; } auto buf = new void[S.sizeof]; S s; s.a = 42; s.b = 43; auto s1 = emplace!S(buf, s); assert(s1.a == 42 && s1.b == 43); } // Bulk of emplace unittests starts here @system unittest /* unions */ { static union U { string a; int b; struct { long c; int[] d; } } U u1 = void; U u2 = { "hello" }; emplace(&u1, u2); assert(u1.a == "hello"); } version(unittest) private struct __conv_EmplaceTest { int i = 3; this(int i) { assert(this.i == 3 && i == 5); this.i = i; } this(int i, ref int j) { assert(i == 5 && j == 6); this.i = i; ++j; } @disable: this(); this(this); void opAssign(); } version(unittest) private class __conv_EmplaceTestClass { int i = 3; this(int i) @nogc @safe pure nothrow { assert(this.i == 3 && i == 5); this.i = i; } this(int i, ref int j) @nogc @safe pure nothrow { assert(i == 5 && j == 6); this.i = i; ++j; } } @system unittest // bugzilla 15772 { abstract class Foo {} class Bar: Foo {} void[] memory; // test in emplaceInitializer static assert(!is(typeof(emplace!Foo(cast(Foo*) memory.ptr)))); static assert( is(typeof(emplace!Bar(cast(Bar*) memory.ptr)))); // test in the emplace overload that takes void[] static assert(!is(typeof(emplace!Foo(memory)))); static assert( is(typeof(emplace!Bar(memory)))); } @system unittest { struct S { @disable this(); } S s = void; static assert(!__traits(compiles, emplace(&s))); emplace(&s, S.init); } @system unittest { struct S1 {} struct S2 { void opAssign(S2); } S1 s1 = void; S2 s2 = void; S1[2] as1 = void; S2[2] as2 = void; emplace(&s1); emplace(&s2); emplace(&as1); emplace(&as2); } @system unittest { static struct S1 { this(this) @disable; } static struct S2 { this() @disable; } S1[2] ss1 = void; S2[2] ss2 = void; emplace(&ss1); static assert(!__traits(compiles, emplace(&ss2))); S1 s1 = S1.init; S2 s2 = S2.init; static assert(!__traits(compiles, emplace(&ss1, s1))); emplace(&ss2, s2); } @system unittest { struct S { immutable int i; } S s = void; S[2] ss1 = void; S[2] ss2 = void; emplace(&s, 5); assert(s.i == 5); emplace(&ss1, s); assert(ss1[0].i == 5 && ss1[1].i == 5); emplace(&ss2, ss1); assert(ss2 == ss1); } //Start testing emplace-args here @system unittest { interface I {} class K : I {} K k = null, k2 = new K; assert(k !is k2); emplace!K(&k, k2); assert(k is k2); I i = null; assert(i !is k); emplace!I(&i, k); assert(i is k); } @system unittest { static struct S { int i = 5; void opAssign(S){assert(0);} } S[2] sa = void; S[2] sb; emplace(&sa, sb); assert(sa[0].i == 5 && sa[1].i == 5); } //Start testing emplace-struct here // Test constructor branch @system unittest { struct S { double x = 5, y = 6; this(int a, int b) { assert(x == 5 && y == 6); x = a; y = b; } } auto s1 = new void[S.sizeof]; auto s2 = S(42, 43); assert(*emplace!S(cast(S*) s1.ptr, s2) == s2); assert(*emplace!S(cast(S*) s1, 44, 45) == S(44, 45)); } @system unittest { __conv_EmplaceTest k = void; emplace(&k, 5); assert(k.i == 5); } @system unittest { int var = 6; __conv_EmplaceTest k = void; emplace(&k, 5, var); assert(k.i == 5); assert(var == 7); } // Test matching fields branch @system unittest { struct S { uint n; } S s; emplace!S(&s, 2U); assert(s.n == 2); } @safe unittest { struct S { int a, b; this(int){} } S s; static assert(!__traits(compiles, emplace!S(&s, 2, 3))); } @system unittest { struct S { int a, b = 7; } S s1 = void, s2 = void; emplace!S(&s1, 2); assert(s1.a == 2 && s1.b == 7); emplace!S(&s2, 2, 3); assert(s2.a == 2 && s2.b == 3); } //opAssign @system unittest { static struct S { int i = 5; void opAssign(int){assert(0);} void opAssign(S){assert(0);} } S sa1 = void; S sa2 = void; S sb1 = S(1); emplace(&sa1, sb1); emplace(&sa2, 2); assert(sa1.i == 1); assert(sa2.i == 2); } //postblit precedence @system unittest { //Works, but breaks in "-w -O" because of @@@9332@@@. //Uncomment test when 9332 is fixed. static struct S { int i; this(S other){assert(false);} this(int i){this.i = i;} this(this){} } S a = void; assert(is(typeof({S b = a;}))); //Postblit assert(is(typeof({S b = S(a);}))); //Constructor auto b = S(5); emplace(&a, b); assert(a.i == 5); static struct S2 { int* p; this(const S2){} } static assert(!is(immutable S2 : S2)); S2 s2 = void; immutable is2 = (immutable S2).init; emplace(&s2, is2); } //nested structs and postblit @system unittest { static struct S { int* p; this(int i){p = [i].ptr;} this(this) { if (p) p = [*p].ptr; } } static struct SS { S s; void opAssign(const SS) { assert(0); } } SS ssa = void; SS ssb = SS(S(5)); emplace(&ssa, ssb); assert(*ssa.s.p == 5); assert(ssa.s.p != ssb.s.p); } //disabled postblit @system unittest { static struct S1 { int i; @disable this(this); } S1 s1 = void; emplace(&s1, 1); assert(s1.i == 1); static assert(!__traits(compiles, emplace(&s1, S1.init))); static struct S2 { int i; @disable this(this); this(ref S2){} } S2 s2 = void; static assert(!__traits(compiles, emplace(&s2, 1))); emplace(&s2, S2.init); static struct SS1 { S1 s; } SS1 ss1 = void; emplace(&ss1); static assert(!__traits(compiles, emplace(&ss1, SS1.init))); static struct SS2 { S2 s; } SS2 ss2 = void; emplace(&ss2); static assert(!__traits(compiles, emplace(&ss2, SS2.init))); // SS1 sss1 = s1; //This doesn't compile // SS1 sss1 = SS1(s1); //This doesn't compile // So emplace shouldn't compile either static assert(!__traits(compiles, emplace(&sss1, s1))); static assert(!__traits(compiles, emplace(&sss2, s2))); } //Imutability @system unittest { //Castable immutability { static struct S1 { int i; } static assert(is( immutable(S1) : S1)); S1 sa = void; auto sb = immutable(S1)(5); emplace(&sa, sb); assert(sa.i == 5); } //Un-castable immutability { static struct S2 { int* p; } static assert(!is(immutable(S2) : S2)); S2 sa = void; auto sb = immutable(S2)(null); assert(!__traits(compiles, emplace(&sa, sb))); } } @system unittest { static struct S { immutable int i; immutable(int)* j; } S s = void; emplace(&s, 1, null); emplace(&s, 2, &s.i); assert(s is S(2, &s.i)); } //Context pointer @system unittest { int i = 0; { struct S1 { void foo(){++i;} } S1 sa = void; S1 sb; emplace(&sa, sb); sa.foo(); assert(i == 1); } { struct S2 { void foo(){++i;} this(this){} } S2 sa = void; S2 sb; emplace(&sa, sb); sa.foo(); assert(i == 2); } } //Alias this @system unittest { static struct S { int i; } //By Ref { static struct SS1 { int j; S s; alias s this; } S s = void; SS1 ss = SS1(1, S(2)); emplace(&s, ss); assert(s.i == 2); } //By Value { static struct SS2 { int j; S s; S foo() @property{return s;} alias foo this; } S s = void; SS2 ss = SS2(1, S(2)); emplace(&s, ss); assert(s.i == 2); } } version(unittest) { //Ambiguity struct __std_conv_S { int i; this(__std_conv_SS ss) {assert(0);} static opCall(__std_conv_SS ss) { __std_conv_S s; s.i = ss.j; return s; } } struct __std_conv_SS { int j; __std_conv_S s; ref __std_conv_S foo() return @property {s.i = j; return s;} alias foo this; } static assert(is(__std_conv_SS : __std_conv_S)); @system unittest { __std_conv_S s = void; __std_conv_SS ss = __std_conv_SS(1); __std_conv_S sTest1 = ss; //this calls "SS alias this" (and not "S.this(SS)") emplace(&s, ss); //"alias this" should take precedence in emplace over "opCall" assert(s.i == 1); } } //Nested classes @system unittest { class A{} static struct S { A a; } S s1 = void; S s2 = S(new A); emplace(&s1, s2); assert(s1.a is s2.a); } //safety & nothrow & CTFE @system unittest { //emplace should be safe for anything with no elaborate opassign static struct S1 { int i; } static struct S2 { int i; this(int j)@safe nothrow{i = j;} } int i; S1 s1 = void; S2 s2 = void; auto pi = &i; auto ps1 = &s1; auto ps2 = &s2; void foo() @safe nothrow { emplace(pi); emplace(pi, 5); emplace(ps1); emplace(ps1, 5); emplace(ps1, S1.init); emplace(ps2); emplace(ps2, 5); emplace(ps2, S2.init); } foo(); T bar(T)() @property { T t/+ = void+/; //CTFE void illegal emplace(&t, 5); return t; } // CTFE enum a = bar!int; static assert(a == 5); enum b = bar!S1; static assert(b.i == 5); enum c = bar!S2; static assert(c.i == 5); // runtime auto aa = bar!int; assert(aa == 5); auto bb = bar!S1; assert(bb.i == 5); auto cc = bar!S2; assert(cc.i == 5); } @system unittest { struct S { int[2] get(){return [1, 2];} alias get this; } struct SS { int[2] ii; } struct ISS { int[2] ii; } S s; SS ss = void; ISS iss = void; emplace(&ss, s); emplace(&iss, s); assert(ss.ii == [1, 2]); assert(iss.ii == [1, 2]); } //disable opAssign @system unittest { static struct S { @disable void opAssign(S); } S s; emplace(&s, S.init); } //opCall @system unittest { int i; //Without constructor { static struct S1 { int i; static S1 opCall(int*){assert(0);} } S1 s = void; static assert(!__traits(compiles, emplace(&s, 1))); } //With constructor { static struct S2 { int i = 0; static S2 opCall(int*){assert(0);} static S2 opCall(int){assert(0);} this(int i){this.i = i;} } S2 s = void; emplace(&s, 1); assert(s.i == 1); } //With postblit ambiguity { static struct S3 { int i = 0; static S3 opCall(ref S3){assert(0);} } S3 s = void; emplace(&s, S3.init); } } @safe unittest //@@@9559@@@ { import std.algorithm.iteration : map; import std.array : array; import std.typecons : Nullable; alias I = Nullable!int; auto ints = [0, 1, 2].map!(i => i & 1 ? I.init : I(i))(); auto asArray = array(ints); } @system unittest //http://forum.dlang.org/post/nxbdgtdlmwscocbiypjs@forum.dlang.org { import std.array : array; import std.datetime : SysTime, UTC; import std.math : isNaN; static struct A { double i; } static struct B { invariant() { if (j == 0) assert(a.i.isNaN(), "why is 'j' zero?? and i is not NaN?"); else assert(!a.i.isNaN()); } SysTime when; // comment this line avoid the breakage int j; A a; } B b1 = B.init; assert(&b1); // verify that default eyes invariants are ok; auto b2 = B(SysTime(0, UTC()), 1, A(1)); assert(&b2); auto b3 = B(SysTime(0, UTC()), 1, A(1)); assert(&b3); auto arr = [b2, b3]; assert(arr[0].j == 1); assert(arr[1].j == 1); auto a2 = arr.array(); // << bang, invariant is raised, also if b2 and b3 are good } //static arrays @system unittest { static struct S { int[2] ii; } static struct IS { immutable int[2] ii; } int[2] ii; S s = void; IS ims = void; ubyte ub = 2; emplace(&s, ub); emplace(&s, ii); emplace(&ims, ub); emplace(&ims, ii); uint[2] uu; static assert(!__traits(compiles, {S ss = S(uu);})); static assert(!__traits(compiles, emplace(&s, uu))); } @system unittest { int[2] sii; int[2] sii2; uint[2] uii; uint[2] uii2; emplace(&sii, 1); emplace(&sii, 1U); emplace(&uii, 1); emplace(&uii, 1U); emplace(&sii, sii2); //emplace(&sii, uii2); //Sorry, this implementation doesn't know how to... //emplace(&uii, sii2); //Sorry, this implementation doesn't know how to... emplace(&uii, uii2); emplace(&sii, sii2[]); //emplace(&sii, uii2[]); //Sorry, this implementation doesn't know how to... //emplace(&uii, sii2[]); //Sorry, this implementation doesn't know how to... emplace(&uii, uii2[]); } @system unittest { bool allowDestruction = false; struct S { int i; this(this){} ~this(){assert(allowDestruction);} } S s = S(1); S[2] ss1 = void; S[2] ss2 = void; S[2] ss3 = void; emplace(&ss1, s); emplace(&ss2, ss1); emplace(&ss3, ss2[]); assert(ss1[1] == s); assert(ss2[1] == s); assert(ss3[1] == s); allowDestruction = true; } @system unittest { //Checks postblit, construction, and context pointer int count = 0; struct S { this(this) { ++count; } ~this() { --count; } } S s; { S[4] ss = void; emplace(&ss, s); assert(count == 4); } assert(count == 0); } @system unittest { struct S { int i; } S s; S[2][2][2] sss = void; emplace(&sss, s); } @system unittest //Constness { import std.stdio; int a = void; emplaceRef!(const int)(a, 5); immutable i = 5; const(int)* p = void; emplaceRef!(const int*)(p, &i); struct S { int* p; } alias IS = immutable(S); S s = void; emplaceRef!IS(s, IS()); S[2] ss = void; emplaceRef!(IS[2])(ss, IS()); IS[2] iss = IS.init; emplaceRef!(IS[2])(ss, iss); emplaceRef!(IS[2])(ss, iss[]); } pure nothrow @safe @nogc unittest { int i; emplaceRef(i); emplaceRef!int(i); emplaceRef(i, 5); emplaceRef!int(i, 5); } // Test attribute propagation for UDTs pure nothrow @safe /* @nogc */ unittest { static struct Safe { this(this) pure nothrow @safe @nogc {} } Safe safe = void; emplaceRef(safe, Safe()); Safe[1] safeArr = [Safe()]; Safe[1] uninitializedSafeArr = void; emplaceRef(uninitializedSafeArr, safe); emplaceRef(uninitializedSafeArr, safeArr); static struct Unsafe { this(this) @system {} } Unsafe unsafe = void; static assert(!__traits(compiles, emplaceRef(unsafe, Unsafe()))); Unsafe[1] unsafeArr = [Unsafe()]; Unsafe[1] uninitializedUnsafeArr = void; static assert(!__traits(compiles, emplaceRef(uninitializedUnsafeArr, unsafe))); static assert(!__traits(compiles, emplaceRef(uninitializedUnsafeArr, unsafeArr))); } @system unittest { // Issue 15313 static struct Node { int payload; Node* next; uint refs; } import core.stdc.stdlib : malloc; void[] buf = malloc(Node.sizeof)[0 .. Node.sizeof]; import std.conv : emplace; const Node* n = emplace!(const Node)(buf, 42, null, 10); assert(n.payload == 42); assert(n.next == null); assert(n.refs == 10); } @system unittest { int var = 6; auto k = emplace!__conv_EmplaceTest(new void[__conv_EmplaceTest.sizeof], 5, var); assert(k.i == 5); assert(var == 7); } @system unittest { class A { int x = 5; int y = 42; this(int z) { assert(x == 5 && y == 42); x = y = z; } } void[] buf; static if (__VERSION__ >= 2072) mixin(` static align(A.alignof) byte[__traits(classInstanceSize, A)] sbuf; buf = sbuf[]; auto a = emplace!A(buf, 55); assert(a.x == 55 && a.y == 55); // emplace in bigger buffer buf = new byte[](__traits(classInstanceSize, A) + 10); a = emplace!A(buf, 55); assert(a.x == 55 && a.y == 55); // need ctor args static assert(!is(typeof(emplace!A(buf)))); `); } // Bulk of emplace unittests ends here static if (__VERSION__ >= 2072) { public import std.typecons : Ternary; } else { public import std.experimental.allocator.common : Ternary; } /** Check whether a number is an integer power of two. Note that only positive numbers can be integer powers of two. This function always return `false` if `x` is negative or zero. Params: x = the number to test Returns: `true` if `x` is an integer power of two. */ bool isPowerOf2(X)(const X x) pure @safe nothrow @nogc if (isNumeric!X) { static if (isFloatingPoint!X) { import std.math : frexp; int exp; const X sig = frexp(x, exp); return (exp != int.min) && (sig is cast(X) 0.5L); } else { static if (isSigned!X) { auto y = cast(typeof(x + 0))x; return y > 0 && !(y & (y - 1)); } else { auto y = cast(typeof(x + 0u))x; return (y & -y) > (y - 1); } } } /// @safe unittest { import std.math : pow; assert( isPowerOf2(1.0L)); assert( isPowerOf2(2.0L)); assert( isPowerOf2(0.5L)); assert( isPowerOf2(pow(2.0L, 96))); assert( isPowerOf2(pow(2.0L, -77))); assert(!isPowerOf2(-2.0L)); assert(!isPowerOf2(-0.5L)); assert(!isPowerOf2(0.0L)); assert(!isPowerOf2(4.315)); assert(!isPowerOf2(1.0L / 3.0L)); assert(!isPowerOf2(real.nan)); assert(!isPowerOf2(real.infinity)); } /// @safe unittest { assert( isPowerOf2(1)); assert( isPowerOf2(2)); assert( isPowerOf2(1uL << 63)); assert(!isPowerOf2(-4)); assert(!isPowerOf2(0)); assert(!isPowerOf2(1337u)); } @safe unittest { import std.meta : AliasSeq; import std.math : pow; immutable smallP2 = pow(2.0L, -62); immutable bigP2 = pow(2.0L, 50); immutable smallP7 = pow(7.0L, -35); immutable bigP7 = pow(7.0L, 30); foreach (X; AliasSeq!(float, double, real)) { immutable min_sub = X.min_normal * X.epsilon; foreach (x; [smallP2, min_sub, X.min_normal, .25L, 0.5L, 1.0L, 2.0L, 8.0L, pow(2.0L, X.max_exp - 1), bigP2]) { assert( isPowerOf2(cast(X) x)); assert(!isPowerOf2(cast(X)-x)); } foreach (x; [0.0L, 3 * min_sub, smallP7, 0.1L, 1337.0L, bigP7, X.max, real.nan, real.infinity]) { assert(!isPowerOf2(cast(X) x)); assert(!isPowerOf2(cast(X)-x)); } } foreach (X; AliasSeq!(byte, ubyte, short, ushort, int, uint, long, ulong)) { foreach (x; [1, 2, 4, 8, (X.max >>> 1) + 1]) { assert( isPowerOf2(cast(X) x)); static if (isSigned!X) assert(!isPowerOf2(cast(X)-x)); } foreach (x; [0, 3, 5, 13, 77, X.min, X.max]) assert(!isPowerOf2(cast(X) x)); } } /** Determines whether `T` is a class nested inside another class and that `T.outer` is the implicit reference to the outer class (i.e. `outer` has not been used as a field or method name) Params: T = type to test Returns: `true` if `T` is a class nested inside another, with the conditions described above; `false` otherwise */ template isInnerClass(T) if (is(T == class)) { import std.meta : staticIndexOf; static if (is(typeof(T.outer))) enum isInnerClass = __traits(isSame, typeof(T.outer), __traits(parent, T)) && (staticIndexOf!(__traits(allMembers, T), "outer") == -1); else enum isInnerClass = false; } /// @safe unittest { class C { int outer; } static assert(!isInnerClass!C); class Outer1 { class Inner1 { } class Inner2 { int outer; } } static assert(isInnerClass!(Outer1.Inner1)); static assert(!isInnerClass!(Outer1.Inner2)); static class Outer2 { static class Inner { int outer; } } static assert(!isInnerClass!(Outer2.Inner)); }