authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-28 02:24:18 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-28 02:24:18 -08:00
log19ca05f53b4d9b03605ce434cf84d681e135aaa3
treed686c9f839336ce5f4e4e28aa24b09d02152ea35
parentbdfc6f76c3054142539b43264886a44921b800f5

add more tests


9 files changed, 70 insertions(+), 3 deletions(-)

src/asciiUpper.zig+16
......@@ -9,3 +9,19 @@ pub fn asciiUpper(alloc: std.mem.Allocator, input: string) ![]u8 {
99 }
1010 return buf;
1111}
12
13test {
14 const allocator = std.testing.allocator;
15 const input = "hello";
16 const upper = try asciiUpper(allocator, input);
17 defer allocator.free(upper);
18 try std.testing.expect(std.mem.eql(u8, upper, "HELLO"));
19}
20
21test {
22 const allocator = std.testing.allocator;
23 const input = "bUtTer?!";
24 const upper = try asciiUpper(allocator, input);
25 defer allocator.free(upper);
26 try std.testing.expect(std.mem.eql(u8, upper, "BUTTER?!"));
27}
src/dirSize.zig+7-2
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const fileSize = extras.fileSize;
45
56pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 {
67 var res: u64 = 0;
......@@ -8,8 +9,12 @@ pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 {
89 var walk = try dir.walk(alloc);
910 defer walk.deinit();
1011 while (try walk.next()) |entry| {
11 if (entry.kind != .File) continue;
12 res += try extras.fileSize(dir.dir, entry.path);
12 if (entry.kind != .file) continue;
13 res += try fileSize(dir.dir, entry.path);
1314 }
1415 return res;
1516}
17
18test {
19 std.testing.refAllDecls(@This());
20}
src/doesFileExist.zig+4
......@@ -11,3 +11,7 @@ pub fn doesFileExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
1111 defer file.close();
1212 return true;
1313}
14
15test {
16 std.testing.refAllDecls(@This());
17}
src/doesFolderExist.zig+4
......@@ -15,3 +15,7 @@ pub fn doesFolderExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
1515 }
1616 return true;
1717}
18
19test {
20 std.testing.refAllDecls(@This());
21}
src/fileList.zig+4
......@@ -14,3 +14,7 @@ pub fn fileList(alloc: std.mem.Allocator, dir: std.fs.IterableDir) ![]string {
1414 }
1515 return list.toOwnedSlice();
1616}
17
18test {
19 std.testing.refAllDecls(@This());
20}
src/fileSize.zig+4
......@@ -8,3 +8,7 @@ pub fn fileSize(dir: std.fs.Dir, sub_path: string) !u64 {
88 const s = try f.stat();
99 return s.size;
1010}
11
12test {
13 std.testing.refAllDecls(@This());
14}
src/hashFile.zig+11-1
......@@ -1,16 +1,26 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const pipe = extras.pipe;
5const rawInt = extras.rawInt;
46
57pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {
68 const file = try dir.openFile(sub_path, .{});
79 defer file.close();
810 var h = Algo.init(.{});
911 var out: [Algo.digest_length]u8 = undefined;
10 try extras.pipe(file.reader(), h.writer());
12 try pipe(file.reader(), h.writer());
1113 h.final(&out);
1214 var res: [Algo.digest_length * 2]u8 = undefined;
1315 var fbs = std.io.fixedBufferStream(&res);
1416 try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)});
1517 return res;
1618}
19
20test {
21 const tdir = std.testing.tmpDir(.{}).dir;
22 try tdir.writeFile("yo.txt", "hello");
23 const A = std.crypto.hash.sha2.Sha256;
24 const expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".*;
25 try std.testing.expect(std.mem.eql(u8, &try hashFile(tdir, "yo.txt", A), &expected));
26}
src/pipe.zig+10
......@@ -8,3 +8,13 @@ pub fn pipe(reader_from: anytype, writer_to: anytype) !void {
88 defer fifo.deinit();
99 try fifo.pump(reader_from, writer_to);
1010}
11
12test {
13 const bytes = "abcdefghijklmnopqrstuvwxyz".*;
14 var fba = std.io.fixedBufferStream(&bytes);
15 const allocator = std.testing.allocator;
16 var list = std.ArrayList(u8).init(allocator);
17 defer list.deinit();
18 try pipe(fba.reader(), list.writer());
19 try std.testing.expect(std.mem.eql(u8, &bytes, list.items));
20}
src/sliceToInt.zig+10
......@@ -14,3 +14,13 @@ pub fn sliceToInt(comptime T: type, comptime E: type, slice: []const E) !T {
1414 }
1515 return n;
1616}
17
18test {
19 try std.testing.expect(try sliceToInt(u32, u4, &.{ 0x4, 0xe, 0x5, 0xa, 0x7, 0xd, 0xa, 0x9 }) == 0x4e5a7da9);
20}
21test {
22 try std.testing.expect(try sliceToInt(u30, u3, &.{ 0b010, 0b011, 0b100, 0b101, 0b101, 0b001, 0b111, 0b101, 0b101, 0b010 }) == 0b010011100101101001111101101010);
23}
24test {
25 try std.testing.expect(try sliceToInt(u30, u5, &.{ 0b01001, 0b11001, 0b01101, 0b00111, 0b11011, 0b01010 }) == 0b010011100101101001111101101010);
26}