|
| 1 | +module deinitialization; |
| 2 | + |
| 3 | +@("global.deinitialize") |
| 4 | +unittest |
| 5 | +{ |
| 6 | + import dmd.globals : global; |
| 7 | + |
| 8 | + static void assertStructsEqual(T)(const ref T a, const ref T b) @nogc pure nothrow |
| 9 | + if (is(T == struct)) |
| 10 | + { |
| 11 | + foreach (i, _; typeof(a.tupleof)) |
| 12 | + { |
| 13 | + enum name = __traits(identifier, a.tupleof[i]); |
| 14 | + |
| 15 | + static if (is(typeof(a.tupleof[i]) == const(char*))) |
| 16 | + assert(a.tupleof[i].fromStringz == b.tupleof[i].fromStringz, name); |
| 17 | + else |
| 18 | + assert(a.tupleof[i] == b.tupleof[i], name); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + immutable init = global.init; |
| 23 | + assertStructsEqual(global, init); |
| 24 | + |
| 25 | + global._init(); |
| 26 | + global.deinitialize(); |
| 27 | + |
| 28 | + assert(global == global.init); |
| 29 | +} |
| 30 | + |
| 31 | +@("Type.deinitialize") |
| 32 | +unittest |
| 33 | +{ |
| 34 | + import dmd.mars : addDefaultVersionIdentifiers, setTarget; |
| 35 | + import dmd.mtype : Type; |
| 36 | + import dmd.globals : global; |
| 37 | + |
| 38 | + assert(Type.stringtable == Type.stringtable.init); |
| 39 | + |
| 40 | + global._init(); |
| 41 | + setTarget(global.params); |
| 42 | + |
| 43 | + Type._init(); |
| 44 | + Type.deinitialize(); |
| 45 | + |
| 46 | + global.deinitialize(); |
| 47 | + |
| 48 | + assert(Type.stringtable == Type.stringtable.init); |
| 49 | +} |
| 50 | + |
| 51 | +@("Id.deinitialize") |
| 52 | +unittest |
| 53 | +{ |
| 54 | + import dmd.id : Id; |
| 55 | + |
| 56 | + static void assertInitialState() |
| 57 | + { |
| 58 | + foreach (e ; __traits(allMembers, Id)) |
| 59 | + { |
| 60 | + static if (!__traits(isStaticFunction, mixin("Id." ~ e))) |
| 61 | + assert(__traits(getMember, Id, e) is null); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + assertInitialState(); |
| 66 | + |
| 67 | + Id.initialize(); |
| 68 | + Id.deinitialize(); |
| 69 | + |
| 70 | + assertInitialState(); |
| 71 | +} |
| 72 | + |
| 73 | +@("Module.deinitialize") |
| 74 | +unittest |
| 75 | +{ |
| 76 | + import dmd.dmodule : Module; |
| 77 | + |
| 78 | + assert(Module.modules is Module.modules.init); |
| 79 | + |
| 80 | + Module._init(); |
| 81 | + Module.deinitialize(); |
| 82 | + |
| 83 | + assert(Module.modules is Module.modules.init); |
| 84 | +} |
| 85 | + |
| 86 | +@("Target.deinitialize") |
| 87 | +unittest |
| 88 | +{ |
| 89 | + import dmd.globals : Param; |
| 90 | + import dmd.mars : setTarget; |
| 91 | + import dmd.target : target, Target; |
| 92 | + |
| 93 | + static bool isFPTypeProperties(T)() |
| 94 | + { |
| 95 | + return is(T == const(typeof(Target.FloatProperties))) || |
| 96 | + is (T == const(typeof(Target.DoubleProperties))) || |
| 97 | + is (T == const(typeof(Target.RealProperties))); |
| 98 | + } |
| 99 | + |
| 100 | + static void assertStructsEqual(T)(const ref T a, const ref T b) @nogc pure nothrow |
| 101 | + if (is(T == struct)) |
| 102 | + { |
| 103 | + foreach (i, _; typeof(a.tupleof)) |
| 104 | + { |
| 105 | + alias Type = typeof(a.tupleof[i]); |
| 106 | + enum name = __traits(identifier, a.tupleof[i]); |
| 107 | + |
| 108 | + static if (!isFPTypeProperties!Type) |
| 109 | + assert(a.tupleof[i] == b.tupleof[i], name); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + const init = target.init; |
| 114 | + assertStructsEqual(target, init); |
| 115 | + |
| 116 | + Param params; |
| 117 | + setTarget(params); |
| 118 | + |
| 119 | + target._init(params); |
| 120 | + target.deinitialize(); |
| 121 | + |
| 122 | + assertStructsEqual(target, init); |
| 123 | +} |
| 124 | + |
| 125 | +@("Expression.deinitialize") |
| 126 | +unittest |
| 127 | +{ |
| 128 | + import dmd.ctfeexpr : CTFEExp; |
| 129 | + import dmd.expression : Expression; |
| 130 | + |
| 131 | + static void assertInitialState() |
| 132 | + { |
| 133 | + assert(CTFEExp.cantexp is null); |
| 134 | + assert(CTFEExp.voidexp is null); |
| 135 | + assert(CTFEExp.breakexp is null); |
| 136 | + assert(CTFEExp.continueexp is null); |
| 137 | + assert(CTFEExp.gotoexp is null); |
| 138 | + assert(CTFEExp.showcontext is null); |
| 139 | + } |
| 140 | + |
| 141 | + assertInitialState(); |
| 142 | + |
| 143 | + Expression._init(); |
| 144 | + Expression.deinitialize(); |
| 145 | + |
| 146 | + assertInitialState(); |
| 147 | +} |
| 148 | + |
| 149 | +@("Objc.deinitialize") |
| 150 | +unittest |
| 151 | +{ |
| 152 | + import dmd.objc : Objc, objc; |
| 153 | + |
| 154 | + assert(objc is null); |
| 155 | + |
| 156 | + Objc._init(); |
| 157 | + Objc.deinitialize(); |
| 158 | + |
| 159 | + assert(objc is null); |
| 160 | +} |
| 161 | + |
| 162 | +private inout(char)[] fromStringz(inout(char)* cString) @nogc @system pure nothrow |
| 163 | +{ |
| 164 | + import core.stdc.string : strlen; |
| 165 | + return cString ? cString[0 .. strlen(cString)] : null; |
| 166 | +} |
0 commit comments