Skip to content

Commit c5aca8a

Browse files
isolate test cases
1 parent 15a7ec2 commit c5aca8a

2 files changed

Lines changed: 52 additions & 34 deletions

File tree

src/boyer_moore.zig

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const Allocator = std.mem.Allocator;
33
const assert = std.debug.assert;
44
const testing = std.testing;
55

6+
const test_suites = @import("test_cases.zig").test_suites;
7+
68
pub fn StringFinder(comptime T: type) type {
79
assert(std.meta.trait.isIndexable(T));
810
const ElemType = std.meta.Elem(T);
@@ -128,41 +130,15 @@ pub fn StringFinder(comptime T: type) type {
128130
};
129131
}
130132

131-
test "empty pattern" {
132-
const allocator = testing.allocator;
133-
134-
var sf = try StringFinder([]const u8).init(allocator, "");
135-
defer sf.deinit();
136-
137-
testing.expectEqual(@as(?usize, 0), sf.next("zig"));
138-
testing.expectEqual(@as(?usize, 0), sf.next(""));
139-
testing.expectEqual(@as(?usize, 0), sf.next("a"));
140-
testing.expectEqual(@as(?usize, 0), sf.next("lang"));
141-
}
142-
143-
test "pattern with length 1" {
133+
test "boyer moore" {
144134
const allocator = testing.allocator;
145135

146-
var sf = try StringFinder([]const u8).init(allocator, "a");
147-
defer sf.deinit();
136+
for (test_suites) |suite| {
137+
var sf = try StringFinder([]const u8).init(allocator, suite.pattern);
138+
defer sf.deinit();
148139

149-
testing.expectEqual(@as(?usize, null), sf.next("zig"));
150-
testing.expectEqual(@as(?usize, null), sf.next(""));
151-
testing.expectEqual(@as(?usize, 0), sf.next("a"));
152-
testing.expectEqual(@as(?usize, 1), sf.next("lang"));
153-
}
154-
155-
test "test matching" {
156-
const allocator = testing.allocator;
157-
158-
var sf = try StringFinder([]const u8).init(allocator, "zig");
159-
defer sf.deinit();
160-
161-
testing.expectEqual(@as(?usize, 0), sf.next("zig"));
162-
testing.expectEqual(@as(?usize, 0), sf.next("ziglang"));
163-
testing.expectEqual(@as(?usize, 4), sf.next("langzig"));
164-
testing.expectEqual(@as(?usize, 4), sf.next("langziglang"));
165-
testing.expectEqual(@as(?usize, null), sf.next(""));
166-
testing.expectEqual(@as(?usize, null), sf.next("firefox"));
167-
testing.expectEqual(@as(?usize, 8), sf.next("abc abc ziglang"));
140+
for (suite.cases) |case| {
141+
testing.expectEqual(case.expected, sf.next(case.text));
142+
}
143+
}
168144
}

src/test_cases.zig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pub const Testcase = struct {
2+
text: []const u8,
3+
expected: ?usize,
4+
};
5+
6+
pub const Testsuite = struct {
7+
pattern: []const u8,
8+
cases: []const Testcase,
9+
};
10+
11+
pub const test_suites = [_]Testsuite{
12+
Testsuite{
13+
.pattern = "",
14+
.cases = &[_]Testcase{
15+
.{ .text = "zig", .expected = 0 },
16+
.{ .text = "", .expected = 0 },
17+
.{ .text = "a", .expected = 0 },
18+
.{ .text = "lang", .expected = 0 },
19+
},
20+
},
21+
Testsuite{
22+
.pattern = "a",
23+
.cases = &[_]Testcase{
24+
.{ .text = "zig", .expected = null },
25+
.{ .text = "", .expected = null },
26+
.{ .text = "a", .expected = 0 },
27+
.{ .text = "lang", .expected = 1 },
28+
},
29+
},
30+
Testsuite{
31+
.pattern = "zig",
32+
.cases = &[_]Testcase{
33+
.{ .text = "zig", .expected = 0 },
34+
.{ .text = "ziglang", .expected = 0 },
35+
.{ .text = "langzig", .expected = 4 },
36+
.{ .text = "langziglang", .expected = 4 },
37+
.{ .text = "", .expected = null },
38+
.{ .text = "firefox", .expected = null },
39+
.{ .text = "abc abc ziglang", .expected = 8 },
40+
},
41+
},
42+
};

0 commit comments

Comments
 (0)