| author | |
| committer | |
| log | a605a3ea057212f0e1cd825d0e18f78ae8ef8a2b |
| tree | 1e41cf38a984edfcc1969e642f797ec04983a3b3 |
| parent | 8ac9d9061a70f1b0e234daa918685cdf49bd5c4a |
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 | 10 | } |
| 11 | 11 | return true; |
| 12 | 12 | } |
| 13 | ||
| 14 | const S = struct { | |
| 15 | fn needle(item: u8) bool { | |
| 16 | return item == 4; | |
| 17 | } | |
| 18 | }; | |
| 19 | test { | |
| 20 | try std.testing.expect(matchesAll(u8, &.{ 4, 4, 4, 4, 4, 4 }, S.needle)); | |
| 21 | } | |
| 22 | test { | |
| 23 | try std.testing.expect(!matchesAll(u8, &.{ 4, 4, 4, 3, 4, 4 }, S.needle)); | |
| 24 | } | |
| 25 | test { | |
| 26 | try std.testing.expect(!matchesAll(u8, &.{ 0, 1, 2, 3, 4, 5 }, S.needle)); | |
| 27 | } | |
| 28 | test { | |
| 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 | 10 | } |
| 11 | 11 | return false; |
| 12 | 12 | } |
| 13 | ||
| 14 | test { | |
| 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 | ||
| 23 | test { | |
| 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 | 7 | if (s.?.len == 0) return null; |
| 8 | 8 | return s.?; |
| 9 | 9 | } |
| 10 | ||
| 11 | test { | |
| 12 | try std.testing.expect(nullifyS(null) == null); | |
| 13 | } | |
| 14 | test { | |
| 15 | try std.testing.expect(nullifyS("") == null); | |
| 16 | } | |
| 17 | test { | |
| 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 | 6 | if (slice.len <= index) return null; |
| 7 | 7 | return slice[index]; |
| 8 | 8 | } |
| 9 | ||
| 10 | test { | |
| 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 | 8 | } |
| 9 | 9 | return haystack; |
| 10 | 10 | } |
| 11 | ||
| 12 | test { | |
| 13 | try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'd'), "abc")); | |
| 14 | } | |
| 15 | test { | |
| 16 | try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'r'), "abcdefgh")); | |
| 17 | } | |
| 18 | test { | |
| 19 | try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'a'), "")); | |
| 20 | } |