| 1 |
import core.vararg; |
| 2 |
import core.stdc.stdio; |
| 3 |
|
| 4 |
/*********************************************************/ |
| 5 |
|
| 6 |
void ifthen(bool cond, lazy void dg) |
| 7 |
{ |
| 8 |
if (cond) |
| 9 |
dg(); |
| 10 |
} |
| 11 |
|
| 12 |
void ifthen(bool cond, lazy void dgthen, lazy void dgelse) |
| 13 |
{ |
| 14 |
if (cond) |
| 15 |
dgthen(); |
| 16 |
else |
| 17 |
dgelse(); |
| 18 |
} |
| 19 |
|
| 20 |
void dotimes(int i, lazy int dg) |
| 21 |
{ |
| 22 |
for (int j = 0; j < i; j++) |
| 23 |
dg(); |
| 24 |
} |
| 25 |
|
| 26 |
void switcher(bool delegate()[] cases...) |
| 27 |
{ |
| 28 |
foreach (c; cases) |
| 29 |
{ |
| 30 |
if (c()) |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
bool scase(bool b, lazy void dg) |
| 36 |
{ |
| 37 |
if (b) |
| 38 |
{ |
| 39 |
dg(); |
| 40 |
return true; |
| 41 |
} |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
bool sdefault(lazy void dg) |
| 46 |
{ |
| 47 |
dg(); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
void whiler(lazy bool cond, lazy void bdy) |
| 52 |
{ |
| 53 |
while (cond()) |
| 54 |
bdy(); |
| 55 |
} |
| 56 |
|
| 57 |
void test1() |
| 58 |
{ |
| 59 |
int x = 3; |
| 60 |
dotimes(5, printf("%d\n", ++x)); |
| 61 |
|
| 62 |
ifthen(true, printf("yes\n")); |
| 63 |
ifthen(false, printf("no\n")); |
| 64 |
|
| 65 |
ifthen(true, printf("yes\n"), printf("no\n")); |
| 66 |
ifthen(false, printf("yes\n"), printf("no\n")); |
| 67 |
|
| 68 |
int v = 2; |
| 69 |
switcher( |
| 70 |
scase(v == 1, printf("it is 1\n")), |
| 71 |
scase(v == 2, printf("it is 2\n")), |
| 72 |
scase(v == 3, printf("it is 3\n")), |
| 73 |
sdefault( printf("it is default\n")) |
| 74 |
); |
| 75 |
|
| 76 |
whiler( x < 100, |
| 77 |
(){ printf("%d\n", x); x *= 2; }() |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/*********************************************************/ |
| 82 |
|
| 83 |
void fooa(lazy void dg) |
| 84 |
{ |
| 85 |
dg(); |
| 86 |
} |
| 87 |
|
| 88 |
void test2() |
| 89 |
{ |
| 90 |
fooa(cast(void)null); |
| 91 |
} |
| 92 |
|
| 93 |
/*********************************************************/ |
| 94 |
|
| 95 |
void dotimes3(int count, lazy void exp) |
| 96 |
{ |
| 97 |
for (int i = 0; i < count; i++) |
| 98 |
exp; |
| 99 |
} |
| 100 |
|
| 101 |
void bar3(...) |
| 102 |
{ |
| 103 |
assert(_arguments.length == 1); |
| 104 |
assert(va_arg!int(_argptr) == 14); |
| 105 |
} |
| 106 |
|
| 107 |
void arr3(...) |
| 108 |
{ |
| 109 |
assert(_arguments.length == 1); |
| 110 |
assert(va_arg!(int[])(_argptr) == [1,0,0,0,0,0,0,0,0,9]); |
| 111 |
} |
| 112 |
|
| 113 |
void abc3(int* p) |
| 114 |
{ |
| 115 |
assert(*p == 3); |
| 116 |
} |
| 117 |
|
| 118 |
void test3() |
| 119 |
{ |
| 120 |
int x = 3; |
| 121 |
dotimes3(10, abc3(&x)); |
| 122 |
dotimes3(10, ++x); |
| 123 |
dotimes3(1, bar3(++x)); |
| 124 |
|
| 125 |
int[10] a = new int[10]; |
| 126 |
a[0] = 1; |
| 127 |
a[$ - 1] = 9; |
| 128 |
dotimes3(3, arr3(a[0..$])); |
| 129 |
} |
| 130 |
|
| 131 |
/*********************************************************/ |
| 132 |
|
| 133 |
int p4; |
| 134 |
|
| 135 |
void foo4(void* delegate()[] dgs...) |
| 136 |
{ |
| 137 |
assert(dgs.length == 4); |
| 138 |
assert(dgs[0]() == cast(void*)&p4); |
| 139 |
assert(dgs[1]() == cast(void*)&p4); |
| 140 |
assert(dgs[2]() == null); |
| 141 |
assert(dgs[3] == null); |
| 142 |
} |
| 143 |
|
| 144 |
void test4() |
| 145 |
{ |
| 146 |
void *abc() |
| 147 |
{ |
| 148 |
return cast(void*)&p4; |
| 149 |
} |
| 150 |
|
| 151 |
foo4(&abc, cast(void* delegate())&abc, null, cast(void* delegate())null); |
| 152 |
} |
| 153 |
|
| 154 |
/*********************************************************/ |
| 155 |
|
| 156 |
bool nextis(void delegate() dgpositive = {}) |
| 157 |
{ |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
bool looping(lazy bool condition) |
| 162 |
{ |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
void test5() |
| 167 |
{ |
| 168 |
looping(nextis({})); |
| 169 |
looping(nextis({})); |
| 170 |
} |
| 171 |
|
| 172 |
/*********************************************************/ |
| 173 |
|
| 174 |
void foo6(lazy int expr, ...) |
| 175 |
{ |
| 176 |
char[] tmp_msg = va_arg!(char[])(_argptr); |
| 177 |
if (cast(int)(tmp_msg.ptr)=="food_for_thought".length) |
| 178 |
assert(0, "length is in the pointer!"); |
| 179 |
assert(tmp_msg=="food_for_thought"); |
| 180 |
} |
| 181 |
|
| 182 |
int bar6() { return 3; } |
| 183 |
|
| 184 |
void test6() |
| 185 |
{ |
| 186 |
foo6(bar6(),"food_for_thought"); |
| 187 |
} |
| 188 |
|
| 189 |
/*********************************************************/ |
| 190 |
|
| 191 |
void foo7(long delegate()[] dg...) |
| 192 |
{ |
| 193 |
assert(dg[0]() == 1024); |
| 194 |
assert(dg[1]() == 1024); |
| 195 |
} |
| 196 |
|
| 197 |
void bar7(lazy long n) |
| 198 |
{ |
| 199 |
assert(n == 1024); |
| 200 |
} |
| 201 |
|
| 202 |
void test7() |
| 203 |
{ |
| 204 |
int n = 1024; |
| 205 |
foo7(n, n); |
| 206 |
bar7(n); |
| 207 |
} |
| 208 |
|
| 209 |
/*********************************************************/ |
| 210 |
|
| 211 |
struct Bug5750 { int a, b; } |
| 212 |
pure Bug5750 bug5750(lazy int y) { |
| 213 |
Bug5750 retval; |
| 214 |
retval.a = y; |
| 215 |
retval.b = y; |
| 216 |
return retval; |
| 217 |
} |
| 218 |
|
| 219 |
pure void test5750a() { |
| 220 |
auto z1 = bug5750(3); |
| 221 |
assert(z1 == Bug5750(3, 3)); |
| 222 |
auto z2 = bug5750(z1.a); |
| 223 |
assert(z2 == Bug5750(3, 3)); |
| 224 |
auto z3 = bug5750(z1.a+z2.a+3); |
| 225 |
assert(z3 == Bug5750(9, 9)); |
| 226 |
auto z4 = bug5750(++z1.a); // expected??? |
| 227 |
assert(z4 == Bug5750(4, 5)); |
| 228 |
assert(z1 == Bug5750(5, 3)); |
| 229 |
} |
| 230 |
|
| 231 |
int bug5750Global = 7; |
| 232 |
void test5750b() { |
| 233 |
auto z4 = bug5750(bug5750Global); |
| 234 |
assert(z4 == Bug5750(7, 7)); |
| 235 |
auto z5 = bug5750(++bug5750Global); |
| 236 |
assert(z5 == Bug5750(8, 9)); // expected??? |
| 237 |
assert(bug5750Global == 9); |
| 238 |
} |
| 239 |
// Note: also need to make up some fail-compilation tests. |
| 240 |
|
| 241 |
/*********************************************************/ |
| 242 |
|
| 243 |
T calcLazy6682(T)(lazy T n) |
| 244 |
{ |
| 245 |
return n; |
| 246 |
} |
| 247 |
int purefunc6682() pure |
| 248 |
{ |
| 249 |
return calcLazy6682(1); |
| 250 |
} |
| 251 |
void test6682() |
| 252 |
{ |
| 253 |
assert(purefunc6682() == 1); |
| 254 |
} |
| 255 |
|
| 256 |
/*********************************************************/ |
| 257 |
// https://issues.dlang.org/show_bug.cgi?id=9109 |
| 258 |
|
| 259 |
void test9109() |
| 260 |
{ |
| 261 |
void foo(int delegate()[] dgs ...) |
| 262 |
{ |
| 263 |
assert(dgs[0]() + dgs[1]() == 1 + 13); |
| 264 |
} |
| 265 |
|
| 266 |
int x = 10; |
| 267 |
int delegate() dg; |
| 268 |
foo({ return 1; }, { return 3+x; }, dg, null); |
| 269 |
} |
| 270 |
|
| 271 |
/*********************************************************/ |
| 272 |
// https://issues.dlang.org/show_bug.cgi?id=15835 |
| 273 |
|
| 274 |
class C15835 {} |
| 275 |
|
| 276 |
string fun15835(lazy string s) |
| 277 |
{ |
| 278 |
return s; |
| 279 |
} |
| 280 |
|
| 281 |
void test15835() |
| 282 |
{ |
| 283 |
auto c = new C15835; |
| 284 |
auto s = typeid(c).name; |
| 285 |
assert(fun15835(typeid(c).name) == s); |
| 286 |
|
| 287 |
auto a = [c]; |
| 288 |
assert(fun15835(typeid(a[0]).name) == s); |
| 289 |
} |
| 290 |
|
| 291 |
/*********************************************************/ |
| 292 |
|
| 293 |
int main() |
| 294 |
{ |
| 295 |
test1(); |
| 296 |
test2(); |
| 297 |
test3(); |
| 298 |
test4(); |
| 299 |
test5(); |
| 300 |
test6(); |
| 301 |
test7(); |
| 302 |
|
| 303 |
test5750a(); |
| 304 |
test5750b(); |
| 305 |
test6682(); |
| 306 |
test9109(); |
| 307 |
test15835(); |
| 308 |
|
| 309 |
printf("Success\n"); |
| 310 |
return 0; |
| 311 |
} |