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" {...@@ -1384,8 +1384,8 @@ test "check that changing a file makes cache fail" {
1384 try tmp.dir.writeFile(io, .{ .sub_path = temp_file, .data = original_temp_file_contents });1384 try tmp.dir.writeFile(io, .{ .sub_path = temp_file, .data = original_temp_file_contents });
13851385
1386 // Wait for file timestamps to tick1386 // Wait for file timestamps to tick
1387 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);1387 const initial_time = try testGetCurrentFileTimestamp(io, tmp.dir);
1388 while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time.nanoseconds) {1388 while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time.nanoseconds) {
1389 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);1389 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
1390 }1390 }
13911391
...@@ -1502,8 +1502,8 @@ test "Manifest with files added after initial hash work" {...@@ -1502,8 +1502,8 @@ test "Manifest with files added after initial hash work" {
1502 try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second!\n" });1502 try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second!\n" });
15031503
1504 // Wait for file timestamps to tick1504 // Wait for file timestamps to tick
1505 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);1505 const initial_time = try testGetCurrentFileTimestamp(io, tmp.dir);
1506 while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time.nanoseconds) {1506 while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time.nanoseconds) {
1507 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);1507 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
1508 }1508 }
15091509
...@@ -1553,8 +1553,8 @@ test "Manifest with files added after initial hash work" {...@@ -1553,8 +1553,8 @@ test "Manifest with files added after initial hash work" {
1553 try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second, updated\n" });1553 try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second, updated\n" });
15541554
1555 // Wait for file timestamps to tick1555 // Wait for file timestamps to tick
1556 const initial_time2 = try testGetCurrentFileTimestamp(tmp.dir);1556 const initial_time2 = try testGetCurrentFileTimestamp(io, tmp.dir);
1557 while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time2.nanoseconds) {1557 while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time2.nanoseconds) {
1558 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);1558 try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
1559 }1559 }
15601560
lib/std/Io/File/Reader.zig+2-2
...@@ -18,8 +18,8 @@ io: Io,...@@ -18,8 +18,8 @@ io: Io,
18file: File,18file: File,
19err: ?Error = null,19err: ?Error = null,
20mode: Mode = .positional,20mode: Mode = .positional,
21/// Tracks the true seek position in the file. To obtain the logical21/// Tracks the true seek position in the file. To obtain the logical position,
22/// position, use `logicalPos`.22/// use `logicalPos`.
23pos: u64 = 0,23pos: u64 = 0,
24size: ?u64 = null,24size: ?u64 = null,
25size_err: ?SizeError = null,25size_err: ?SizeError = null,
lib/std/Io/File/Writer.zig+6-2
...@@ -11,8 +11,8 @@ io: Io,...@@ -11,8 +11,8 @@ io: Io,
11file: File,11file: File,
12err: ?Error = null,12err: ?Error = null,
13mode: Mode = .positional,13mode: Mode = .positional,
14/// Tracks the true seek position in the file. To obtain the logical14/// Tracks the true seek position in the file. To obtain the logical position,
15/// position, add the buffer size to this value.15/// use `logicalPos`.
16pos: u64 = 0,16pos: u64 = 0,
17write_file_err: ?WriteFileError = null,17write_file_err: ?WriteFileError = null,
18seek_err: ?SeekError = null,18seek_err: ?SeekError = null,
...@@ -221,6 +221,10 @@ pub fn seekTo(w: *Writer, offset: u64) (SeekError || Io.Writer.Error)!void {...@@ -221,6 +221,10 @@ pub fn seekTo(w: *Writer, offset: u64) (SeekError || Io.Writer.Error)!void {
221 try seekToUnbuffered(w, offset);221 try seekToUnbuffered(w, offset);
222}222}
223223
224pub fn logicalPos(w: *const Writer) u64 {
225 return w.pos + w.interface.end;
226}
227
224/// Asserts that no data is currently buffered.228/// Asserts that no data is currently buffered.
225pub fn seekToUnbuffered(w: *Writer, offset: u64) SeekError!void {229pub fn seekToUnbuffered(w: *Writer, offset: u64) SeekError!void {
226 assert(w.interface.buffered().len == 0);230 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" {...@@ -64,33 +64,28 @@ test "write a file, read it, then delete it" {
64 try tmp.dir.deleteFile(io, tmp_file_name);64 try tmp.dir.deleteFile(io, tmp_file_name);
65}65}
6666
67test "File seek ops" {67test "File.Writer.seekTo" {
68 var tmp = tmpDir(.{});68 var tmp = tmpDir(.{});
69 defer tmp.cleanup();69 defer tmp.cleanup();
7070
71 const io = testing.io;71 const io = testing.io;
7272
73 var data: [8192]u8 = undefined;
74 @memset(&data, 0x55);
75
73 const tmp_file_name = "temp_test_file.txt";76 const tmp_file_name = "temp_test_file.txt";
74 var file = try tmp.dir.createFile(io, tmp_file_name, .{});77 var file = try tmp.dir.createFile(io, tmp_file_name, .{});
75 defer file.close(io);78 defer file.close(io);
7679
77 try file.writeAll(&([_]u8{0x55} ** 8192));80 var fw = file.writerStreaming(io, &.{});
7881
79 // Seek to the end82 try fw.interface.writeAll(&data);
80 try file.seekFromEnd(0);83 try expect(fw.logicalPos() == try file.length(io));
81 try expect((try file.getPos()) == try file.length(io));84 try fw.seekTo(1234);
82 // Negative delta85 try expect(fw.logicalPos() == 1234);
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);
91}86}
9287
93test "setLength" {88test "File.setLength" {
94 const io = testing.io;89 const io = testing.io;
9590
96 var tmp = tmpDir(.{});91 var tmp = tmpDir(.{});
...@@ -100,19 +95,21 @@ test "setLength" {...@@ -100,19 +95,21 @@ test "setLength" {
100 var file = try tmp.dir.createFile(io, tmp_file_name, .{});95 var file = try tmp.dir.createFile(io, tmp_file_name, .{});
101 defer file.close(io);96 defer file.close(io);
10297
98 var fw = file.writerStreaming(io, &.{});
99
103 // Verify that the file size changes and the file offset is not moved100 // Verify that the file size changes and the file offset is not moved
104 try expect((try file.length(io)) == 0);101 try expect((try file.length(io)) == 0);
105 try expect((try file.getPos()) == 0);102 try expect(fw.logicalPos() == 0);
106 try file.setLength(io, 8192);103 try file.setLength(io, 8192);
107 try expect((try file.length(io)) == 8192);104 try expect((try file.length(io)) == 8192);
108 try expect((try file.getPos()) == 0);105 try expect(fw.logicalPos() == 0);
109 try file.seekTo(100);106 try fw.seekTo(100);
110 try file.setLength(io, 4096);107 try file.setLength(io, 4096);
111 try expect((try file.length(io)) == 4096);108 try expect((try file.length(io)) == 4096);
112 try expect((try file.getPos()) == 100);109 try expect(fw.logicalPos() == 100);
113 try file.setLength(io, 0);110 try file.setLength(io, 0);
114 try expect((try file.length(io)) == 0);111 try expect((try file.length(io)) == 0);
115 try expect((try file.getPos()) == 100);112 try expect(fw.logicalPos() == 100);
116}113}
117114
118test "legacy setLength" {115test "legacy setLength" {
lib/std/Thread.zig+10-8
...@@ -211,7 +211,7 @@ pub fn setName(self: Thread, io: Io, name: []const u8) SetNameError!void {...@@ -211,7 +211,7 @@ pub fn setName(self: Thread, io: Io, name: []const u8) SetNameError!void {
211 const file = try Io.Dir.cwd().openFile(io, path, .{ .mode = .write_only });211 const file = try Io.Dir.cwd().openFile(io, path, .{ .mode = .write_only });
212 defer file.close(io);212 defer file.close(io);
213213
214 try file.writeAll(name);214 try file.writeStreamingAll(io, name);
215 return;215 return;
216 },216 },
217 .windows => {217 .windows => {
...@@ -1676,14 +1676,14 @@ const LinuxThreadImpl = struct {...@@ -1676,14 +1676,14 @@ const LinuxThreadImpl = struct {
1676 }1676 }
1677};1677};
16781678
1679fn testThreadName(thread: *Thread) !void {1679fn testThreadName(io: Io, thread: *Thread) !void {
1680 const testCases = &[_][]const u8{1680 const testCases = &[_][]const u8{
1681 "mythread",1681 "mythread",
1682 "b" ** max_name_len,1682 "b" ** max_name_len,
1683 };1683 };
16841684
1685 inline for (testCases) |tc| {1685 inline for (testCases) |tc| {
1686 try thread.setName(tc);1686 try thread.setName(io, tc);
16871687
1688 var name_buffer: [max_name_len:0]u8 = undefined;1688 var name_buffer: [max_name_len:0]u8 = undefined;
16891689
...@@ -1698,6 +1698,8 @@ fn testThreadName(thread: *Thread) !void {...@@ -1698,6 +1698,8 @@ fn testThreadName(thread: *Thread) !void {
1698test "setName, getName" {1698test "setName, getName" {
1699 if (builtin.single_threaded) return error.SkipZigTest;1699 if (builtin.single_threaded) return error.SkipZigTest;
17001700
1701 const io = testing.io;
1702
1701 const Context = struct {1703 const Context = struct {
1702 start_wait_event: ResetEvent = .unset,1704 start_wait_event: ResetEvent = .unset,
1703 test_done_event: ResetEvent = .unset,1705 test_done_event: ResetEvent = .unset,
...@@ -1711,11 +1713,11 @@ test "setName, getName" {...@@ -1711,11 +1713,11 @@ test "setName, getName" {
1711 ctx.start_wait_event.wait();1713 ctx.start_wait_event.wait();
17121714
1713 switch (native_os) {1715 switch (native_os) {
1714 .windows => testThreadName(&ctx.thread) catch |err| switch (err) {1716 .windows => testThreadName(io, &ctx.thread) catch |err| switch (err) {
1715 error.Unsupported => return error.SkipZigTest,1717 error.Unsupported => return error.SkipZigTest,
1716 else => return err,1718 else => return err,
1717 },1719 },
1718 else => try testThreadName(&ctx.thread),1720 else => try testThreadName(io, &ctx.thread),
1719 }1721 }
17201722
1721 // Signal our test is done1723 // Signal our test is done
...@@ -1735,14 +1737,14 @@ test "setName, getName" {...@@ -1735,14 +1737,14 @@ test "setName, getName" {
17351737
1736 switch (native_os) {1738 switch (native_os) {
1737 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {1739 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
1738 const res = thread.setName("foobar");1740 const res = thread.setName(io, "foobar");
1739 try std.testing.expectError(error.Unsupported, res);1741 try std.testing.expectError(error.Unsupported, res);
1740 },1742 },
1741 .windows => testThreadName(&thread) catch |err| switch (err) {1743 .windows => testThreadName(io, &thread) catch |err| switch (err) {
1742 error.Unsupported => return error.SkipZigTest,1744 error.Unsupported => return error.SkipZigTest,
1743 else => return err,1745 else => return err,
1744 },1746 },
1745 else => try testThreadName(&thread),1747 else => try testThreadName(io, &thread),
1746 }1748 }
17471749
1748 context.thread_done_event.set();1750 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 {...@@ -384,12 +384,13 @@ pub fn dumpHexFallible(t: Io.Terminal, bytes: []const u8) !void {
384}384}
385385
386test dumpHexFallible {386test dumpHexFallible {
387 const gpa = testing.allocator;
387 const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 };388 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);
389 defer aw.deinit();390 defer aw.deinit();
390391
391 try dumpHexFallible(&aw.writer, .no_color, bytes);392 try dumpHexFallible(.{ .writer = &aw.writer, .mode = .no_color }, bytes);
392 const expected = try std.fmt.allocPrint(testing.allocator,393 const expected = try std.fmt.allocPrint(gpa,
393 \\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........394 \\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........
394 \\{x:0>[2]} 01 12 13 ...395 \\{x:0>[2]} 01 12 13 ...
395 \\396 \\
...@@ -398,7 +399,7 @@ test dumpHexFallible {...@@ -398,7 +399,7 @@ test dumpHexFallible {
398 @intFromPtr(bytes.ptr) + 16,399 @intFromPtr(bytes.ptr) + 16,
399 @sizeOf(usize) * 2,400 @sizeOf(usize) * 2,
400 });401 });
401 defer testing.allocator.free(expected);402 defer gpa.free(expected);
402 try testing.expectEqualStrings(expected, aw.written());403 try testing.expectEqualStrings(expected, aw.written());
403}404}
404405