1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
// This file tests the various forms of switch statement.
//
// We must be extra sensitive to the fact that switch fallthrough is handled
// differently in C and Go. Break statements are removed and fallthrough
// statements are added when necessary.
//
// It is worth mentioning that a SwitchStmt has a CompoundStmt item that
// contains all of the cases. However, if the individual case are not enclosed
// in a scope each of the case statements and their childen are part of the same
// CompoundStmt. For example, the first switch statement below contains a
// CompoundStmt with 12 children.
#include <stdio.h>
#include <stdbool.h>
#include "tests.h"
void match_a_single_case()
{
switch (1)
{
case 0:
fail("code should not reach here");
break;
case 1:
pass(__func__);
break;
case 2:
fail("code should not reach here");
break;
default:
fail("code should not reach here");
break;
}
}
void fallthrough_to_next_case()
{
switch (1)
{
case 0:
fail("code should not reach here");
break;
case 1:
pass(__func__);
case 2:
pass(__func__);
break;
default:
fail("code should not reach here");
break;
}
}
void match_no_cases()
{
switch (1)
{
case 5:
fail("code should not reach here");
break;
case 2:
fail("code should not reach here");
break;
}
}
void match_default()
{
switch (1)
{
case 5:
fail("code should not reach here");
break;
case 2:
fail("code should not reach here");
break;
default:
pass(__func__);
break;
}
}
void fallthrough_several_cases_including_default()
{
switch (1)
{
case 0:
fail("code should not reach here");
case 1:
pass(__func__);
case 2:
pass(__func__);
default:
pass(__func__);
}
}
void fallthrough_several_midway_default()
{
for(int i=0; i<=3; i++) {
int j = -1;
int expected = -1;
if(i==0)
expected = 10;
if(i==1)
expected = 21;
if(i==2)
expected = 22;
if(i==3)
expected = 13;
if(i==4)
expected = 666;
switch(i) {
case 4:
fail("code should not reach here");
case 0:
default:
j = i+10;
break;
case 1:
case 2:
j = i+20;
break;
}
is_eq(j, expected);
}
}
void goto_label(bool use_goto)
{
for (;;) {
switch (0)
{
case 3:
continue;
case 0:
if (use_goto) {
for (;;)
break;
goto LABEL;
fail("code should not reach here");
} else if (false) {
goto LABELX;
goto LABELY;
fail("code should not reach here");
}
/* other comment */
// some comment
/* fallthrough */
LABELY:
case 4:
LABEL:
printf("x");
case 1:
pass(__func__);
break;
case 2:
;
LABELX:
default:
fail("code should not reach here");
break;
}
break;
}
}
void scoped_match_a_single_case()
{
switch (1)
{
case 0:
{
fail("code should not reach here");
break;
}
case 1:
{
pass(__func__);
break;
}
case 2:
{
fail("code should not reach here");
break;
}
default:
{
fail("code should not reach here");
break;
}
}
}
void scoped_fallthrough_to_next_case()
{
switch (1)
{
case 0:
{
fail("code should not reach here");
break;
}
case 1:
{
pass(__func__);
}
case 2:
{
pass(__func__);
break;
}
default:
{
fail("code should not reach here");
break;
}
}
}
void scoped_match_no_cases()
{
switch (1)
{
case 5:
{
fail("code should not reach here");
break;
}
case 2:
{
fail("code should not reach here");
break;
}
}
}
void scoped_match_default()
{
switch (1)
{
case 5:
{
fail("code should not reach here");
break;
}
case 2:
{
fail("code should not reach here");
break;
}
default:
{
pass(__func__);
break;
}
}
}
void scoped_fallthrough_several_cases_including_default()
{
switch (1)
{
case 0:
{
fail("code should not reach here");
}
case 1:
{
pass(__func__);
}
case 2:
{
pass(__func__);
}
default:
{
pass(__func__);
}
}
}
void scoped_fallthrough_several_midway_default()
{
for(int i=0; i<=3; i++) {
int j = -1;
int expected = -1;
if(i==0)
expected = 10;
if(i==1)
expected = 21;
if(i==2)
expected = 22;
if(i==3)
expected = 13;
if(i==4)
expected = 666;
switch(i) {
case 4:
{
fail("code should not reach here");
}
case 0:
{}
default:
{
j = i+10;
break;
}
case 1:
{}
case 2:
{
j = i+20;
}
}
is_eq(j, expected);
}
}
void scoped_goto_label(bool use_goto)
{
for (;;) {
switch (0)
{
case 3:
{
continue;
}
case 0:
{
if (use_goto) {
for (;;) {
break;
}
goto LABEL;
fail("code should not reach here");
} else if (false) {
goto LABELX;
goto LABELY;
fail("code should not reach here");
}
/* other comment */
// some comment
/* fallthrough */
}
LABELY: {}
case 4: {}
LABEL:
{
printf("x");
}
case 1:
{
pass(__func__);
break;
}
case 2:
{
int x = 0;
printf("%d", x);
break;
}
LABELX: {}
default:
{
fail("code should not reach here");
break;
}
}
break;
}
}
typedef struct I67 I67;
struct I67{
int x,y;
};
void run( I67 * i ,int pos)
{
switch (pos) {
case 0:
(*i).x += 1;
(*i).y += 1;
break;
case 1:
(*i).x -= 1;
(*i).y -= 1;
break;
}
}
void run_with_block( I67 * i ,int pos)
{
switch (pos) {
case 0:
{
(*i).x += 1;
(*i).y += 1;
break;
}
case 1:
{
(*i).x -= 1;
(*i).y -= 1;
}
break;
case 2:
(*i).x *= 1;
(*i).y *= 1;
break;
default:
(*i).x *= 10;
(*i).y *= 10;
}
}
void switch_issue67()
{
I67 i;
i.x = 0;
i.y = 0;
run(&i, 0);
is_eq(i.x, 1);
is_eq(i.y, 1);
run(&i, 1);
is_eq(i.x, 0);
is_eq(i.y, 0);
run_with_block(&i,0);
is_eq(i.x, 1);
is_eq(i.y, 1);
run_with_block(&i, 1);
is_eq(i.x, 0);
is_eq(i.y, 0);
}
void empty_switch()
{
int pos = 0;
switch (pos){
}
is_eq(pos,0);
}
void default_only_switch()
{
int pos = 0;
switch (pos){
case -1: // empty case
case -1-4: // empty case
case (-1-4-4): // empty case
case (-3): // empty case
case -2: // empty case
default:
pos++;
}
is_eq(pos,1);
}
void switch_without_input()
{
int pos = 0;
switch (0){
default:
pos++;
}
is_eq(pos,1);
}
int main()
{
plan(37);
match_a_single_case();
fallthrough_to_next_case();
match_no_cases();
match_default();
fallthrough_several_cases_including_default();
fallthrough_several_midway_default();
goto_label(false);
goto_label(true);
// For each of the tests above there will be identical cases that use scopes
// for the case statements.
scoped_match_a_single_case();
scoped_fallthrough_to_next_case();
scoped_match_no_cases();
scoped_match_default();
scoped_fallthrough_several_cases_including_default();
scoped_fallthrough_several_midway_default();
scoped_goto_label(false);
scoped_goto_label(true);
switch_issue67();
empty_switch();
default_only_switch();
switch_without_input();
done_testing();
}
|