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_2d_array_outer_length(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_outer_length");
|
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[][] a = new int[4][5];\n"
|
|
|
|
|
" return a.length;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 4, "2d array outer length should return 4");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_2d_array_inner_length(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_inner_length");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int[][] a = new int[4][5];\n"
|
|
|
|
|
" return a[0].length;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 5, "2d array inner length 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_2d_array_basic_store_load(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_basic_store_load");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int[][] a = new int[2][3];\n"
|
|
|
|
|
" a[0][1] = 15;\n"
|
|
|
|
|
" return a[0][1];\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 15, "2d array basic store and load should return 15");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_2d_array_multiple_cells(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_multiple_cells");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int[][] a = new int[2][3];\n"
|
|
|
|
|
" a[0][0] = 1;\n"
|
|
|
|
|
" a[0][1] = 2;\n"
|
|
|
|
|
" a[1][0] = 3;\n"
|
|
|
|
|
" a[1][1] = 4;\n"
|
|
|
|
|
" return a[0][0] + a[0][1] + a[1][0] + a[1][1];\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 10, "2d array multiple cells sum should return 10");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_2d_array_row_iteration(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_row_iteration");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int[][] a = new int[3][2];\n"
|
|
|
|
|
" a[0][0] = 1; a[0][1] = 2;\n"
|
|
|
|
|
" a[1][0] = 3; a[1][1] = 4;\n"
|
|
|
|
|
" a[2][0] = 5; a[2][1] = 6;\n"
|
|
|
|
|
" int sum = 0;\n"
|
|
|
|
|
" for (int i = 0; i < a.length; i++) {\n"
|
|
|
|
|
" sum = sum + a[i][0] + a[i][1];\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return sum;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 21, "2d array row iteration sum should return 21");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_2d_array_nested_loop(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_nested_loop");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int[][] a = new int[2][3];\n"
|
|
|
|
|
" int v = 1;\n"
|
|
|
|
|
" for (int i = 0; i < 2; i++) {\n"
|
|
|
|
|
" for (int j = 0; j < 3; j++) {\n"
|
|
|
|
|
" a[i][j] = v;\n"
|
|
|
|
|
" v = v + 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return a[0][0] + a[0][2] + a[1][1];\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 9, "2d array nested loop sum should return 9");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_2d_array_different_inner_lengths(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestMultidimArrays", "test_2d_array_different_inner_lengths");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int[][] a = new int[3][4];\n"
|
|
|
|
|
" int[][] b = new int[2][6];\n"
|
|
|
|
|
" return a[0].length + b[0].length;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 10, "2d array different inner lengths sum should return 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("Multi-dimensional Array Tests");
|
|
|
|
|
|
|
|
|
|
UnittestTestCase_t *tc = unittest_test_case_create("TestMultidimArrays");
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_outer_length());
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_inner_length());
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_basic_store_load());
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_multiple_cells());
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_row_iteration());
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_nested_loop());
|
|
|
|
|
unittest_test_case_add_result(tc, test_2d_array_different_inner_lengths());
|
|
|
|
|
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
|
|
|
}
|