1 /* Check that various C constructs are handled correctly by profile-directed 2 block ordering. 3 4 This test is the same as gcov-4.c. The "count" comments are left in to 5 make comparisons easier; they are ignored for this test. */ 6 7 int do_something (int i) 8 { 9 return i; 10 } 11 12 /* Check static inline functions. */ 13 14 int unref_val; 15 16 static inline int 17 unreferenced (int i, int j) 18 { 19 return i - j; 20 } 21 22 static inline int 23 uncalled (int i, int j) 24 { 25 return i * j; 26 } 27 28 static inline int 29 called (int i, int j) 30 { 31 return i + j; /* count(1) */ 32 } 33 34 void 35 call_unref () 36 { 37 if (unref_val) /* count(1) */ 38 unref_val = uncalled (1, 2); 39 unref_val = called (unref_val, 4); /* count(1) */ 40 } 41 42 43 /* Check for loops. */ 44 45 int for_val1; 46 int for_val2; 47 int for_temp; 48 49 int 50 test_for1 (int n) 51 { 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) */ 57 } 58 59 int 60 test_for2 (int m, int n, int o) 61 { 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) */ 69 } 70 71 void 72 call_for () 73 { 74 for_val1 += test_for1 (0); 75 for_val1 += test_for1 (2); 76 for_val1 += test_for1 (7); 77 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); 84 } 85 86 /* Check the use of goto. */ 87 88 int goto_val; 89 90 int 91 test_goto1 (int f) 92 { 93 if (f) /* count(2) */ 94 goto lab1; /* count(1) */ 95 return 1; /* count(1) */ 96 lab1: 97 return 2; /* count(1) */ 98 } 99 100 int 101 test_goto2 (int f) 102 { 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) */ 109 } 110 111 void 112 call_goto () 113 { 114 goto_val += test_goto1 (0); 115 goto_val += test_goto1 (1); 116 goto_val += test_goto2 (3); 117 goto_val += test_goto2 (30); 118 } 119 120 /* Check nested if-then-else statements. */ 121 122 int ifelse_val1; 123 int ifelse_val2; 124 int ifelse_val3; 125 126 int 127 test_ifelse1 (int i, int j) 128 { 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) */ 146 } 147 148 int 149 test_ifelse2 (int i) 150 { 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) */ 163 } 164 165 int 166 test_ifelse3 (int i, int j) 167 { 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) */ 178 } 179 180 void 181 call_ifelse () 182 { 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); 188 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); 195 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); 207 } 208 209 /* Check switch statements. */ 210 211 int switch_val, switch_m; 212 213 int 214 test_switch (int i, int j) 215 { 216 int result = 0; /* count(5) */ 217 218 switch (i) /* count(5) */ 219 { 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; 236 } 237 return result; /* count(4) */ 238 } 239 240 void 241 call_switch () 242 { 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; 249 } 250 251 int 252 main() 253 { 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; 269 } 270