From 6fade93571c4c25f661e0984ecda780318324152 Mon Sep 17 00:00:00 2001 From: git Date: Mon, 2 Feb 2026 03:23:01 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20AI=20=E9=98=9F=E5=8F=8B=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=94=9F=E6=88=90=EF=BC=9A=E4=B8=BA=20writeToAllFile?= =?UTF-8?q?=20=E5=87=BD=E6=95=B0=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为重命名后的 writeToAllFile 函数创建完整的单元测试用例。 测试覆盖场景: - 空数据和空文本 - 只有数据,没有文本 - 只有文本,没有数据 - 两者都有数据 - 无效文件描述符的错误处理 --- src/backend/Assembly_test.zig | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/backend/Assembly_test.zig diff --git a/src/backend/Assembly_test.zig b/src/backend/Assembly_test.zig new file mode 100644 index 0000000..4457e14 --- /dev/null +++ b/src/backend/Assembly_test.zig @@ -0,0 +1,62 @@ +const std = @import("std"); +const Assembly = @import("Assembly.zig").Assembly; + +test "writeToAllFile - empty data and empty text" { + const assembly = Assembly{ + .data = &.{}, + .text = &.{}, + }; + var buffer: [1024]u8 = undefined; + var fbs = std.io.FixedBufferStream(&buffer); + const file = try std.fs.File.openWriter(fbs.writer()); + try assembly.writeToAllFile(file); + try std.testing.expectEqualStrings("", fbs.getWritten()); +} + +test "writeToAllFile - only data, no text" { + const assembly = Assembly{ + .data = "hello data", + .text = &.{}; + }; + var buffer: [1024]u8 = undefined; + var fbs = std.io.FixedBufferStream(&buffer); + const file = try std.fs.File.openWriter(fbs.writer()); + try assembly.writeToAllFile(file); + try std.testing.expectEqualStrings("hello data", fbs.getWritten()); +} + +test "writeToAllFile - only text, no data" { + const assembly = Assembly{ + .data = &.{}, + .text = "hello text", + }; + var buffer: [1024]u8 = undefined; + var fbs = std.io.FixedBufferStream(&buffer); + const file = try std.fs.File.openWriter(fbs.writer()); + try assembly.writeToAllFile(file); + try std.testing.expectEqualStrings("hello text", fbs.getWritten()); +} + +test "writeToAllFile - both data and text" { + const assembly = Assembly{ + .data = "data content", + .text = " text content", + }; + var buffer: [1024]u8 = undefined; + var fbs = std.io.FixedBufferStream(&buffer); + const file = try std.fs.File.openWriter(fbs.writer()); + try assembly.writeToAllFile(file); + try std.testing.expectEqualStrings("data content text content", fbs.getWritten()); +} + +test "writeToAllFile - invalid file descriptor" { + const assembly = Assembly{ + .data = "test data", + .text = "test text", + }; + const invalid_file = std.fs.File{ + .handle = -1, + .mode = .read_write, + }; + try std.testing.expectError(error.InputOutput, assembly.writeToAllFile(invalid_file)); +} -- Gitee