]> pd.if.org Git - lice/blob - tests/function.c
autocommit for files dated 2014-11-17 20:13:36
[lice] / tests / function.c
1 // functions
2
3 int i1()     { return 42; }
4 int i2(void) { return 42; }
5
6 int splat(int a, int b, int c, int d, int e, int f) {
7     expecti(a, 1);
8     expecti(b, 2);
9     expecti(c, 3);
10     expecti(d, 4);
11     expecti(e, 5);
12     expecti(f, 6);
13 }
14
15 int deref(int *a) {
16     return *a;
17 }
18
19 int proto1();
20 int proto1() { return 1024; }
21
22 int proto2(int a, int b);
23 int proto2(int a, int b) {
24     return a + b;
25 }
26
27 void proto3(int a, ...);
28 void proto3(int a, ...) {
29     expecti(a, 1024);
30 }
31
32 void ignore(void) {
33     return;
34 }
35
36 void ___func___(void) {
37     expectstr(__func__, "___func___");
38 }
39
40 int funptr1(void) {
41     return 1024;
42 }
43
44 int funptr2(int a) {
45     return funptr1() * 2;
46 }
47
48 float funptr3(float a) {
49     return a * 2;
50 }
51
52 int funptr4(int (*callback)(void), int *data) {
53     *data = callback();
54     return *data;
55 }
56
57 // emptyies
58 void empty1(void) {
59     // nothing to see here
60 }
61
62 void empty2(void) {
63     // empty semicolons
64     ;;;
65 }
66
67 int main(void) {
68     expecti(i1(), 42);
69     expecti(i2(), 42);
70
71     int a = 1024;
72     expecti(deref(&a),       1024);
73     expecti(proto1(),        1024);
74     expecti(proto2(512,512), 1024);
75
76     proto3(1024);
77     splat(1, 2, 3, 4, 5, 6);
78
79     ignore();
80     ___func___();
81
82     // function pointer tests
83     int   (*ptr1)(void)              = funptr1;
84     int   (*ptr2)(int)               = funptr2;
85     float (*ptr3)(float)             = funptr3;
86     int   (*ptr4)(int(*)(void),int*) = funptr4;
87
88     expecti(ptr1(),        1024);
89     expecti(ptr2(a),       2048);
90     expecti(ptr4(ptr1,&a), 1024);
91     expecti(a,             1024);
92     expectf(ptr3(3.14),    6.28);
93
94     empty1();
95     empty2();
96
97     return 0;
98 }