114 lines
3.4 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_sum_array_elements(void) {
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_sum_array_elements");
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[] arr = new int[3];\n"
" arr[0] = 1;\n"
" arr[1] = 2;\n"
" arr[2] = 3;\n"
" int sum = 0;\n"
" for (int x : arr) {\n"
" sum = sum + x;\n"
" }\n"
" return sum;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 6, "sum array elements should return 6");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_count_elements(void) {
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_count_elements");
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[] arr = new int[5];\n"
" int count = 0;\n"
" for (int x : arr) {\n"
" count = count + 1;\n"
" }\n"
" return count;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 5, "count elements should return 5");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_find_max(void) {
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_find_max");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int[] arr = new int[4];\n"
" arr[0] = 5;\n"
" arr[1] = 9;\n"
" arr[2] = 3;\n"
" arr[3] = 7;\n"
" int max = 0;\n"
" for (int x : arr) {\n"
" if (x > max) {\n"
" max = x;\n"
" }\n"
" }\n"
" return max;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 9, "find max should return 9");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_empty_array(void) {
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_empty_array");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int[] arr = new int[0];\n"
" int sum = 0;\n"
" for (int x : arr) {\n"
" sum = sum + 1;\n"
" }\n"
" return sum;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 0, "empty array should return 0");
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("Enhanced For Loop Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestEnhancedFor");
unittest_test_case_add_result(tc, test_sum_array_elements());
unittest_test_case_add_result(tc, test_count_elements());
unittest_test_case_add_result(tc, test_find_max());
unittest_test_case_add_result(tc, test_empty_array());
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
}