1 | use rayon::prelude::*; |
2 | |
3 | fn check<I>(iter: I) |
4 | where |
5 | I: ParallelIterator + Clone, |
6 | I::Item: std::fmt::Debug + PartialEq, |
7 | { |
8 | let a: Vec<_> = iter.clone().collect(); |
9 | let b: Vec<_> = iter.collect(); |
10 | assert_eq!(a, b); |
11 | } |
12 | |
13 | fn check_count<I>(iter: I) |
14 | where |
15 | I: ParallelIterator + Clone, |
16 | { |
17 | assert_eq!(iter.clone().count(), iter.count()); |
18 | } |
19 | |
20 | #[test] |
21 | fn clone_binary_heap() { |
22 | use std::collections::BinaryHeap; |
23 | let heap: BinaryHeap<_> = (0..1000).collect(); |
24 | check(heap.par_iter()); |
25 | check(heap.into_par_iter()); |
26 | } |
27 | |
28 | #[test] |
29 | fn clone_btree_map() { |
30 | use std::collections::BTreeMap; |
31 | let map: BTreeMap<_, _> = (0..1000).enumerate().collect(); |
32 | check(map.par_iter()); |
33 | } |
34 | |
35 | #[test] |
36 | fn clone_btree_set() { |
37 | use std::collections::BTreeSet; |
38 | let set: BTreeSet<_> = (0..1000).collect(); |
39 | check(set.par_iter()); |
40 | } |
41 | |
42 | #[test] |
43 | fn clone_hash_map() { |
44 | use std::collections::HashMap; |
45 | let map: HashMap<_, _> = (0..1000).enumerate().collect(); |
46 | check(map.par_iter()); |
47 | } |
48 | |
49 | #[test] |
50 | fn clone_hash_set() { |
51 | use std::collections::HashSet; |
52 | let set: HashSet<_> = (0..1000).collect(); |
53 | check(set.par_iter()); |
54 | } |
55 | |
56 | #[test] |
57 | fn clone_linked_list() { |
58 | use std::collections::LinkedList; |
59 | let list: LinkedList<_> = (0..1000).collect(); |
60 | check(list.par_iter()); |
61 | check(list.into_par_iter()); |
62 | } |
63 | |
64 | #[test] |
65 | fn clone_vec_deque() { |
66 | use std::collections::VecDeque; |
67 | let deque: VecDeque<_> = (0..1000).collect(); |
68 | check(deque.par_iter()); |
69 | check(deque.into_par_iter()); |
70 | } |
71 | |
72 | #[test] |
73 | fn clone_option() { |
74 | let option = Some(0); |
75 | check(option.par_iter()); |
76 | check(option.into_par_iter()); |
77 | } |
78 | |
79 | #[test] |
80 | fn clone_result() { |
81 | let result = Ok::<_, ()>(0); |
82 | check(result.par_iter()); |
83 | check(result.into_par_iter()); |
84 | } |
85 | |
86 | #[test] |
87 | fn clone_range() { |
88 | check((0..1000).into_par_iter()); |
89 | } |
90 | |
91 | #[test] |
92 | fn clone_range_inclusive() { |
93 | check((0..=1000).into_par_iter()); |
94 | } |
95 | |
96 | #[test] |
97 | fn clone_str() { |
98 | let s = include_str!("clones.rs" ); |
99 | check(s.par_chars()); |
100 | check(s.par_lines()); |
101 | check(s.par_split(' \n' )); |
102 | check(s.par_split_terminator(' \n' )); |
103 | check(s.par_split_whitespace()); |
104 | } |
105 | |
106 | #[test] |
107 | fn clone_vec() { |
108 | let v: Vec<_> = (0..1000).collect(); |
109 | check(v.par_iter()); |
110 | check(v.par_chunks(42)); |
111 | check(v.par_chunks_exact(42)); |
112 | check(v.par_rchunks(42)); |
113 | check(v.par_rchunks_exact(42)); |
114 | check(v.par_windows(42)); |
115 | check(v.par_split(|x| x % 3 == 0)); |
116 | check(v.into_par_iter()); |
117 | } |
118 | |
119 | #[test] |
120 | fn clone_array() { |
121 | let a = [0i32; 100]; |
122 | check(a.into_par_iter()); |
123 | } |
124 | |
125 | #[test] |
126 | fn clone_adaptors() { |
127 | let v: Vec<_> = (0..1000).map(Some).collect(); |
128 | check(v.par_iter().chain(&v)); |
129 | check(v.par_iter().cloned()); |
130 | check(v.par_iter().copied()); |
131 | check(v.par_iter().enumerate()); |
132 | check(v.par_iter().filter(|_| true)); |
133 | check(v.par_iter().filter_map(|x| *x)); |
134 | check(v.par_iter().flat_map(|x| *x)); |
135 | check(v.par_iter().flat_map_iter(|x| *x)); |
136 | check(v.par_iter().flatten()); |
137 | check(v.par_iter().flatten_iter()); |
138 | check(v.par_iter().with_max_len(1).fold(|| 0, |x, _| x)); |
139 | check(v.par_iter().with_max_len(1).fold_with(0, |x, _| x)); |
140 | check(v.par_iter().with_max_len(1).fold_chunks(1, || 0, |x, _| x)); |
141 | check( |
142 | v.par_iter() |
143 | .with_max_len(1) |
144 | .fold_chunks_with(1, 0, |x, _| x), |
145 | ); |
146 | check(v.par_iter().with_max_len(1).try_fold(|| 0, |_, &x| x)); |
147 | check(v.par_iter().with_max_len(1).try_fold_with(0, |_, &x| x)); |
148 | check(v.par_iter().inspect(|_| ())); |
149 | check(v.par_iter().update(|_| ())); |
150 | check(v.par_iter().interleave(&v)); |
151 | check(v.par_iter().interleave_shortest(&v)); |
152 | check(v.par_iter().intersperse(&None)); |
153 | check(v.par_iter().chunks(3)); |
154 | check(v.par_iter().map(|x| x)); |
155 | check(v.par_iter().map_with(0, |_, x| x)); |
156 | check(v.par_iter().map_init(|| 0, |_, x| x)); |
157 | check(v.par_iter().panic_fuse()); |
158 | check(v.par_iter().positions(|_| true)); |
159 | check(v.par_iter().rev()); |
160 | check(v.par_iter().skip(42)); |
161 | check(v.par_iter().skip_any_while(|_| false)); |
162 | check(v.par_iter().take(42)); |
163 | check(v.par_iter().take_any_while(|_| true)); |
164 | check(v.par_iter().cloned().while_some()); |
165 | check(v.par_iter().with_max_len(1)); |
166 | check(v.par_iter().with_min_len(1)); |
167 | check(v.par_iter().zip(&v)); |
168 | check(v.par_iter().zip_eq(&v)); |
169 | check(v.par_iter().step_by(2)); |
170 | } |
171 | |
172 | #[test] |
173 | fn clone_counted_adaptors() { |
174 | let v: Vec<_> = (0..1000).collect(); |
175 | check_count(v.par_iter().skip_any(42)); |
176 | check_count(v.par_iter().take_any(42)); |
177 | } |
178 | |
179 | #[test] |
180 | fn clone_empty() { |
181 | check(rayon::iter::empty::<i32>()); |
182 | } |
183 | |
184 | #[test] |
185 | fn clone_once() { |
186 | check(rayon::iter::once(10)); |
187 | } |
188 | |
189 | #[test] |
190 | fn clone_repeat() { |
191 | let x: Option<i32> = None; |
192 | check(rayon::iter::repeat(x).while_some()); |
193 | check(rayon::iter::repeatn(x, 1000)); |
194 | } |
195 | |
196 | #[test] |
197 | fn clone_splitter() { |
198 | check(rayon::iter::split(0..1000, |x| (x, None))); |
199 | } |
200 | |
201 | #[test] |
202 | fn clone_multizip() { |
203 | let v: &Vec<_> = &(0..1000).collect(); |
204 | check((v,).into_par_iter()); |
205 | check((v, v).into_par_iter()); |
206 | check((v, v, v).into_par_iter()); |
207 | check((v, v, v, v).into_par_iter()); |
208 | check((v, v, v, v, v).into_par_iter()); |
209 | check((v, v, v, v, v, v).into_par_iter()); |
210 | check((v, v, v, v, v, v, v).into_par_iter()); |
211 | check((v, v, v, v, v, v, v, v).into_par_iter()); |
212 | check((v, v, v, v, v, v, v, v, v).into_par_iter()); |
213 | check((v, v, v, v, v, v, v, v, v, v).into_par_iter()); |
214 | check((v, v, v, v, v, v, v, v, v, v, v).into_par_iter()); |
215 | check((v, v, v, v, v, v, v, v, v, v, v, v).into_par_iter()); |
216 | } |
217 | |