| author | |
| committer | |
| log | 19ca05f53b4d9b03605ce434cf84d681e135aaa3 |
| tree | d686c9f839336ce5f4e4e28aa24b09d02152ea35 |
| parent | bdfc6f76c3054142539b43264886a44921b800f5 |
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 | 9 | } |
| 10 | 10 | return buf; |
| 11 | 11 | } |
| 12 | ||
| 13 | test { | |
| 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 | ||
| 21 | test { | |
| 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 | 1 | const std = @import("std"); |
| 2 | 2 | const string = []const u8; |
| 3 | 3 | const extras = @import("./lib.zig"); |
| 4 | const fileSize = extras.fileSize; | |
| 4 | 5 | |
| 5 | 6 | pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 { |
| 6 | 7 | var res: u64 = 0; |
| ... | ... | @@ -8,8 +9,12 @@ pub fn dirSize(alloc: std.mem.Allocator, dir: std.fs.IterableDir) !u64 { |
| 8 | 9 | var walk = try dir.walk(alloc); |
| 9 | 10 | defer walk.deinit(); |
| 10 | 11 | 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); | |
| 13 | 14 | } |
| 14 | 15 | return res; |
| 15 | 16 | } |
| 17 | ||
| 18 | test { | |
| 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 | 11 | defer file.close(); |
| 12 | 12 | return true; |
| 13 | 13 | } |
| 14 | ||
| 15 | test { | |
| 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 | 15 | } |
| 16 | 16 | return true; |
| 17 | 17 | } |
| 18 | ||
| 19 | test { | |
| 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 | 14 | } |
| 15 | 15 | return list.toOwnedSlice(); |
| 16 | 16 | } |
| 17 | ||
| 18 | test { | |
| 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 | 8 | const s = try f.stat(); |
| 9 | 9 | return s.size; |
| 10 | 10 | } |
| 11 | ||
| 12 | test { | |
| 13 | std.testing.refAllDecls(@This()); | |
| 14 | } |
src/hashFile.zig+11-1| ... | ... | @@ -1,16 +1,26 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const string = []const u8; |
| 3 | 3 | const extras = @import("./lib.zig"); |
| 4 | const pipe = extras.pipe; | |
| 5 | const rawInt = extras.rawInt; | |
| 4 | 6 | |
| 5 | 7 | pub fn hashFile(dir: std.fs.Dir, sub_path: string, comptime Algo: type) ![Algo.digest_length * 2]u8 { |
| 6 | 8 | const file = try dir.openFile(sub_path, .{}); |
| 7 | 9 | defer file.close(); |
| 8 | 10 | var h = Algo.init(.{}); |
| 9 | 11 | var out: [Algo.digest_length]u8 = undefined; |
| 10 | try extras.pipe(file.reader(), h.writer()); | |
| 12 | try pipe(file.reader(), h.writer()); | |
| 11 | 13 | h.final(&out); |
| 12 | 14 | var res: [Algo.digest_length * 2]u8 = undefined; |
| 13 | 15 | var fbs = std.io.fixedBufferStream(&res); |
| 14 | 16 | try std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&out)}); |
| 15 | 17 | return res; |
| 16 | 18 | } |
| 19 | ||
| 20 | test { | |
| 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 | 8 | defer fifo.deinit(); |
| 9 | 9 | try fifo.pump(reader_from, writer_to); |
| 10 | 10 | } |
| 11 | ||
| 12 | test { | |
| 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 | 14 | } |
| 15 | 15 | return n; |
| 16 | 16 | } |
| 17 | ||
| 18 | test { | |
| 19 | try std.testing.expect(try sliceToInt(u32, u4, &.{ 0x4, 0xe, 0x5, 0xa, 0x7, 0xd, 0xa, 0x9 }) == 0x4e5a7da9); | |
| 20 | } | |
| 21 | test { | |
| 22 | try std.testing.expect(try sliceToInt(u30, u3, &.{ 0b010, 0b011, 0b100, 0b101, 0b101, 0b001, 0b111, 0b101, 0b101, 0b010 }) == 0b010011100101101001111101101010); | |
| 23 | } | |
| 24 | test { | |
| 25 | try std.testing.expect(try sliceToInt(u30, u5, &.{ 0b01001, 0b11001, 0b01101, 0b00111, 0b11011, 0b01010 }) == 0b010011100101101001111101101010); | |
| 26 | } |