| 1 |
/* |
| 2 |
REQUIRED_ARGS: |
| 3 |
RUN_OUTPUT: |
| 4 |
--- |
| 5 |
47 47 |
| 6 |
47 47 |
| 7 |
48 48 |
| 8 |
48 48 |
| 9 |
i = 1 |
| 10 |
6 |
| 11 |
here 3 |
| 12 |
here 3 |
| 13 |
here 3 |
| 14 |
Success |
| 15 |
--- |
| 16 |
*/ |
| 17 |
|
| 18 |
import core.stdc.stdio; |
| 19 |
|
| 20 |
/********************************************************/ |
| 21 |
|
| 22 |
//int delegate(int, char[]) *p; |
| 23 |
|
| 24 |
class Foo |
| 25 |
{ |
| 26 |
int bar(int i, char[] s) { return 4; } |
| 27 |
} |
| 28 |
|
| 29 |
class Bar : Foo |
| 30 |
{ |
| 31 |
override int bar(int i, char[] s) { return 5; } |
| 32 |
} |
| 33 |
|
| 34 |
void test1() |
| 35 |
{ |
| 36 |
int delegate(int, char[]) dg; |
| 37 |
Foo f = new Foo(); |
| 38 |
Bar b = new Bar(); |
| 39 |
int x; |
| 40 |
|
| 41 |
dg = &f.bar; |
| 42 |
x = dg(3, null); |
| 43 |
assert(x == 4); |
| 44 |
|
| 45 |
f = b; |
| 46 |
dg = &f.bar; |
| 47 |
x = dg(3, null); |
| 48 |
assert(x == 5); |
| 49 |
} |
| 50 |
|
| 51 |
/********************************************************/ |
| 52 |
|
| 53 |
int foo2() |
| 54 |
{ |
| 55 |
return 3; |
| 56 |
} |
| 57 |
|
| 58 |
void test2() |
| 59 |
{ |
| 60 |
int function () fp; |
| 61 |
|
| 62 |
fp = &foo2; |
| 63 |
assert(fp() == 3); |
| 64 |
} |
| 65 |
|
| 66 |
/********************************************************/ |
| 67 |
|
| 68 |
class Foo3 |
| 69 |
{ |
| 70 |
int bar(int i, char[] s) { return 47; } |
| 71 |
|
| 72 |
void test() |
| 73 |
{ |
| 74 |
int delegate(int, char[]) dg; |
| 75 |
|
| 76 |
dg = &bar; |
| 77 |
printf("%d %d\n", dg(3, null), result()); |
| 78 |
assert(dg(3, null) == result()); |
| 79 |
|
| 80 |
dg = &this.bar; |
| 81 |
printf("%d %d\n", dg(3, null), result()); |
| 82 |
assert(dg(3, null) == result()); |
| 83 |
} |
| 84 |
|
| 85 |
int result() { return 47; } |
| 86 |
} |
| 87 |
|
| 88 |
class Bar3 : Foo3 |
| 89 |
{ |
| 90 |
override int bar(int i, char[] s) { return 48; } |
| 91 |
|
| 92 |
override int result() { return 48; } |
| 93 |
|
| 94 |
void test2() |
| 95 |
{ |
| 96 |
int delegate(int, char[]) dg; |
| 97 |
|
| 98 |
dg = &super.bar; |
| 99 |
assert(dg(3, null) == 47); |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
void test3() |
| 105 |
{ |
| 106 |
Foo3 f = new Foo3(); |
| 107 |
f.test(); |
| 108 |
|
| 109 |
Bar3 b = new Bar3(); |
| 110 |
b.test(); |
| 111 |
b.test2(); |
| 112 |
} |
| 113 |
|
| 114 |
/********************************************************/ |
| 115 |
|
| 116 |
|
| 117 |
int foo4(int x) { return 1; } |
| 118 |
int foo4(char x) { return 2; } |
| 119 |
int foo4(int x, int y) { return 3; } |
| 120 |
|
| 121 |
void test4() |
| 122 |
{ |
| 123 |
int function (char) fp; |
| 124 |
|
| 125 |
fp = &foo4; |
| 126 |
assert(fp(0) == 2); |
| 127 |
} |
| 128 |
|
| 129 |
/********************************************************/ |
| 130 |
|
| 131 |
class Abc |
| 132 |
{ |
| 133 |
int foo1(int x) { return 1; } |
| 134 |
int foo1(char x) { return 2; } |
| 135 |
int foo1(int x, int y) { return 3; } |
| 136 |
} |
| 137 |
|
| 138 |
void test5() |
| 139 |
{ |
| 140 |
int delegate(char) dg; |
| 141 |
Abc a = new Abc(); |
| 142 |
|
| 143 |
dg = &a.foo1; |
| 144 |
assert(dg(0) == 2); |
| 145 |
} |
| 146 |
|
| 147 |
/********************************************************/ |
| 148 |
|
| 149 |
int delegate(int) bar6; |
| 150 |
|
| 151 |
int[int delegate(int)] aa6; |
| 152 |
|
| 153 |
void test6() |
| 154 |
{ |
| 155 |
aa6[bar6] = 0; |
| 156 |
} |
| 157 |
|
| 158 |
/********************************************************/ |
| 159 |
|
| 160 |
void foo7(void delegate(int) dg) |
| 161 |
{ |
| 162 |
dg(1); |
| 163 |
//writefln("%s", dg(3)); |
| 164 |
} |
| 165 |
|
| 166 |
void test7() |
| 167 |
{ |
| 168 |
foo7(delegate(int i) |
| 169 |
{ |
| 170 |
printf("i = %d\n", i); |
| 171 |
} |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/********************************************************/ |
| 176 |
|
| 177 |
void foo8(int delegate(int) dg) |
| 178 |
{ |
| 179 |
printf("%d\n", dg(3)); |
| 180 |
assert(dg(3) == 6); |
| 181 |
} |
| 182 |
|
| 183 |
void test8() |
| 184 |
{ |
| 185 |
foo8(delegate(int i) |
| 186 |
{ |
| 187 |
return i * 2; |
| 188 |
} |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
/********************************************************/ |
| 193 |
|
| 194 |
void foo9(int delegate(int) dg) |
| 195 |
{ |
| 196 |
assert(dg(3) == 6); |
| 197 |
} |
| 198 |
|
| 199 |
void test9() |
| 200 |
{ |
| 201 |
foo9( (int i) { return i * 2; } ); |
| 202 |
} |
| 203 |
|
| 204 |
/********************************************************/ |
| 205 |
// https://issues.dlang.org/show_bug.cgi?id=8257 |
| 206 |
|
| 207 |
struct S8257 { |
| 208 |
static int g() { |
| 209 |
return 6; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void test8257() |
| 214 |
{ |
| 215 |
S8257 s; |
| 216 |
int w = 2; |
| 217 |
S8257 func() { ++w; return s; } |
| 218 |
auto k = &(func()).g; |
| 219 |
// check that the call to func() still happened |
| 220 |
assert(w == 3); |
| 221 |
assert( k() == 6); |
| 222 |
} |
| 223 |
|
| 224 |
auto f8257(string m)() { return &__traits(getMember, S8257.init, m); } |
| 225 |
static assert (__traits(compiles, f8257!"g"())); |
| 226 |
|
| 227 |
/********************************************************/ |
| 228 |
|
| 229 |
void foo10(int delegate() dg) |
| 230 |
{ |
| 231 |
assert(dg() == 6); |
| 232 |
} |
| 233 |
|
| 234 |
void test10() |
| 235 |
{ int i = 3; |
| 236 |
|
| 237 |
foo10( { return i * 2; } ); |
| 238 |
} |
| 239 |
|
| 240 |
/********************************************************/ |
| 241 |
|
| 242 |
class A12 |
| 243 |
{ |
| 244 |
public: |
| 245 |
int delegate(int, int)[4] dgs; |
| 246 |
int function(int, int)[4] fps; |
| 247 |
int delegate(int, int) dg; |
| 248 |
int function(int, int) fp; |
| 249 |
int f(int x, int y) { |
| 250 |
printf("here "); |
| 251 |
int res = x + y; |
| 252 |
printf("%d\n", res); |
| 253 |
return res; |
| 254 |
} |
| 255 |
|
| 256 |
void bug_1() { |
| 257 |
// fp = &f; |
| 258 |
// fp(1,2); |
| 259 |
dg = &f; |
| 260 |
dg(1,2); |
| 261 |
// fps[] = [&f, &f, &f, &(f)]; // bug 1: this line shouldn't compile |
| 262 |
// this.fps[0](1, 2); // seg-faults here! |
| 263 |
|
| 264 |
dgs[] = [&(f), &(f), &(f), &(f)]; // bug 1: why this line can't compile? |
| 265 |
this.dgs[0](1, 2); |
| 266 |
|
| 267 |
dgs[] = [&(this.f), &(this.f), &(this.f), &(this.f)]; |
| 268 |
this.dgs[0](1, 2); |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
void test12() |
| 274 |
{ |
| 275 |
A12 a = new A12(); |
| 276 |
|
| 277 |
a.bug_1(); |
| 278 |
} |
| 279 |
|
| 280 |
/********************************************************/ |
| 281 |
// https://issues.dlang.org/show_bug.cgi?id=1570 |
| 282 |
|
| 283 |
class A13 |
| 284 |
{ |
| 285 |
int f() |
| 286 |
{ |
| 287 |
return 1; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
class B13 : A13 |
| 292 |
{ |
| 293 |
override int f() |
| 294 |
{ |
| 295 |
return 2; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
void test13() |
| 300 |
{ |
| 301 |
B13 b = new B13; |
| 302 |
assert(b.f() == 2); |
| 303 |
assert(b.A13.f() == 1); |
| 304 |
assert((&b.f)() == 2); |
| 305 |
assert((&b.A13.f)() == 1); |
| 306 |
} |
| 307 |
|
| 308 |
/********************************************************/ |
| 309 |
|
| 310 |
enum dg14 = delegate { ++a14; b14 += 2; }; |
| 311 |
|
| 312 |
int a14, b14; |
| 313 |
|
| 314 |
void test14() |
| 315 |
{ |
| 316 |
a14 = b14 = 10; |
| 317 |
|
| 318 |
auto var = dg14; |
| 319 |
var(); |
| 320 |
|
| 321 |
assert(a14 == 11); |
| 322 |
assert(b14 == 12); |
| 323 |
} |
| 324 |
|
| 325 |
/********************************************************/ |
| 326 |
// https://issues.dlang.org/show_bug.cgi?id=2472 |
| 327 |
|
| 328 |
class A2472 |
| 329 |
{ |
| 330 |
void foo() {} |
| 331 |
} |
| 332 |
|
| 333 |
void test2472() |
| 334 |
{ |
| 335 |
auto a = new A2472; |
| 336 |
auto fp1 = (&a.foo).funcptr; |
| 337 |
auto dg = &a.foo; |
| 338 |
auto fp2 = dg.funcptr; |
| 339 |
assert(fp1 == fp2); |
| 340 |
} |
| 341 |
|
| 342 |
/********************************************************/ |
| 343 |
|
| 344 |
void testAssign() |
| 345 |
{ |
| 346 |
static class C |
| 347 |
{ |
| 348 |
int a; |
| 349 |
this(int a) { this.a = a; } |
| 350 |
int funca() { return a; } |
| 351 |
int funcb() { return a + 1; } |
| 352 |
} |
| 353 |
|
| 354 |
auto x = new C(5); |
| 355 |
auto y = new C(7); |
| 356 |
|
| 357 |
auto dg = &x.funca; |
| 358 |
assert(dg() == 5); |
| 359 |
dg.funcptr = &C.funcb; |
| 360 |
assert(dg() == 6); |
| 361 |
dg.ptr = cast(void*)y; |
| 362 |
assert(dg() == 8); |
| 363 |
dg.funcptr = &C.funca; |
| 364 |
assert(dg() == 7); |
| 365 |
} |
| 366 |
|
| 367 |
/********************************************************/ |
| 368 |
|
| 369 |
int main() |
| 370 |
{ |
| 371 |
test1(); |
| 372 |
test2(); |
| 373 |
test3(); |
| 374 |
test4(); |
| 375 |
test5(); |
| 376 |
test6(); |
| 377 |
test7(); |
| 378 |
test8(); |
| 379 |
test9(); |
| 380 |
test10(); |
| 381 |
test12(); |
| 382 |
test13(); |
| 383 |
test14(); |
| 384 |
test2472(); |
| 385 |
test8257(); |
| 386 |
testAssign(); |
| 387 |
|
| 388 |
printf("Success\n"); |
| 389 |
return 0; |
| 390 |
} |