Mercurial > projects > ldc
annotate test/funcptr.d @ 119:79c9ac745fbc trunk
[svn r123] Fixed some typeinfo module name mismatches.
Fixed D-style varargs returning through pointer.
Fixed converting nested function to delegate.
Added std.string and std.format to Phobos.
author | lindquist |
---|---|
date | Mon, 26 Nov 2007 06:45:13 +0100 |
parents | 027b8d8b71ec |
children | d9d5d59873d8 |
rev | line source |
---|---|
1 | 1 int return_six() |
2 { | |
3 return 6; | |
4 } | |
5 | |
6 int add_int(int a, int b) | |
7 { | |
8 return a+b; | |
9 } | |
10 | |
11 int sub_int(int a, int b) | |
12 { | |
13 return a-b; | |
14 } | |
15 | |
16 alias int function(int,int) binfn_t; | |
17 | |
18 int binop_int(binfn_t op, int a, int b) | |
19 { | |
20 return op(a,b); | |
21 } | |
22 | |
23 binfn_t get_binop_int(char op) | |
24 { | |
25 binfn_t fn; | |
26 if (op == '+') | |
27 fn = &add_int; | |
28 else if (op == '-') | |
29 fn = &sub_int; | |
30 return fn; | |
31 } | |
32 | |
33 extern(C) float mul_float(float a, float b) | |
34 { | |
35 return a * b; | |
36 } | |
37 | |
38 void function_pointers() | |
39 { | |
40 int function() fn = &return_six; | |
41 assert(fn() == 6); | |
42 | |
43 binfn_t binfn = &add_int; | |
44 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
|
45 |
1 | 46 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
|
47 |
1 | 48 binfn = get_binop_int('+'); |
49 assert(binop_int(binfn, 10,100) == 110); | |
50 binfn = get_binop_int('-'); | |
51 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
|
52 |
1 | 53 { |
54 auto ffn = &mul_float; | |
55 float ftmp = mul_float(2.5,5); | |
56 assert(ftmp == 12.5); | |
57 assert(ftmp > 12.49 && ftmp < 12.51); | |
58 } | |
59 } | |
60 | |
61 void main() | |
62 { | |
63 printf("Function pointer test\n"); | |
64 function_pointers(); | |
65 printf(" SUCCESS\n"); | |
66 } |