2025-12-04 22:46:38 +01:00
|
|
|
#include "test_utils.h"
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_break_continue_example(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestBreakContinue", "test_break_continue_example");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_FILE_EXECUTES(_unittest_result,
|
|
|
|
|
"examples/19_BreakContinue.java",
|
|
|
|
|
"BreakContinue", "main",
|
|
|
|
|
"break/continue example should execute successfully");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_break_in_for(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestBreakContinue", "test_break_in_for");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int sum = 0;\n"
|
|
|
|
|
" for (int i = 0; i < 10; i++) {\n"
|
|
|
|
|
" if (i == 5) {\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" sum = sum + i;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return sum;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 10, "break should exit for loop (0+1+2+3+4=10)");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_break_in_while(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestBreakContinue", "test_break_in_while");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int i = 0;\n"
|
|
|
|
|
" int sum = 0;\n"
|
|
|
|
|
" while (i < 100) {\n"
|
|
|
|
|
" if (i >= 5) {\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" sum = sum + i;\n"
|
|
|
|
|
" i = i + 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return sum;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 10, "break should exit while loop");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_continue_in_for(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestBreakContinue", "test_continue_in_for");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int sum = 0;\n"
|
|
|
|
|
" for (int i = 0; i < 10; i++) {\n"
|
|
|
|
|
" if (i % 2 == 0) {\n"
|
|
|
|
|
" continue;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" sum = sum + i;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return sum;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 25, "continue should skip even numbers (1+3+5+7+9=25)");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
2025-12-04 16:55:18 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 22:46:38 +01:00
|
|
|
UnittestTestResult_t* test_continue_in_while(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestBreakContinue", "test_continue_in_while");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int i = 0;\n"
|
|
|
|
|
" int sum = 0;\n"
|
|
|
|
|
" while (i < 10) {\n"
|
|
|
|
|
" i = i + 1;\n"
|
|
|
|
|
" if (i % 2 == 0) {\n"
|
|
|
|
|
" continue;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" sum = sum + i;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return sum;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 25, "continue in while should skip even iterations");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_nested_break(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestBreakContinue", "test_nested_break");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int count = 0;\n"
|
|
|
|
|
" for (int i = 0; i < 5; i++) {\n"
|
|
|
|
|
" for (int j = 0; j < 5; j++) {\n"
|
|
|
|
|
" if (j == 2) {\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" count = count + 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return count;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 10, "nested break exits inner loop only (5*2=10)");
|
|
|
|
|
|
|
|
|
|
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("Break/Continue Tests");
|
|
|
|
|
|
|
|
|
|
UnittestTestCase_t *tc = unittest_test_case_create("TestBreakContinue");
|
|
|
|
|
unittest_test_case_add_result(tc, test_break_continue_example());
|
|
|
|
|
unittest_test_case_add_result(tc, test_break_in_for());
|
|
|
|
|
unittest_test_case_add_result(tc, test_break_in_while());
|
|
|
|
|
unittest_test_case_add_result(tc, test_continue_in_for());
|
|
|
|
|
unittest_test_case_add_result(tc, test_continue_in_while());
|
|
|
|
|
unittest_test_case_add_result(tc, test_nested_break());
|
|
|
|
|
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;
|
2025-12-04 16:55:18 +01:00
|
|
|
}
|