1 use rand::distributions::Standard;
2 use rand::{Rng, SeedableRng};
3 use rand_xorshift::XorShiftRng;
4 use rayon::prelude::*;
5
seeded_rng() -> XorShiftRng6 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]
execute_strings()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]
execute_strings_split()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 serial_fn: Vec<_> = string.split(|c| c == separator).collect();
68 let parallel_fn: Vec<_> = string.par_split(|c| c == separator).collect();
69 assert_eq!(serial_fn, parallel_fn);
70 }
71
72 for &(string, separator) in &tests {
73 let serial: Vec<_> = string.split_terminator(separator).collect();
74 let parallel: Vec<_> = string.par_split_terminator(separator).collect();
75 assert_eq!(serial, parallel);
76 }
77
78 for &(string, separator) in &tests {
79 let serial: Vec<_> = string.split_terminator(|c| c == separator).collect();
80 let parallel: Vec<_> = string.par_split_terminator(|c| c == separator).collect();
81 assert_eq!(serial, parallel);
82 }
83
84 for &(string, _) in &tests {
85 let serial: Vec<_> = string.lines().collect();
86 let parallel: Vec<_> = string.par_lines().collect();
87 assert_eq!(serial, parallel);
88 }
89
90 for &(string, _) in &tests {
91 let serial: Vec<_> = string.split_whitespace().collect();
92 let parallel: Vec<_> = string.par_split_whitespace().collect();
93 assert_eq!(serial, parallel);
94 }
95
96 // try matching separators too!
97 for &(string, separator) in &tests {
98 let serial: Vec<_> = string.matches(separator).collect();
99 let parallel: Vec<_> = string.par_matches(separator).collect();
100 assert_eq!(serial, parallel);
101
102 let serial_fn: Vec<_> = string.matches(|c| c == separator).collect();
103 let parallel_fn: Vec<_> = string.par_matches(|c| c == separator).collect();
104 assert_eq!(serial_fn, parallel_fn);
105 }
106
107 for &(string, separator) in &tests {
108 let serial: Vec<_> = string.match_indices(separator).collect();
109 let parallel: Vec<_> = string.par_match_indices(separator).collect();
110 assert_eq!(serial, parallel);
111
112 let serial_fn: Vec<_> = string.match_indices(|c| c == separator).collect();
113 let parallel_fn: Vec<_> = string.par_match_indices(|c| c == separator).collect();
114 assert_eq!(serial_fn, parallel_fn);
115 }
116 }
117