aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/function_basics.c
blob: 83fb66e4f980cadff07dd089067802df6a93c7fe (plain)
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
#include <helpers.h>

int sprintf(char*, const char*, ...);
int strcmp(const char*, const char*);

int foo() {
    int i;
    int ret;
    i = 0;
    ret = 0;
    for (i = 0; i < 100; i = i + 1) {
        if (i == 12) {
            break;
        }
        ret = ret + i;
    }
    return ret;
}

int f(int a, int b, int c, int d, int e, int f) {
    return a;
}

int f2(int a, int b, int c, int d, int e, int f) {
    return b;
}

int f3(int a, int b, int c, int d, int e, int f) {
    return c;
}

int f4(int a, int b, int c, int d, int e, int f) {
    return d;
}

int f5(int a, int b, int c, int d, int e, int f) {
    return e;
}

int f6(int a, int b, int c, int d, int e, int f) {
    return f;
}

int f7(int select, int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
    switch (select) {
    case 0:
        return a;
    case 1:
        return b;
    case 2:
        return c;
    case 3:
        return d;
    case 4:
        return e;
    case 5:
        return f;
    case 6:
        return g;
    case 7:
        return h;
    case 8:
        return i;
    case 9:
        return j;
    }
}

char* f8(char* buf, int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
    sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", a, b, c, d, e, f, g, h, i, j);
    return buf;
}

typedef struct {
    long x, y;
} S;

int f9(int select, int a, int b, int c, int d, S e, int f, int g) {
    switch (select) {
    case 0:
        return a;
    case 1:
        return b;
    case 2:
        return c;
    case 3:
        return d;
    case 4:
        return e.x;
    case 5:
        return e.y;
    case 6:
        return f;
    case 7:
        return g;
    }
}

int main() {
    ASSERT_EQ(66, foo());
    ASSERT_EQ(10, 10 * f(1, 2, 3, 4, 5, 6));
    ASSERT_EQ(20, 10 * f2(1, 2, 3, 4, 5, 6));
    ASSERT_EQ(30, 10 * f3(1, 2, 3, 4, 5, 6));
    ASSERT_EQ(40, 10 * f4(1, 2, 3, 4, 5, 6));
    ASSERT_EQ(50, 10 * f5(1, 2, 3, 4, 5, 6));
    ASSERT_EQ(60, 10 * f6(1, 2, 3, 4, 5, 6));

    ASSERT_EQ(1, f7(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    ASSERT_EQ(2, f7(1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    ASSERT_EQ(6, f7(5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    ASSERT_EQ(7, f7(6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    ASSERT_EQ(8, f7(7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    ASSERT_EQ(10, f7(9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

    char buf[100];
    ASSERT_EQ(0, strcmp("1,2,3,4,5,6,7,8,9,10", f8(buf, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));

    S s;
    s.x = 5;
    s.y = 6;
    ASSERT_EQ(1, f9(0, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(2, f9(1, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(3, f9(2, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(4, f9(3, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(5, f9(4, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(6, f9(5, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(7, f9(6, 1, 2, 3, 4, s, 7, 8));
    ASSERT_EQ(8, f9(7, 1, 2, 3, 4, s, 7, 8));
}