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 {...@@ -9,3 +9,19 @@ pub fn asciiUpper(alloc: std.mem.Allocator, input: string) ![]u8 {
9 }9 }
10 return buf;10 return buf;
11}11}
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 @@...@@ -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 fileSize = extras.fileSize;
45
5pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 {6pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 {
6 var res: u64 = 0;7 var res: u64 = 0;
...@@ -8,8 +9,12 @@ pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 {...@@ -8,8 +9,12 @@ pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 {
8 var walk = try dir.walk(alloc);9 var walk = try dir.walk(alloc);
9 defer walk.deinit();10 defer walk.deinit();
10 while (try walk.next()) |entry| {11 while (try walk.next()) |entry| {
11 if (entry.kind != .File) continue;12 if (entry.kind != .file) continue;
12 res += try extras.fileSize(dir.dir, entry.path);13 res += try fileSize(dir.dir, entry.path);
13 }14 }
14 return res;15 return res;
15}16}
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 {...@@ -11,3 +11,7 @@ pub fn doesFileExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
11 defer file.close();11 defer file.close();
12 return true;12 return true;
13}13}
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 {...@@ -15,3 +15,7 @@ pub fn doesFolderExist(dir: ?std.fs.Dir, fpath: []const u8) !bool {
15 }15 }
16 return true;16 return true;
17}17}
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 {...@@ -14,3 +14,7 @@ pub fn fileList(alloc: std.mem.Allocator, dir: std.fs.IterableDir) ![]string {
14 }14 }
15 return list.toOwnedSlice();15 return list.toOwnedSlice();
16}16}
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 {...@@ -8,3 +8,7 @@ pub fn fileSize(dir: std.fs.Dir, sub_path: string) !u64 {
8 const s = try f.stat();8 const s = try f.stat();
9 return s.size;9 return s.size;
10}10}
11
12test {
13 std.testing.refAllDecls(@This());
14}
src/hashFile.zig+11-1
...@@ -1,16 +1,26 @@...@@ -1,16 +1,26 @@
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 pipe = extras.pipe;
5const rawInt = extras.rawInt;
46
5pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {7pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 {
6 const file = try dir.openFile(sub_path, .{});8 const file = try dir.openFile(sub_path, .{});
7 defer file.close();9 defer file.close();
8 var h = Algo.init(.{});10 var h = Algo.init(.{});
9 var out: [Algo.digest_length]u8 = undefined;11 var out: [Algo.digest_length]u8 = undefined;
10 try extras.pipe(file.reader(), h.writer());12 try pipe(file.reader(), h.writer());
11 h.final(&out);13 h.final(&out);
12 var res: [Algo.digest_length * 2]u8 = undefined;14 var res: [Algo.digest_length * 2]u8 = undefined;
13 var fbs = std.io.fixedBufferStream(&res);15 var fbs = std.io.fixedBufferStream(&res);
14 try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)});16 try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)});
15 return res;17 return res;
16}18}
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 {...@@ -8,3 +8,13 @@ pub fn pipe(reader_from: anytype, writer_to: anytype) !void {
8 defer fifo.deinit();8 defer fifo.deinit();
9 try fifo.pump(reader_from, writer_to);9 try fifo.pump(reader_from, writer_to);
10}10}
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 {...@@ -14,3 +14,13 @@ pub fn sliceToInt(comptime T: type, comptime E: type, slice: []const E) !T {
14 }14 }
15 return n;15 return n;
16}16}
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}