156 lines
4.8 KiB
C
Raw Normal View History

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_ternary_true_condition(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_true_condition");
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"
" return 1 == 1 ? 42 : 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 42, "ternary true condition should return 42");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_ternary_false_condition(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_false_condition");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" return 1 == 2 ? 42 : 99;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 99, "ternary false condition should return 99");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_ternary_with_variables(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_with_variables");
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 x = 10;\n"
" int y = 20;\n"
" return x < y ? x : y;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 10, "ternary with variables should return 10");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_ternary_min_value(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_min_value");
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 = 5;\n"
" int b = 3;\n"
" return a < b ? a : b;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 3, "ternary min value should return 3");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_ternary_max_value(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_max_value");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int a = 5;\n"
" int b = 3;\n"
" return a > b ? a : b;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 5, "ternary max value should return 5");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_nested_ternary(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_nested_ternary");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int x = 2;\n"
" return x == 1 ? 10 : (x == 2 ? 20 : 30);\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 20, "nested ternary should return 20");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_ternary_in_assignment(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_in_assignment");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" int x = 5;\n"
" int result = x > 3 ? 100 : 200;\n"
" return result;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 100, "ternary in variable assignment should return 100");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_ternary_with_arithmetic(void) {
UNITTEST_BEGIN_TEST("TestTernary", "test_ternary_with_arithmetic");
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 = 4;\n"
" int b = 6;\n"
" return a + b > 8 ? a * b : a + b;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 24, "ternary with arithmetic should return 24");
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("Ternary Operator Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestTernary");
unittest_test_case_add_result(tc, test_ternary_true_condition());
unittest_test_case_add_result(tc, test_ternary_false_condition());
unittest_test_case_add_result(tc, test_ternary_with_variables());
unittest_test_case_add_result(tc, test_ternary_min_value());
unittest_test_case_add_result(tc, test_ternary_max_value());
unittest_test_case_add_result(tc, test_nested_ternary());
unittest_test_case_add_result(tc, test_ternary_in_assignment());
unittest_test_case_add_result(tc, test_ternary_with_arithmetic());
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
}