diff --git a/src/backend/Assembly.zig b/src/backend/Assembly.zig index 914bffb033bfff5923a987cb6e997a23973c3155..decf8d225676b82d73cdae928d30915ffa76bbfe 100644 --- a/src/backend/Assembly.zig +++ b/src/backend/Assembly.zig @@ -18,3 +18,99 @@ pub fn writeToAllFile(self: Assembly, file: std.fs.File) !void { }; return file.writevAll(&vec); } + +test "Assembly deinit with empty data" { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + const assembly = Assembly{ + .data = &.{}, + .text = &.{}, + }; + assembly.deinit(gpa.allocator()); +} + +test "Assembly deinit with non-empty data" { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + const data = try gpa.allocator().dupe(u8, "data section"); + const text = try gpa.allocator().dupe(u8, ".text\n"); + const assembly = Assembly{ + .data = data, + .text = text, + }; + assembly.deinit(gpa.allocator()); +} + +test "Assembly writeToAllFile with empty data" { + const tmp_dir = std.testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + + const file = try tmp_dir.dir.createFile("test.s", .{}); + defer file.close(); + + const assembly = Assembly{ + .data = &.{}, + .text = &.{}, + }; + try assembly.writeToAllFile(file); + try file.seekTo(0); + const content = try file.readToEndAlloc(std.testing.allocator, std.math.maxInt(usize)); + defer std.testing.allocator.free(content); + try std.testing.expectEqualStrings("", content); +} + +test "Assembly writeToAllFile with data and text" { + const tmp_dir = std.testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + + const file = try tmp_dir.dir.createFile("test.s", .{}); + defer file.close(); + + const assembly = Assembly{ + .data = ".data\n", + .text = ".text\n\tret\n", + }; + try assembly.writeToAllFile(file); + try file.seekTo(0); + const content = try file.readToEndAlloc(std.testing.allocator, std.math.maxInt(usize)); + defer std.testing.allocator.free(content); + try std.testing.expectEqualStrings(".data\n.text\n\tret\n", content); +} + +test "Assembly writeToAllFile with only data" { + const tmp_dir = std.testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + + const file = try tmp_dir.dir.createFile("test.s", .{}); + defer file.close(); + + const assembly = Assembly{ + .data = ".data\n\t.byte 0x42\n", + .text = &.{}, + }; + try assembly.writeToAllFile(file); + try file.seekTo(0); + const content = try file.readToEndAlloc(std.testing.allocator, std.math.maxInt(usize)); + defer std.testing.allocator.free(content); + try std.testing.expectEqualStrings(".data\n\t.byte 0x42\n", content); +} + +test "Assembly writeToAllFile with only text" { + const tmp_dir = std.testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + + const file = try tmp_dir.dir.createFile("test.s", .{}); + defer file.close(); + + const assembly = Assembly{ + .data = &.{}, + .text = ".text\n\tmov rax, 1\n", + }; + try assembly.writeToAllFile(file); + try file.seekTo(0); + const content = try file.readToEndAlloc(std.testing.allocator, std.math.maxInt(usize)); + defer std.testing.allocator.free(content); + try std.testing.expectEqualStrings(".text\n\tmov rax, 1\n", content); +}