#include "test_utils.h"
UnittestTestResult_t* test_simple_switch(void) {
UNITTEST_BEGIN_TEST("TestSwitch", "test_simple_switch");
RAVA_TEST_RUN(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 2;\n"
" int result = 0;\n"
" switch (x) {\n"
" case 1:\n"
" result = 10;\n"
" break;\n"
" case 2:\n"
" result = 20;\n"
" break;\n"
" case 3:\n"
" result = 30;\n"
" break;\n"
" }\n"
" return result;\n"
" }\n"
"}\n",
"Test", "main", 20, "switch x=2 should return 20");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_switch_with_default(void) {
UNITTEST_BEGIN_TEST("TestSwitch", "test_switch_with_default");
RAVA_TEST_RUN(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 99;\n"
" int result = 0;\n"
" switch (x) {\n"
" case 1:\n"
" result = 10;\n"
" break;\n"
" case 2:\n"
" result = 20;\n"
" break;\n"
" default:\n"
" result = 100;\n"
" break;\n"
" }\n"
" return result;\n"
" }\n"
"}\n",
"Test", "main", 100, "switch x=99 should hit default returning 100");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_switch_fallthrough(void) {
UNITTEST_BEGIN_TEST("TestSwitch", "test_switch_fallthrough");
RAVA_TEST_RUN(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 1;\n"
" int result = 0;\n"
" switch (x) {\n"
" case 1:\n"
" result = result + 10;\n"
" case 2:\n"
" result = result + 20;\n"
" break;\n"
" case 3:\n"
" result = result + 30;\n"
" break;\n"
" }\n"
" return result;\n"
" }\n"
"}\n",
"Test", "main", 30, "fallthrough from case 1 to case 2 should add 10+20=30");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_switch_first_case(void) {
UNITTEST_BEGIN_TEST("TestSwitch", "test_switch_first_case");
RAVA_TEST_RUN(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 1;\n"
" int result = 0;\n"
" switch (x) {\n"
" case 1:\n"
" result = 111;\n"
" break;\n"
" case 2:\n"
" result = 222;\n"
" break;\n"
" }\n"
" return result;\n"
" }\n"
"}\n",
"Test", "main", 111, "switch x=1 should return 111");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_switch_no_match_no_default(void) {
UNITTEST_BEGIN_TEST("TestSwitch", "test_switch_no_match_no_default");
RAVA_TEST_RUN(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 99;\n"
" int result = 42;\n"
" switch (x) {\n"
" case 1:\n"
" result = 10;\n"
" break;\n"
" case 2:\n"
" result = 20;\n"
" break;\n"
" }\n"
" return result;\n"
" }\n"
"}\n",
"Test", "main", 42, "no match, no default should keep original 42");
UNITTEST_END_TEST();
}
int main(int argc, char **argv) {
UnittestConfig_t *config = unittest_config_create();
config->verbosity = 2;
if (argc > 1 && strcmp(argv[1], "--json") == 0) {
config->output_format = UNITTEST_FORMAT_JSON;
config->use_colors = false;
}
UnittestTestSuite_t *suite = unittest_test_suite_create("Switch/Case Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestSwitch");
unittest_test_case_add_result(tc, test_simple_switch());
unittest_test_case_add_result(tc, test_switch_with_default());
unittest_test_case_add_result(tc, test_switch_fallthrough());
unittest_test_case_add_result(tc, test_switch_first_case());
unittest_test_case_add_result(tc, test_switch_no_match_no_default());
unittest_test_suite_add_test_case(suite, tc);
unittest_generate_report(suite, config);
int failures = suite->total_failed + suite->total_errors;
unittest_test_suite_destroy(suite);
unittest_config_destroy(config);
return failures > 0 ? 1 : 0;
}