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 {...@@ -74,3 +74,17 @@ test {
74 struct { a: u32, b: u8, c: u16 },74 struct { a: u32, b: u8, c: u16 },
75 );75 );
76}76}
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");...@@ -2,7 +2,6 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const pipe = extras.pipe;4const pipe = extras.pipe;
5const rawInt = extras.rawInt;
65
7pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {6pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {
8 const file = try dir.openFile(sub_path, .{});7 const file = try dir.openFile(sub_path, .{});
src/joinPartial.zig+21
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const Partial = extras.Partial;
45
5pub fn joinPartial(comptime P: type, a: P, b: P) P {6pub fn joinPartial(comptime P: type, a: P, b: P) P {
6 var temp = a;7 var temp = a;
...@@ -9,3 +10,23 @@ pub fn joinPartial(comptime P: type, a: P, b: P) P {...@@ -9,3 +10,23 @@ pub fn joinPartial(comptime P: type, a: P, b: P) P {
9 }10 }
10 return temp;11 return temp;
11}12}
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;...@@ -5,10 +5,22 @@ const assert = std.debug.assert;
55
6pub fn readBytes(reader: anytype, comptime len: usize) ![len]u8 {6pub fn readBytes(reader: anytype, comptime len: usize) ![len]u8 {
7 var bytes: [len]u8 = undefined;7 var bytes: [len]u8 = undefined;
8 assert(try reader.readAll(&bytes) == len);8 if (try reader.readAll(&bytes) != len) return error.EndOfStream;
9 return bytes;9 return bytes;
10}10}
1111
12test {12test {
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);
14}26}
src/readBytesAlloc.zig-1
...@@ -4,7 +4,6 @@ const extras = @import("./lib.zig");...@@ -4,7 +4,6 @@ const extras = @import("./lib.zig");
44
5pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 {5pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 {
6 var list = try std.ArrayListUnmanaged(u8).initCapacity(alloc, len);6 var list = try std.ArrayListUnmanaged(u8).initCapacity(alloc, len);
7 try list.ensureTotalCapacityPrecise(alloc, len);
8 errdefer list.deinit(alloc);7 errdefer list.deinit(alloc);
9 list.appendNTimesAssumeCapacity(0, len);8 list.appendNTimesAssumeCapacity(0, len);
10 try reader.readNoEof(list.items[0..len]);9 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 {...@@ -13,5 +13,19 @@ pub fn readExpected(reader: anytype, expected: []const u8) !bool {
13}13}
1414
15test {15test {
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(), &.{}));
17}31}