1 module automem.test_utils; 2 3 mixin template TestUtils() { 4 version(unittest) { 5 import unit_threaded; 6 import test_allocator; 7 8 @Setup 9 void before() { 10 } 11 12 @Shutdown 13 void after() { 14 reset; 15 } 16 17 void reset() { 18 Struct.numStructs = 0; 19 Class.numClasses = 0; 20 SharedStruct.numStructs = 0; 21 NoGcStruct.numStructs = 0; 22 } 23 24 25 void _writelnUt(T...)(T args) { 26 try { 27 () @trusted { writelnUt(args); }(); 28 } catch(Exception ex) { 29 assert(false); 30 } 31 } 32 33 private struct Struct { 34 int i; 35 static int numStructs = 0; 36 37 this(int i) @safe nothrow { 38 this.i = i; 39 40 ++numStructs; 41 _writelnUt("Struct ", &this, " normal ctor, i=", i, ", N=", numStructs); 42 } 43 44 this(this) @safe nothrow { 45 ++numStructs; 46 _writelnUt("Struct ", &this, " postBlit ctor, i=", i, ", N=", numStructs); 47 } 48 49 ~this() @safe nothrow const { 50 --numStructs; 51 _writelnUt("Struct ", &this, " dtor, i=", i, ", N=", numStructs); 52 } 53 54 int twice() @safe pure const nothrow { 55 return i * 2; 56 } 57 } 58 59 private struct SharedStruct { 60 int i; 61 static int numStructs = 0; 62 63 this(int i) @safe nothrow shared { 64 this.i = i; 65 66 ++numStructs; 67 try () @trusted { 68 _writelnUt("Struct normal ctor ", &this, ", i=", i, ", N=", numStructs); 69 }(); 70 catch(Exception ex) {} 71 } 72 73 this(this) @safe nothrow shared { 74 ++numStructs; 75 try () @trusted { 76 _writelnUt("Struct postBlit ctor ", &this, ", i=", i, ", N=", numStructs); 77 }(); 78 catch(Exception ex) {} 79 } 80 81 ~this() @safe nothrow shared { 82 --numStructs; 83 try () @trusted { _writelnUt("Struct dtor ", &this, ", i=", i, ", N=", numStructs); }(); 84 catch(Exception ex) {} 85 } 86 87 int twice() @safe pure const nothrow shared { 88 return i * 2; 89 } 90 } 91 92 private class Class { 93 int i; 94 static int numClasses = 0; 95 96 this(int i) @safe nothrow { 97 this.i = i; 98 ++numClasses; 99 } 100 101 ~this() @safe nothrow { 102 --numClasses; 103 } 104 105 int twice() @safe pure const nothrow { 106 return i * 2; 107 } 108 } 109 110 private struct SafeAllocator { 111 112 import std.experimental.allocator.mallocator: Mallocator; 113 114 void[] allocate(size_t i) @trusted nothrow @nogc { 115 return Mallocator.instance.allocate(i); 116 } 117 118 void deallocate(void[] bytes) @trusted nothrow @nogc { 119 Mallocator.instance.deallocate(bytes); 120 } 121 } 122 123 static struct NoGcStruct { 124 int i; 125 126 static int numStructs = 0; 127 128 this(int i) @safe @nogc nothrow { 129 this.i = i; 130 131 ++numStructs; 132 } 133 134 this(this) @safe @nogc nothrow { 135 ++numStructs; 136 } 137 138 ~this() @safe @nogc nothrow { 139 --numStructs; 140 } 141 142 } 143 } 144 }