#include "test_utils.h" UnittestTestResult_t* test_gc_basic_allocation(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_basic_allocation"); const char *source = "public class Test {\n" " public static int main() {\n" " int[] arr = new int[100];\n" " for (int i = 0; i < 100; i++) {\n" " arr[i] = i;\n" " }\n" " return arr[99];\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 99, "Should allocate array without crash"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_object_allocation(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_object_allocation"); const char *source = "public class Point {\n" " public int x;\n" " public int y;\n" "}\n" "public class Test {\n" " public static int main() {\n" " Point p = new Point();\n" " p.x = 10;\n" " p.y = 20;\n" " return p.x + p.y;\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 30, "Should allocate object without crash"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_many_allocations(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_many_allocations"); const char *source = "public class Test {\n" " public static int main() {\n" " int sum = 0;\n" " for (int i = 0; i < 1000; i++) {\n" " int[] temp = new int[10];\n" " temp[0] = i;\n" " sum = sum + temp[0];\n" " }\n" " return sum;\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 499500, "Should handle many allocations"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_arraylist(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_arraylist"); const char *source = "public class Test {\n" " public static int main() {\n" " ArrayList list = new ArrayList();\n" " for (int i = 0; i < 100; i++) {\n" " list.add(i);\n" " }\n" " return list.size();\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 100, "Should handle ArrayList allocations"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_hashmap(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_hashmap"); const char *source = "public class Test {\n" " public static int main() {\n" " HashMap map = new HashMap();\n" " map.put(\"one\", 1);\n" " map.put(\"two\", 2);\n" " map.put(\"three\", 3);\n" " return map.size();\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 3, "Should handle HashMap allocations"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_collection_triggered(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_collection_triggered"); const char *source = "public class Test {\n" " public static int main() {\n" " int count = 0;\n" " for (int i = 0; i < 10000; i++) {\n" " int[] temp = new int[100];\n" " temp[0] = i;\n" " count = count + 1;\n" " }\n" " return count;\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 10000, "Should handle many temporary allocations via GC"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_circular_references(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_circular_references"); const char *source = "public class Test {\n" " public static int main() {\n" " int sum = 0;\n" " for (int i = 0; i < 100; i++) {\n" " int[] a = new int[10];\n" " int[] b = new int[10];\n" " a[0] = i;\n" " b[0] = i * 2;\n" " sum = sum + a[0] + b[0];\n" " }\n" " return sum;\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 14850, "Should handle many paired allocations"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_deep_object_graph(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_deep_object_graph"); const char *source = "public class Test {\n" " public static int main() {\n" " int[][] matrix = new int[50][50];\n" " int sum = 0;\n" " for (int i = 0; i < 50; i++) {\n" " for (int j = 0; j < 50; j++) {\n" " matrix[i][j] = i + j;\n" " sum = sum + matrix[i][j];\n" " }\n" " }\n" " return sum;\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 122500, "Should handle nested array allocations"); UNITTEST_END_TEST(); } UnittestTestResult_t* test_gc_mixed_types(void) { UNITTEST_BEGIN_TEST("TestGC", "test_gc_mixed_types"); const char *source = "public class Test {\n" " public static int main() {\n" " int[] data = new int[50];\n" " for (int i = 0; i < 50; i++) {\n" " data[i] = i * 2;\n" " }\n" " int sum = 0;\n" " for (int j = 0; j < 50; j++) {\n" " sum = sum + data[j];\n" " }\n" " return sum;\n" " }\n" "}\n"; RAVA_TEST_RUN(_unittest_result, source, "Test", "main", 2450, "Should handle array operations"); 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("GC Tests"); UnittestTestCase_t *tc = unittest_test_case_create("TestGC"); unittest_test_case_add_result(tc, test_gc_basic_allocation()); unittest_test_case_add_result(tc, test_gc_object_allocation()); unittest_test_case_add_result(tc, test_gc_many_allocations()); unittest_test_case_add_result(tc, test_gc_arraylist()); unittest_test_case_add_result(tc, test_gc_hashmap()); unittest_test_case_add_result(tc, test_gc_collection_triggered()); unittest_test_case_add_result(tc, test_gc_circular_references()); unittest_test_case_add_result(tc, test_gc_deep_object_graph()); unittest_test_case_add_result(tc, test_gc_mixed_types()); 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; }