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_arraylist_create_size(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_create_size");
|
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"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" return list.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 0, "new ArrayList().size() should return 0");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_arraylist_add_size(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_add_size");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(10);\n"
|
|
|
|
|
" list.add(20);\n"
|
|
|
|
|
" list.add(30);\n"
|
|
|
|
|
" return list.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 3, "ArrayList with 3 elements should have size 3");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_arraylist_add_get(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_add_get");
|
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"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(42);\n"
|
|
|
|
|
" return list.get(0);\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 42, "list.get(0) should return 42");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_arraylist_set(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_set");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(10);\n"
|
|
|
|
|
" list.set(0, 99);\n"
|
|
|
|
|
" return list.get(0);\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 99, "list.set(0, 99) then get(0) should return 99");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_arraylist_isEmpty_true(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_isEmpty_true");
|
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"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" if (list.isEmpty()) {\n"
|
|
|
|
|
" return 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return 0;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 1, "new ArrayList().isEmpty() should return true");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_arraylist_isEmpty_false(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_isEmpty_false");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(1);\n"
|
|
|
|
|
" if (list.isEmpty()) {\n"
|
|
|
|
|
" return 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return 0;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 0, "non-empty list.isEmpty() should return false");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_arraylist_clear(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_clear");
|
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"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(1);\n"
|
|
|
|
|
" list.add(2);\n"
|
|
|
|
|
" list.clear();\n"
|
|
|
|
|
" return list.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 0, "list.clear() should make size 0");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_arraylist_remove(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_remove");
|
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"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(10);\n"
|
|
|
|
|
" list.add(20);\n"
|
|
|
|
|
" list.add(30);\n"
|
|
|
|
|
" int removed = list.remove(1);\n"
|
|
|
|
|
" return removed;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 20, "list.remove(1) should return 20");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_arraylist_sum(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_arraylist_sum");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" ArrayList list = new ArrayList();\n"
|
|
|
|
|
" list.add(1);\n"
|
|
|
|
|
" list.add(2);\n"
|
|
|
|
|
" list.add(3);\n"
|
|
|
|
|
" list.add(4);\n"
|
|
|
|
|
" int sum = 0;\n"
|
|
|
|
|
" for (int i = 0; i < list.size(); i = i + 1) {\n"
|
|
|
|
|
" sum = sum + list.get(i);\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return sum;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 10, "sum of ArrayList elements should be 10");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_hashmap_create_size(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_create_size");
|
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"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" return map.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 0, "new HashMap().size() should return 0");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_hashmap_put_size(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_put_size");
|
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"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"a\", 1);\n"
|
|
|
|
|
" map.put(\"b\", 2);\n"
|
|
|
|
|
" return map.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 2, "HashMap with 2 keys should have size 2");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_hashmap_put_get(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_put_get");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"key\", 42);\n"
|
|
|
|
|
" return map.get(\"key\");\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 42, "map.get(\"key\") should return 42");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_hashmap_containsKey_true(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_containsKey_true");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"test\", 99);\n"
|
|
|
|
|
" if (map.containsKey(\"test\")) {\n"
|
|
|
|
|
" return 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return 0;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 1, "containsKey() for existing key should return true");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_hashmap_containsKey_false(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_containsKey_false");
|
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"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"test\", 99);\n"
|
|
|
|
|
" if (map.containsKey(\"other\")) {\n"
|
|
|
|
|
" return 1;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return 0;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 0, "containsKey() for missing key should return false");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_hashmap_remove(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_remove");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"a\", 10);\n"
|
|
|
|
|
" map.put(\"b\", 20);\n"
|
|
|
|
|
" int removed = map.remove(\"a\");\n"
|
|
|
|
|
" return removed + map.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 11, "remove + size should return 11");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_hashmap_clear(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_clear");
|
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"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"x\", 1);\n"
|
|
|
|
|
" map.put(\"y\", 2);\n"
|
|
|
|
|
" map.clear();\n"
|
|
|
|
|
" return map.size();\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 0, "map.clear() should make size 0");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_hashmap_update(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestCollections", "test_hashmap_update");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" HashMap map = new HashMap();\n"
|
|
|
|
|
" map.put(\"key\", 10);\n"
|
|
|
|
|
" map.put(\"key\", 20);\n"
|
|
|
|
|
" return map.get(\"key\");\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 20, "updating key should return new value 20");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestSuite_t *suite = unittest_test_suite_create("Collections Tests");
|
|
|
|
|
|
|
|
|
|
UnittestTestCase_t *tc_arraylist = unittest_test_case_create("TestArrayList");
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_create_size());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_add_size());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_add_get());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_set());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_isEmpty_true());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_isEmpty_false());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_clear());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_remove());
|
|
|
|
|
unittest_test_case_add_result(tc_arraylist, test_arraylist_sum());
|
|
|
|
|
unittest_test_suite_add_test_case(suite, tc_arraylist);
|
|
|
|
|
|
|
|
|
|
UnittestTestCase_t *tc_hashmap = unittest_test_case_create("TestHashMap");
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_create_size());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_put_size());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_put_get());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_containsKey_true());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_containsKey_false());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_remove());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_clear());
|
|
|
|
|
unittest_test_case_add_result(tc_hashmap, test_hashmap_update());
|
|
|
|
|
unittest_test_suite_add_test_case(suite, tc_hashmap);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|