#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 #include #include 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("=== String Methods Tests ===\n\n"); run_test_int("String.length", "public class Test {\n" " public static int main() {\n" " String s = \"Hello\";\n" " return s.length();\n" " }\n" "}\n", 5); run_test_int("String.charAt", "public class Test {\n" " public static int main() {\n" " String s = \"Hello\";\n" " return s.charAt(1);\n" " }\n" "}\n", 'e'); run_test_int("String.substring", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " String sub = s.substring(0, 5);\n" " return sub.length();\n" " }\n" "}\n", 5); run_test_int("String.equals true", "public class Test {\n" " public static int main() {\n" " String a = \"test\";\n" " String b = \"test\";\n" " if (a.equals(b)) { return 1; }\n" " return 0;\n" " }\n" "}\n", 1); run_test_int("String.equals false", "public class Test {\n" " public static int main() {\n" " String a = \"test\";\n" " String b = \"other\";\n" " if (a.equals(b)) { return 1; }\n" " return 0;\n" " }\n" "}\n", 0); run_test_int("String.compareTo equal", "public class Test {\n" " public static int main() {\n" " String a = \"abc\";\n" " String b = \"abc\";\n" " return a.compareTo(b);\n" " }\n" "}\n", 0); run_test_int("String.compareTo less", "public class Test {\n" " public static int main() {\n" " String a = \"abc\";\n" " String b = \"abd\";\n" " if (a.compareTo(b) < 0) { return 1; }\n" " return 0;\n" " }\n" "}\n", 1); run_test_int("String.compareTo greater", "public class Test {\n" " public static int main() {\n" " String a = \"abd\";\n" " String b = \"abc\";\n" " if (a.compareTo(b) > 0) { return 1; }\n" " return 0;\n" " }\n" "}\n", 1); run_test_int("String.indexOf found", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " return s.indexOf(\"World\");\n" " }\n" "}\n", 6); run_test_int("String.indexOf not found", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " return s.indexOf(\"xyz\");\n" " }\n" "}\n", -1); run_test_int("String.contains true", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " if (s.contains(\"World\")) { return 1; }\n" " return 0;\n" " }\n" "}\n", 1); run_test_int("String.contains false", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " if (s.contains(\"xyz\")) { return 1; }\n" " return 0;\n" " }\n" "}\n", 0); run_test_int("String.startsWith true", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " if (s.startsWith(\"Hello\")) { return 1; }\n" " return 0;\n" " }\n" "}\n", 1); run_test_int("String.startsWith false", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " if (s.startsWith(\"World\")) { return 1; }\n" " return 0;\n" " }\n" "}\n", 0); run_test_int("String.endsWith true", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " if (s.endsWith(\"World\")) { return 1; }\n" " return 0;\n" " }\n" "}\n", 1); run_test_int("String.endsWith false", "public class Test {\n" " public static int main() {\n" " String s = \"Hello World\";\n" " if (s.endsWith(\"Hello\")) { return 1; }\n" " return 0;\n" " }\n" "}\n", 0); run_test_int("String.toLowerCase length", "public class Test {\n" " public static int main() {\n" " String s = \"HELLO\";\n" " String lower = s.toLowerCase();\n" " return lower.length();\n" " }\n" "}\n", 5); run_test_int("String.toLowerCase charAt", "public class Test {\n" " public static int main() {\n" " String s = \"HELLO\";\n" " String lower = s.toLowerCase();\n" " return lower.charAt(0);\n" " }\n" "}\n", 'h'); run_test_int("String.toUpperCase charAt", "public class Test {\n" " public static int main() {\n" " String s = \"hello\";\n" " String upper = s.toUpperCase();\n" " return upper.charAt(0);\n" " }\n" "}\n", 'H'); run_test_int("String.trim length", "public class Test {\n" " public static int main() {\n" " String s = \" Hello \";\n" " String trimmed = s.trim();\n" " return trimmed.length();\n" " }\n" "}\n", 5); printf("\n=== Results: %d/%d tests passed ===\n", pass_count, test_count); return (pass_count == test_count) ? 0 : 1; }