aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/functions.sh
blob: ddae9b21e0b9865a1db956cdee551fe713899d91 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# function pointers
cat <<'EOF' > expected
a
h
g
EOF

test_diff <<'EOF'
int* f1(int a);
int (*f2)(int a);

extern int atexit (void (*) (void));
extern int atexit (void (*fn) (void));

int printf(const char*, ...);

void g() { printf("g\n"); }
void h() { printf("h\n"); }

int main() {
    atexit(g);
    atexit(h);
    printf("a\n");
}
EOF

# void functions
cat <<'EOF' > expected
123
EOF
test_diff <<'EOF'
int printf();

void foo_bar(int hoge_piyo) {
    printf("%d\n", hoge_piyo);
}

int main() {
    foo_bar(123);
    return 0;
}
EOF

cat <<'EOF' > expected
EOF
test_diff <<'EOF'
struct S {
    int a;
};

struct S* f();

struct S* g() {}

int main() {
    return 0;
}
EOF

cat <<'EOF' > expected
hi
EOF
test_diff <<'EOF'
int printf();

void f() {
    printf("hi\n");
    return;
}

int main() {
    f();
    return 0;
}
EOF

# variadic functions
cat <<'EOF' > expected
123
456 789
EOF

test_diff <<'EOF'
#include <stdarg.h>

int fprintf();

struct FILE;
typedef struct FILE FILE;

extern FILE* stdout;

int vfprintf(FILE*, const char*, va_list);

void fatal_error(const char* msg, ...) {
    va_list args;
    va_start(args, msg);
    vfprintf(stdout, msg, args);
    va_end(args);
    fprintf(stdout, "\n");
}

int main() {
    fatal_error("%d", 123);
    fatal_error("%d %d", 456, 789);
    return 0;
}
EOF

test_exit_code 0 <<'EOF'
#include <stdarg.h>
#include <helpers.h>

int sum(int n, ...) {
    va_list args;
    va_start(args, n);
    int s = 0;
    for (int i = 0; i < n; ++i) {
        s += va_arg(args, int);
    }
    va_end(args);
    return s;
}

int main() {
    ASSERT_EQ(400, sum(5, 100, 90, 80, 70, 60));
}
EOF

# implicit return
# C99: 5.1.2.2.3
test_exit_code 0 <<'EOF'
int main() {
}
EOF

test_exit_code 0 <<'EOF'
int main() {
    1 + 2 + 3;
}
EOF

test_exit_code 0 <<'EOF'
int main() {
    if (1);
    else return 1;
}
EOF