#include "../lexer/lexer.h"
#include "../parser/parser.h"
#include "../semantic/semantic.h"
#include "../ir/ir.h"
#include "../ir/ir_gen.h"
#include "../runtime/runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int test_count = 0;
static int pass_count = 0;
static void run_test_int(const char* name, const char* source, int expected) {
test_count++;
RavaLexer_t* lexer = rava_lexer_create(source);
RavaParser_t* parser = rava_parser_create(lexer);
RavaASTNode_t* ast = rava_parser_parse(parser);
if (!ast || parser->had_error) {
printf("FAIL: %s - Parse error: %s\n", name, parser->error_message ? parser->error_message : "unknown");
rava_parser_destroy(parser);
rava_lexer_destroy(lexer);
return;
}
RavaSemanticAnalyzer_t* analyzer = rava_semantic_analyzer_create();
rava_semantic_analyze(analyzer, ast);
RavaIRGenerator_t* ir_gen = rava_ir_generator_create(analyzer);
RavaProgram_t* program = rava_ir_generate(ir_gen, ast);
RavaVM_t* vm = rava_vm_create(program);
if (!rava_vm_execute(vm, "Test", "main")) {
printf("FAIL: %s - Runtime error: %s\n", name, vm->error_message ? vm->error_message : "unknown");
rava_vm_destroy(vm);
rava_program_destroy(program);
rava_ir_generator_destroy(ir_gen);
rava_semantic_analyzer_destroy(analyzer);
rava_ast_node_destroy(ast);
rava_parser_destroy(parser);
rava_lexer_destroy(lexer);
return;
}
RavaValue_t result = rava_vm_get_result(vm);
int32_t result_int = rava_value_as_int(result);
if (result_int == expected) {
printf("PASS: %s (result=%d)\n", name, result_int);
pass_count++;
} else {
printf("FAIL: %s (expected=%d, got=%d)\n", name, expected, result_int);
}
rava_vm_destroy(vm);
rava_program_destroy(program);
rava_ir_generator_destroy(ir_gen);
rava_semantic_analyzer_destroy(analyzer);
rava_ast_node_destroy(ast);
rava_parser_destroy(parser);
rava_lexer_destroy(lexer);
}
int main(void) {
printf("=== Ternary Operator Tests ===\n\n");
run_test_int("ternary true condition",
"public class Test {\n"
" public static int main() {\n"
" return 1 == 1 ? 42 : 0;\n"
" }\n"
"}\n", 42);
run_test_int("ternary false condition",
"public class Test {\n"
" public static int main() {\n"
" return 1 == 2 ? 42 : 99;\n"
" }\n"
"}\n", 99);
run_test_int("ternary with variables",
"public class Test {\n"
" public static int main() {\n"
" int x = 10;\n"
" int y = 20;\n"
" return x < y ? x : y;\n"
" }\n"
"}\n", 10);
run_test_int("ternary min value",
"public class Test {\n"
" public static int main() {\n"
" int a = 5;\n"
" int b = 3;\n"
" return a < b ? a : b;\n"
" }\n"
"}\n", 3);
run_test_int("ternary max value",
"public class Test {\n"
" public static int main() {\n"
" int a = 5;\n"
" int b = 3;\n"
" return a > b ? a : b;\n"
" }\n"
"}\n", 5);
run_test_int("nested ternary",
"public class Test {\n"
" public static int main() {\n"
" int x = 2;\n"
" return x == 1 ? 10 : (x == 2 ? 20 : 30);\n"
" }\n"
"}\n", 20);
run_test_int("ternary in variable assignment",
"public class Test {\n"
" public static int main() {\n"
" int x = 5;\n"
" int result = x > 3 ? 100 : 200;\n"
" return result;\n"
" }\n"
"}\n", 100);
run_test_int("ternary with arithmetic",
"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"
"}\n", 24);
printf("\n=== Results: %d/%d tests passed ===\n", pass_count, test_count);
return (pass_count == test_count) ? 0 : 1;
}