authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-18 22:03:10-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:10-08:00
log7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c
tree6d1d5f066c72636fc1da74fddce4e44e211b431f
parent21d0264c61ac29724b98187aa87d192f97b52425

std: update remaining unit tests for std.Io API changes


6 files changed, 47 insertions(+), 43 deletions(-)

lib/std/Build/Cache.zig+6-6
......@@ -1384,8 +1384,8 @@ test "check that changing a file makes cache fail" {
13841384 try tmp.dir.writeFile(io, .{ .sub_path = temp_file, .data = original_temp_file_contents });
13851385
13861386 // Wait for file timestamps to tick
1387 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1388 while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time.nanoseconds) {
1387 const initial_time = try testGetCurrentFileTimestamp(io, tmp.dir);
1388 while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time.nanoseconds) {
13891389 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
13901390 }
13911391
......@@ -1502,8 +1502,8 @@ test "Manifest with files added after initial hash work" {
15021502 try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second!\n" });
15031503
15041504 // Wait for file timestamps to tick
1505 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1506 while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time.nanoseconds) {
1505 const initial_time = try testGetCurrentFileTimestamp(io, tmp.dir);
1506 while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time.nanoseconds) {
15071507 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
15081508 }
15091509
......@@ -1553,8 +1553,8 @@ test "Manifest with files added after initial hash work" {
15531553 try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second, updated\n" });
15541554
15551555 // Wait for file timestamps to tick
1556 const initial_time2 = try testGetCurrentFileTimestamp(tmp.dir);
1557 while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time2.nanoseconds) {
1556 const initial_time2 = try testGetCurrentFileTimestamp(io, tmp.dir);
1557 while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time2.nanoseconds) {
15581558 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
15591559 }
15601560
lib/std/Io/File/Reader.zig+2-2
......@@ -18,8 +18,8 @@ io: Io,
1818file: File,
1919err: ?Error = null,
2020mode: Mode = .positional,
21/// Tracks the true seek position in the file. To obtain the logical
22/// position, use `logicalPos`.
21/// Tracks the true seek position in the file. To obtain the logical position,
22/// use `logicalPos`.
2323pos: u64 = 0,
2424size: ?u64 = null,
2525size_err: ?SizeError = null,
lib/std/Io/File/Writer.zig+6-2
......@@ -11,8 +11,8 @@ io: Io,
1111file: File,
1212err: ?Error = null,
1313mode: Mode = .positional,
14/// Tracks the true seek position in the file. To obtain the logical
15/// position, add the buffer size to this value.
14/// Tracks the true seek position in the file. To obtain the logical position,
15/// use `logicalPos`.
1616pos: u64 = 0,
1717write_file_err: ?WriteFileError = null,
1818seek_err: ?SeekError = null,
......@@ -221,6 +221,10 @@ pub fn seekTo(w: *Writer, offset: u64) (SeekError || Io.Writer.Error)!void {
221221 try seekToUnbuffered(w, offset);
222222}
223223
224pub fn logicalPos(w: *const Writer) u64 {
225 return w.pos + w.interface.end;
226}
227
224228/// Asserts that no data is currently buffered.
225229pub fn seekToUnbuffered(w: *Writer, offset: u64) SeekError!void {
226230 assert(w.interface.buffered().len == 0);
lib/std/Io/test.zig+18-21
......@@ -64,33 +64,28 @@ test "write a file, read it, then delete it" {
6464 try tmp.dir.deleteFile(io, tmp_file_name);
6565}
6666
67test "File seek ops" {
67test "File.Writer.seekTo" {
6868 var tmp = tmpDir(.{});
6969 defer tmp.cleanup();
7070
7171 const io = testing.io;
7272
73 var data: [8192]u8 = undefined;
74 @memset(&data, 0x55);
75
7376 const tmp_file_name = "temp_test_file.txt";
7477 var file = try tmp.dir.createFile(io, tmp_file_name, .{});
7578 defer file.close(io);
7679
77 try file.writeAll(&([_]u8{0x55} ** 8192));
78
79 // Seek to the end
80 try file.seekFromEnd(0);
81 try expect((try file.getPos()) == try file.length(io));
82 // Negative delta
83 try file.seekBy(-4096);
84 try expect((try file.getPos()) == 4096);
85 // Positive delta
86 try file.seekBy(10);
87 try expect((try file.getPos()) == 4106);
88 // Absolute position
89 try file.seekTo(1234);
90 try expect((try file.getPos()) == 1234);
80 var fw = file.writerStreaming(io, &.{});
81
82 try fw.interface.writeAll(&data);
83 try expect(fw.logicalPos() == try file.length(io));
84 try fw.seekTo(1234);
85 try expect(fw.logicalPos() == 1234);
9186}
9287
93test "setLength" {
88test "File.setLength" {
9489 const io = testing.io;
9590
9691 var tmp = tmpDir(.{});
......@@ -100,19 +95,21 @@ test "setLength" {
10095 var file = try tmp.dir.createFile(io, tmp_file_name, .{});
10196 defer file.close(io);
10297
98 var fw = file.writerStreaming(io, &.{});
99
103100 // Verify that the file size changes and the file offset is not moved
104101 try expect((try file.length(io)) == 0);
105 try expect((try file.getPos()) == 0);
102 try expect(fw.logicalPos() == 0);
106103 try file.setLength(io, 8192);
107104 try expect((try file.length(io)) == 8192);
108 try expect((try file.getPos()) == 0);
109 try file.seekTo(100);
105 try expect(fw.logicalPos() == 0);
106 try fw.seekTo(100);
110107 try file.setLength(io, 4096);
111108 try expect((try file.length(io)) == 4096);
112 try expect((try file.getPos()) == 100);
109 try expect(fw.logicalPos() == 100);
113110 try file.setLength(io, 0);
114111 try expect((try file.length(io)) == 0);
115 try expect((try file.getPos()) == 100);
112 try expect(fw.logicalPos() == 100);
116113}
117114
118115test "legacy setLength" {
lib/std/Thread.zig+10-8
......@@ -211,7 +211,7 @@ pub fn setName(self: Thread, io: Io, name: []const u8) SetNameError!void {
211211 const file = try Io.Dir.cwd().openFile(io, path, .{ .mode = .write_only });
212212 defer file.close(io);
213213
214 try file.writeAll(name);
214 try file.writeStreamingAll(io, name);
215215 return;
216216 },
217217 .windows => {
......@@ -1676,14 +1676,14 @@ const LinuxThreadImpl = struct {
16761676 }
16771677};
16781678
1679fn testThreadName(thread: *Thread) !void {
1679fn testThreadName(io: Io, thread: *Thread) !void {
16801680 const testCases = &[_][]const u8{
16811681 "mythread",
16821682 "b" ** max_name_len,
16831683 };
16841684
16851685 inline for (testCases) |tc| {
1686 try thread.setName(tc);
1686 try thread.setName(io, tc);
16871687
16881688 var name_buffer: [max_name_len:0]u8 = undefined;
16891689
......@@ -1698,6 +1698,8 @@ fn testThreadName(thread: *Thread) !void {
16981698test "setName, getName" {
16991699 if (builtin.single_threaded) return error.SkipZigTest;
17001700
1701 const io = testing.io;
1702
17011703 const Context = struct {
17021704 start_wait_event: ResetEvent = .unset,
17031705 test_done_event: ResetEvent = .unset,
......@@ -1711,11 +1713,11 @@ test "setName, getName" {
17111713 ctx.start_wait_event.wait();
17121714
17131715 switch (native_os) {
1714 .windows => testThreadName(&ctx.thread) catch |err| switch (err) {
1716 .windows => testThreadName(io, &ctx.thread) catch |err| switch (err) {
17151717 error.Unsupported => return error.SkipZigTest,
17161718 else => return err,
17171719 },
1718 else => try testThreadName(&ctx.thread),
1720 else => try testThreadName(io, &ctx.thread),
17191721 }
17201722
17211723 // Signal our test is done
......@@ -1735,14 +1737,14 @@ test "setName, getName" {
17351737
17361738 switch (native_os) {
17371739 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
1738 const res = thread.setName("foobar");
1740 const res = thread.setName(io, "foobar");
17391741 try std.testing.expectError(error.Unsupported, res);
17401742 },
1741 .windows => testThreadName(&thread) catch |err| switch (err) {
1743 .windows => testThreadName(io, &thread) catch |err| switch (err) {
17421744 error.Unsupported => return error.SkipZigTest,
17431745 else => return err,
17441746 },
1745 else => try testThreadName(&thread),
1747 else => try testThreadName(io, &thread),
17461748 }
17471749
17481750 context.thread_done_event.set();
lib/std/debug.zig+5-4
......@@ -384,12 +384,13 @@ pub fn dumpHexFallible(t: Io.Terminal, bytes: []const u8) !void {
384384}
385385
386386test dumpHexFallible {
387 const gpa = testing.allocator;
387388 const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 };
388 var aw: Writer.Allocating = .init(testing.allocator);
389 var aw: Writer.Allocating = .init(gpa);
389390 defer aw.deinit();
390391
391 try dumpHexFallible(&aw.writer, .no_color, bytes);
392 const expected = try std.fmt.allocPrint(testing.allocator,
392 try dumpHexFallible(.{ .writer = &aw.writer, .mode = .no_color }, bytes);
393 const expected = try std.fmt.allocPrint(gpa,
393394 \\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........
394395 \\{x:0>[2]} 01 12 13 ...
395396 \\
......@@ -398,7 +399,7 @@ test dumpHexFallible {
398399 @intFromPtr(bytes.ptr) + 16,
399400 @sizeOf(usize) * 2,
400401 });
401 defer testing.allocator.free(expected);
402 defer gpa.free(expected);
402403 try testing.expectEqualStrings(expected, aw.written());
403404}
404405