Cleanup switch.c
+// 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 nessesary.
+//
+// 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>
-int main()
+void match_a_single_case()
{
- int num = 1;
-
- // Match the single case.
- switch (num)
+ switch (1)
{
case 0:
printf("a 0\n");
@@ -20,9 +29,11 @@ int main()
printf("a default\n");
break;
}
+}
- // Fallthrough to next case.
- switch (num)
+void fallthrough_to_next_case()
+{
+ switch (1)
{
case 0:
printf("b 0\n");
@@ -37,3 +48,58 @@ int main()
break;
}
}
+
+void match_no_cases()
+{
+ switch (1)
+ {
+ case 5:
+ printf("c 5\n");
+ break;
+ case 2:
+ printf("c 2\n");
+ break;
+ }
+}
+
+void match_default()
+{
+ switch (1)
+ {
+ case 5:
+ printf("d 5\n");
+ break;
+ case 2:
+ printf("d 2\n");
+ break;
+ default:
+ printf("d default\n");
+ break;
+ }
+}
+
+void fallthrough_several_cases_including_default()
+{
+ switch (1)
+ {
+ case 0:
+ printf("e 0\n");
+ case 1:
+ printf("e 1\n");
+ case 2:
+ printf("e 2\n");
+ default:
+ printf("e default\n");
+ }
+}
+
+int main()
+{
+ // For each of the tests below there will be an identical case that uses
+ // scopes for the case statements.
+ match_a_single_case();
+ fallthrough_to_next_case();
+ match_no_cases();
+ match_default();
+ fallthrough_several_cases_including_default();
+}
