2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.misc-tests / gcov-4b.c
blob139f10d86c901b7b0bf9da02a4a18b9d898d715b
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 for loops. */
14 int for_val1;
15 int for_val2;
16 int for_temp;
18 int
19 test_for1 (int n)
21 int i;
22 for_temp = 1;
23 for (i = 0; i < n; i++) /* branch(25) */
24 /* branch(end) */
25 for_temp++;
26 return for_temp;
29 int
30 test_for2 (int m, int n, int o)
32 int i, j, k;
33 for_temp = 1;
34 for (i = 0; i < n; i++) /* branch(30) */
35 /* branch(end) */
36 for (j = 0; j < m; j++) /* branch(32) */
37 /* branch(end) */
38 for (k = 0; k < o; k++) /* branch(27) */
39 /* branch(end) */
40 for_temp++;
41 return for_temp;
44 void
45 call_for ()
47 for_val1 += test_for1 (0);
48 for_val1 += test_for1 (2);
49 for_val1 += test_for1 (7);
51 for_val2 += test_for2 (0, 0, 0);
52 for_val2 += test_for2 (1, 0, 0);
53 for_val2 += test_for2 (1, 3, 0);
54 for_val2 += test_for2 (1, 3, 1);
55 for_val2 += test_for2 (3, 1, 5);
56 for_val2 += test_for2 (3, 7, 3);
59 /* Check the use of goto. */
61 int goto_val;
63 int
64 test_goto1 (int f)
66 if (f) /* branch(50) */
67 /* branch(end) */
68 goto lab1;
69 return 1;
70 lab1:
71 return 2;
74 int
75 test_goto2 (int f)
77 int i;
78 for (i = 0; i < 10; i++) /* branch(7) */
79 /* branch(end) */
80 if (i == f) goto lab2;
81 return 4;
82 lab2:
83 return 8;
86 void
87 call_goto ()
89 goto_val += test_goto1 (0);
90 goto_val += test_goto1 (1);
91 goto_val += test_goto2 (3);
92 goto_val += test_goto2 (30);
95 /* Check nested if-then-else statements. */
97 int ifelse_val1;
98 int ifelse_val2;
99 int ifelse_val3;
102 test_ifelse1 (int i, int j)
104 int result = 0;
105 if (i) /* branch(40) */
106 /* branch(end) */
107 if (j) /* branch(0) */
108 /* branch(end) */
109 result = do_something (4);
110 else
111 result = do_something (1024);
112 else
113 if (j) /* branch(50) */
114 /* branch(end) */
115 result = do_something (1);
116 else
117 result = do_something (2);
118 if (i > j) /* branch(80) */
119 /* branch(end) */
120 result = do_something (result*2);
121 if (i > 10) /* branch(80) */
122 /* branch(end) */
123 if (j > 10) /* branch(100) */
124 result = do_something (result*4);
125 return result;
129 test_ifelse2 (int i)
131 int result = 0;
132 if (!i) /* branch(83) */
133 /* branch(end) */
134 result = do_something (1);
135 if (i == 1) /* branch(100) */
136 /* branch(end) */
137 result = do_something (1024);
138 if (i == 2) /* branch(50) */
139 /* branch(end) */
140 result = do_something (2);
141 if (i == 3) /* branch(67) */
142 /* branch(end) */
143 return do_something (8);
144 if (i == 4) /* branch(100) */
145 /* branch(end) */
146 return do_something (2048);
147 return result;
151 test_ifelse3 (int i, int j)
153 int result = 1;
154 if (i > 10 && j > i && j < 20) /* branch(27 50 75) */
155 /* branch(end) */
156 result = do_something (16);
157 if (i > 20) /* branch(55) */
158 /* branch(end) */
159 if (j > i) /* branch(60) */
160 /* branch(end) */
161 if (j < 30) /* branch(50) */
162 /* branch(end) */
163 result = do_something (32);
164 if (i == 3 || j == 47 || i == j) /* branch(9 10 89) */
165 /* branch(end) */
166 result = do_something (64);
167 return result;
170 void
171 call_ifelse ()
173 ifelse_val1 += test_ifelse1 (0, 2);
174 ifelse_val1 += test_ifelse1 (0, 0);
175 ifelse_val1 += test_ifelse1 (1, 2);
176 ifelse_val1 += test_ifelse1 (10, 2);
177 ifelse_val1 += test_ifelse1 (11, 11);
179 ifelse_val2 += test_ifelse2 (0);
180 ifelse_val2 += test_ifelse2 (2);
181 ifelse_val2 += test_ifelse2 (2);
182 ifelse_val2 += test_ifelse2 (2);
183 ifelse_val2 += test_ifelse2 (3);
184 ifelse_val2 += test_ifelse2 (3);
186 ifelse_val3 += test_ifelse3 (11, 19);
187 ifelse_val3 += test_ifelse3 (25, 27);
188 ifelse_val3 += test_ifelse3 (11, 22);
189 ifelse_val3 += test_ifelse3 (11, 10);
190 ifelse_val3 += test_ifelse3 (21, 32);
191 ifelse_val3 += test_ifelse3 (21, 20);
192 ifelse_val3 += test_ifelse3 (1, 2);
193 ifelse_val3 += test_ifelse3 (32, 31);
194 ifelse_val3 += test_ifelse3 (3, 0);
195 ifelse_val3 += test_ifelse3 (0, 47);
196 ifelse_val3 += test_ifelse3 (65, 65);
199 /* Check switch statements. */
201 int switch_val, switch_m;
204 test_switch (int i, int j)
206 int result = 0;
208 switch (i) /* branch(80 25) */
209 /* branch(end) */
211 case 1:
212 result = do_something (2);
213 break;
214 case 2:
215 result = do_something (1024);
216 break;
217 case 3:
218 case 4:
219 if (j == 2) /* branch(67) */
220 /* branch(end) */
221 return do_something (4);
222 result = do_something (8);
223 break;
224 default:
225 result = do_something (32);
226 switch_m++;
227 break;
229 return result;
232 void
233 call_switch ()
235 switch_val += test_switch (1, 0);
236 switch_val += test_switch (3, 0);
237 switch_val += test_switch (3, 2);
238 switch_val += test_switch (4, 0);
239 switch_val += test_switch (16, 0);
240 switch_val += switch_m;
244 main()
246 call_for ();
247 call_goto ();
248 call_ifelse ();
249 call_switch ();
250 if ((for_val1 != 12)
251 || (for_val2 != 87)
252 || (goto_val != 15)
253 || (ifelse_val1 != 31)
254 || (ifelse_val2 != 23)
255 || (ifelse_val3 != 246)
256 || (switch_val != 55))
257 abort ();
258 return 0;
261 /* { dg-final { run-gcov branches { -b gcov-4b.c } } } */