1 extern crate rand;
2 extern crate rayon;
3 
4 use rand::{Rng, SeedableRng, XorShiftRng};
5 use rand::distributions::Standard;
6 use rayon::prelude::*;
7 
seeded_rng() -> XorShiftRng8 fn seeded_rng() -> XorShiftRng {
9     let mut seed = <XorShiftRng as SeedableRng>::Seed::default();
10     (0..).zip(seed.as_mut()).for_each(|(i, x)| *x = i);
11     XorShiftRng::from_seed(seed)
12 }
13 
14 #[test]
execute_strings()15 pub fn execute_strings() {
16     let mut rng = seeded_rng();
17     let s: String = rng.sample_iter::<char, _>(&Standard).take(1024).collect();
18 
19     let par_chars: String = s.par_chars().collect();
20     assert_eq!(s, par_chars);
21 
22     let par_even: String = s.par_chars().filter(|&c| (c as u32) & 1 == 0).collect();
23     let ser_even: String = s.chars().filter(|&c| (c as u32) & 1 == 0).collect();
24     assert_eq!(par_even, ser_even);
25 
26     // test `FromParallelIterator<&char> for String`
27     let vchars: Vec<char> = s.par_chars().collect();
28     let par_chars: String = vchars.par_iter().collect();
29     assert_eq!(s, par_chars);
30 
31     let par_bytes: Vec<u8> = s.par_bytes().collect();
32     assert_eq!(s.as_bytes(), &*par_bytes);
33 
34     let par_utf16: Vec<u16> = s.par_encode_utf16().collect();
35     let ser_utf16: Vec<u16> = s.encode_utf16().collect();
36     assert_eq!(par_utf16, ser_utf16);
37 
38     let par_charind: Vec<_> = s.par_char_indices().collect();
39     let ser_charind: Vec<_> = s.char_indices().collect();
40     assert_eq!(par_charind, ser_charind);
41 }
42 
43 #[test]
execute_strings_split()44 pub fn execute_strings_split() {
45     // char testcases from examples in `str::split` etc.,
46     // plus a large self-test for good measure.
47     let tests = vec![("Mary had a little lamb", ' '),
48                      ("", 'X'),
49                      ("lionXXtigerXleopard", 'X'),
50                      ("||||a||b|c", '|'),
51                      ("(///)", '/'),
52                      ("010", '0'),
53                      ("    a  b c", ' '),
54                      ("A.B.", '.'),
55                      ("A..B..", '.'),
56                      ("foo\r\nbar\n\nbaz\n", '\n'),
57                      ("foo\nbar\n\r\nbaz", '\n'),
58                      ("A few words", ' '),
59                      (" Mary   had\ta\u{2009}little  \n\t lamb", ' '),
60                      (include_str!("str.rs"), ' ')];
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