annotate tests/mini/dgs.d @ 341:1bb99290e03a trunk

[svn r362] Started merging the old 'test' dir as well as the newer 'tangotests' dir into 'tests/mini' and 'tests/minicomplex'.
author lindquist
date Sun, 13 Jul 2008 02:51:19 +0200
parents test/dgs.d@d9d5d59873d8
children
rev   line source
lindquist@270 1
lindquist@270 2 extern(C) int printf(char*, ...);
lindquist@270 3
lindquist@1 4 struct S
lindquist@1 5 {
lindquist@1 6 int i;
lindquist@1 7 int square()
lindquist@1 8 {
lindquist@1 9 return i*i;
lindquist@1 10 }
lindquist@1 11 int plus(int a)
lindquist@1 12 {
lindquist@1 13 return i + a;
lindquist@1 14 }
lindquist@1 15 int minus(int a)
lindquist@1 16 {
lindquist@1 17 return i - a;
lindquist@1 18 }
lindquist@1 19 int delegate(int) get(char op)
lindquist@1 20 {
lindquist@1 21 int delegate(int) rval;
lindquist@1 22 if (op == '+')
lindquist@1 23 rval = +
lindquist@1 24 else if (op == '-')
lindquist@1 25 rval = −
lindquist@1 26 return rval;
lindquist@1 27 }
lindquist@1 28 }
lindquist@1 29
lindquist@1 30 int calldg1(int delegate(int) dg, int i)
lindquist@1 31 {
lindquist@1 32 return dg(i);
lindquist@1 33 }
lindquist@1 34
lindquist@1 35 void delegate() retdg()
lindquist@1 36 {
lindquist@1 37 void delegate() dg;
lindquist@1 38 return dg;
lindquist@1 39 }
lindquist@1 40
lindquist@1 41 void getretdg()
lindquist@1 42 {
lindquist@1 43 void delegate() dg;
lindquist@1 44 dg = retdg();
lindquist@1 45 }
lindquist@1 46
lindquist@1 47 class C
lindquist@1 48 {
lindquist@1 49 int i;
lindquist@1 50 void m()
lindquist@1 51 {
lindquist@1 52 i = 42;
lindquist@1 53 }
lindquist@1 54 }
lindquist@1 55
lindquist@1 56 void getclassdg()
lindquist@1 57 {
lindquist@1 58 scope c = new C;
lindquist@1 59 void delegate() dg = &c.m;
lindquist@1 60 assert(c.i != 42);
lindquist@1 61 dg();
lindquist@1 62 assert(c.i == 42);
lindquist@1 63 }
lindquist@1 64
lindquist@1 65 void main()
lindquist@1 66 {
lindquist@1 67 printf("Delegate test\n");
lindquist@1 68 S s = S(4);
lindquist@1 69
lindquist@1 70 auto dg = &s.square;
lindquist@1 71 //assert(dg() == 16);
lindquist@1 72 //dg();
lindquist@1 73
lindquist@1 74 /*auto dg1 = &s.plus;
lindquist@1 75 assert(dg1(6) == 10);
lindquist@1 76
lindquist@1 77 auto dg2 = &s.minus;
lindquist@1 78 assert(calldg1(dg2,30) == -26);
lindquist@1 79
lindquist@1 80 auto dg3 = s.get('+');
lindquist@1 81 assert(dg3(16) == 20);
lindquist@1 82
lindquist@1 83 getretdg();
lindquist@1 84 getclassdg();*/
lindquist@1 85
lindquist@1 86 printf(" SUCCESS\n");
lindquist@1 87 }