// examples/test_switch // BEGIN mycpp output #include "gc_heap.h" using gc_heap::Alloc; using gc_heap::kZeroMask; using gc_heap::StackRoots; #include "my_runtime.h" #include "mylib2.h" using gc_heap::NewStr; using gc_heap::NewList; using gc_heap::NewDict; GLOBAL_STR(str0, "zero"); GLOBAL_STR(str1, "zero"); GLOBAL_STR(str2, "one or two"); GLOBAL_STR(str3, "three or four"); GLOBAL_STR(str4, "default"); GLOBAL_STR(str5, "another"); namespace test_switch { // forward declare } // forward declare namespace test_switch namespace test_switch { // declare void run_tests(); void run_benchmarks(); } // declare namespace test_switch namespace test_switch { // define void run_tests() { int x; x = 5; switch (x) { case 0: { print(str0); print(str1); } break; case 1: case 2: { print(str2); } break; case 3: case 4: { print(str3); } break; default: { print(str4); print(str5); } } } void run_benchmarks() { throw Alloc(); } } // define namespace test_switch int main(int argc, char **argv) { // gc_heap::gHeap.Init(512); gc_heap::gHeap.Init(128 << 10); // 128 KiB; doubling in size // gc_heap::gHeap.Init(400 << 20); // 400 MiB to avoid garbage collection if (getenv("BENCHMARK")) { fprintf(stderr, "Benchmarking...\n"); test_switch::run_benchmarks(); } else { test_switch::run_tests(); } }