| 1 |
// REQUIRED_ARGS: -preview=dip1000 -preview=in |
| 2 |
|
| 3 |
void main () |
| 4 |
{ |
| 5 |
testWithAllAttributes(); |
| 6 |
testForeach(); |
| 7 |
} |
| 8 |
|
| 9 |
void testWithAllAttributes() @safe pure nothrow @nogc |
| 10 |
{ |
| 11 |
// Used to test dtors |
| 12 |
bool isTestOver = false; |
| 13 |
|
| 14 |
// rvalues |
| 15 |
testin1(42); |
| 16 |
testin2((ulong[64]).init); |
| 17 |
testin3(ValueT(42)); |
| 18 |
testin3(RefT(42)); |
| 19 |
testin4((ValueT[64]).init); |
| 20 |
testin4([RefT(42), RefT(84), RefT(126), RefT(4)]); |
| 21 |
testin5(NonCopyable(true)); |
| 22 |
testin6(WithPostblit(true)); |
| 23 |
testin7(WithCopyCtor(true)); |
| 24 |
isTestOver = false; |
| 25 |
testin8(WithDtor(&isTestOver), &isTestOver); |
| 26 |
isTestOver = false; |
| 27 |
|
| 28 |
// lvalues |
| 29 |
uint a1; |
| 30 |
ulong[64] a2; |
| 31 |
ValueT a3; |
| 32 |
ValueT[64] a4; |
| 33 |
RefT a5; |
| 34 |
RefT[4] a6; |
| 35 |
NonCopyable a7 = NonCopyable(true); |
| 36 |
WithPostblit a8; |
| 37 |
WithCopyCtor a9; |
| 38 |
WithDtor a10 = WithDtor(&isTestOver); |
| 39 |
|
| 40 |
testin1(a1); |
| 41 |
testin2(a2); |
| 42 |
testin3(a3); |
| 43 |
testin3(a5); |
| 44 |
testin4(a4); |
| 45 |
testin4(a6); |
| 46 |
testin5(a7); |
| 47 |
testin6(a8); |
| 48 |
testin7(a9); |
| 49 |
isTestOver = false; |
| 50 |
testin8(a10, null); |
| 51 |
|
| 52 |
// Arguments are all values, no `ref` needed |
| 53 |
testin9(int.init); |
| 54 |
testin9(char.init, ubyte.init, short.init, int.init, size_t.init); |
| 55 |
// Arguments are all refs |
| 56 |
testin9(a2, a4, a5, a6, a7, a8, a9, a10); |
| 57 |
testin9(NonCopyable(true), WithPostblit(true), WithCopyCtor(true)); |
| 58 |
// Mixed values and ref |
| 59 |
testin9(char.init, ubyte.init, a2, a4, a5, a6, a7, a8, a9, a10, size_t.init); |
| 60 |
|
| 61 |
// With dtor |
| 62 |
isTestOver = false; |
| 63 |
testin10(&isTestOver, NonCopyable(true), WithPostblit(true), |
| 64 |
WithCopyCtor(true), WithDtor(&isTestOver)); |
| 65 |
isTestOver = true; |
| 66 |
} |
| 67 |
|
| 68 |
void testForeach() @safe pure |
| 69 |
{ |
| 70 |
int testCallNC (in NonCopyable k, in NonCopyable v) |
| 71 |
{ |
| 72 |
assert(k == v); |
| 73 |
return k.value - v.value; |
| 74 |
} |
| 75 |
|
| 76 |
NonCopyable[NonCopyable] nc; |
| 77 |
nc[NonCopyable(0)] = NonCopyable(0); |
| 78 |
nc[NonCopyable(42)] = NonCopyable(42); |
| 79 |
nc[NonCopyable(int.min)] = NonCopyable(int.min); |
| 80 |
nc[NonCopyable(int.max)] = NonCopyable(int.max); |
| 81 |
foreach (ref k, const ref v; nc) |
| 82 |
{ |
| 83 |
assert(k.value == v.value); |
| 84 |
assert(testCallNC(k, v) == 0); |
| 85 |
} |
| 86 |
assert(nc == nc); |
| 87 |
assert(nc.length == 4); |
| 88 |
|
| 89 |
RefT[RefT] rt; |
| 90 |
rt[RefT(42)] = RefT(42); |
| 91 |
rt[RefT(4)] = RefT(4); |
| 92 |
rt[RefT(242)] = RefT(242); |
| 93 |
rt[RefT(24)] = RefT(24); |
| 94 |
foreach (k, v; rt) |
| 95 |
assert(k.value == v.value); |
| 96 |
assert(rt == rt); |
| 97 |
|
| 98 |
static struct Msg |
| 99 |
{ |
| 100 |
ubyte[3] value; |
| 101 |
const(char)[] msg; |
| 102 |
} |
| 103 |
|
| 104 |
static void testMsg (in Msg k_func, in Msg v_func) |
| 105 |
{ |
| 106 |
assert(k_func.value == v_func.value); |
| 107 |
assert(k_func.msg == v_func.msg); |
| 108 |
assert(k_func == v_func); |
| 109 |
} |
| 110 |
|
| 111 |
Msg[Msg] msg; |
| 112 |
msg[Msg([1, 2, 3], "123")] = Msg([1, 2, 3], "123"); |
| 113 |
msg[Msg([42, 4, 2], "4242")] = Msg([42, 4, 2], "4242"); |
| 114 |
msg[Msg([242, 4, 0], "2424")] = Msg([242, 4, 0], "2424"); |
| 115 |
foreach (ref k_loop, ref v_loop; msg) |
| 116 |
testMsg(k_loop, v_loop); |
| 117 |
} |
| 118 |
|
| 119 |
struct ValueT { int value; } |
| 120 |
struct RefT { ulong[64] value; } |
| 121 |
|
| 122 |
struct NonCopyable |
| 123 |
{ |
| 124 |
@safe pure nothrow @nogc: |
| 125 |
|
| 126 |
int value; |
| 127 |
this(int b) { this.value = b; } |
| 128 |
|
| 129 |
@disable this(this); |
| 130 |
@disable this(ref NonCopyable); |
| 131 |
} |
| 132 |
|
| 133 |
struct WithPostblit |
| 134 |
{ |
| 135 |
int value; |
| 136 |
this(this) @safe pure nothrow @nogc { assert(0); } |
| 137 |
} |
| 138 |
|
| 139 |
struct WithCopyCtor |
| 140 |
{ |
| 141 |
@safe pure nothrow @nogc: |
| 142 |
|
| 143 |
int value; |
| 144 |
this(int b) { this.value = b; } |
| 145 |
this(ref WithCopyCtor) { assert(0); } |
| 146 |
} |
| 147 |
|
| 148 |
struct WithDtor |
| 149 |
{ |
| 150 |
bool* value; |
| 151 |
~this() scope @safe pure nothrow @nogc { assert(*value); } |
| 152 |
} |
| 153 |
|
| 154 |
@safe pure nothrow @nogc: |
| 155 |
|
| 156 |
// By value |
| 157 |
void testin1(in uint p) { } |
| 158 |
// By ref because of size |
| 159 |
void testin2(in ulong[64] p) { } |
| 160 |
// By value or ref depending on size (or structs always passed by reference) |
| 161 |
void testin3(in ValueT p) { } |
| 162 |
void testin3(in RefT p) { } |
| 163 |
// By ref because of size (or arrays always passed by reference) |
| 164 |
void testin4(in ValueT[64] p) { } |
| 165 |
void testin4(in RefT[4] p) { } |
| 166 |
|
| 167 |
// By ref because of non-copyability |
| 168 |
void testin5(in NonCopyable noncopy) { } |
| 169 |
// By ref because of postblit |
| 170 |
void testin6(in WithPostblit withpostblit) { } |
| 171 |
// By ref because of copy ctor |
| 172 |
void testin7(in WithCopyCtor withcopy) { } |
| 173 |
// By ref because of dtor |
| 174 |
void testin8(in WithDtor withdtor, scope bool* isTestOver) |
| 175 |
{ |
| 176 |
if (isTestOver) |
| 177 |
*isTestOver = true; |
| 178 |
} |
| 179 |
|
| 180 |
// Allow to test various tuples (e.g. `(int, int)` and `(int, WithDtor)`) |
| 181 |
// `ref` is only applied to the members which need it |
| 182 |
void testin9(T...)(in T args) {} |
| 183 |
void testin10(T...)(scope bool* isTestOver, in T args) |
| 184 |
{ |
| 185 |
if (isTestOver) |
| 186 |
*isTestOver = true; |
| 187 |
} |