Merge to HEAD at tree-cleanup-merge-20041024 .
[official-gcc.git] / gcc / testsuite / gcc.misc-tests / gcov-4b.c
blob7653c5897ad7e18ad5b31a43277a5d4232d4f13d
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 for loops. */
16 int for_val1;
17 int for_val2;
18 int for_temp;
20 int
21 test_for1 (int n)
23 int i;
24 for_temp = 1;
25 for (i = 0; i < n; i++) /* branch(25) */
26 /* branch(end) */
27 for_temp++;
28 return for_temp;
31 int
32 test_for2 (int m, int n, int o)
34 int i, j, k;
35 for_temp = 1;
36 for (i = 0; i < n; i++) /* branch(30) */
37 /* branch(end) */
38 for (j = 0; j < m; j++) /* branch(32) */
39 /* branch(end) */
40 for (k = 0; k < o; k++) /* branch(27) */
41 /* branch(end) */
42 for_temp++;
43 return for_temp;
46 void
47 call_for ()
49 for_val1 += test_for1 (0);
50 for_val1 += test_for1 (2);
51 for_val1 += test_for1 (7);
53 for_val2 += test_for2 (0, 0, 0);
54 for_val2 += test_for2 (1, 0, 0);
55 for_val2 += test_for2 (1, 3, 0);
56 for_val2 += test_for2 (1, 3, 1);
57 for_val2 += test_for2 (3, 1, 5);
58 for_val2 += test_for2 (3, 7, 3);
61 /* Check the use of goto. */
63 int goto_val;
65 int
66 test_goto1 (int f)
68 if (f) /* branch(50) */
69 /* branch(end) */
70 goto lab1;
71 return 1;
72 lab1:
73 return 2;
76 int
77 test_goto2 (int f)
79 int i;
80 for (i = 0; i < 10; i++) /* branch(7) */
81 /* branch(end) */
82 if (i == f) goto lab2;
83 return 4;
84 lab2:
85 return 8;
88 void
89 call_goto ()
91 goto_val += test_goto1 (0);
92 goto_val += test_goto1 (1);
93 goto_val += test_goto2 (3);
94 goto_val += test_goto2 (30);
97 /* Check nested if-then-else statements. */
99 int ifelse_val1;
100 int ifelse_val2;
101 int ifelse_val3;
104 test_ifelse1 (int i, int j)
106 int result = 0;
107 if (i) /* branch(40) */
108 /* branch(end) */
109 if (j) /* branch(0) */
110 /* branch(end) */
111 result = do_something (4);
112 else
113 result = do_something (1024);
114 else
115 if (j) /* branch(50) */
116 /* branch(end) */
117 result = do_something (1);
118 else
119 result = do_something (2);
120 if (i > j) /* branch(80) */
121 /* branch(end) */
122 result = do_something (result*2);
123 if (i > 10) /* branch(80) */
124 /* branch(end) */
125 if (j > 10) /* branch(100) */
126 result = do_something (result*4);
127 return result;
131 test_ifelse2 (int i)
133 int result = 0;
134 if (!i) /* branch(83) */
135 /* branch(end) */
136 result = do_something (1);
137 if (i == 1) /* branch(100) */
138 /* branch(end) */
139 result = do_something (1024);
140 if (i == 2) /* branch(50) */
141 /* branch(end) */
142 result = do_something (2);
143 if (i == 3) /* branch(67) */
144 /* branch(end) */
145 return do_something (8);
146 if (i == 4) /* branch(100) */
147 /* branch(end) */
148 return do_something (2048);
149 return result;
153 test_ifelse3 (int i, int j)
155 int result = 1;
156 if (i > 10 && j > i && j < 20) /* branch(27 50 75) */
157 /* branch(end) */
158 result = do_something (16);
159 if (i > 20) /* branch(55) */
160 /* branch(end) */
161 if (j > i) /* branch(60) */
162 /* branch(end) */
163 if (j < 30) /* branch(50) */
164 /* branch(end) */
165 result = do_something (32);
166 if (i == 3 || j == 47 || i == j) /* branch(9 10 89) */
167 /* branch(end) */
168 result = do_something (64);
169 return result;
172 void
173 call_ifelse ()
175 ifelse_val1 += test_ifelse1 (0, 2);
176 ifelse_val1 += test_ifelse1 (0, 0);
177 ifelse_val1 += test_ifelse1 (1, 2);
178 ifelse_val1 += test_ifelse1 (10, 2);
179 ifelse_val1 += test_ifelse1 (11, 11);
181 ifelse_val2 += test_ifelse2 (0);
182 ifelse_val2 += test_ifelse2 (2);
183 ifelse_val2 += test_ifelse2 (2);
184 ifelse_val2 += test_ifelse2 (2);
185 ifelse_val2 += test_ifelse2 (3);
186 ifelse_val2 += test_ifelse2 (3);
188 ifelse_val3 += test_ifelse3 (11, 19);
189 ifelse_val3 += test_ifelse3 (25, 27);
190 ifelse_val3 += test_ifelse3 (11, 22);
191 ifelse_val3 += test_ifelse3 (11, 10);
192 ifelse_val3 += test_ifelse3 (21, 32);
193 ifelse_val3 += test_ifelse3 (21, 20);
194 ifelse_val3 += test_ifelse3 (1, 2);
195 ifelse_val3 += test_ifelse3 (32, 31);
196 ifelse_val3 += test_ifelse3 (3, 0);
197 ifelse_val3 += test_ifelse3 (0, 47);
198 ifelse_val3 += test_ifelse3 (65, 65);
201 /* Check switch statements. */
203 int switch_val, switch_m;
206 test_switch (int i, int j)
208 int result = 0;
210 switch (i) /* branch(80 25) */
211 /* branch(end) */
213 case 1:
214 result = do_something (2);
215 break;
216 case 2:
217 result = do_something (1024);
218 break;
219 case 3:
220 case 4:
221 if (j == 2) /* branch(67) */
222 /* branch(end) */
223 return do_something (4);
224 result = do_something (8);
225 break;
226 default:
227 result = do_something (32);
228 switch_m++;
229 break;
231 return result;
234 void
235 call_switch ()
237 switch_val += test_switch (1, 0);
238 switch_val += test_switch (3, 0);
239 switch_val += test_switch (3, 2);
240 switch_val += test_switch (4, 0);
241 switch_val += test_switch (16, 0);
242 switch_val += switch_m;
246 main()
248 call_for ();
249 call_goto ();
250 call_ifelse ();
251 call_switch ();
252 if ((for_val1 != 12)
253 || (for_val2 != 87)
254 || (goto_val != 15)
255 || (ifelse_val1 != 31)
256 || (ifelse_val2 != 23)
257 || (ifelse_val3 != 246)
258 || (switch_val != 55))
259 abort ();
260 return 0;
263 /* { dg-final { run-gcov branches { -b gcov-4b.c } } } */