131 lines
3.9 KiB
C
Raw Normal View History

2025-12-04 22:38:05 +01:00
#include "test_utils.h"
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_simple_dowhile(void) {
UNITTEST_BEGIN_TEST("TestDoWhile", "test_simple_dowhile");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int i = 0;\n"
" do {\n"
" i = i + 1;\n"
" } while (i < 5);\n"
" return i;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 5, "do-while loop should iterate until i=5");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_dowhile_sum(void) {
UNITTEST_BEGIN_TEST("TestDoWhile", "test_dowhile_sum");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int sum = 0;\n"
" int i = 1;\n"
" do {\n"
" sum = sum + i;\n"
" i = i + 1;\n"
" } while (i <= 10);\n"
" return sum;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 55, "sum of 1..10 should be 55");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_dowhile_executes_once(void) {
UNITTEST_BEGIN_TEST("TestDoWhile", "test_dowhile_executes_once");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int x = 0;\n"
" do {\n"
" x = 42;\n"
" } while (false);\n"
" return x;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 42, "do-while executes at least once");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_dowhile_with_break(void) {
UNITTEST_BEGIN_TEST("TestDoWhile", "test_dowhile_with_break");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int i = 0;\n"
" do {\n"
" i = i + 1;\n"
" if (i == 3) {\n"
" break;\n"
" }\n"
" } while (i < 10);\n"
" return i;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 3, "break at i=3 should exit loop");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_nested_dowhile(void) {
UNITTEST_BEGIN_TEST("TestDoWhile", "test_nested_dowhile");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int count = 0;\n"
" int i = 0;\n"
" do {\n"
" int j = 0;\n"
" do {\n"
" count = count + 1;\n"
" j = j + 1;\n"
" } while (j < 3);\n"
" i = i + 1;\n"
" } while (i < 2);\n"
" return count;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 6, "nested 2x3 iterations should count 6");
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("Do-While Loop Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestDoWhile");
unittest_test_case_add_result(tc, test_simple_dowhile());
unittest_test_case_add_result(tc, test_dowhile_sum());
unittest_test_case_add_result(tc, test_dowhile_executes_once());
unittest_test_case_add_result(tc, test_dowhile_with_break());
unittest_test_case_add_result(tc, test_nested_dowhile());
unittest_test_suite_add_test_case(suite, tc);
unittest_generate_report(suite, config);
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
int failures = suite->total_failed + suite->total_errors;
unittest_test_suite_destroy(suite);
unittest_config_destroy(config);
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
return failures > 0 ? 1 : 0;
2025-12-04 16:55:18 +01:00
}