]> git.proxmox.com Git - rustc.git/blob - vendor/regex-automata-0.2.0/tests/nfa/thompson/pikevm/suite.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / vendor / regex-automata-0.2.0 / tests / nfa / thompson / pikevm / suite.rs
1 use regex_automata::{
2 nfa::thompson::{
3 self,
4 pikevm::{self, PikeVM},
5 },
6 MatchKind, SyntaxConfig,
7 };
8 use regex_syntax as syntax;
9
10 use regex_test::{
11 bstr::{BString, ByteSlice},
12 CompiledRegex, Match, MatchKind as TestMatchKind, RegexTest, RegexTests,
13 SearchKind as TestSearchKind, TestResult, TestRunner,
14 };
15
16 use crate::{suite, Result};
17
18 /// Tests the default configuration of the hybrid NFA/DFA.
19 #[test]
20 fn default() -> Result<()> {
21 let builder = PikeVM::builder();
22 TestRunner::new()?.test_iter(suite()?.iter(), compiler(builder)).assert();
23 Ok(())
24 }
25
26 fn compiler(
27 mut builder: pikevm::Builder,
28 ) -> impl FnMut(&RegexTest, &[BString]) -> Result<CompiledRegex> {
29 move |test, regexes| {
30 let regexes = regexes
31 .iter()
32 .map(|r| r.to_str().map(|s| s.to_string()))
33 .collect::<std::result::Result<Vec<String>, _>>()?;
34 if !configure_pikevm_builder(test, &mut builder) {
35 return Ok(CompiledRegex::skip());
36 }
37 let re = builder.build_many(&regexes)?;
38 let mut cache = re.create_cache();
39 Ok(CompiledRegex::compiled(move |test| -> Vec<TestResult> {
40 run_test(&re, &mut cache, test)
41 }))
42 }
43 }
44
45 fn run_test(
46 re: &PikeVM,
47 cache: &mut pikevm::Cache,
48 test: &RegexTest,
49 ) -> Vec<TestResult> {
50 // let is_match = if re.is_match(cache, test.input()) {
51 // TestResult::matched()
52 // } else {
53 // TestResult::no_match()
54 // };
55 // let is_match = is_match.name("is_match");
56
57 let find_matches = match test.search_kind() {
58 TestSearchKind::Earliest => {
59 TestResult::skip().name("find_earliest_iter")
60 }
61 TestSearchKind::Leftmost => {
62 let it = re
63 .find_leftmost_iter(cache, test.input())
64 .take(test.match_limit().unwrap_or(std::usize::MAX))
65 .map(|m| Match {
66 id: m.pattern().as_usize(),
67 start: m.start(),
68 end: m.end(),
69 });
70 TestResult::matches(it).name("find_leftmost_iter")
71 }
72 TestSearchKind::Overlapping => {
73 TestResult::skip().name("find_overlapping_iter")
74 }
75 };
76 // vec![is_match, find_matches]
77 vec![find_matches]
78 }
79
80 /// Configures the given regex builder with all relevant settings on the given
81 /// regex test.
82 ///
83 /// If the regex test has a setting that is unsupported, then this returns
84 /// false (implying the test should be skipped).
85 fn configure_pikevm_builder(
86 test: &RegexTest,
87 builder: &mut pikevm::Builder,
88 ) -> bool {
89 let pikevm_config =
90 PikeVM::config().anchored(test.anchored()).utf8(test.utf8());
91 builder
92 .configure(pikevm_config)
93 .syntax(config_syntax(test))
94 .thompson(config_thompson(test));
95 true
96 }
97
98 /// Configuration of a Thompson NFA compiler from a regex test.
99 fn config_thompson(test: &RegexTest) -> thompson::Config {
100 thompson::Config::new().utf8(test.utf8())
101 }
102
103 /// Configuration of the regex parser from a regex test.
104 fn config_syntax(test: &RegexTest) -> SyntaxConfig {
105 SyntaxConfig::new()
106 .case_insensitive(test.case_insensitive())
107 .unicode(test.unicode())
108 .utf8(test.utf8())
109 }