diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-12-18 22:03:10 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-12-23 22:15:10 -0800 |
| commit | 7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c (patch) | |
| tree | 6d1d5f066c72636fc1da74fddce4e44e211b431f /lib/std/Io | |
| parent | 21d0264c61ac29724b98187aa87d192f97b52425 (diff) | |
| download | zig-7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c.tar.gz zig-7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c.zip | |
std: update remaining unit tests for std.Io API changes
Diffstat (limited to 'lib/std/Io')
| -rw-r--r-- | lib/std/Io/File/Reader.zig | 4 | ||||
| -rw-r--r-- | lib/std/Io/File/Writer.zig | 8 | ||||
| -rw-r--r-- | lib/std/Io/test.zig | 39 |
3 files changed, 26 insertions, 25 deletions
diff --git a/lib/std/Io/File/Reader.zig b/lib/std/Io/File/Reader.zig index 2359fad722..0c573c9ae1 100644 --- a/lib/std/Io/File/Reader.zig +++ b/lib/std/Io/File/Reader.zig @@ -18,8 +18,8 @@ io: Io, file: File, err: ?Error = null, mode: Mode = .positional, -/// Tracks the true seek position in the file. To obtain the logical -/// position, use `logicalPos`. +/// Tracks the true seek position in the file. To obtain the logical position, +/// use `logicalPos`. pos: u64 = 0, size: ?u64 = null, size_err: ?SizeError = null, diff --git a/lib/std/Io/File/Writer.zig b/lib/std/Io/File/Writer.zig index 56a1c09340..3487416719 100644 --- a/lib/std/Io/File/Writer.zig +++ b/lib/std/Io/File/Writer.zig @@ -11,8 +11,8 @@ io: Io, file: File, err: ?Error = null, mode: Mode = .positional, -/// Tracks the true seek position in the file. To obtain the logical -/// position, add the buffer size to this value. +/// Tracks the true seek position in the file. To obtain the logical position, +/// use `logicalPos`. pos: u64 = 0, write_file_err: ?WriteFileError = null, seek_err: ?SeekError = null, @@ -221,6 +221,10 @@ pub fn seekTo(w: *Writer, offset: u64) (SeekError || Io.Writer.Error)!void { try seekToUnbuffered(w, offset); } +pub fn logicalPos(w: *const Writer) u64 { + return w.pos + w.interface.end; +} + /// Asserts that no data is currently buffered. pub fn seekToUnbuffered(w: *Writer, offset: u64) SeekError!void { assert(w.interface.buffered().len == 0); diff --git a/lib/std/Io/test.zig b/lib/std/Io/test.zig index 8f32555b52..ef0d45d953 100644 --- a/lib/std/Io/test.zig +++ b/lib/std/Io/test.zig @@ -64,33 +64,28 @@ test "write a file, read it, then delete it" { try tmp.dir.deleteFile(io, tmp_file_name); } -test "File seek ops" { +test "File.Writer.seekTo" { var tmp = tmpDir(.{}); defer tmp.cleanup(); const io = testing.io; + var data: [8192]u8 = undefined; + @memset(&data, 0x55); + const tmp_file_name = "temp_test_file.txt"; var file = try tmp.dir.createFile(io, tmp_file_name, .{}); defer file.close(io); - try file.writeAll(&([_]u8{0x55} ** 8192)); - - // Seek to the end - try file.seekFromEnd(0); - try expect((try file.getPos()) == try file.length(io)); - // Negative delta - try file.seekBy(-4096); - try expect((try file.getPos()) == 4096); - // Positive delta - try file.seekBy(10); - try expect((try file.getPos()) == 4106); - // Absolute position - try file.seekTo(1234); - try expect((try file.getPos()) == 1234); + var fw = file.writerStreaming(io, &.{}); + + try fw.interface.writeAll(&data); + try expect(fw.logicalPos() == try file.length(io)); + try fw.seekTo(1234); + try expect(fw.logicalPos() == 1234); } -test "setLength" { +test "File.setLength" { const io = testing.io; var tmp = tmpDir(.{}); @@ -100,19 +95,21 @@ test "setLength" { var file = try tmp.dir.createFile(io, tmp_file_name, .{}); defer file.close(io); + var fw = file.writerStreaming(io, &.{}); + // Verify that the file size changes and the file offset is not moved try expect((try file.length(io)) == 0); - try expect((try file.getPos()) == 0); + try expect(fw.logicalPos() == 0); try file.setLength(io, 8192); try expect((try file.length(io)) == 8192); - try expect((try file.getPos()) == 0); - try file.seekTo(100); + try expect(fw.logicalPos() == 0); + try fw.seekTo(100); try file.setLength(io, 4096); try expect((try file.length(io)) == 4096); - try expect((try file.getPos()) == 100); + try expect(fw.logicalPos() == 100); try file.setLength(io, 0); try expect((try file.length(io)) == 0); - try expect((try file.getPos()) == 100); + try expect(fw.logicalPos() == 100); } test "legacy setLength" { |
