File: delegate.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (390 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
REQUIRED_ARGS:
RUN_OUTPUT:
---
47 47
47 47
48 48
48 48
i = 1
6
here 3
here 3
here 3
Success
---
*/

import core.stdc.stdio;

/********************************************************/

//int delegate(int, char[]) *p;

class Foo
{
   int bar(int i, char[] s) { return 4; }
}

class Bar : Foo
{
   override int bar(int i, char[] s) { return 5; }
}

void test1()
{
    int delegate(int, char[]) dg;
    Foo f = new Foo();
    Bar b = new Bar();
    int x;

    dg = &f.bar;
    x = dg(3, null);
    assert(x == 4);

    f = b;
    dg = &f.bar;
    x = dg(3, null);
    assert(x == 5);
}

/********************************************************/

int foo2()
{
    return 3;
}

void test2()
{
    int function () fp;

    fp = &foo2;
    assert(fp() == 3);
}

/********************************************************/

class Foo3
{
    int bar(int i, char[] s) { return 47; }

    void test()
    {
        int delegate(int, char[]) dg;

        dg = &bar;
        printf("%d %d\n", dg(3, null), result());
        assert(dg(3, null) == result());

        dg = &this.bar;
        printf("%d %d\n", dg(3, null), result());
        assert(dg(3, null) == result());
    }

    int result() { return 47; }
}

class Bar3 : Foo3
{
    override int bar(int i, char[] s) { return 48; }

    override int result() { return 48; }

    void test2()
    {
        int delegate(int, char[]) dg;

        dg = &super.bar;
        assert(dg(3, null) == 47);
    }

}

void test3()
{
    Foo3 f = new Foo3();
    f.test();

    Bar3 b = new Bar3();
    b.test();
    b.test2();
}

/********************************************************/


int foo4(int x) { return 1; }
int foo4(char x) { return 2; }
int foo4(int x, int y) { return 3; }

void test4()
{
    int function (char) fp;

    fp = &foo4;
    assert(fp(0) == 2);
}

/********************************************************/

class Abc
{
    int foo1(int x) { return 1; }
    int foo1(char x) { return 2; }
    int foo1(int x, int y) { return 3; }
}

void test5()
{
    int delegate(char) dg;
    Abc a = new Abc();

    dg = &a.foo1;
    assert(dg(0) == 2);
}

/********************************************************/

int delegate(int) bar6;

int[int delegate(int)] aa6;

void test6()
{
    aa6[bar6] = 0;
}

/********************************************************/

void foo7(void delegate(int) dg)
{
    dg(1);
    //writefln("%s", dg(3));
}

void test7()
{
    foo7(delegate(int i)
        {
            printf("i = %d\n", i);
        }
        );
}

/********************************************************/

void foo8(int delegate(int) dg)
{
    printf("%d\n", dg(3));
    assert(dg(3) == 6);
}

void test8()
{
    foo8(delegate(int i)
        {
            return i * 2;
        }
        );
}

/********************************************************/

void foo9(int delegate(int) dg)
{
    assert(dg(3) == 6);
}

void test9()
{
    foo9( (int i) { return i * 2; } );
}

/********************************************************/
// https://issues.dlang.org/show_bug.cgi?id=8257

struct S8257 {
    static int g() {
        return 6;
    }
}

void test8257()
{
     S8257 s;
     int w = 2;
     S8257 func() { ++w; return s; }
     auto k = &(func()).g;
     // check that the call to func() still happened
     assert(w == 3);
     assert( k() == 6);
}

auto f8257(string m)() { return &__traits(getMember, S8257.init, m); }
static assert (__traits(compiles, f8257!"g"()));

/********************************************************/

void foo10(int delegate() dg)
{
    assert(dg() == 6);
}

void test10()
{   int i = 3;

    foo10( { return i * 2; } );
}

/********************************************************/

class A12
{
public:
  int delegate(int, int)[4] dgs;
  int function(int, int)[4] fps;
  int delegate(int, int) dg;
  int function(int, int) fp;
  int f(int x, int y) {
    printf("here ");
    int res = x + y;
    printf("%d\n", res);
    return res;
  }

  void bug_1() {
//    fp = &f;
//    fp(1,2);
    dg = &f;
    dg(1,2);
//    fps[] = [&f, &f, &f, &(f)];  // bug 1: this line shouldn't compile
//    this.fps[0](1, 2);  // seg-faults here!

    dgs[] = [&(f), &(f), &(f), &(f)];  // bug 1: why this line can't compile?
    this.dgs[0](1, 2);

    dgs[] = [&(this.f), &(this.f), &(this.f), &(this.f)];
    this.dgs[0](1, 2);
  }

}

void test12()
{
  A12 a = new A12();

  a.bug_1();
}

/********************************************************/
// https://issues.dlang.org/show_bug.cgi?id=1570

class A13
{
    int f()
    {
        return 1;
    }
}

class B13 : A13
{
    override int f()
    {
        return 2;
    }
}

void test13()
{
    B13 b = new B13;
    assert(b.f() == 2);
    assert(b.A13.f() == 1);
    assert((&b.f)() == 2);
    assert((&b.A13.f)() == 1);
}

/********************************************************/

enum dg14 = delegate { ++a14; b14 += 2; };

int a14, b14;

void test14()
{
    a14 = b14 = 10;

    auto var = dg14;
    var();

    assert(a14 == 11);
    assert(b14 == 12);
}

/********************************************************/
// https://issues.dlang.org/show_bug.cgi?id=2472

class A2472
{
    void foo() {}
}

void test2472()
{
    auto a = new A2472;
    auto fp1 = (&a.foo).funcptr;
    auto dg = &a.foo;
    auto fp2 = dg.funcptr;
    assert(fp1 == fp2);
}

/********************************************************/

void testAssign()
{
    static class C
    {
        int a;
        this(int a) { this.a = a; }
        int funca() { return a; }
        int funcb() { return a + 1; }
    }

    auto x = new C(5);
    auto y = new C(7);

    auto dg = &x.funca;
    assert(dg() == 5);
    dg.funcptr = &C.funcb;
    assert(dg() == 6);
    dg.ptr = cast(void*)y;
    assert(dg() == 8);
    dg.funcptr = &C.funca;
    assert(dg() == 7);
}

/********************************************************/

int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
    test12();
    test13();
    test14();
    test2472();
    test8257();
    testAssign();

    printf("Success\n");
    return 0;
}