authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-23 11:16:38 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-23 11:16:38 -08:00
loga605a3ea057212f0e1cd825d0e18f78ae8ef8a2b
tree1e41cf38a984edfcc1969e642f797ec04983a3b3
parent8ac9d9061a70f1b0e234daa918685cdf49bd5c4a

add some tests


5 files changed, 71 insertions(+), 0 deletions(-)

src/matchesAll.zig+18
...@@ -10,3 +10,21 @@ pub fn matchesAll(comptime T: type, haystack: []const T, comptime needle: fn (T)...@@ -10,3 +10,21 @@ pub fn matchesAll(comptime T: type, haystack: []const T, comptime needle: fn (T)
10 }10 }
11 return true;11 return true;
12}12}
13
14const S = struct {
15 fn needle(item: u8) bool {
16 return item == 4;
17 }
18};
19test {
20 try std.testing.expect(matchesAll(u8, &.{ 4, 4, 4, 4, 4, 4 }, S.needle));
21}
22test {
23 try std.testing.expect(!matchesAll(u8, &.{ 4, 4, 4, 3, 4, 4 }, S.needle));
24}
25test {
26 try std.testing.expect(!matchesAll(u8, &.{ 0, 1, 2, 3, 4, 5 }, S.needle));
27}
28test {
29 try std.testing.expect(!matchesAll(u8, &.{ 5, 6, 7, 8, 9, 0 }, S.needle));
30}
src/matchesAny.zig+18
...@@ -10,3 +10,21 @@ pub fn matchesAny(comptime T: type, haystack: []const T, comptime needle: fn (T)...@@ -10,3 +10,21 @@ pub fn matchesAny(comptime T: type, haystack: []const T, comptime needle: fn (T)
10 }10 }
11 return false;11 return false;
12}12}
13
14test {
15 const S = struct {
16 fn needle(item: u8) bool {
17 return item == 4;
18 }
19 };
20 try std.testing.expect(matchesAny(u8, &.{ 0, 1, 2, 3, 4, 5 }, S.needle));
21}
22
23test {
24 const S = struct {
25 fn needle(item: u8) bool {
26 return item == 7;
27 }
28 };
29 try std.testing.expect(!matchesAny(u8, &.{ 0, 1, 2, 3, 4, 5 }, S.needle));
30}
src/nullifyS.zig+12
...@@ -7,3 +7,15 @@ pub fn nullifyS(s: ?string) ?string {...@@ -7,3 +7,15 @@ pub fn nullifyS(s: ?string) ?string {
7 if (s.?.len == 0) return null;7 if (s.?.len == 0) return null;
8 return s.?;8 return s.?;
9}9}
10
11test {
12 try std.testing.expect(nullifyS(null) == null);
13}
14test {
15 try std.testing.expect(nullifyS("") == null);
16}
17test {
18 try std.testing.expect(nullifyS("a") != null);
19 try std.testing.expect(nullifyS("a").?.len == 1);
20 try std.testing.expect(nullifyS("a").?[0] == 'a');
21}
src/opslice.zig+13
...@@ -6,3 +6,16 @@ pub fn opslice(slice: anytype, index: usize) ?std.meta.Child(@TypeOf(slice)) {...@@ -6,3 +6,16 @@ pub fn opslice(slice: anytype, index: usize) ?std.meta.Child(@TypeOf(slice)) {
6 if (slice.len <= index) return null;6 if (slice.len <= index) return null;
7 return slice[index];7 return slice[index];
8}8}
9
10test {
11 const array = [_]u8{ 0, 1, 2, 3, 4, 5 };
12 const slice: []const u8 = &array;
13
14 try std.testing.expect(opslice(slice, 0) == 0);
15 try std.testing.expect(opslice(slice, 1) == 1);
16 try std.testing.expect(opslice(slice, 2) == 2);
17 try std.testing.expect(opslice(slice, 3) == 3);
18 try std.testing.expect(opslice(slice, 4) == 4);
19 try std.testing.expect(opslice(slice, 5) == 5);
20 try std.testing.expect(opslice(slice, 6) == null);
21}
src/sliceTo.zig+10
...@@ -8,3 +8,13 @@ pub fn sliceTo(comptime T: type, haystack: []const T, needle: T) []const T {...@@ -8,3 +8,13 @@ pub fn sliceTo(comptime T: type, haystack: []const T, needle: T) []const T {
8 }8 }
9 return haystack;9 return haystack;
10}10}
11
12test {
13 try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'd'), "abc"));
14}
15test {
16 try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'r'), "abcdefgh"));
17}
18test {
19 try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'a'), ""));
20}