|
#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();
|
|
}
|
|
|
|
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_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;
|
|
}
|