1 | use rand::distributions::Standard; |
2 | use rand::{Rng, SeedableRng}; |
3 | use rand_xorshift::XorShiftRng; |
4 | use rayon::prelude::*; |
5 | |
6 | fn seeded_rng() -> XorShiftRng { |
7 | let mut seed = <XorShiftRng as SeedableRng>::Seed::default(); |
8 | (0..).zip(seed.as_mut()).for_each(|(i, x)| *x = i); |
9 | XorShiftRng::from_seed(seed) |
10 | } |
11 | |
12 | #[test] |
13 | pub fn execute_strings() { |
14 | let rng = seeded_rng(); |
15 | let s: String = rng.sample_iter::<char, _>(&Standard).take(1024).collect(); |
16 | |
17 | let par_chars: String = s.par_chars().collect(); |
18 | assert_eq!(s, par_chars); |
19 | |
20 | let par_even: String = s.par_chars().filter(|&c| (c as u32) & 1 == 0).collect(); |
21 | let ser_even: String = s.chars().filter(|&c| (c as u32) & 1 == 0).collect(); |
22 | assert_eq!(par_even, ser_even); |
23 | |
24 | // test `FromParallelIterator<&char> for String` |
25 | let vchars: Vec<char> = s.par_chars().collect(); |
26 | let par_chars: String = vchars.par_iter().collect(); |
27 | assert_eq!(s, par_chars); |
28 | |
29 | let par_bytes: Vec<u8> = s.par_bytes().collect(); |
30 | assert_eq!(s.as_bytes(), &*par_bytes); |
31 | |
32 | let par_utf16: Vec<u16> = s.par_encode_utf16().collect(); |
33 | let ser_utf16: Vec<u16> = s.encode_utf16().collect(); |
34 | assert_eq!(par_utf16, ser_utf16); |
35 | |
36 | let par_charind: Vec<_> = s.par_char_indices().collect(); |
37 | let ser_charind: Vec<_> = s.char_indices().collect(); |
38 | assert_eq!(par_charind, ser_charind); |
39 | } |
40 | |
41 | #[test] |
42 | pub fn execute_strings_split() { |
43 | // char testcases from examples in `str::split` etc., |
44 | // plus a large self-test for good measure. |
45 | let tests = vec![ |
46 | ("Mary had a little lamb" , ' ' ), |
47 | ("" , 'X' ), |
48 | ("lionXXtigerXleopard" , 'X' ), |
49 | ("||||a||b|c" , '|' ), |
50 | ("(///)" , '/' ), |
51 | ("010" , '0' ), |
52 | (" a b c" , ' ' ), |
53 | ("A.B." , '.' ), |
54 | ("A..B.." , '.' ), |
55 | ("foo \r\nbar \n\nbaz \n" , ' \n' ), |
56 | ("foo \nbar \n\r\nbaz" , ' \n' ), |
57 | ("A few words" , ' ' ), |
58 | (" Mary had \ta \u{2009}little \n\t lamb" , ' ' ), |
59 | (include_str!("str.rs" ), ' ' ), |
60 | ]; |
61 | |
62 | for &(string, separator) in &tests { |
63 | let serial: Vec<_> = string.split(separator).collect(); |
64 | let parallel: Vec<_> = string.par_split(separator).collect(); |
65 | assert_eq!(serial, parallel); |
66 | |
67 | let pattern: &[char] = &[' \u{0}' , separator, ' \u{1F980}' ]; |
68 | let serial: Vec<_> = string.split(pattern).collect(); |
69 | let parallel: Vec<_> = string.par_split(pattern).collect(); |
70 | assert_eq!(serial, parallel); |
71 | |
72 | let serial_fn: Vec<_> = string.split(|c| c == separator).collect(); |
73 | let parallel_fn: Vec<_> = string.par_split(|c| c == separator).collect(); |
74 | assert_eq!(serial_fn, parallel_fn); |
75 | } |
76 | |
77 | for &(string, separator) in &tests { |
78 | let serial: Vec<_> = string.split_terminator(separator).collect(); |
79 | let parallel: Vec<_> = string.par_split_terminator(separator).collect(); |
80 | assert_eq!(serial, parallel); |
81 | |
82 | let pattern: &[char] = &[' \u{0}' , separator, ' \u{1F980}' ]; |
83 | let serial: Vec<_> = string.split_terminator(pattern).collect(); |
84 | let parallel: Vec<_> = string.par_split_terminator(pattern).collect(); |
85 | assert_eq!(serial, parallel); |
86 | |
87 | let serial: Vec<_> = string.split_terminator(|c| c == separator).collect(); |
88 | let parallel: Vec<_> = string.par_split_terminator(|c| c == separator).collect(); |
89 | assert_eq!(serial, parallel); |
90 | } |
91 | |
92 | for &(string, _) in &tests { |
93 | let serial: Vec<_> = string.lines().collect(); |
94 | let parallel: Vec<_> = string.par_lines().collect(); |
95 | assert_eq!(serial, parallel); |
96 | } |
97 | |
98 | for &(string, _) in &tests { |
99 | let serial: Vec<_> = string.split_whitespace().collect(); |
100 | let parallel: Vec<_> = string.par_split_whitespace().collect(); |
101 | assert_eq!(serial, parallel); |
102 | } |
103 | |
104 | // try matching separators too! |
105 | for &(string, separator) in &tests { |
106 | let serial: Vec<_> = string.matches(separator).collect(); |
107 | let parallel: Vec<_> = string.par_matches(separator).collect(); |
108 | assert_eq!(serial, parallel); |
109 | |
110 | let pattern: &[char] = &[' \u{0}' , separator, ' \u{1F980}' ]; |
111 | let serial: Vec<_> = string.matches(pattern).collect(); |
112 | let parallel: Vec<_> = string.par_matches(pattern).collect(); |
113 | assert_eq!(serial, parallel); |
114 | |
115 | let serial_fn: Vec<_> = string.matches(|c| c == separator).collect(); |
116 | let parallel_fn: Vec<_> = string.par_matches(|c| c == separator).collect(); |
117 | assert_eq!(serial_fn, parallel_fn); |
118 | } |
119 | |
120 | for &(string, separator) in &tests { |
121 | let serial: Vec<_> = string.match_indices(separator).collect(); |
122 | let parallel: Vec<_> = string.par_match_indices(separator).collect(); |
123 | assert_eq!(serial, parallel); |
124 | |
125 | let pattern: &[char] = &[' \u{0}' , separator, ' \u{1F980}' ]; |
126 | let serial: Vec<_> = string.match_indices(pattern).collect(); |
127 | let parallel: Vec<_> = string.par_match_indices(pattern).collect(); |
128 | assert_eq!(serial, parallel); |
129 | |
130 | let serial_fn: Vec<_> = string.match_indices(|c| c == separator).collect(); |
131 | let parallel_fn: Vec<_> = string.par_match_indices(|c| c == separator).collect(); |
132 | assert_eq!(serial_fn, parallel_fn); |
133 | } |
134 | } |
135 | |