// examples/test_switch translated by mycpp // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, "spam"); GLOBAL_STR(str1, "== %s =="); GLOBAL_STR(str2, "SPAM"); GLOBAL_STR(str3, "yes"); GLOBAL_STR(str4, "foo"); GLOBAL_STR(str5, "== %s =="); GLOBAL_STR(str6, "FOO"); GLOBAL_STR(str7, "bar"); GLOBAL_STR(str8, "== %s =="); GLOBAL_STR(str9, "BAR"); GLOBAL_STR(str10, "== %s =="); GLOBAL_STR(str11, "neither"); GLOBAL_STR(str12, "--"); GLOBAL_STR(str13, ""); GLOBAL_STR(str14, "zero"); GLOBAL_STR(str15, "zero"); GLOBAL_STR(str16, "one or two"); GLOBAL_STR(str17, "three or four"); GLOBAL_STR(str18, "default"); GLOBAL_STR(str19, "another"); GLOBAL_STR(str20, "spam"); GLOBAL_STR(str21, "bar"); GLOBAL_STR(str22, "zzz"); GLOBAL_STR(str23, "different len"); namespace test_switch { // forward declare } // forward declare namespace test_switch namespace test_switch { // declare void TestString(BigStr* s); void TestNumSwitch(); void run_tests(); void run_benchmarks(); } // declare namespace test_switch namespace test_switch { // define void TestString(BigStr* s) { StackRoot _root0(&s); switch (len(s)) { case 3: { if (str_equals_c(s, "foo", 3)) { print(StrFormat("== %s ==", s)); print(str6); } else if (str_equals_c(s, "bar", 3)) { print(StrFormat("== %s ==", s)); print(str9); } else { goto str_switch_default; } } break; case 4: { if (str_equals_c(s, "spam", 4)) { print(StrFormat("== %s ==", s)); print(str2); print(str3); } else { goto str_switch_default; } } break; str_switch_default: default: { print(StrFormat("== %s ==", s)); print(str11); } } print(str12); print(str13); } void TestNumSwitch() { int x; x = 5; switch (x) { case 0: { print(str14); print(str15); } break; case 1: case 2: { print(str16); } break; case 3: case 4: { print(str17); } break; default: { print(str18); print(str19); } } } void run_tests() { TestString(str20); TestString(str21); TestString(str22); TestString(str23); TestNumSwitch(); } void run_benchmarks() { FAIL(kNotImplemented); // Python NotImplementedError } } // define namespace test_switch int main(int argc, char **argv) { gHeap.Init(); char* b = getenv("BENCHMARK"); if (b && strlen(b)) { // match Python's logic fprintf(stderr, "Benchmarking...\n"); test_switch::run_benchmarks(); } else { test_switch::run_tests(); } gHeap.CleanProcessExit(); }