From 4be4e25e83833f84072ae9a3ecb0860c59abffe8 Mon Sep 17 00:00:00 2001 From: alberic89 Date: Wed, 14 Aug 2024 09:53:21 +0200 Subject: [PATCH 1/5] feat: add an `hasSize` function --- src/root.zig | 14 ++++++++++++++ src/unicode.zig | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/src/root.zig b/src/root.zig index 2740083..8f4612c 100644 --- a/src/root.zig +++ b/src/root.zig @@ -210,6 +210,15 @@ pub fn AlgorithmType( self.max_needle = max_needle; } + // Check is there is enough memory allocated + pub fn hasSize(self: *Self, max_haystack: usize, max_needle: usize) bool { + if (self.max_haystack < max_haystack or self.max_needle < max_needle) { + return false; + } else { + return true; + } + } + /// Compute matching score pub fn score( self: *Self, @@ -649,6 +658,11 @@ pub const Ascii = struct { pub fn resize(self: *Ascii, max_haystack: usize, max_needle: usize) !void { try self.alg.resize(max_haystack, max_needle); } + + // Check is there is enough memory allocated + pub fn hasSize(self: *Ascii, max_haystack: usize, max_needle: usize) bool { + return self.alg.hasSize(max_haystack, max_needle); + } }; fn doTestScore(alg: *Ascii, haystack: []const u8, needle: []const u8, comptime score: i32) !void { diff --git a/src/unicode.zig b/src/unicode.zig index 8afe232..4877698 100644 --- a/src/unicode.zig +++ b/src/unicode.zig @@ -219,6 +219,11 @@ pub const Unicode = struct { pub fn resize(self: *Unicode, max_haystack: usize, max_needle: usize) !void { try self.alg.resize(max_haystack, max_needle); } + + // Check is there is enough memory allocated + pub fn hasSize(self: *Unicode, max_haystack: usize, max_needle: usize) bool { + return self.alg.hasSize(max_haystack, max_needle); + } }; fn doTestScoreUnicode( From a4587b9f5e69caed7d2ca094273252f3643175ec Mon Sep 17 00:00:00 2001 From: alberic89 Date: Wed, 14 Aug 2024 13:03:26 +0200 Subject: [PATCH 2/5] fix: simplify `hasSize` --- src/root.zig | 18 ++++++++---------- src/unicode.zig | 5 +++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/root.zig b/src/root.zig index 8f4612c..ccf1ed9 100644 --- a/src/root.zig +++ b/src/root.zig @@ -210,13 +210,10 @@ pub fn AlgorithmType( self.max_needle = max_needle; } - // Check is there is enough memory allocated - pub fn hasSize(self: *Self, max_haystack: usize, max_needle: usize) bool { - if (self.max_haystack < max_haystack or self.max_needle < max_needle) { - return false; - } else { - return true; - } + // Check if buffers have sufficient memory for a given haystack and + // needle length. + pub fn hasSize(self: *const Self, max_haystack: usize, max_needle: usize) bool { + return (max_haystack <= self.max_haystack) and (max_needle <= self.max_needle); } /// Compute matching score @@ -262,7 +259,7 @@ pub fn AlgorithmType( }; std.debug.assert(haystack.len <= self.maximumHaystackLen()); - std.debug.assert(needle.len < self.maximumNeedleLen()); + std.debug.assert(needle.len <= self.maximumNeedleLen()); const rows = needle.len; const cols = haystack.len; @@ -659,8 +656,9 @@ pub const Ascii = struct { try self.alg.resize(max_haystack, max_needle); } - // Check is there is enough memory allocated - pub fn hasSize(self: *Ascii, max_haystack: usize, max_needle: usize) bool { + // Check if buffers have sufficient memory for a given haystack and + // needle length. + pub fn hasSize(self: *const Ascii, max_haystack: usize, max_needle: usize) bool { return self.alg.hasSize(max_haystack, max_needle); } }; diff --git a/src/unicode.zig b/src/unicode.zig index 4877698..b5ebc1f 100644 --- a/src/unicode.zig +++ b/src/unicode.zig @@ -220,8 +220,9 @@ pub const Unicode = struct { try self.alg.resize(max_haystack, max_needle); } - // Check is there is enough memory allocated - pub fn hasSize(self: *Unicode, max_haystack: usize, max_needle: usize) bool { + // Check if buffers have sufficient memory for a given haystack and + // needle length. + pub fn hasSize(self: *const Unicode, max_haystack: usize, max_needle: usize) bool { return self.alg.hasSize(max_haystack, max_needle); } }; From e70e541736d76f949230923a40ae7f5f869842ae Mon Sep 17 00:00:00 2001 From: alberic89 Date: Wed, 14 Aug 2024 13:04:10 +0200 Subject: [PATCH 3/5] chore: add test for `resize` and `hasSize` --- src/root.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/root.zig b/src/root.zig index ccf1ed9..37f3dec 100644 --- a/src/root.zig +++ b/src/root.zig @@ -861,3 +861,35 @@ test "traceback" { try doTestTraceback(&alg, "A" ++ "a" ** 20 ++ "B", "AB", &.{ 0, 21 }); try doTestTraceback(&alg, "./src/main.zig", "main", &.{ 6, 7, 8, 9 }); } + +test "resize" { + const o = Ascii.Scores{}; + var alg = try Ascii.init( + std.testing.allocator, + 16, + 4, + .{}, + ); + defer alg.deinit(); + + try std.testing.expect(alg.hasSize(2, 2)); + try alg.resize(2, 2); + try std.testing.expect(alg.hasSize(2, 2)); + try doTestScore(&alg, "ab", "ab", o.score_match * 2 + + (o.bonus_head * o.bonus_first_character_multiplier) + + o.bonus_consecutive); + + try std.testing.expect(!alg.hasSize(8, 2)); + try alg.resize(8, 2); + try std.testing.expect(alg.hasSize(8, 2)); + try doTestScore(&alg, "ab" ** 4, "ab", o.score_match * 2 + + (o.bonus_head * o.bonus_first_character_multiplier) + + o.bonus_consecutive); + + try std.testing.expect(alg.hasSize(3, 2)); + try alg.resize("abc".len, "ab".len); + try std.testing.expect(alg.hasSize(3, 2)); + try doTestScore(&alg, "abc", "ab", o.score_match * 2 + + (o.bonus_head * o.bonus_first_character_multiplier) + + o.bonus_consecutive); +} From 368af07c87047e87e0d42b6933fd8609014959a5 Mon Sep 17 00:00:00 2001 From: alberic89 Date: Wed, 14 Aug 2024 18:12:41 +0200 Subject: [PATCH 4/5] test: add more test case for resize Co-authored-by: Fergus Baker --- src/root.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/root.zig b/src/root.zig index 37f3dec..79b9094 100644 --- a/src/root.zig +++ b/src/root.zig @@ -882,6 +882,10 @@ test "resize" { try std.testing.expect(!alg.hasSize(8, 2)); try alg.resize(8, 2); try std.testing.expect(alg.hasSize(8, 2)); + // larger sizes should fail + try std.testing.expect(!alg.hasSize(8, 3)); + // smaller sizes should be fine + try std.testing.expect(alg.hasSize(2, 1)); try doTestScore(&alg, "ab" ** 4, "ab", o.score_match * 2 + (o.bonus_head * o.bonus_first_character_multiplier) + o.bonus_consecutive); From af638595435a3e224248f7ad0666f4c81f0a8543 Mon Sep 17 00:00:00 2001 From: alberic89 Date: Wed, 14 Aug 2024 18:59:32 +0200 Subject: [PATCH 5/5] test: resize to 0 --- src/root.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/root.zig b/src/root.zig index 79b9094..7af8f8a 100644 --- a/src/root.zig +++ b/src/root.zig @@ -896,4 +896,7 @@ test "resize" { try doTestScore(&alg, "abc", "ab", o.score_match * 2 + (o.bonus_head * o.bonus_first_character_multiplier) + o.bonus_consecutive); + + // normally works + try alg.resize(0, 0); }