Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #7

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 114 additions & 45 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,127 @@
use trie_match::trie_match;

#[test]
fn test_match() {
let text = "abb";
assert_eq!(
trie_match!(match text {
"abba" => 0,
"abb" => 1,
"ab" => 2,
"" => 3,
_ => 4,
}),
1,
);
fn test_only_wildcard() {
let f = |text| {
trie_match! {
match text {
_ => 4,
}
}
};
assert_eq!(f(""), 4);
assert_eq!(f("a"), 4);
assert_eq!(f("ab"), 4);
}

#[test]
fn test_match_empty() {
let text = "";
assert_eq!(
trie_match!(match text {
"abba" => 0,
"abb" => 1,
"ab" => 2,
"" => 3,
_ => 4,
}),
3,
);
fn test_prefix_patterns() {
// 0 -a-> 1 -b-> 2 -c-> * -d-> 3
let f = |text| {
trie_match! {
match text {
"" => 0,
"a" => 1,
"ab" => 2,
"abcd" => 3,
_ => 4,
}
}
};
assert_eq!(f(""), 0);
assert_eq!(f("a"), 1);
assert_eq!(f("ab"), 2);
assert_eq!(f("abc"), 4);
assert_eq!(f("abcd"), 3);
assert_eq!(f("b"), 4);
}

#[test]
fn test_match_wildcard() {
let text = "ba";
assert_eq!(
trie_match!(match text {
"abba" => 0,
"abb" => 1,
"ab" => 2,
"" => 3,
_ => 4,
}),
4,
);
fn test_longer_query() {
// * -a-> * -b-> 0
let f = |text| {
trie_match! {
match text {
"ab" => 0,
_ => 1,
}
}
};
assert_eq!(f("ab"), 0);
assert_eq!(f("abcdefg"), 1);
}

// Issue: https://github.com/daac-tools/trie-match/pull/4
#[test]
fn test_match_only_wildcard() {
let text = "ba";
assert_eq!(
trie_match!(match text {
_ => 4,
}),
4,
);
fn test_branch_root() {
// * -a-> 0
// \
// \-b-> 1
let f = |text| {
trie_match! {
match text {
"a" => 0,
"b" => 1,
_ => 2,
}
}
};
assert_eq!(f("a"), 0);
assert_eq!(f("b"), 1);
assert_eq!(f("c"), 2);
}

#[test]
fn test_branch_multiple_times() {
// /-e-> 5
// /
// * --a--> 0 --b--> * --c--> * --d--> 1
// \ \ \
// \-b-> 2 \-c-> 3 \-d-> * --e--> 4
let f = |text| {
trie_match! {
match text {
"a" => 0,
"abcd" => 1,
"b" => 2,
"ac" => 3,
"abde" => 4,
"abe" => 5,
_ => 6,
}
}
};
assert_eq!(f(""), 6);
assert_eq!(f("a"), 0);
assert_eq!(f("ab"), 6);
assert_eq!(f("abc"), 6);
assert_eq!(f("abcd"), 1);
assert_eq!(f("abd"), 6);
assert_eq!(f("abde"), 4);
assert_eq!(f("abe"), 5);
assert_eq!(f("ac"), 3);
assert_eq!(f("b"), 2);
assert_eq!(f("abcde"), 6);
assert_eq!(f("abdef"), 6);
assert_eq!(f("acd"), 6);
assert_eq!(f("ad"), 6);
assert_eq!(f("bc"), 6);
assert_eq!(f("c"), 6);
}

// This test confirms that the generator prevents base value conflictions.
#[test]
fn test_try_base_conflict() {
let f = |text| {
trie_match! {
match text {
// The following pattern adds multiple zeros into a base array in a normal
// double-array, but it is not allowed in a compact double-array.
"\u{1}\u{2}\u{3}" => 0,
_ => 1,
}
}
};
assert_eq!(f("\u{1}\u{2}\u{3}"), 0);
assert_eq!(f("\u{2}\u{3}"), 1);
assert_eq!(f("\u{3}"), 1);
}