diff --git a/src/main.zig b/src/main.zig index cd0c05632289ba89802c4d7f8ec40263555aa09f..a3795464b99944ed74fc6c0fbc444d5639301acf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,23 +1,45 @@ const std = @import("std"); const process = std.process; +const testing = std.testing; const aro = @import("aro"); const Compilation = aro.Compilation; const Driver = aro.Driver; const Toolchain = aro.Toolchain; const assembly_backend = @import("assembly_backend"); +/// Handles out of memory error by printing a message and optionally exiting. +/// If fast_exit is true, terminates the process immediately; otherwise returns exit code 1. +/// Returns: Exit code (0 on successful handling with fast_exit=false, 1 otherwise) fn handleOutOfMemory(fast_exit: bool) u8 { std.debug.print("out of memory\n", .{}); if (fast_exit) process.exit(1); return 1; } +/// Handles generic errors by dispatching to appropriate handlers based on error type. +/// Currently handles OutOfMemory errors by calling handleOutOfMemory. +/// Returns: Exit code from the specific error handler (typically 0 on success, 1 on failure) fn handleError(fast_exit: bool, er: anyerror) u8 { return switch (er) { error.OutOfMemory => handleOutOfMemory(fast_exit), }; } +test "handleOutOfMemory returns 1 when fast_exit is false" { + const result = handleOutOfMemory(false); + try testing.expectEqual(@as(u8, 1), result); +} + +test "handleError returns 1 for OutOfMemory error when fast_exit is false" { + const result = handleError(false, error.OutOfMemory); + try testing.expectEqual(@as(u8, 1), result); +} + +test "handleError handles OutOfMemory error with fast_exit true" { + const result = handleError(true, error.OutOfMemory); + try testing.expectEqual(@as(u8, 1), result); +} + pub fn main() u8 { const link_libc = @import("builtin").link_libc; const gpa = if (link_libc) std.heap.raw_c_allocator else std.heap.GeneralPurposeAllocator(.{}).allocator();