authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 01:20:49 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 01:20:49 -08:00
log9dbbd77fe92435e810bf1da88ba964c1b6852823
tree2a4e865f7f81f7e6041e0345443128b298c274fc
parent72d70b47cc7314371eb65c106a06b01f70b3abde

add more tests


6 files changed, 64 insertions(+), 5 deletions(-)

src/expectSimilarType.zig+14
......@@ -74,3 +74,17 @@ test {
7474 struct { a: u32, b: u8, c: u16 },
7575 );
7676}
77
78test {
79 try expectSimilarType(
80 union(enum) { a: u32, b: u8, c: u16 },
81 union(enum) { a: u32, b: u8, c: u16 },
82 );
83}
84
85test {
86 try expectSimilarType(
87 enum { a, b, c, d },
88 enum { a, b, c, d },
89 );
90}
src/hashFile.zig-1
......@@ -2,7 +2,6 @@ const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
44const pipe = extras.pipe;
5const rawInt = extras.rawInt;
65
76pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {
87 const file = try dir.openFile(sub_path, .{});
src/joinPartial.zig+21
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const Partial = extras.Partial;
45
56pub fn joinPartial(comptime P: type, a: P, b: P) P {
67 var temp = a;
......@@ -9,3 +10,23 @@ pub fn joinPartial(comptime P: type, a: P, b: P) P {
910 }
1011 return temp;
1112}
13
14test {
15 const S = struct {
16 a: u8 = 4,
17 b: u8 = 7,
18 c: u8 = 1,
19 };
20 try std.testing.expect(std.meta.eql(
21 joinPartial(
22 Partial(S),
23 .{ .a = 5 },
24 .{ .b = 9 },
25 ),
26 .{
27 .a = 5,
28 .b = 9,
29 .c = 1,
30 },
31 ));
32}
src/readBytes.zig+14-2
......@@ -5,10 +5,22 @@ const assert = std.debug.assert;
55
66pub fn readBytes(reader: anytype, comptime len: usize) ![len]u8 {
77 var bytes: [len]u8 = undefined;
8 assert(try reader.readAll(&bytes) == len);
8 if (try reader.readAll(&bytes) != len) return error.EndOfStream;
99 return bytes;
1010}
1111
1212test {
13 std.testing.refAllDecls(@This());
13 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
14 var fba = std.io.fixedBufferStream(&array);
15 const reader = fba.reader();
16 const res = try readBytes(reader, 2);
17 try std.testing.expect(res.len == 2);
18 try std.testing.expect(std.mem.eql(u8, &res, &.{ 9, 8 }));
19}
20
21test {
22 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
23 var fba = std.io.fixedBufferStream(&array);
24 const reader = fba.reader();
25 try std.testing.expect(readBytes(reader, 13) == error.EndOfStream);
1426}
src/readBytesAlloc.zig-1
......@@ -4,7 +4,6 @@ const extras = @import("./lib.zig");
44
55pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 {
66 var list = try std.ArrayListUnmanaged(u8).initCapacity(alloc, len);
7 try list.ensureTotalCapacityPrecise(alloc, len);
87 errdefer list.deinit(alloc);
98 list.appendNTimesAssumeCapacity(0, len);
109 try reader.readNoEof(list.items[0..len]);
src/readExpected.zig+15-1
......@@ -13,5 +13,19 @@ pub fn readExpected(reader: anytype, expected: []const u8) !bool {
1313}
1414
1515test {
16 std.testing.refAllDecls(@This());
16 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
17 var fba = std.io.fixedBufferStream(&array);
18 try std.testing.expect(try readExpected(fba.reader(), &.{ 9, 8, 7, 6 }));
19}
20
21test {
22 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
23 var fba = std.io.fixedBufferStream(&array);
24 try std.testing.expect(!try readExpected(fba.reader(), &.{ 1, 2, 3 }));
25}
26
27test {
28 const array = [_]u8{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
29 var fba = std.io.fixedBufferStream(&array);
30 try std.testing.expect(try readExpected(fba.reader(), &.{}));
1731}