|
#include "test_utils.h"
|
|
|
|
UnittestTestResult_t* test_sum_array_elements(void) {
|
|
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_sum_array_elements");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"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"
|
|
"}\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");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"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"
|
|
"}\n",
|
|
"Test", "main", 5, "count elements should return 5");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_find_max(void) {
|
|
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_find_max");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"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"
|
|
"}\n",
|
|
"Test", "main", 9, "find max should return 9");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_empty_array(void) {
|
|
UNITTEST_BEGIN_TEST("TestEnhancedFor", "test_empty_array");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"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"
|
|
"}\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);
|
|
|
|
int failures = suite->total_failed + suite->total_errors;
|
|
unittest_test_suite_destroy(suite);
|
|
unittest_config_destroy(config);
|
|
|
|
return failures > 0 ? 1 : 0;
|
|
}
|