Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Dec 12, 2023
1 parent 0167f28 commit 79557ed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion examples/src/01-01.zig
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
const std = @import("std");
const fs = std.fs;
const print = std.debug.print;
const expectEqualStrings = std.testing.expectEqualStrings;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const file = try fs.cwd().openFile("build.zig", .{});
const file = try fs.cwd().openFile("tests/hello-world.txt", .{});
defer file.close();

const rdr = file.reader();
var line_no: usize = 0;
const expected = [_][]const u8{
"Hello Zig. 😁",
"",
"I love Zig very much!",
};
while (try rdr.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096)) |line| {
defer allocator.free(line);

try expectEqualStrings(expected[line_no], line);
line_no += 1;
print("{d}--{s}\n", .{ line_no, line });
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/02-01.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn main() !void {
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const file = try fs.cwd().openFile("build.zig", .{});
const file = try fs.cwd().openFile("tests/hello-world.txt", .{});
defer file.close();

const digest = try sha256_digest(allocator, file);
Expand Down
3 changes: 3 additions & 0 deletions examples/tests/hello-world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello Zig. 😁

I love Zig very much!

0 comments on commit 79557ed

Please sign in to comment.