use rustpython_sre_engine::{Request, State, StrDrive};

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};

struct Pattern {
    pattern: &'static str,
    code: &'static [u32],
}

impl Pattern {
    fn state<'a, S: StrDrive>(&self, string: S) -> (Request<'a, S>, State) {
        self.state_range(string, 0..usize::MAX)
    }

    fn state_range<'a, S: StrDrive>(
        &self,
        string: S,
        range: core::ops::Range<usize>,
    ) -> (Request<'a, S>, State) {
        let req = Request::new(string, range.start, range.end, self.code, false);
        let state = State::default();
        (req, state)
    }
}

fn basic(c: &mut Criterion) {
    // # test common prefix
    // pattern p1 = re.compile('Python|Perl') # , 'Perl'),    # Alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p1 = Pattern { pattern: "Python|Perl", code: &[14, 8, 1, 4, 6, 1, 1, 80, 0, 16, 80, 7, 13, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 11, 9, 16, 101, 16, 114, 16, 108, 15, 2, 0, 1] };
    // END GENERATED
    // pattern p2 = re.compile('(Python|Perl)') #, 'Perl'),  # Grouped alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p2 = Pattern { pattern: "(Python|Perl)", code: &[14, 8, 1, 4, 6, 1, 0, 80, 0, 17, 0, 16, 80, 7, 13, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 11, 9, 16, 101, 16, 114, 16, 108, 15, 2, 0, 17, 1, 1] };
    // END GENERATED
    // pattern p3 = re.compile('Python|Perl|Tcl') #, 'Perl'),        # Alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p3 = Pattern { pattern: "Python|Perl|Tcl", code: &[14, 9, 4, 3, 6, 16, 80, 16, 84, 0, 7, 15, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 22, 11, 16, 80, 16, 101, 16, 114, 16, 108, 15, 11, 9, 16, 84, 16, 99, 16, 108, 15, 2, 0, 1] };
    // END GENERATED
    // pattern p4 = re.compile('(Python|Perl|Tcl)') #, 'Perl'),      # Grouped alternation
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p4 = Pattern { pattern: "(Python|Perl|Tcl)", code: &[14, 9, 4, 3, 6, 16, 80, 16, 84, 0, 17, 0, 7, 15, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 22, 11, 16, 80, 16, 101, 16, 114, 16, 108, 15, 11, 9, 16, 84, 16, 99, 16, 108, 15, 2, 0, 17, 1, 1] };
    // END GENERATED
    // pattern p5 = re.compile('(Python)\\1') #, 'PythonPython'),    # Backreference
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p5 = Pattern { pattern: "(Python)\\1", code: &[14, 18, 1, 12, 12, 6, 0, 80, 121, 116, 104, 111, 110, 0, 0, 0, 0, 0, 0, 17, 0, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 17, 1, 11, 0, 1] };
    // END GENERATED
    // pattern p6 = re.compile('([0a-z][a-z0-9]*,)+') #, 'a5,b7,c9,'), # Disable the fast map optimization
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p6 = Pattern { pattern: "([0a-z][a-z0-9]*,)+", code: &[14, 4, 0, 2, 4294967295, 23, 31, 1, 4294967295, 17, 0, 13, 7, 16, 48, 22, 97, 122, 0, 24, 13, 0, 4294967295, 13, 8, 22, 97, 122, 22, 48, 57, 0, 1, 16, 44, 17, 1, 18, 1] };
    // END GENERATED
    // pattern p7 = re.compile('([a-z][a-z0-9]*,)+') #, 'a5,b7,c9,'), # A few sets
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p7 = Pattern { pattern: "([a-z][a-z0-9]*,)+", code: &[14, 4, 0, 2, 4294967295, 23, 29, 1, 4294967295, 17, 0, 13, 5, 22, 97, 122, 0, 24, 13, 0, 4294967295, 13, 8, 22, 97, 122, 22, 48, 57, 0, 1, 16, 44, 17, 1, 18, 1] };
    // END GENERATED
    // pattern p8 = re.compile('Python') #, 'Python'),               # Simple text literal
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p8 = Pattern { pattern: "Python", code: &[14, 18, 3, 6, 6, 6, 6, 80, 121, 116, 104, 111, 110, 0, 0, 0, 0, 0, 0, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 1] };
    // END GENERATED
    // pattern p9 = re.compile('.*Python') #, 'Python'),             # Bad text literal
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p9 = Pattern { pattern: ".*Python", code: &[14, 4, 0, 6, 4294967295, 24, 5, 0, 4294967295, 2, 1, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 1] };
    // END GENERATED
    // pattern p10 = re.compile('.*Python.*') #, 'Python'),           # Worse text literal
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p10 = Pattern { pattern: ".*Python.*", code: &[14, 4, 0, 6, 4294967295, 24, 5, 0, 4294967295, 2, 1, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 24, 5, 0, 4294967295, 2, 1, 1] };
    // END GENERATED
    // pattern p11 = re.compile('.*(Python)') #, 'Python'),           # Bad text literal with grouping
    // START GENERATED by generate_tests.py
    #[rustfmt::skip] let p11 = Pattern { pattern: ".*(Python)", code: &[14, 4, 0, 6, 4294967295, 24, 5, 0, 4294967295, 2, 1, 17, 0, 16, 80, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 17, 1, 1] };
    // END GENERATED

    let tests = [
        (p1, "Perl"),
        (p2, "Perl"),
        (p3, "Perl"),
        (p4, "Perl"),
        (p5, "PythonPython"),
        (p6, "a5,b7,c9,"),
        (p7, "a5,b7,c9,"),
        (p8, "Python"),
        (p9, "Python"),
        (p10, "Python"),
        (p11, "Python"),
    ];

    let mut group = c.benchmark_group("basic");

    for (p, s) in tests {
        group.bench_with_input(BenchmarkId::new(p.pattern, s), s, |b, s| {
            b.iter(|| {
                let (req, mut state) = p.state(s);
                assert!(state.search(req));
                let (req, mut state) = p.state(s);
                assert!(state.py_match(&req));
                let (mut req, mut state) = p.state(s);
                req.match_all = true;
                assert!(state.py_match(&req));
                let s2 = format!("{}{}{}", " ".repeat(10000), s, " ".repeat(10000));
                let (req, mut state) = p.state_range(s2.as_str(), 0..usize::MAX);
                assert!(state.search(req));
                let (req, mut state) = p.state_range(s2.as_str(), 10000..usize::MAX);
                assert!(state.py_match(&req));
                let (req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
                assert!(state.py_match(&req));
                let (mut req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
                req.match_all = true;
                assert!(state.py_match(&req));
            });
        });
    }
}

criterion_group!(benches, basic);

criterion_main!(benches);
