1 /* Check that execution counts and branch probabilities for various C
2 constructs are reported correctly by gcov. */
4 /* { dg-options "-fprofile-arcs -ftest-coverage" } */
5 /* { dg-do run { target native } } */
7 extern "C" void abort (void);
9 int do_something (int i)
14 /* Check static inline functions. */
19 unreferenced (int i, int j)
25 uncalled (int i, int j)
33 return i + j; /* count(1) */
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. */
55 for_temp = 1; /* count(3) */
56 for (i = 0; i < n; i++) /* branch(25) */
58 for_temp++; /* count(9) */
59 return for_temp; /* count(3) */
63 test_for2 (int m, int n, int o)
66 for_temp = 1; /* count(6) */
67 for (i = 0; i < n; i++) /* branch(30) */
69 for (j = 0; j < m; j++) /* branch(32) */
71 for (k = 0; k < o; k++) /* branch(27) */
73 for_temp++; /* count(81) */
74 return for_temp; /* count(6) */
80 for_val1 += test_for1 (0);
81 for_val1 += test_for1 (2);
82 for_val1 += test_for1 (7);
84 for_val2 += test_for2 (0, 0, 0);
85 for_val2 += test_for2 (1, 0, 0);
86 for_val2 += test_for2 (1, 3, 0);
87 for_val2 += test_for2 (1, 3, 1);
88 for_val2 += test_for2 (3, 1, 5);
89 for_val2 += test_for2 (3, 7, 3);
92 /* Check the use of goto. */
100 if (f) /* count(2) */
102 goto lab1; /* count(1) */
103 return 1; /* count(1) */
105 return 2; /* count(1) */
113 for (i = 0; i < 10; i++) /* count(15) */
115 if (i == f) goto lab2; /* count(14) */
116 return 4; /* count(1) */
118 return 8; /* count(1) */
124 goto_val += test_goto1 (0);
125 goto_val += test_goto1 (1);
126 goto_val += test_goto2 (3);
127 goto_val += test_goto2 (30);
130 /* Check nested if-then-else statements. */
137 test_ifelse1 (int i, int j)
141 if (i) /* count(5) */
143 if (j) /* count(3) */
145 result = do_something (4); /* count(3) */
147 result = do_something (1024);
150 if (j) /* count(2) */
152 result = do_something (1); /* count(1) */
154 result = do_something (2); /* count(1) */
156 if (i > j) /* count(5) */
158 result = do_something (result*2); /* count(1) */
160 if (i > 10) /* count(5) */
162 if (j > 10) /* count(1) */
164 result = do_something (result*4); /* count(1) */
165 return result; /* count(5) */
173 if (!i) /* count(6) */
175 result = do_something (1); /* count(1) */
177 if (i == 1) /* count(6) */
179 result = do_something (1024);
181 if (i == 2) /* count(6) */
183 result = do_something (2); /* count(3) */
185 if (i == 3) /* count(6) */
187 return do_something (8); /* count(2) */
189 if (i == 4) /* count(4) */
191 return do_something (2048);
192 return result; /* count(4) */
196 test_ifelse3 (int i, int j)
199 /* branch(27 50 75) */
200 if (i > 10 && j > i && j < 20) /* count(11) */
202 result = do_something (16); /* count(1) */
204 if (i > 20) /* count(11) */
206 if (j > i) /* count(5) */
208 if (j < 30) /* count(2) */
210 result = do_something (32); /* count(1) */
211 /* branch(9 10 89) */
212 if (i == 3 || j == 47 || i == j) /* count(11) */
214 result = do_something (64); /* count(3) */
215 return result; /* count(11) */
221 ifelse_val1 += test_ifelse1 (0, 2);
222 ifelse_val1 += test_ifelse1 (0, 0);
223 ifelse_val1 += test_ifelse1 (1, 2);
224 ifelse_val1 += test_ifelse1 (10, 2);
225 ifelse_val1 += test_ifelse1 (11, 11);
227 ifelse_val2 += test_ifelse2 (0);
228 ifelse_val2 += test_ifelse2 (2);
229 ifelse_val2 += test_ifelse2 (2);
230 ifelse_val2 += test_ifelse2 (2);
231 ifelse_val2 += test_ifelse2 (3);
232 ifelse_val2 += test_ifelse2 (3);
234 ifelse_val3 += test_ifelse3 (11, 19);
235 ifelse_val3 += test_ifelse3 (25, 27);
236 ifelse_val3 += test_ifelse3 (11, 22);
237 ifelse_val3 += test_ifelse3 (11, 10);
238 ifelse_val3 += test_ifelse3 (21, 32);
239 ifelse_val3 += test_ifelse3 (21, 20);
240 ifelse_val3 += test_ifelse3 (1, 2);
241 ifelse_val3 += test_ifelse3 (32, 31);
242 ifelse_val3 += test_ifelse3 (3, 0);
243 ifelse_val3 += test_ifelse3 (0, 47);
244 ifelse_val3 += test_ifelse3 (65, 65);
247 /* Check switch statements. */
249 int switch_val, switch_m;
252 test_switch (int i, int j)
254 int result = 0; /* count(5) */
256 /* branch(20 0 60 20) */
257 switch (i) /* count(5) */
261 result = do_something (2); /* count(1) */
264 result = do_something (1024);
269 if (j == 2) /* count(3) */
271 return do_something (4); /* count(1) */
272 result = do_something (8); /* count(2) */
275 result = do_something (32); /* count(1) */
276 switch_m++; /* count(1) */
279 return result; /* count(4) */
285 switch_val += test_switch (1, 0);
286 switch_val += test_switch (3, 0);
287 switch_val += test_switch (3, 2);
288 switch_val += test_switch (4, 0);
289 switch_val += test_switch (16, 0);
290 switch_val += switch_m;
304 || (ifelse_val1 != 31)
305 || (ifelse_val2 != 23)
306 || (ifelse_val3 != 246)
307 || (switch_val != 55)
313 /* { dg-final { run-gcov branches { -b gcov-1.C } } } */