|
#include "test_utils.h"
|
|
|
|
UnittestTestResult_t* test_simple_addition(void) {
|
|
UNITTEST_BEGIN_TEST("TestRuntime", "test_simple_addition");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"public class Test {\n"
|
|
" public static int add() {\n"
|
|
" int a = 10;\n"
|
|
" int b = 20;\n"
|
|
" int result = a + b;\n"
|
|
" return result;\n"
|
|
" }\n"
|
|
"}\n",
|
|
"Test", "add", 30, "10 + 20 should equal 30");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_multiple_operations(void) {
|
|
UNITTEST_BEGIN_TEST("TestRuntime", "test_multiple_operations");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"public class Test {\n"
|
|
" public static int compute() {\n"
|
|
" int a = 5;\n"
|
|
" int b = 3;\n"
|
|
" int sum = a + b;\n"
|
|
" int product = a * b;\n"
|
|
" int result = sum + product;\n"
|
|
" return result;\n"
|
|
" }\n"
|
|
"}\n",
|
|
"Test", "compute", 23, "(5+3) + (5*3) should equal 23");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_conditional_true_branch(void) {
|
|
UNITTEST_BEGIN_TEST("TestRuntime", "test_conditional_true_branch");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"public class Test {\n"
|
|
" public static int conditional() {\n"
|
|
" int x = 10;\n"
|
|
" int result = 0;\n"
|
|
" if (x > 5) {\n"
|
|
" result = 100;\n"
|
|
" }\n"
|
|
" return result;\n"
|
|
" }\n"
|
|
"}\n",
|
|
"Test", "conditional", 100, "x=10 > 5 should take true branch returning 100");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_conditional_false_branch(void) {
|
|
UNITTEST_BEGIN_TEST("TestRuntime", "test_conditional_false_branch");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"public class Test {\n"
|
|
" public static int conditional() {\n"
|
|
" int x = 3;\n"
|
|
" int result = 0;\n"
|
|
" if (x > 5) {\n"
|
|
" result = 100;\n"
|
|
" }\n"
|
|
" return result;\n"
|
|
" }\n"
|
|
"}\n",
|
|
"Test", "conditional", 0, "x=3 not > 5 should skip branch returning 0");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_factorial_recursive(void) {
|
|
UNITTEST_BEGIN_TEST("TestRuntime", "test_factorial_recursive");
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
|
"public class Test {\n"
|
|
" public static int factorial(int n) {\n"
|
|
" if (n <= 1) {\n"
|
|
" return 1;\n"
|
|
" }\n"
|
|
" return n * factorial(n - 1);\n"
|
|
" }\n"
|
|
"}\n",
|
|
"Test", "factorial", 1, "factorial(1) should return 1");
|
|
|
|
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("Rava Runtime Tests");
|
|
|
|
UnittestTestCase_t *tc = unittest_test_case_create("TestRuntime");
|
|
unittest_test_case_add_result(tc, test_simple_addition());
|
|
unittest_test_case_add_result(tc, test_multiple_operations());
|
|
unittest_test_case_add_result(tc, test_conditional_true_branch());
|
|
unittest_test_case_add_result(tc, test_conditional_false_branch());
|
|
unittest_test_case_add_result(tc, test_factorial_recursive());
|
|
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;
|
|
}
|