| 1 |
use fancy_regex::{Captures, Matches, Regex, RegexSet, RegexSetMatch}; |
| 2 |
|
| 3 |
#[cfg(feature = "std")] |
| 4 |
use std::sync::Arc; |
| 5 |
|
| 6 |
// Helper functions to ensure types implement Send and Sync |
| 7 |
fn assert_send<T: Send>() {} |
| 8 |
fn assert_sync<T: Sync>() {} |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn test_regex_is_send() { |
| 12 |
assert_send::<Regex>(); |
| 13 |
} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn test_regex_is_sync() { |
| 17 |
assert_sync::<Regex>(); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn test_matches_is_send() { |
| 22 |
assert_send::<Matches<'static, 'static, str>>(); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_captures_is_send() { |
| 27 |
assert_send::<Captures<'static, str>>(); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_regexset_is_send() { |
| 32 |
assert_send::<RegexSet>(); |
| 33 |
} |
| 34 |
|
| 35 |
#[test] |
| 36 |
fn test_regexset_is_sync() { |
| 37 |
assert_sync::<RegexSet>(); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_regexsetmatches_is_send() { |
| 42 |
assert_send::<RegexSetMatch<'static, str>>(); |
| 43 |
} |
| 44 |
|
| 45 |
#[cfg(all(feature = "std", feature = "variable-lookbehinds"))] |
| 46 |
#[test] |
| 47 |
fn test_threading_with_arc_and_variable_lookbehinds() { |
| 48 |
// Create a single regex with variable-length lookbehind wrapped in Arc |
| 49 |
let re = Arc::new(Regex::new(r"(?<=ab+)x").unwrap()); |
| 50 |
|
| 51 |
// Test data: different haystacks to match against |
| 52 |
let test_cases = vec![ |
| 53 |
("abbx", true), |
| 54 |
("abbbx", true), |
| 55 |
("ax", false), |
| 56 |
("x", false), |
| 57 |
("abbbbbbx", true), |
| 58 |
]; |
| 59 |
|
| 60 |
assert_test_cases(re, test_cases); |
| 61 |
} |
| 62 |
|
| 63 |
#[cfg(feature = "std")] |
| 64 |
#[test] |
| 65 |
fn test_threading_with_arc() { |
| 66 |
// Create a single regex |
| 67 |
let re = Arc::new(Regex::new(r"(?<=ab)x").unwrap()); |
| 68 |
|
| 69 |
// Test data: different haystacks to match against |
| 70 |
let test_cases = vec![ |
| 71 |
("abx", true), |
| 72 |
("aaabx", true), |
| 73 |
("ab", false), |
| 74 |
("aby", false), |
| 75 |
("bbx", false), |
| 76 |
]; |
| 77 |
|
| 78 |
assert_test_cases(re, test_cases); |
| 79 |
} |
| 80 |
|
| 81 |
#[cfg(all(test, feature = "std"))] |
| 82 |
fn assert_test_cases(re: Arc<Regex>, test_cases: Vec<(&str, bool)>) { |
| 83 |
use std::thread; |
| 84 |
|
| 85 |
// Create multiple threads that share the same regex instance |
| 86 |
let mut handles = vec![]; |
| 87 |
|
| 88 |
for (haystack, expected) in test_cases { |
| 89 |
let re_clone = Arc::clone(&re); |
| 90 |
let haystack_owned = haystack.to_string(); |
| 91 |
let handle = thread::spawn(move || { |
| 92 |
// Use the same regex instance in each thread |
| 93 |
let result = re_clone.is_match(&haystack_owned).unwrap(); |
| 94 |
(haystack_owned, result) |
| 95 |
}); |
| 96 |
handles.push((handle, expected)); |
| 97 |
} |
| 98 |
|
| 99 |
// Collect results from all threads |
| 100 |
for (handle, expected) in handles { |
| 101 |
let (haystack, result) = handle.join().unwrap(); |
| 102 |
assert_eq!( |
| 103 |
result, expected, |
| 104 |
"Failed for haystack '{}': expected {}, got {}", |
| 105 |
haystack, expected, result |
| 106 |
); |
| 107 |
} |
| 108 |
} |