Fix issue with writeVtk and add file writing test
This commit is contained in:
parent
9029a9e580
commit
ba50c740a6
1 changed files with 34 additions and 2 deletions
|
|
@ -125,8 +125,8 @@ pub fn writeVtk(allocator: std.mem.Allocator, io: std.Io, mesh: *Mesh, file_path
|
||||||
const file = try cwd.openFile(io, file_path, .{ .mode = .read_write });
|
const file = try cwd.openFile(io, file_path, .{ .mode = .read_write });
|
||||||
defer file.close(io);
|
defer file.close(io);
|
||||||
|
|
||||||
const buf: [266]u8 = undefined;
|
var buf: [256]u8 = undefined;
|
||||||
const writer = file.writer(io, buf);
|
var writer = file.writer(io, &buf);
|
||||||
try writer.interface.writeAll(out_string);
|
try writer.interface.writeAll(out_string);
|
||||||
try writer.interface.flush();
|
try writer.interface.flush();
|
||||||
return;
|
return;
|
||||||
|
|
@ -364,3 +364,35 @@ test "vtk string is written correctly" {
|
||||||
|
|
||||||
try std.testing.expect(std.mem.eql(u8, vtk_string, expected));
|
try std.testing.expect(std.mem.eql(u8, vtk_string, expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "writeVtk writes the correct file contents" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const io = std.testing.io;
|
||||||
|
|
||||||
|
var mesh = try createDummyMesh(allocator);
|
||||||
|
defer mesh.deinit(allocator);
|
||||||
|
|
||||||
|
const file_path = "/tmp/zig_write_vtk_test.vtk";
|
||||||
|
// writeVtk opens the file in read_write mode, so it must exist first.
|
||||||
|
{
|
||||||
|
const cwd = std.Io.Dir.cwd();
|
||||||
|
const file = try cwd.createFile(io, file_path, .{});
|
||||||
|
defer file.close(io);
|
||||||
|
}
|
||||||
|
|
||||||
|
try writeVtk(allocator, io, &mesh, file_path);
|
||||||
|
|
||||||
|
// Read the file back and compare it to the expected VTK string.
|
||||||
|
const cwd = std.Io.Dir.cwd();
|
||||||
|
const file = try cwd.openFile(io, file_path, .{ .mode = .read_only });
|
||||||
|
defer file.close(io);
|
||||||
|
|
||||||
|
var reader = file.reader(io, &.{});
|
||||||
|
const contents = try reader.interface.allocRemaining(allocator, .unlimited);
|
||||||
|
defer allocator.free(contents);
|
||||||
|
|
||||||
|
const expected = try createVtkString(allocator, &mesh);
|
||||||
|
defer allocator.free(expected);
|
||||||
|
|
||||||
|
try std.testing.expect(std.mem.eql(u8, contents, expected));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue