aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-12-18 22:03:10 -0800
committerAndrew Kelley <andrew@ziglang.org>2025-12-23 22:15:10 -0800
commit7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c (patch)
tree6d1d5f066c72636fc1da74fddce4e44e211b431f /lib/std
parent21d0264c61ac29724b98187aa87d192f97b52425 (diff)
downloadzig-7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c.tar.gz
zig-7ce5ee2e92bf1bf1f39ccc08df19f9a1044e9f2c.zip
std: update remaining unit tests for std.Io API changes
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Build/Cache.zig12
-rw-r--r--lib/std/Io/File/Reader.zig4
-rw-r--r--lib/std/Io/File/Writer.zig8
-rw-r--r--lib/std/Io/test.zig39
-rw-r--r--lib/std/Thread.zig18
-rw-r--r--lib/std/debug.zig9
6 files changed, 47 insertions, 43 deletions
diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig
index e2c848b6fd..43f8691000 100644
--- a/lib/std/Build/Cache.zig
+++ b/lib/std/Build/Cache.zig
@@ -1384,8 +1384,8 @@ test "check that changing a file makes cache fail" {
try tmp.dir.writeFile(io, .{ .sub_path = temp_file, .data = original_temp_file_contents });
// Wait for file timestamps to tick
- const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
- while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time.nanoseconds) {
+ const initial_time = try testGetCurrentFileTimestamp(io, tmp.dir);
+ while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time.nanoseconds) {
try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
}
@@ -1502,8 +1502,8 @@ test "Manifest with files added after initial hash work" {
try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second!\n" });
// Wait for file timestamps to tick
- const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
- while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time.nanoseconds) {
+ const initial_time = try testGetCurrentFileTimestamp(io, tmp.dir);
+ while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time.nanoseconds) {
try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
}
@@ -1553,8 +1553,8 @@ test "Manifest with files added after initial hash work" {
try tmp.dir.writeFile(io, .{ .sub_path = temp_file2, .data = "Hello world the second, updated\n" });
// Wait for file timestamps to tick
- const initial_time2 = try testGetCurrentFileTimestamp(tmp.dir);
- while ((try testGetCurrentFileTimestamp(tmp.dir)).nanoseconds == initial_time2.nanoseconds) {
+ const initial_time2 = try testGetCurrentFileTimestamp(io, tmp.dir);
+ while ((try testGetCurrentFileTimestamp(io, tmp.dir)).nanoseconds == initial_time2.nanoseconds) {
try std.Io.Clock.Duration.sleep(.{ .clock = .boot, .raw = .fromNanoseconds(1) }, io);
}
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" {
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig
index 8453bc4c81..fbce1cd000 100644
--- a/lib/std/Thread.zig
+++ b/lib/std/Thread.zig
@@ -211,7 +211,7 @@ pub fn setName(self: Thread, io: Io, name: []const u8) SetNameError!void {
const file = try Io.Dir.cwd().openFile(io, path, .{ .mode = .write_only });
defer file.close(io);
- try file.writeAll(name);
+ try file.writeStreamingAll(io, name);
return;
},
.windows => {
@@ -1676,14 +1676,14 @@ const LinuxThreadImpl = struct {
}
};
-fn testThreadName(thread: *Thread) !void {
+fn testThreadName(io: Io, thread: *Thread) !void {
const testCases = &[_][]const u8{
"mythread",
"b" ** max_name_len,
};
inline for (testCases) |tc| {
- try thread.setName(tc);
+ try thread.setName(io, tc);
var name_buffer: [max_name_len:0]u8 = undefined;
@@ -1698,6 +1698,8 @@ fn testThreadName(thread: *Thread) !void {
test "setName, getName" {
if (builtin.single_threaded) return error.SkipZigTest;
+ const io = testing.io;
+
const Context = struct {
start_wait_event: ResetEvent = .unset,
test_done_event: ResetEvent = .unset,
@@ -1711,11 +1713,11 @@ test "setName, getName" {
ctx.start_wait_event.wait();
switch (native_os) {
- .windows => testThreadName(&ctx.thread) catch |err| switch (err) {
+ .windows => testThreadName(io, &ctx.thread) catch |err| switch (err) {
error.Unsupported => return error.SkipZigTest,
else => return err,
},
- else => try testThreadName(&ctx.thread),
+ else => try testThreadName(io, &ctx.thread),
}
// Signal our test is done
@@ -1735,14 +1737,14 @@ test "setName, getName" {
switch (native_os) {
.driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
- const res = thread.setName("foobar");
+ const res = thread.setName(io, "foobar");
try std.testing.expectError(error.Unsupported, res);
},
- .windows => testThreadName(&thread) catch |err| switch (err) {
+ .windows => testThreadName(io, &thread) catch |err| switch (err) {
error.Unsupported => return error.SkipZigTest,
else => return err,
},
- else => try testThreadName(&thread),
+ else => try testThreadName(io, &thread),
}
context.thread_done_event.set();
diff --git a/lib/std/debug.zig b/lib/std/debug.zig
index 3850ec3e8c..cef4233495 100644
--- a/lib/std/debug.zig
+++ b/lib/std/debug.zig
@@ -384,12 +384,13 @@ pub fn dumpHexFallible(t: Io.Terminal, bytes: []const u8) !void {
}
test dumpHexFallible {
+ const gpa = testing.allocator;
const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 };
- var aw: Writer.Allocating = .init(testing.allocator);
+ var aw: Writer.Allocating = .init(gpa);
defer aw.deinit();
- try dumpHexFallible(&aw.writer, .no_color, bytes);
- const expected = try std.fmt.allocPrint(testing.allocator,
+ try dumpHexFallible(.{ .writer = &aw.writer, .mode = .no_color }, bytes);
+ const expected = try std.fmt.allocPrint(gpa,
\\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........
\\{x:0>[2]} 01 12 13 ...
\\
@@ -398,7 +399,7 @@ test dumpHexFallible {
@intFromPtr(bytes.ptr) + 16,
@sizeOf(usize) * 2,
});
- defer testing.allocator.free(expected);
+ defer gpa.free(expected);
try testing.expectEqualStrings(expected, aw.written());
}