mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 02:24:33 +01:00
std: update remaining unit tests for std.Io API changes
This commit is contained in:
parent
21d0264c61
commit
7ce5ee2e92
6 changed files with 46 additions and 42 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
var fw = file.writerStreaming(io, &.{});
|
||||
|
||||
// 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);
|
||||
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" {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue