diff --git a/zigsdof/src/stl.zig b/zigsdof/src/stl.zig index 85f2e13..f73f8df 100644 --- a/zigsdof/src/stl.zig +++ b/zigsdof/src/stl.zig @@ -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 }); defer file.close(io); - const buf: [266]u8 = undefined; - const writer = file.writer(io, buf); + var buf: [256]u8 = undefined; + var writer = file.writer(io, &buf); try writer.interface.writeAll(out_string); try writer.interface.flush(); return; @@ -364,3 +364,35 @@ test "vtk string is written correctly" { 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)); +}