167 lines
4.9 KiB
C
Raw Normal View History

2025-12-04 16:55:18 +01:00
#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>
#include <math.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("=== Math Class Tests ===\n\n");
run_test_int("Math.abs positive",
"public class Test {\n"
" public static int main() {\n"
" return Math.abs(42);\n"
" }\n"
"}\n", 42);
run_test_int("Math.abs negative",
"public class Test {\n"
" public static int main() {\n"
" return Math.abs(-42);\n"
" }\n"
"}\n", 42);
run_test_int("Math.min",
"public class Test {\n"
" public static int main() {\n"
" return Math.min(10, 5);\n"
" }\n"
"}\n", 5);
run_test_int("Math.max",
"public class Test {\n"
" public static int main() {\n"
" return Math.max(10, 5);\n"
" }\n"
"}\n", 10);
run_test_int("(int)Math.pow 2^8",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)Math.pow(2, 8);\n"
" return result;\n"
" }\n"
"}\n", 256);
run_test_int("(int)Math.sqrt 25",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)Math.sqrt(25);\n"
" return result;\n"
" }\n"
"}\n", 5);
run_test_int("(int)Math.floor 4.9",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)Math.floor(4.9);\n"
" return result;\n"
" }\n"
"}\n", 4);
run_test_int("(int)Math.ceil 4.1",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)Math.ceil(4.1);\n"
" return result;\n"
" }\n"
"}\n", 5);
run_test_int("(int)Math.round 4.7",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)Math.round(4.7);\n"
" return result;\n"
" }\n"
"}\n", 5);
run_test_int("(int)Math.round 4.2",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)Math.round(4.2);\n"
" return result;\n"
" }\n"
"}\n", 4);
run_test_int("(int) double cast",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)3.14159;\n"
" return result;\n"
" }\n"
"}\n", 3);
run_test_int("(int) negative double",
"public class Test {\n"
" public static int main() {\n"
" int result = (int)(-7.8);\n"
" return result;\n"
" }\n"
"}\n", -7);
printf("\n=== Results: %d/%d tests passed ===\n", pass_count, test_count);
return (pass_count == test_count) ? 0 : 1;
}