Mercurial > projects > ldc
annotate tests/mini/funcptr.d @ 854:0ffa92f4ac99
added a patch to tango's stdio for ldc on win32
author | elrood |
---|---|
date | Sun, 14 Dec 2008 17:53:20 +0100 |
parents | 1bb99290e03a |
children |
rev | line source |
---|---|
270
d9d5d59873d8
[svn r291] Fixed a bunch of the old Phobos tests to work with Tango.
lindquist
parents:
102
diff
changeset
|
1 extern(C) int printf(char*, ...); |
d9d5d59873d8
[svn r291] Fixed a bunch of the old Phobos tests to work with Tango.
lindquist
parents:
102
diff
changeset
|
2 |
1 | 3 int return_six() |
4 { | |
5 return 6; | |
6 } | |
7 | |
8 int add_int(int a, int b) | |
9 { | |
10 return a+b; | |
11 } | |
12 | |
13 int sub_int(int a, int b) | |
14 { | |
15 return a-b; | |
16 } | |
17 | |
18 alias int function(int,int) binfn_t; | |
19 | |
20 int binop_int(binfn_t op, int a, int b) | |
21 { | |
22 return op(a,b); | |
23 } | |
24 | |
25 binfn_t get_binop_int(char op) | |
26 { | |
27 binfn_t fn; | |
28 if (op == '+') | |
29 fn = &add_int; | |
30 else if (op == '-') | |
31 fn = &sub_int; | |
32 return fn; | |
33 } | |
34 | |
35 extern(C) float mul_float(float a, float b) | |
36 { | |
37 return a * b; | |
38 } | |
39 | |
40 void function_pointers() | |
41 { | |
42 int function() fn = &return_six; | |
43 assert(fn() == 6); | |
44 | |
45 binfn_t binfn = &add_int; | |
46 assert(binfn(4,1045) == 1049); | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
1
diff
changeset
|
47 |
1 | 48 assert(binop_int(binfn, 10,656) == 666); |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
1
diff
changeset
|
49 |
1 | 50 binfn = get_binop_int('+'); |
51 assert(binop_int(binfn, 10,100) == 110); | |
52 binfn = get_binop_int('-'); | |
53 assert(binop_int(binfn, 10,100) == -90); | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
1
diff
changeset
|
54 |
1 | 55 { |
56 auto ffn = &mul_float; | |
57 float ftmp = mul_float(2.5,5); | |
58 assert(ftmp == 12.5); | |
59 assert(ftmp > 12.49 && ftmp < 12.51); | |
60 } | |
61 } | |
62 | |
63 void main() | |
64 { | |
65 printf("Function pointer test\n"); | |
66 function_pointers(); | |
67 printf(" SUCCESS\n"); | |
68 } |