#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("=== Bitwise Operator Tests ===\n\n"); run_test_int("bitwise AND with vars", "public class Test {\n" " public static int main() {\n" " int a = 12;\n" " int b = 10;\n" " return a & b;\n" " }\n" "}\n", 8); run_test_int("bitwise OR with vars", "public class Test {\n" " public static int main() {\n" " int a = 12;\n" " int b = 10;\n" " return a | b;\n" " }\n" "}\n", 14); run_test_int("bitwise XOR", "public class Test {\n" " public static int main() {\n" " return 12 ^ 10;\n" " }\n" "}\n", 6); run_test_int("left shift", "public class Test {\n" " public static int main() {\n" " return 1 << 4;\n" " }\n" "}\n", 16); run_test_int("right shift", "public class Test {\n" " public static int main() {\n" " return 16 >> 2;\n" " }\n" "}\n", 4); run_test_int("unsigned right shift", "public class Test {\n" " public static int main() {\n" " return 16 >>> 2;\n" " }\n" "}\n", 4); run_test_int("combined bitwise ops", "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", 7); run_test_int("shift with variable", "public class Test {\n" " public static int main() {\n" " int x = 2;\n" " return 1 << x;\n" " }\n" "}\n", 4); run_test_int("bitwise mask", "public class Test {\n" " public static int main() {\n" " int value = 255;\n" " int mask = 0x0F;\n" " return value & mask;\n" " }\n" "}\n", 15); run_test_int("bit toggle with XOR", "public class Test {\n" " public static int main() {\n" " int flags = 5;\n" " return flags ^ 1;\n" " }\n" "}\n", 4); run_test_int("bitwise NOT", "public class Test {\n" " public static int main() {\n" " int x = 0;\n" " return ~x;\n" " }\n" "}\n", -1); run_test_int("bitwise NOT with value", "public class Test {\n" " public static int main() {\n" " int x = 5;\n" " return ~x;\n" " }\n" "}\n", -6); run_test_int("compound AND assign", "public class Test {\n" " public static int main() {\n" " int x = 15;\n" " x &= 7;\n" " return x;\n" " }\n" "}\n", 7); run_test_int("compound OR assign", "public class Test {\n" " public static int main() {\n" " int x = 8;\n" " x |= 3;\n" " return x;\n" " }\n" "}\n", 11); run_test_int("compound XOR assign", "public class Test {\n" " public static int main() {\n" " int x = 12;\n" " x ^= 5;\n" " return x;\n" " }\n" "}\n", 9); run_test_int("compound left shift assign", "public class Test {\n" " public static int main() {\n" " int x = 1;\n" " x <<= 3;\n" " return x;\n" " }\n" "}\n", 8); run_test_int("compound right shift assign", "public class Test {\n" " public static int main() {\n" " int x = 16;\n" " x >>= 2;\n" " return x;\n" " }\n" "}\n", 4); printf("\n=== Results: %d/%d tests passed ===\n", pass_count, test_count); return (pass_count == test_count) ? 0 : 1; }