MISRA Discussion Forums
Combining case and default? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.15 Switch Statements (https://forum.misra.org.uk/forumdisplay.php?fid=45)
+---- Thread: Combining case and default? (/showthread.php?tid=675)



Combining case and default? - exoson - 07-08-2009

I would be interested in comments on the following simplified fragment.

switch (err) {
case 0:
...
break;

case 1:
case 2:
case 3:
default:
...
break;
}

I am seeing code examples like this where multiple cases are being combined with default. Most compiler will simply optimize this out to a simple test for case 0. The programmers are using this as a way to identify the results they might expect. I think having the other cases in a comment would serve this as well. MISRA C does not seem to mention this.
From a test perspective I feel it is necessary to test all declared cases.
Can anyone offer a comment on this?

Scott
Validated Software


Re: Combining case and default? - misra-c - 09-09-2009

The code fragment does not violate any MISRA C rules relating to the switch statement.

The number of tests cases suggested by this code fragment will depend on the project testing strategy. This is not something that the MISRA C Guidelines addresses.