From 9dbbd77fe92435e810bf1da88ba964c1b6852823 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 29 Jan 2024 01:20:49 -0800 Subject: [PATCH] add more tests --- src/expectSimilarType.zig | 14 ++++++++++++++ src/hashFile.zig | 1 - src/joinPartial.zig | 21 +++++++++++++++++++++ src/readBytes.zig | 16 ++++++++++++++-- src/readBytesAlloc.zig | 1 - src/readExpected.zig | 16 +++++++++++++++- 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/expectSimilarType.zig b/src/expectSimilarType.zig index c73841d40947bdd45c9ac9106e052f14d2d325b0..80c99e4424064bac04e5321a071aa78bcc57bf9c 100644 --- a/src/expectSimilarType.zig +++ b/src/expectSimilarType.zig @@ -74,3 +74,17 @@ test { struct { a: u32, b: u8, c: u16 }, ); } + +test { + try expectSimilarType( + union(enum) { a: u32, b: u8, c: u16 }, + union(enum) { a: u32, b: u8, c: u16 }, + ); +} + +test { + try expectSimilarType( + enum { a, b, c, d }, + enum { a, b, c, d }, + ); +} diff --git a/src/hashFile.zig b/src/hashFile.zig index 3ad16eb15d5fa36a5dc4992e231452e3b9d81aa6..acb4461f7558d28ab9e29c7195d07b983359ffcb 100644 --- a/src/hashFile.zig +++ b/src/hashFile.zig @@ -2,7 +2,6 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); const pipe = extras.pipe; -const rawInt = extras.rawInt; pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 { const file = try dir.openFile(sub_path, .{}); diff --git a/src/joinPartial.zig b/src/joinPartial.zig index b0b7fe26d2784ed135a11479f2cd1ec544773d4b..d9475b1ec06454ba26e19aabc807f8196ae2a32d 100644 --- a/src/joinPartial.zig +++ b/src/joinPartial.zig @@ -1,6 +1,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); +const Partial = extras.Partial; pub fn joinPartial(comptime P: type, a: P, b: P) P { var temp = a; @@ -9,3 +10,23 @@ pub fn joinPartial(comptime P: type, a: P, b: P) P { } return temp; } + +test { + const S = struct { + a: u8 = 4, + b: u8 = 7, + c: u8 = 1, + }; + try std.testing.expect(std.meta.eql( + joinPartial( + Partial(S), + .{ .a = 5 }, + .{ .b = 9 }, + ), + .{ + .a = 5, + .b = 9, + .c = 1, + }, + )); +} diff --git a/src/readBytes.zig b/src/readBytes.zig index ac6d3f5c4f597fe5e4488311c868228bb1c114e5..39268aa07f43b03cd2b0f7177ac8d98667333049 100644 --- a/src/readBytes.zig +++ b/src/readBytes.zig @@ -5,10 +5,22 @@ const assert = std.debug.assert; pub fn readBytes(reader: anytype, comptime len: usize) ![len]u8 { var bytes: [len]u8 = undefined; - assert(try reader.readAll(&bytes) == len); + if (try reader.readAll(&bytes) != len) return error.EndOfStream; return bytes; } test { - std.testing.refAllDecls(@This()); + const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + var fba = std.io.fixedBufferStream(&array); + const reader = fba.reader(); + const res = try readBytes(reader, 2); + try std.testing.expect(res.len == 2); + try std.testing.expect(std.mem.eql(u8, &res, &.{ 9, 8 })); +} + +test { + const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + var fba = std.io.fixedBufferStream(&array); + const reader = fba.reader(); + try std.testing.expect(readBytes(reader, 13) == error.EndOfStream); } diff --git a/src/readBytesAlloc.zig b/src/readBytesAlloc.zig index 65b903f1240a2cf8cfcb8345b35cf62733583c8c..bafd005b85fb6c93691545cbecaaff94565e563b 100644 --- a/src/readBytesAlloc.zig +++ b/src/readBytesAlloc.zig @@ -4,7 +4,6 @@ const extras = @import("./lib.zig"); pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 { var list = try std.ArrayListUnmanaged(u8).initCapacity(alloc, len); - try list.ensureTotalCapacityPrecise(alloc, len); errdefer list.deinit(alloc); list.appendNTimesAssumeCapacity(0, len); try reader.readNoEof(list.items[0..len]); diff --git a/src/readExpected.zig b/src/readExpected.zig index 959a106ea5c6487c1d7fadd23d1b9597455bfc4a..3d221c9558f047a7bf006c56a6034f3e5966da55 100644 --- a/src/readExpected.zig +++ b/src/readExpected.zig @@ -13,5 +13,19 @@ pub fn readExpected(reader: anytype, expected: []const u8) !bool { } test { - std.testing.refAllDecls(@This()); + const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + var fba = std.io.fixedBufferStream(&array); + try std.testing.expect(try readExpected(fba.reader(), &.{ 9, 8, 7, 6 })); +} + +test { + const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + var fba = std.io.fixedBufferStream(&array); + try std.testing.expect(!try readExpected(fba.reader(), &.{ 1, 2, 3 })); +} + +test { + const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + var fba = std.io.fixedBufferStream(&array); + try std.testing.expect(try readExpected(fba.reader(), &.{})); } -- 2.54.0