C99 testsuite readiness: Compile more tests with -std=gnu89
[official-gcc.git] / gcc / testsuite / gcc.misc-tests / gcov-4.c
blobda7929ef7fcd30f00d0c68a451e95c3d8ee4a587
1 /* Check that execution counts for various C constructs are reported
2 correctly by gcov. */
4 /* { dg-options "-fprofile-arcs -ftest-coverage" } */
5 /* { dg-do run { target native } } */
7 extern void abort (void);
9 int do_something (int i)
11 return i;
14 /* Check static inline functions. */
16 int unref_val;
18 static inline int
19 unreferenced (int i, int j)
21 return i - j;
24 static inline int
25 uncalled (int i, int j)
27 return i * j;
30 static inline int
31 called (int i, int j)
33 return i + j; /* count(1) */
36 void
37 call_unref ()
39 if (unref_val) /* count(1) */
40 unref_val = uncalled (1, 2);
41 unref_val = called (unref_val, 4); /* count(1) */
45 /* Check for loops. */
47 int for_val1;
48 int for_val2;
49 int for_temp;
51 int
52 test_for1 (int n)
54 int i;
55 for_temp = 1; /* count(3) */
56 for (i = 0; i < n; i++)
57 for_temp++; /* count(9) */
58 return for_temp; /* count(3) */
61 int
62 test_for2 (int m, int n, int o)
64 int i, j, k;
65 for_temp = 1; /* count(6) */
66 for (i = 0; i < n; i++)
67 for (j = 0; j < m; j++)
68 for (k = 0; k < o; k++)
69 for_temp++; /* count(81) */
70 return for_temp; /* count(6) */
73 void
74 call_for ()
76 for_val1 += test_for1 (0);
77 for_val1 += test_for1 (2);
78 for_val1 += test_for1 (7);
80 for_val2 += test_for2 (0, 0, 0);
81 for_val2 += test_for2 (1, 0, 0);
82 for_val2 += test_for2 (1, 3, 0);
83 for_val2 += test_for2 (1, 3, 1);
84 for_val2 += test_for2 (3, 1, 5);
85 for_val2 += test_for2 (3, 7, 3);
88 /* Check the use of goto. */
90 int goto_val;
92 int
93 test_goto1 (int f)
95 if (f) /* count(2) */
96 goto lab1; /* count(1) */
97 return 1; /* count(1) */
98 lab1:
99 return 2; /* count(1) */
103 test_goto2 (int f)
105 int i;
106 for (i = 0; i < 10; i++) /* count(15) */
107 if (i == f) goto lab2; /* count(14) */
108 return 4; /* count(1) */
109 lab2:
110 return 8; /* count(1) */
114 test_goto3 (int i, int j)
116 if (j) goto else_; /* count(1) */
118 top:
119 if (i) /* count(1) */
121 i = do_something (i);
123 else
125 else_: /* count(1) */
126 j = do_something (j); /* count(2) */
127 if (j) /* count(2) */
129 j = 0; /* count(1) */
130 goto top; /* count(1) */
133 return 16;
136 void
137 call_goto ()
139 goto_val += test_goto1 (0);
140 goto_val += test_goto1 (1);
141 goto_val += test_goto2 (3);
142 goto_val += test_goto2 (30);
143 goto_val += test_goto3 (0, 1);
146 /* Check nested if-then-else statements. */
148 int ifelse_val1;
149 int ifelse_val2;
150 int ifelse_val3;
153 test_ifelse1 (int i, int j)
155 int result = 0;
156 if (i) /* count(5) */
157 if (j) /* count(3) */
158 result = do_something (4); /* count(3) */
159 else
160 result = do_something (1024);
161 else
162 if (j) /* count(2) */
163 result = do_something (1); /* count(1) */
164 else
165 result = do_something (2); /* count(1) */
166 if (i > j) /* count(5) */
167 result = do_something (result*2); /* count(1) */
168 if (i > 10) /* count(5) */
169 if (j > 10) /* count(1) */
170 result = do_something (result*4); /* count(1) */
171 return result; /* count(5) */
175 test_ifelse2 (int i)
177 int result = 0;
178 if (!i) /* count(6) */
179 result = do_something (1); /* count(1) */
180 if (i == 1) /* count(6) */
181 result = do_something (1024);
182 if (i == 2) /* count(6) */
183 result = do_something (2); /* count(3) */
184 if (i == 3) /* count(6) */
185 return do_something (8); /* count(2) */
186 if (i == 4) /* count(4) */
187 return do_something (2048);
188 return result; /* count(4) */
192 test_ifelse3 (int i, int j)
194 int result = 1;
195 if (i > 10 && j > i && j < 20) /* count(11) */
196 result = do_something (16); /* count(1) */
197 if (i > 20) /* count(11) */
198 if (j > i) /* count(5) */
199 if (j < 30) /* count(2) */
200 result = do_something (32); /* count(1) */
201 if (i == 3 || j == 47 || i == j) /* count(11) */
202 result = do_something (64); /* count(3) */
203 return result; /* count(11) */
206 void
207 call_ifelse ()
209 ifelse_val1 += test_ifelse1 (0, 2);
210 ifelse_val1 += test_ifelse1 (0, 0);
211 ifelse_val1 += test_ifelse1 (1, 2);
212 ifelse_val1 += test_ifelse1 (10, 2);
213 ifelse_val1 += test_ifelse1 (11, 11);
215 ifelse_val2 += test_ifelse2 (0);
216 ifelse_val2 += test_ifelse2 (2);
217 ifelse_val2 += test_ifelse2 (2);
218 ifelse_val2 += test_ifelse2 (2);
219 ifelse_val2 += test_ifelse2 (3);
220 ifelse_val2 += test_ifelse2 (3);
222 ifelse_val3 += test_ifelse3 (11, 19);
223 ifelse_val3 += test_ifelse3 (25, 27);
224 ifelse_val3 += test_ifelse3 (11, 22);
225 ifelse_val3 += test_ifelse3 (11, 10);
226 ifelse_val3 += test_ifelse3 (21, 32);
227 ifelse_val3 += test_ifelse3 (21, 20);
228 ifelse_val3 += test_ifelse3 (1, 2);
229 ifelse_val3 += test_ifelse3 (32, 31);
230 ifelse_val3 += test_ifelse3 (3, 0);
231 ifelse_val3 += test_ifelse3 (0, 47);
232 ifelse_val3 += test_ifelse3 (65, 65);
235 /* Check switch statements. */
237 int switch_val, switch_m;
240 test_switch (int i, int j)
242 int result = 0; /* count(5) */
244 switch (i) /* count(5) */
246 case 1:
247 result = do_something (2); /* count(1) */
248 break; /* count(1) */
249 case 2:
250 result = do_something (1024);
251 break;
252 case 3:
253 case 4:
254 if (j == 2) /* count(3) */
255 return do_something (4); /* count(1) */
256 result = do_something (8); /* count(2) */
257 break; /* count(2) */
258 default:
259 result = do_something (32); /* count(1) */
260 switch_m++; /* count(1) */
261 break;
263 return result; /* count(4) */
266 void
267 call_switch ()
269 switch_val += test_switch (1, 0);
270 switch_val += test_switch (3, 0);
271 switch_val += test_switch (3, 2);
272 switch_val += test_switch (4, 0);
273 switch_val += test_switch (16, 0);
274 switch_val += switch_m;
278 main()
280 call_for ();
281 call_goto ();
282 call_ifelse ();
283 call_switch ();
284 call_unref ();
285 if ((for_val1 != 12)
286 || (for_val2 != 87)
287 || (goto_val != 31)
288 || (ifelse_val1 != 31)
289 || (ifelse_val2 != 23)
290 || (ifelse_val3 != 246)
291 || (switch_val != 55)
292 || (unref_val != 4))
293 abort ();
294 return 0;
297 /* { dg-final { run-gcov gcov-4.c } } */