//! Memoization behavior.
//!
//! The cache is process-wide and shared by every test in this binary. Tests run
//! in parallel by default, so these cases never assert an exact cache size.
//! They check observable behavior instead: a repeat call returns the same
//! string, and clearing then recomputing yields the same string.

use regex_range::{clear_cache, to_regex_range, Options};

#[test]
fn repeated_calls_return_identical_results() {
    let opts = Options::default();
    let first = to_regex_range("1", Some("3456"), &opts).unwrap();
    let second = to_regex_range("1", Some("3456"), &opts).unwrap();
    assert_eq!(first, second);
}

#[test]
fn clear_cache_does_not_change_results() {
    let opts = Options::default();
    let before = to_regex_range("99", Some("100000"), &opts).unwrap();
    clear_cache();
    let after = to_regex_range("99", Some("100000"), &opts).unwrap();
    assert_eq!(before, after);
}

#[test]
fn repeat_call_on_a_fresh_key_returns_the_same_string() {
    // A repeat call hits the cache. That is observable through identical output,
    // which holds regardless of what other parallel tests do to the shared map.
    let opts = Options::default();
    let first = to_regex_range("4242", Some("8484"), &opts).unwrap();
    let second = to_regex_range("4242", Some("8484"), &opts).unwrap();
    assert_eq!(first, second);
}

#[test]
fn distinct_options_use_distinct_keys() {
    // Same range, different options can yield different output and distinct
    // cache entries.
    let plain = to_regex_range("-10", Some("10"), &Options::default()).unwrap();
    let captured = to_regex_range("-10", Some("10"), &Options::default().capture(true)).unwrap();
    assert_ne!(plain, captured);
}

#[test]
fn raw_form_distinguishes_keys() {
    // Padded and unpadded inputs are different keys and can differ in output.
    let padded = to_regex_range("001", Some("100"), &Options::default()).unwrap();
    let plain = to_regex_range("1", Some("100"), &Options::default()).unwrap();
    assert_ne!(padded, plain);
}
