2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.misc-tests / gcov-4.c
blob27ef508cfe6cc39889897b24918dcc54ecda0ea9
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 int do_something (int i)
9 return i;
12 /* Check static inline functions. */
14 int unref_val;
16 static inline int
17 unreferenced (int i, int j)
19 return i - j;
22 static inline int
23 uncalled (int i, int j)
25 return i * j;
28 static inline int
29 called (int i, int j)
31 return i + j; /* count(1) */
34 void
35 call_unref ()
37 if (unref_val) /* count(1) */
38 unref_val = uncalled (1, 2);
39 unref_val = called (unref_val, 4); /* count(1) */
43 /* Check for loops. */
45 int for_val1;
46 int for_val2;
47 int for_temp;
49 int
50 test_for1 (int n)
52 int i;
53 for_temp = 1; /* count(3) */
54 for (i = 0; i < n; i++)
55 for_temp++; /* count(9) */
56 return for_temp; /* count(3) */
59 int
60 test_for2 (int m, int n, int o)
62 int i, j, k;
63 for_temp = 1; /* count(6) */
64 for (i = 0; i < n; i++)
65 for (j = 0; j < m; j++)
66 for (k = 0; k < o; k++)
67 for_temp++; /* count(81) */
68 return for_temp; /* count(6) */
71 void
72 call_for ()
74 for_val1 += test_for1 (0);
75 for_val1 += test_for1 (2);
76 for_val1 += test_for1 (7);
78 for_val2 += test_for2 (0, 0, 0);
79 for_val2 += test_for2 (1, 0, 0);
80 for_val2 += test_for2 (1, 3, 0);
81 for_val2 += test_for2 (1, 3, 1);
82 for_val2 += test_for2 (3, 1, 5);
83 for_val2 += test_for2 (3, 7, 3);
86 /* Check the use of goto. */
88 int goto_val;
90 int
91 test_goto1 (int f)
93 if (f) /* count(2) */
94 goto lab1; /* count(1) */
95 return 1; /* count(1) */
96 lab1:
97 return 2; /* count(1) */
101 test_goto2 (int f)
103 int i;
104 for (i = 0; i < 10; i++) /* count(15) */
105 if (i == f) goto lab2; /* count(14) */
106 return 4; /* count(1) */
107 lab2:
108 return 8; /* count(1) */
111 void
112 call_goto ()
114 goto_val += test_goto1 (0);
115 goto_val += test_goto1 (1);
116 goto_val += test_goto2 (3);
117 goto_val += test_goto2 (30);
120 /* Check nested if-then-else statements. */
122 int ifelse_val1;
123 int ifelse_val2;
124 int ifelse_val3;
127 test_ifelse1 (int i, int j)
129 int result = 0;
130 if (i) /* count(5) */
131 if (j) /* count(3) */
132 result = do_something (4); /* count(3) */
133 else
134 result = do_something (1024);
135 else
136 if (j) /* count(2) */
137 result = do_something (1); /* count(1) */
138 else
139 result = do_something (2); /* count(1) */
140 if (i > j) /* count(5) */
141 result = do_something (result*2); /* count(1) */
142 if (i > 10) /* count(5) */
143 if (j > 10) /* count(1) */
144 result = do_something (result*4); /* count(1) */
145 return result; /* count(5) */
149 test_ifelse2 (int i)
151 int result = 0;
152 if (!i) /* count(6) */
153 result = do_something (1); /* count(1) */
154 if (i == 1) /* count(6) */
155 result = do_something (1024);
156 if (i == 2) /* count(6) */
157 result = do_something (2); /* count(3) */
158 if (i == 3) /* count(6) */
159 return do_something (8); /* count(2) */
160 if (i == 4) /* count(4) */
161 return do_something (2048);
162 return result; /* count(4) */
166 test_ifelse3 (int i, int j)
168 int result = 1;
169 if (i > 10 && j > i && j < 20) /* count(11) */
170 result = do_something (16); /* count(1) */
171 if (i > 20) /* count(11) */
172 if (j > i) /* count(5) */
173 if (j < 30) /* count(2) */
174 result = do_something (32); /* count(1) */
175 if (i == 3 || j == 47 || i == j) /* count(11) */
176 result = do_something (64); /* count(3) */
177 return result; /* count(11) */
180 void
181 call_ifelse ()
183 ifelse_val1 += test_ifelse1 (0, 2);
184 ifelse_val1 += test_ifelse1 (0, 0);
185 ifelse_val1 += test_ifelse1 (1, 2);
186 ifelse_val1 += test_ifelse1 (10, 2);
187 ifelse_val1 += test_ifelse1 (11, 11);
189 ifelse_val2 += test_ifelse2 (0);
190 ifelse_val2 += test_ifelse2 (2);
191 ifelse_val2 += test_ifelse2 (2);
192 ifelse_val2 += test_ifelse2 (2);
193 ifelse_val2 += test_ifelse2 (3);
194 ifelse_val2 += test_ifelse2 (3);
196 ifelse_val3 += test_ifelse3 (11, 19);
197 ifelse_val3 += test_ifelse3 (25, 27);
198 ifelse_val3 += test_ifelse3 (11, 22);
199 ifelse_val3 += test_ifelse3 (11, 10);
200 ifelse_val3 += test_ifelse3 (21, 32);
201 ifelse_val3 += test_ifelse3 (21, 20);
202 ifelse_val3 += test_ifelse3 (1, 2);
203 ifelse_val3 += test_ifelse3 (32, 31);
204 ifelse_val3 += test_ifelse3 (3, 0);
205 ifelse_val3 += test_ifelse3 (0, 47);
206 ifelse_val3 += test_ifelse3 (65, 65);
209 /* Check switch statements. */
211 int switch_val, switch_m;
214 test_switch (int i, int j)
216 int result = 0; /* count(5) */
218 switch (i) /* count(5) */
220 case 1:
221 result = do_something (2); /* count(1) */
222 break;
223 case 2:
224 result = do_something (1024);
225 break;
226 case 3:
227 case 4:
228 if (j == 2) /* count(3) */
229 return do_something (4); /* count(1) */
230 result = do_something (8); /* count(2) */
231 break;
232 default:
233 result = do_something (32); /* count(1) */
234 switch_m++; /* count(1) */
235 break;
237 return result; /* count(4) */
240 void
241 call_switch ()
243 switch_val += test_switch (1, 0);
244 switch_val += test_switch (3, 0);
245 switch_val += test_switch (3, 2);
246 switch_val += test_switch (4, 0);
247 switch_val += test_switch (16, 0);
248 switch_val += switch_m;
252 main()
254 call_for ();
255 call_goto ();
256 call_ifelse ();
257 call_switch ();
258 call_unref ();
259 if ((for_val1 != 12)
260 || (for_val2 != 87)
261 || (goto_val != 15)
262 || (ifelse_val1 != 31)
263 || (ifelse_val2 != 23)
264 || (ifelse_val3 != 246)
265 || (switch_val != 55)
266 || (unref_val != 4))
267 abort ();
268 return 0;
271 /* { dg-final { run-gcov gcov-4.c } } */