1 | use std::sync::atomic::{AtomicUsize, Ordering}; |
2 | |
3 | use super::*; |
4 | use crate::prelude::*; |
5 | use rayon_core::*; |
6 | |
7 | use rand::distributions::Standard; |
8 | use rand::{Rng, SeedableRng}; |
9 | use rand_xorshift::XorShiftRng; |
10 | use std::collections::LinkedList; |
11 | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; |
12 | use std::collections::{BinaryHeap, VecDeque}; |
13 | use std::f64; |
14 | use std::fmt::Debug; |
15 | use std::sync::mpsc; |
16 | use std::usize; |
17 | |
18 | fn is_indexed<T: IndexedParallelIterator>(_: T) {} |
19 | |
20 | fn seeded_rng() -> XorShiftRng { |
21 | let mut seed = <XorShiftRng as SeedableRng>::Seed::default(); |
22 | (0..).zip(seed.as_mut()).for_each(|(i, x)| *x = i); |
23 | XorShiftRng::from_seed(seed) |
24 | } |
25 | |
26 | #[test] |
27 | fn execute() { |
28 | let a: Vec<i32> = (0..1024).collect(); |
29 | let mut b = vec![]; |
30 | a.par_iter().map(|&i| i + 1).collect_into_vec(&mut b); |
31 | let c: Vec<i32> = (0..1024).map(|i| i + 1).collect(); |
32 | assert_eq!(b, c); |
33 | } |
34 | |
35 | #[test] |
36 | fn execute_cloned() { |
37 | let a: Vec<i32> = (0..1024).collect(); |
38 | let mut b: Vec<i32> = vec![]; |
39 | a.par_iter().cloned().collect_into_vec(&mut b); |
40 | let c: Vec<i32> = (0..1024).collect(); |
41 | assert_eq!(b, c); |
42 | } |
43 | |
44 | #[test] |
45 | fn execute_range() { |
46 | let a = 0i32..1024; |
47 | let mut b = vec![]; |
48 | a.into_par_iter().map(|i| i + 1).collect_into_vec(&mut b); |
49 | let c: Vec<i32> = (0..1024).map(|i| i + 1).collect(); |
50 | assert_eq!(b, c); |
51 | } |
52 | |
53 | #[test] |
54 | fn execute_unindexed_range() { |
55 | let a = 0i64..1024; |
56 | let b: LinkedList<i64> = a.into_par_iter().map(|i| i + 1).collect(); |
57 | let c: LinkedList<i64> = (0..1024).map(|i| i + 1).collect(); |
58 | assert_eq!(b, c); |
59 | } |
60 | |
61 | #[test] |
62 | fn execute_pseudo_indexed_range() { |
63 | use std::i128::MAX; |
64 | let range = MAX - 1024..MAX; |
65 | |
66 | // Given `Some` length, collecting `Vec` will try to act indexed. |
67 | let a = range.clone().into_par_iter(); |
68 | assert_eq!(a.opt_len(), Some(1024)); |
69 | |
70 | let b: Vec<i128> = a.map(|i| i + 1).collect(); |
71 | let c: Vec<i128> = range.map(|i| i + 1).collect(); |
72 | assert_eq!(b, c); |
73 | } |
74 | |
75 | #[test] |
76 | fn check_map_indexed() { |
77 | let a = [1, 2, 3]; |
78 | is_indexed(a.par_iter().map(|x| x)); |
79 | } |
80 | |
81 | #[test] |
82 | fn map_sum() { |
83 | let a: Vec<i32> = (0..1024).collect(); |
84 | let r1: i32 = a.par_iter().map(|&i| i + 1).sum(); |
85 | let r2 = a.iter().map(|&i| i + 1).sum(); |
86 | assert_eq!(r1, r2); |
87 | } |
88 | |
89 | #[test] |
90 | fn map_reduce() { |
91 | let a: Vec<i32> = (0..1024).collect(); |
92 | let r1 = a.par_iter().map(|&i| i + 1).reduce(|| 0, |i, j| i + j); |
93 | let r2 = a.iter().map(|&i| i + 1).sum(); |
94 | assert_eq!(r1, r2); |
95 | } |
96 | |
97 | #[test] |
98 | fn map_reduce_with() { |
99 | let a: Vec<i32> = (0..1024).collect(); |
100 | let r1 = a.par_iter().map(|&i| i + 1).reduce_with(|i, j| i + j); |
101 | let r2 = a.iter().map(|&i| i + 1).sum(); |
102 | assert_eq!(r1, Some(r2)); |
103 | } |
104 | |
105 | #[test] |
106 | fn fold_map_reduce() { |
107 | // Kind of a weird test, but it demonstrates various |
108 | // transformations that are taking place. Relies on |
109 | // `with_max_len(1).fold()` being equivalent to `map()`. |
110 | // |
111 | // Take each number from 0 to 32 and fold them by appending to a |
112 | // vector. Because of `with_max_len(1)`, this will produce 32 vectors, |
113 | // each with one item. We then collect all of these into an |
114 | // individual vector by mapping each into their own vector (so we |
115 | // have Vec<Vec<i32>>) and then reducing those into a single |
116 | // vector. |
117 | let r1 = (0_i32..32) |
118 | .into_par_iter() |
119 | .with_max_len(1) |
120 | .fold(Vec::new, |mut v, e| { |
121 | v.push(e); |
122 | v |
123 | }) |
124 | .map(|v| vec![v]) |
125 | .reduce_with(|mut v_a, v_b| { |
126 | v_a.extend(v_b); |
127 | v_a |
128 | }); |
129 | assert_eq!( |
130 | r1, |
131 | Some(vec![ |
132 | vec![0], |
133 | vec![1], |
134 | vec![2], |
135 | vec![3], |
136 | vec![4], |
137 | vec![5], |
138 | vec![6], |
139 | vec![7], |
140 | vec![8], |
141 | vec![9], |
142 | vec![10], |
143 | vec![11], |
144 | vec![12], |
145 | vec![13], |
146 | vec![14], |
147 | vec![15], |
148 | vec![16], |
149 | vec![17], |
150 | vec![18], |
151 | vec![19], |
152 | vec![20], |
153 | vec![21], |
154 | vec![22], |
155 | vec![23], |
156 | vec![24], |
157 | vec![25], |
158 | vec![26], |
159 | vec![27], |
160 | vec![28], |
161 | vec![29], |
162 | vec![30], |
163 | vec![31] |
164 | ]) |
165 | ); |
166 | } |
167 | |
168 | #[test] |
169 | fn fold_is_full() { |
170 | let counter = AtomicUsize::new(0); |
171 | let a = (0_i32..2048) |
172 | .into_par_iter() |
173 | .inspect(|_| { |
174 | counter.fetch_add(1, Ordering::SeqCst); |
175 | }) |
176 | .fold(|| 0, |a, b| a + b) |
177 | .find_any(|_| true); |
178 | assert!(a.is_some()); |
179 | assert!(counter.load(Ordering::SeqCst) < 2048); // should not have visited every single one |
180 | } |
181 | |
182 | #[test] |
183 | fn check_step_by() { |
184 | let a: Vec<i32> = (0..1024).step_by(2).collect(); |
185 | let b: Vec<i32> = (0..1024).into_par_iter().step_by(2).collect(); |
186 | |
187 | assert_eq!(a, b); |
188 | } |
189 | |
190 | #[test] |
191 | fn check_step_by_unaligned() { |
192 | let a: Vec<i32> = (0..1029).step_by(10).collect(); |
193 | let b: Vec<i32> = (0..1029).into_par_iter().step_by(10).collect(); |
194 | |
195 | assert_eq!(a, b) |
196 | } |
197 | |
198 | #[test] |
199 | fn check_step_by_rev() { |
200 | let a: Vec<i32> = (0..1024).step_by(2).rev().collect(); |
201 | let b: Vec<i32> = (0..1024).into_par_iter().step_by(2).rev().collect(); |
202 | |
203 | assert_eq!(a, b); |
204 | } |
205 | |
206 | #[test] |
207 | fn check_enumerate() { |
208 | let a: Vec<usize> = (0..1024).rev().collect(); |
209 | |
210 | let mut b = vec![]; |
211 | a.par_iter() |
212 | .enumerate() |
213 | .map(|(i, &x)| i + x) |
214 | .collect_into_vec(&mut b); |
215 | assert!(b.iter().all(|&x| x == a.len() - 1)); |
216 | } |
217 | |
218 | #[test] |
219 | fn check_enumerate_rev() { |
220 | let a: Vec<usize> = (0..1024).rev().collect(); |
221 | |
222 | let mut b = vec![]; |
223 | a.par_iter() |
224 | .enumerate() |
225 | .rev() |
226 | .map(|(i, &x)| i + x) |
227 | .collect_into_vec(&mut b); |
228 | assert!(b.iter().all(|&x| x == a.len() - 1)); |
229 | } |
230 | |
231 | #[test] |
232 | fn check_indices_after_enumerate_split() { |
233 | let a: Vec<i32> = (0..1024).collect(); |
234 | a.par_iter().enumerate().with_producer(WithProducer); |
235 | |
236 | struct WithProducer; |
237 | impl<'a> ProducerCallback<(usize, &'a i32)> for WithProducer { |
238 | type Output = (); |
239 | fn callback<P>(self, producer: P) |
240 | where |
241 | P: Producer<Item = (usize, &'a i32)>, |
242 | { |
243 | let (a, b) = producer.split_at(512); |
244 | for ((index, value), trusted_index) in a.into_iter().zip(0..) { |
245 | assert_eq!(index, trusted_index); |
246 | assert_eq!(index, *value as usize); |
247 | } |
248 | for ((index, value), trusted_index) in b.into_iter().zip(512..) { |
249 | assert_eq!(index, trusted_index); |
250 | assert_eq!(index, *value as usize); |
251 | } |
252 | } |
253 | } |
254 | } |
255 | |
256 | #[test] |
257 | fn check_increment() { |
258 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
259 | |
260 | a.par_iter_mut().enumerate().for_each(|(i, v)| *v += i); |
261 | |
262 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
263 | } |
264 | |
265 | #[test] |
266 | fn check_skip() { |
267 | let a: Vec<usize> = (0..1024).collect(); |
268 | |
269 | let mut v1 = Vec::new(); |
270 | a.par_iter().skip(16).collect_into_vec(&mut v1); |
271 | let v2 = a.iter().skip(16).collect::<Vec<_>>(); |
272 | assert_eq!(v1, v2); |
273 | |
274 | let mut v1 = Vec::new(); |
275 | a.par_iter().skip(2048).collect_into_vec(&mut v1); |
276 | let v2 = a.iter().skip(2048).collect::<Vec<_>>(); |
277 | assert_eq!(v1, v2); |
278 | |
279 | let mut v1 = Vec::new(); |
280 | a.par_iter().skip(0).collect_into_vec(&mut v1); |
281 | let v2 = a.iter().skip(0).collect::<Vec<_>>(); |
282 | assert_eq!(v1, v2); |
283 | |
284 | // Check that the skipped elements side effects are executed |
285 | use std::sync::atomic::{AtomicUsize, Ordering}; |
286 | let num = AtomicUsize::new(0); |
287 | a.par_iter() |
288 | .map(|&n| num.fetch_add(n, Ordering::Relaxed)) |
289 | .skip(512) |
290 | .count(); |
291 | assert_eq!(num.load(Ordering::Relaxed), a.iter().sum::<usize>()); |
292 | } |
293 | |
294 | #[test] |
295 | fn check_take() { |
296 | let a: Vec<usize> = (0..1024).collect(); |
297 | |
298 | let mut v1 = Vec::new(); |
299 | a.par_iter().take(16).collect_into_vec(&mut v1); |
300 | let v2 = a.iter().take(16).collect::<Vec<_>>(); |
301 | assert_eq!(v1, v2); |
302 | |
303 | let mut v1 = Vec::new(); |
304 | a.par_iter().take(2048).collect_into_vec(&mut v1); |
305 | let v2 = a.iter().take(2048).collect::<Vec<_>>(); |
306 | assert_eq!(v1, v2); |
307 | |
308 | let mut v1 = Vec::new(); |
309 | a.par_iter().take(0).collect_into_vec(&mut v1); |
310 | let v2 = a.iter().take(0).collect::<Vec<_>>(); |
311 | assert_eq!(v1, v2); |
312 | } |
313 | |
314 | #[test] |
315 | fn check_inspect() { |
316 | use std::sync::atomic::{AtomicUsize, Ordering}; |
317 | |
318 | let a = AtomicUsize::new(0); |
319 | let b: usize = (0_usize..1024) |
320 | .into_par_iter() |
321 | .inspect(|&i| { |
322 | a.fetch_add(i, Ordering::Relaxed); |
323 | }) |
324 | .sum(); |
325 | |
326 | assert_eq!(a.load(Ordering::Relaxed), b); |
327 | } |
328 | |
329 | #[test] |
330 | fn check_move() { |
331 | let a = vec![vec![1, 2, 3]]; |
332 | let ptr = a[0].as_ptr(); |
333 | |
334 | let mut b = vec![]; |
335 | a.into_par_iter().collect_into_vec(&mut b); |
336 | |
337 | // a simple move means the inner vec will be completely unchanged |
338 | assert_eq!(ptr, b[0].as_ptr()); |
339 | } |
340 | |
341 | #[test] |
342 | fn check_drops() { |
343 | use std::sync::atomic::{AtomicUsize, Ordering}; |
344 | |
345 | let c = AtomicUsize::new(0); |
346 | let a = vec![DropCounter(&c); 10]; |
347 | |
348 | let mut b = vec![]; |
349 | a.clone().into_par_iter().collect_into_vec(&mut b); |
350 | assert_eq!(c.load(Ordering::Relaxed), 0); |
351 | |
352 | b.into_par_iter(); |
353 | assert_eq!(c.load(Ordering::Relaxed), 10); |
354 | |
355 | a.into_par_iter().with_producer(Partial); |
356 | assert_eq!(c.load(Ordering::Relaxed), 20); |
357 | |
358 | #[derive(Clone)] |
359 | struct DropCounter<'a>(&'a AtomicUsize); |
360 | impl<'a> Drop for DropCounter<'a> { |
361 | fn drop(&mut self) { |
362 | self.0.fetch_add(1, Ordering::Relaxed); |
363 | } |
364 | } |
365 | |
366 | struct Partial; |
367 | impl<'a> ProducerCallback<DropCounter<'a>> for Partial { |
368 | type Output = (); |
369 | fn callback<P>(self, producer: P) |
370 | where |
371 | P: Producer<Item = DropCounter<'a>>, |
372 | { |
373 | let (a, _) = producer.split_at(5); |
374 | a.into_iter().next(); |
375 | } |
376 | } |
377 | } |
378 | |
379 | #[test] |
380 | fn check_slice_indexed() { |
381 | let a = vec![1, 2, 3]; |
382 | is_indexed(a.par_iter()); |
383 | } |
384 | |
385 | #[test] |
386 | fn check_slice_mut_indexed() { |
387 | let mut a = vec![1, 2, 3]; |
388 | is_indexed(a.par_iter_mut()); |
389 | } |
390 | |
391 | #[test] |
392 | fn check_vec_indexed() { |
393 | let a = vec![1, 2, 3]; |
394 | is_indexed(a.into_par_iter()); |
395 | } |
396 | |
397 | #[test] |
398 | fn check_range_indexed() { |
399 | is_indexed((1..5).into_par_iter()); |
400 | } |
401 | |
402 | #[test] |
403 | fn check_cmp_direct() { |
404 | let a = (0..1024).into_par_iter(); |
405 | let b = (0..1024).into_par_iter(); |
406 | |
407 | let result = a.cmp(b); |
408 | |
409 | assert!(result == ::std::cmp::Ordering::Equal); |
410 | } |
411 | |
412 | #[test] |
413 | fn check_cmp_to_seq() { |
414 | assert_eq!( |
415 | (0..1024).into_par_iter().cmp(0..1024), |
416 | (0..1024).cmp(0..1024) |
417 | ); |
418 | } |
419 | |
420 | #[test] |
421 | fn check_cmp_rng_to_seq() { |
422 | let mut rng = seeded_rng(); |
423 | let rng = &mut rng; |
424 | let a: Vec<i32> = rng.sample_iter(&Standard).take(1024).collect(); |
425 | let b: Vec<i32> = rng.sample_iter(&Standard).take(1024).collect(); |
426 | for i in 0..a.len() { |
427 | let par_result = a[i..].par_iter().cmp(b[i..].par_iter()); |
428 | let seq_result = a[i..].iter().cmp(b[i..].iter()); |
429 | |
430 | assert_eq!(par_result, seq_result); |
431 | } |
432 | } |
433 | |
434 | #[test] |
435 | fn check_cmp_lt_direct() { |
436 | let a = (0..1024).into_par_iter(); |
437 | let b = (1..1024).into_par_iter(); |
438 | |
439 | let result = a.cmp(b); |
440 | |
441 | assert!(result == ::std::cmp::Ordering::Less); |
442 | } |
443 | |
444 | #[test] |
445 | fn check_cmp_lt_to_seq() { |
446 | assert_eq!( |
447 | (0..1024).into_par_iter().cmp(1..1024), |
448 | (0..1024).cmp(1..1024) |
449 | ) |
450 | } |
451 | |
452 | #[test] |
453 | fn check_cmp_gt_direct() { |
454 | let a = (1..1024).into_par_iter(); |
455 | let b = (0..1024).into_par_iter(); |
456 | |
457 | let result = a.cmp(b); |
458 | |
459 | assert!(result == ::std::cmp::Ordering::Greater); |
460 | } |
461 | |
462 | #[test] |
463 | fn check_cmp_gt_to_seq() { |
464 | assert_eq!( |
465 | (1..1024).into_par_iter().cmp(0..1024), |
466 | (1..1024).cmp(0..1024) |
467 | ) |
468 | } |
469 | |
470 | #[test] |
471 | #[cfg_attr (any(target_os = "emscripten" , target_family = "wasm" ), ignore)] |
472 | fn check_cmp_short_circuit() { |
473 | // We only use a single thread in order to make the short-circuit behavior deterministic. |
474 | let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap(); |
475 | |
476 | let a = vec![0; 1024]; |
477 | let mut b = a.clone(); |
478 | b[42] = 1; |
479 | |
480 | pool.install(|| { |
481 | let expected = ::std::cmp::Ordering::Less; |
482 | assert_eq!(a.par_iter().cmp(&b), expected); |
483 | |
484 | for len in 1..10 { |
485 | let counter = AtomicUsize::new(0); |
486 | let result = a |
487 | .par_iter() |
488 | .with_max_len(len) |
489 | .inspect(|_| { |
490 | counter.fetch_add(1, Ordering::SeqCst); |
491 | }) |
492 | .cmp(&b); |
493 | assert_eq!(result, expected); |
494 | // should not have visited every single one |
495 | assert!(counter.into_inner() < a.len()); |
496 | } |
497 | }); |
498 | } |
499 | |
500 | #[test] |
501 | #[cfg_attr (any(target_os = "emscripten" , target_family = "wasm" ), ignore)] |
502 | fn check_partial_cmp_short_circuit() { |
503 | // We only use a single thread to make the short-circuit behavior deterministic. |
504 | let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap(); |
505 | |
506 | let a = vec![0; 1024]; |
507 | let mut b = a.clone(); |
508 | b[42] = 1; |
509 | |
510 | pool.install(|| { |
511 | let expected = Some(::std::cmp::Ordering::Less); |
512 | assert_eq!(a.par_iter().partial_cmp(&b), expected); |
513 | |
514 | for len in 1..10 { |
515 | let counter = AtomicUsize::new(0); |
516 | let result = a |
517 | .par_iter() |
518 | .with_max_len(len) |
519 | .inspect(|_| { |
520 | counter.fetch_add(1, Ordering::SeqCst); |
521 | }) |
522 | .partial_cmp(&b); |
523 | assert_eq!(result, expected); |
524 | // should not have visited every single one |
525 | assert!(counter.into_inner() < a.len()); |
526 | } |
527 | }); |
528 | } |
529 | |
530 | #[test] |
531 | #[cfg_attr (any(target_os = "emscripten" , target_family = "wasm" ), ignore)] |
532 | fn check_partial_cmp_nan_short_circuit() { |
533 | // We only use a single thread to make the short-circuit behavior deterministic. |
534 | let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap(); |
535 | |
536 | let a = vec![0.0; 1024]; |
537 | let mut b = a.clone(); |
538 | b[42] = f64::NAN; |
539 | |
540 | pool.install(|| { |
541 | let expected = None; |
542 | assert_eq!(a.par_iter().partial_cmp(&b), expected); |
543 | |
544 | for len in 1..10 { |
545 | let counter = AtomicUsize::new(0); |
546 | let result = a |
547 | .par_iter() |
548 | .with_max_len(len) |
549 | .inspect(|_| { |
550 | counter.fetch_add(1, Ordering::SeqCst); |
551 | }) |
552 | .partial_cmp(&b); |
553 | assert_eq!(result, expected); |
554 | // should not have visited every single one |
555 | assert!(counter.into_inner() < a.len()); |
556 | } |
557 | }); |
558 | } |
559 | |
560 | #[test] |
561 | fn check_partial_cmp_direct() { |
562 | let a = (0..1024).into_par_iter(); |
563 | let b = (0..1024).into_par_iter(); |
564 | |
565 | let result = a.partial_cmp(b); |
566 | |
567 | assert!(result == Some(::std::cmp::Ordering::Equal)); |
568 | } |
569 | |
570 | #[test] |
571 | fn check_partial_cmp_to_seq() { |
572 | let par_result = (0..1024).into_par_iter().partial_cmp(0..1024); |
573 | let seq_result = (0..1024).partial_cmp(0..1024); |
574 | assert_eq!(par_result, seq_result); |
575 | } |
576 | |
577 | #[test] |
578 | fn check_partial_cmp_rng_to_seq() { |
579 | let mut rng = seeded_rng(); |
580 | let rng = &mut rng; |
581 | let a: Vec<i32> = rng.sample_iter(&Standard).take(1024).collect(); |
582 | let b: Vec<i32> = rng.sample_iter(&Standard).take(1024).collect(); |
583 | for i in 0..a.len() { |
584 | let par_result = a[i..].par_iter().partial_cmp(b[i..].par_iter()); |
585 | let seq_result = a[i..].iter().partial_cmp(b[i..].iter()); |
586 | |
587 | assert_eq!(par_result, seq_result); |
588 | } |
589 | } |
590 | |
591 | #[test] |
592 | fn check_partial_cmp_lt_direct() { |
593 | let a = (0..1024).into_par_iter(); |
594 | let b = (1..1024).into_par_iter(); |
595 | |
596 | let result = a.partial_cmp(b); |
597 | |
598 | assert!(result == Some(::std::cmp::Ordering::Less)); |
599 | } |
600 | |
601 | #[test] |
602 | fn check_partial_cmp_lt_to_seq() { |
603 | let par_result = (0..1024).into_par_iter().partial_cmp(1..1024); |
604 | let seq_result = (0..1024).partial_cmp(1..1024); |
605 | assert_eq!(par_result, seq_result); |
606 | } |
607 | |
608 | #[test] |
609 | fn check_partial_cmp_gt_direct() { |
610 | let a = (1..1024).into_par_iter(); |
611 | let b = (0..1024).into_par_iter(); |
612 | |
613 | let result = a.partial_cmp(b); |
614 | |
615 | assert!(result == Some(::std::cmp::Ordering::Greater)); |
616 | } |
617 | |
618 | #[test] |
619 | fn check_partial_cmp_gt_to_seq() { |
620 | let par_result = (1..1024).into_par_iter().partial_cmp(0..1024); |
621 | let seq_result = (1..1024).partial_cmp(0..1024); |
622 | assert_eq!(par_result, seq_result); |
623 | } |
624 | |
625 | #[test] |
626 | fn check_partial_cmp_none_direct() { |
627 | let a = vec![f64::NAN, 0.0]; |
628 | let b = vec![0.0, 1.0]; |
629 | |
630 | let result = a.par_iter().partial_cmp(b.par_iter()); |
631 | |
632 | assert!(result == None); |
633 | } |
634 | |
635 | #[test] |
636 | fn check_partial_cmp_none_to_seq() { |
637 | let a = vec![f64::NAN, 0.0]; |
638 | let b = vec![0.0, 1.0]; |
639 | |
640 | let par_result = a.par_iter().partial_cmp(b.par_iter()); |
641 | let seq_result = a.iter().partial_cmp(b.iter()); |
642 | |
643 | assert_eq!(par_result, seq_result); |
644 | } |
645 | |
646 | #[test] |
647 | fn check_partial_cmp_late_nan_direct() { |
648 | let a = vec![0.0, f64::NAN]; |
649 | let b = vec![1.0, 1.0]; |
650 | |
651 | let result = a.par_iter().partial_cmp(b.par_iter()); |
652 | |
653 | assert!(result == Some(::std::cmp::Ordering::Less)); |
654 | } |
655 | |
656 | #[test] |
657 | fn check_partial_cmp_late_nane_to_seq() { |
658 | let a = vec![0.0, f64::NAN]; |
659 | let b = vec![1.0, 1.0]; |
660 | |
661 | let par_result = a.par_iter().partial_cmp(b.par_iter()); |
662 | let seq_result = a.iter().partial_cmp(b.iter()); |
663 | |
664 | assert_eq!(par_result, seq_result); |
665 | } |
666 | |
667 | #[test] |
668 | fn check_cmp_lengths() { |
669 | // comparisons should consider length if they are otherwise equal |
670 | let a = vec![0; 1024]; |
671 | let b = vec![0; 1025]; |
672 | |
673 | assert_eq!(a.par_iter().cmp(&b), a.iter().cmp(&b)); |
674 | assert_eq!(a.par_iter().partial_cmp(&b), a.iter().partial_cmp(&b)); |
675 | } |
676 | |
677 | #[test] |
678 | fn check_eq_direct() { |
679 | let a = (0..1024).into_par_iter(); |
680 | let b = (0..1024).into_par_iter(); |
681 | |
682 | let result = a.eq(b); |
683 | |
684 | assert!(result); |
685 | } |
686 | |
687 | #[test] |
688 | fn check_eq_to_seq() { |
689 | let par_result = (0..1024).into_par_iter().eq((0..1024).into_par_iter()); |
690 | let seq_result = (0..1024).eq(0..1024); |
691 | |
692 | assert_eq!(par_result, seq_result); |
693 | } |
694 | |
695 | #[test] |
696 | fn check_ne_direct() { |
697 | let a = (0..1024).into_par_iter(); |
698 | let b = (1..1024).into_par_iter(); |
699 | |
700 | let result = a.ne(b); |
701 | |
702 | assert!(result); |
703 | } |
704 | |
705 | #[test] |
706 | fn check_ne_to_seq() { |
707 | let par_result = (0..1024).into_par_iter().ne((1..1025).into_par_iter()); |
708 | let seq_result = (0..1024).ne(1..1025); |
709 | |
710 | assert_eq!(par_result, seq_result); |
711 | } |
712 | |
713 | #[test] |
714 | fn check_ne_lengths() { |
715 | // equality should consider length too |
716 | let a = vec![0; 1024]; |
717 | let b = vec![0; 1025]; |
718 | |
719 | assert_eq!(a.par_iter().eq(&b), a.iter().eq(&b)); |
720 | assert_eq!(a.par_iter().ne(&b), a.iter().ne(&b)); |
721 | } |
722 | |
723 | #[test] |
724 | fn check_lt_direct() { |
725 | assert!((0..1024).into_par_iter().lt(1..1024)); |
726 | assert!(!(1..1024).into_par_iter().lt(0..1024)); |
727 | } |
728 | |
729 | #[test] |
730 | fn check_lt_to_seq() { |
731 | let par_result = (0..1024).into_par_iter().lt((1..1024).into_par_iter()); |
732 | let seq_result = (0..1024).lt(1..1024); |
733 | |
734 | assert_eq!(par_result, seq_result); |
735 | } |
736 | |
737 | #[test] |
738 | fn check_le_equal_direct() { |
739 | assert!((0..1024).into_par_iter().le((0..1024).into_par_iter())); |
740 | } |
741 | |
742 | #[test] |
743 | fn check_le_equal_to_seq() { |
744 | let par_result = (0..1024).into_par_iter().le((0..1024).into_par_iter()); |
745 | let seq_result = (0..1024).le(0..1024); |
746 | |
747 | assert_eq!(par_result, seq_result); |
748 | } |
749 | |
750 | #[test] |
751 | fn check_le_less_direct() { |
752 | assert!((0..1024).into_par_iter().le((1..1024).into_par_iter())); |
753 | } |
754 | |
755 | #[test] |
756 | fn check_le_less_to_seq() { |
757 | let par_result = (0..1024).into_par_iter().le((1..1024).into_par_iter()); |
758 | let seq_result = (0..1024).le(1..1024); |
759 | |
760 | assert_eq!(par_result, seq_result); |
761 | } |
762 | |
763 | #[test] |
764 | fn check_gt_direct() { |
765 | assert!((1..1024).into_par_iter().gt((0..1024).into_par_iter())); |
766 | } |
767 | |
768 | #[test] |
769 | fn check_gt_to_seq() { |
770 | let par_result = (1..1024).into_par_iter().gt((0..1024).into_par_iter()); |
771 | let seq_result = (1..1024).gt(0..1024); |
772 | |
773 | assert_eq!(par_result, seq_result); |
774 | } |
775 | |
776 | #[test] |
777 | fn check_ge_equal_direct() { |
778 | assert!((0..1024).into_par_iter().ge((0..1024).into_par_iter())); |
779 | } |
780 | |
781 | #[test] |
782 | fn check_ge_equal_to_seq() { |
783 | let par_result = (0..1024).into_par_iter().ge((0..1024).into_par_iter()); |
784 | let seq_result = (0..1024).ge(0..1024); |
785 | |
786 | assert_eq!(par_result, seq_result); |
787 | } |
788 | |
789 | #[test] |
790 | fn check_ge_greater_direct() { |
791 | assert!((1..1024).into_par_iter().ge((0..1024).into_par_iter())); |
792 | } |
793 | |
794 | #[test] |
795 | fn check_ge_greater_to_seq() { |
796 | let par_result = (1..1024).into_par_iter().ge((0..1024).into_par_iter()); |
797 | let seq_result = (1..1024).ge(0..1024); |
798 | |
799 | assert_eq!(par_result, seq_result); |
800 | } |
801 | |
802 | #[test] |
803 | fn check_zip() { |
804 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
805 | let b: Vec<usize> = (0..1024).collect(); |
806 | |
807 | a.par_iter_mut().zip(&b[..]).for_each(|(a, &b)| *a += b); |
808 | |
809 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
810 | } |
811 | |
812 | #[test] |
813 | fn check_zip_into_par_iter() { |
814 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
815 | let b: Vec<usize> = (0..1024).collect(); |
816 | |
817 | a.par_iter_mut() |
818 | .zip(&b) // here we rely on &b iterating over &usize |
819 | .for_each(|(a, &b)| *a += b); |
820 | |
821 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
822 | } |
823 | |
824 | #[test] |
825 | fn check_zip_into_mut_par_iter() { |
826 | let a: Vec<usize> = (0..1024).rev().collect(); |
827 | let mut b: Vec<usize> = (0..1024).collect(); |
828 | |
829 | a.par_iter().zip(&mut b).for_each(|(&a, b)| *b += a); |
830 | |
831 | assert!(b.iter().all(|&x| x == b.len() - 1)); |
832 | } |
833 | |
834 | #[test] |
835 | fn check_zip_range() { |
836 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
837 | |
838 | a.par_iter_mut() |
839 | .zip(0usize..1024) |
840 | .for_each(|(a, b)| *a += b); |
841 | |
842 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
843 | } |
844 | |
845 | #[test] |
846 | fn check_zip_eq() { |
847 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
848 | let b: Vec<usize> = (0..1024).collect(); |
849 | |
850 | a.par_iter_mut().zip_eq(&b[..]).for_each(|(a, &b)| *a += b); |
851 | |
852 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
853 | } |
854 | |
855 | #[test] |
856 | fn check_zip_eq_into_par_iter() { |
857 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
858 | let b: Vec<usize> = (0..1024).collect(); |
859 | |
860 | a.par_iter_mut() |
861 | .zip_eq(&b) // here we rely on &b iterating over &usize |
862 | .for_each(|(a, &b)| *a += b); |
863 | |
864 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
865 | } |
866 | |
867 | #[test] |
868 | fn check_zip_eq_into_mut_par_iter() { |
869 | let a: Vec<usize> = (0..1024).rev().collect(); |
870 | let mut b: Vec<usize> = (0..1024).collect(); |
871 | |
872 | a.par_iter().zip_eq(&mut b).for_each(|(&a, b)| *b += a); |
873 | |
874 | assert!(b.iter().all(|&x| x == b.len() - 1)); |
875 | } |
876 | |
877 | #[test] |
878 | fn check_zip_eq_range() { |
879 | let mut a: Vec<usize> = (0..1024).rev().collect(); |
880 | |
881 | a.par_iter_mut() |
882 | .zip_eq(0usize..1024) |
883 | .for_each(|(a, b)| *a += b); |
884 | |
885 | assert!(a.iter().all(|&x| x == a.len() - 1)); |
886 | } |
887 | |
888 | #[test] |
889 | fn check_sum_filtered_ints() { |
890 | let a: Vec<i32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
891 | let par_sum_evens: i32 = a.par_iter().filter(|&x| (x & 1) == 0).sum(); |
892 | let seq_sum_evens = a.iter().filter(|&x| (x & 1) == 0).sum(); |
893 | assert_eq!(par_sum_evens, seq_sum_evens); |
894 | } |
895 | |
896 | #[test] |
897 | fn check_sum_filtermap_ints() { |
898 | let a: Vec<i32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
899 | let par_sum_evens: u32 = a |
900 | .par_iter() |
901 | .filter_map(|&x| if (x & 1) == 0 { Some(x as u32) } else { None }) |
902 | .sum(); |
903 | let seq_sum_evens = a |
904 | .iter() |
905 | .filter_map(|&x| if (x & 1) == 0 { Some(x as u32) } else { None }) |
906 | .sum(); |
907 | assert_eq!(par_sum_evens, seq_sum_evens); |
908 | } |
909 | |
910 | #[test] |
911 | fn check_flat_map_nested_ranges() { |
912 | // FIXME -- why are precise type hints required on the integers here? |
913 | |
914 | let v: i32 = (0_i32..10) |
915 | .into_par_iter() |
916 | .flat_map(|i| (0_i32..10).into_par_iter().map(move |j| (i, j))) |
917 | .map(|(i, j)| i * j) |
918 | .sum(); |
919 | |
920 | let w = (0_i32..10) |
921 | .flat_map(|i| (0_i32..10).map(move |j| (i, j))) |
922 | .map(|(i, j)| i * j) |
923 | .sum(); |
924 | |
925 | assert_eq!(v, w); |
926 | } |
927 | |
928 | #[test] |
929 | fn check_empty_flat_map_sum() { |
930 | let a: Vec<i32> = (0..1024).collect(); |
931 | let empty = &a[..0]; |
932 | |
933 | // empty on the inside |
934 | let b: i32 = a.par_iter().flat_map(|_| empty).sum(); |
935 | assert_eq!(b, 0); |
936 | |
937 | // empty on the outside |
938 | let c: i32 = empty.par_iter().flat_map(|_| a.par_iter()).sum(); |
939 | assert_eq!(c, 0); |
940 | } |
941 | |
942 | #[test] |
943 | fn check_flatten_vec() { |
944 | let a: Vec<i32> = (0..1024).collect(); |
945 | let b: Vec<Vec<i32>> = vec![a.clone(), a.clone(), a.clone(), a.clone()]; |
946 | let c: Vec<i32> = b.par_iter().flatten().cloned().collect(); |
947 | let mut d = a.clone(); |
948 | d.extend(&a); |
949 | d.extend(&a); |
950 | d.extend(&a); |
951 | |
952 | assert_eq!(d, c); |
953 | } |
954 | |
955 | #[test] |
956 | fn check_flatten_vec_empty() { |
957 | let a: Vec<Vec<i32>> = vec![vec![]]; |
958 | let b: Vec<i32> = a.par_iter().flatten().cloned().collect(); |
959 | |
960 | assert_eq!(vec![] as Vec<i32>, b); |
961 | } |
962 | |
963 | #[test] |
964 | fn check_slice_split() { |
965 | let v: Vec<_> = (0..1000).collect(); |
966 | for m in 1..100 { |
967 | let a: Vec<_> = v.split(|x| x % m == 0).collect(); |
968 | let b: Vec<_> = v.par_split(|x| x % m == 0).collect(); |
969 | assert_eq!(a, b); |
970 | } |
971 | |
972 | // same as std::slice::split() examples |
973 | let slice = [10, 40, 33, 20]; |
974 | let v: Vec<_> = slice.par_split(|num| num % 3 == 0).collect(); |
975 | assert_eq!(v, &[&slice[..2], &slice[3..]]); |
976 | |
977 | let slice = [10, 40, 33]; |
978 | let v: Vec<_> = slice.par_split(|num| num % 3 == 0).collect(); |
979 | assert_eq!(v, &[&slice[..2], &slice[..0]]); |
980 | |
981 | let slice = [10, 6, 33, 20]; |
982 | let v: Vec<_> = slice.par_split(|num| num % 3 == 0).collect(); |
983 | assert_eq!(v, &[&slice[..1], &slice[..0], &slice[3..]]); |
984 | } |
985 | |
986 | #[test] |
987 | fn check_slice_split_mut() { |
988 | let mut v1: Vec<_> = (0..1000).collect(); |
989 | let mut v2 = v1.clone(); |
990 | for m in 1..100 { |
991 | let a: Vec<_> = v1.split_mut(|x| x % m == 0).collect(); |
992 | let b: Vec<_> = v2.par_split_mut(|x| x % m == 0).collect(); |
993 | assert_eq!(a, b); |
994 | } |
995 | |
996 | // same as std::slice::split_mut() example |
997 | let mut v = [10, 40, 30, 20, 60, 50]; |
998 | v.par_split_mut(|num| num % 3 == 0).for_each(|group| { |
999 | group[0] = 1; |
1000 | }); |
1001 | assert_eq!(v, [1, 40, 30, 1, 60, 1]); |
1002 | } |
1003 | |
1004 | #[test] |
1005 | fn check_chunks() { |
1006 | let a: Vec<i32> = vec![1, 5, 10, 4, 100, 3, 1000, 2, 10000, 1]; |
1007 | let par_sum_product_pairs: i32 = a.par_chunks(2).map(|c| c.iter().product::<i32>()).sum(); |
1008 | let seq_sum_product_pairs = a.chunks(2).map(|c| c.iter().product::<i32>()).sum(); |
1009 | assert_eq!(par_sum_product_pairs, 12345); |
1010 | assert_eq!(par_sum_product_pairs, seq_sum_product_pairs); |
1011 | |
1012 | let par_sum_product_triples: i32 = a.par_chunks(3).map(|c| c.iter().product::<i32>()).sum(); |
1013 | let seq_sum_product_triples = a.chunks(3).map(|c| c.iter().product::<i32>()).sum(); |
1014 | assert_eq!(par_sum_product_triples, 5_0 + 12_00 + 20_000_000 + 1); |
1015 | assert_eq!(par_sum_product_triples, seq_sum_product_triples); |
1016 | } |
1017 | |
1018 | #[test] |
1019 | fn check_chunks_mut() { |
1020 | let mut a: Vec<i32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
1021 | let mut b: Vec<i32> = a.clone(); |
1022 | a.par_chunks_mut(2).for_each(|c| c[0] = c.iter().sum()); |
1023 | b.chunks_mut(2).for_each(|c| c[0] = c.iter().sum()); |
1024 | assert_eq!(a, &[3, 2, 7, 4, 11, 6, 15, 8, 19, 10]); |
1025 | assert_eq!(a, b); |
1026 | |
1027 | let mut a: Vec<i32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
1028 | let mut b: Vec<i32> = a.clone(); |
1029 | a.par_chunks_mut(3).for_each(|c| c[0] = c.iter().sum()); |
1030 | b.chunks_mut(3).for_each(|c| c[0] = c.iter().sum()); |
1031 | assert_eq!(a, &[6, 2, 3, 15, 5, 6, 24, 8, 9, 10]); |
1032 | assert_eq!(a, b); |
1033 | } |
1034 | |
1035 | #[test] |
1036 | fn check_windows() { |
1037 | let a: Vec<i32> = (0..1024).collect(); |
1038 | let par: Vec<_> = a.par_windows(2).collect(); |
1039 | let seq: Vec<_> = a.windows(2).collect(); |
1040 | assert_eq!(par, seq); |
1041 | |
1042 | let par: Vec<_> = a.par_windows(100).collect(); |
1043 | let seq: Vec<_> = a.windows(100).collect(); |
1044 | assert_eq!(par, seq); |
1045 | |
1046 | let par: Vec<_> = a.par_windows(1_000_000).collect(); |
1047 | let seq: Vec<_> = a.windows(1_000_000).collect(); |
1048 | assert_eq!(par, seq); |
1049 | |
1050 | let par: Vec<_> = a |
1051 | .par_windows(2) |
1052 | .chain(a.par_windows(1_000_000)) |
1053 | .zip(a.par_windows(2)) |
1054 | .collect(); |
1055 | let seq: Vec<_> = a |
1056 | .windows(2) |
1057 | .chain(a.windows(1_000_000)) |
1058 | .zip(a.windows(2)) |
1059 | .collect(); |
1060 | assert_eq!(par, seq); |
1061 | } |
1062 | |
1063 | #[test] |
1064 | fn check_options() { |
1065 | let mut a = vec![None, Some(1), None, None, Some(2), Some(4)]; |
1066 | |
1067 | assert_eq!(7, a.par_iter().flat_map(|opt| opt).sum::<i32>()); |
1068 | assert_eq!(7, a.par_iter().flat_map(|opt| opt).sum::<i32>()); |
1069 | |
1070 | a.par_iter_mut() |
1071 | .flat_map(|opt| opt) |
1072 | .for_each(|x| *x = *x * *x); |
1073 | |
1074 | assert_eq!(21, a.into_par_iter().flat_map(|opt| opt).sum::<i32>()); |
1075 | } |
1076 | |
1077 | #[test] |
1078 | fn check_results() { |
1079 | let mut a = vec![Err(()), Ok(1i32), Err(()), Err(()), Ok(2), Ok(4)]; |
1080 | |
1081 | assert_eq!(7, a.par_iter().flat_map(|res| res).sum::<i32>()); |
1082 | |
1083 | assert_eq!(Err::<i32, ()>(()), a.par_iter().cloned().sum()); |
1084 | assert_eq!(Ok(7), a.par_iter().cloned().filter(Result::is_ok).sum()); |
1085 | |
1086 | assert_eq!(Err::<i32, ()>(()), a.par_iter().cloned().product()); |
1087 | assert_eq!(Ok(8), a.par_iter().cloned().filter(Result::is_ok).product()); |
1088 | |
1089 | a.par_iter_mut() |
1090 | .flat_map(|res| res) |
1091 | .for_each(|x| *x = *x * *x); |
1092 | |
1093 | assert_eq!(21, a.into_par_iter().flat_map(|res| res).sum::<i32>()); |
1094 | } |
1095 | |
1096 | #[test] |
1097 | fn check_binary_heap() { |
1098 | use std::collections::BinaryHeap; |
1099 | |
1100 | let a: BinaryHeap<i32> = (0..10).collect(); |
1101 | |
1102 | assert_eq!(45, a.par_iter().sum::<i32>()); |
1103 | assert_eq!(45, a.into_par_iter().sum::<i32>()); |
1104 | } |
1105 | |
1106 | #[test] |
1107 | fn check_btree_map() { |
1108 | use std::collections::BTreeMap; |
1109 | |
1110 | let mut a: BTreeMap<i32, i32> = (0..10).map(|i| (i, -i)).collect(); |
1111 | |
1112 | assert_eq!(45, a.par_iter().map(|(&k, _)| k).sum::<i32>()); |
1113 | assert_eq!(-45, a.par_iter().map(|(_, &v)| v).sum::<i32>()); |
1114 | |
1115 | a.par_iter_mut().for_each(|(k, v)| *v += *k); |
1116 | |
1117 | assert_eq!(0, a.into_par_iter().map(|(_, v)| v).sum::<i32>()); |
1118 | } |
1119 | |
1120 | #[test] |
1121 | fn check_btree_set() { |
1122 | use std::collections::BTreeSet; |
1123 | |
1124 | let a: BTreeSet<i32> = (0..10).collect(); |
1125 | |
1126 | assert_eq!(45, a.par_iter().sum::<i32>()); |
1127 | assert_eq!(45, a.into_par_iter().sum::<i32>()); |
1128 | } |
1129 | |
1130 | #[test] |
1131 | fn check_hash_map() { |
1132 | use std::collections::HashMap; |
1133 | |
1134 | let mut a: HashMap<i32, i32> = (0..10).map(|i| (i, -i)).collect(); |
1135 | |
1136 | assert_eq!(45, a.par_iter().map(|(&k, _)| k).sum::<i32>()); |
1137 | assert_eq!(-45, a.par_iter().map(|(_, &v)| v).sum::<i32>()); |
1138 | |
1139 | a.par_iter_mut().for_each(|(k, v)| *v += *k); |
1140 | |
1141 | assert_eq!(0, a.into_par_iter().map(|(_, v)| v).sum::<i32>()); |
1142 | } |
1143 | |
1144 | #[test] |
1145 | fn check_hash_set() { |
1146 | use std::collections::HashSet; |
1147 | |
1148 | let a: HashSet<i32> = (0..10).collect(); |
1149 | |
1150 | assert_eq!(45, a.par_iter().sum::<i32>()); |
1151 | assert_eq!(45, a.into_par_iter().sum::<i32>()); |
1152 | } |
1153 | |
1154 | #[test] |
1155 | fn check_linked_list() { |
1156 | use std::collections::LinkedList; |
1157 | |
1158 | let mut a: LinkedList<i32> = (0..10).collect(); |
1159 | |
1160 | assert_eq!(45, a.par_iter().sum::<i32>()); |
1161 | |
1162 | a.par_iter_mut().for_each(|x| *x = -*x); |
1163 | |
1164 | assert_eq!(-45, a.into_par_iter().sum::<i32>()); |
1165 | } |
1166 | |
1167 | #[test] |
1168 | fn check_vec_deque() { |
1169 | use std::collections::VecDeque; |
1170 | |
1171 | let mut a: VecDeque<i32> = (0..10).collect(); |
1172 | |
1173 | // try to get it to wrap around |
1174 | a.drain(..5); |
1175 | a.extend(0..5); |
1176 | |
1177 | assert_eq!(45, a.par_iter().sum::<i32>()); |
1178 | |
1179 | a.par_iter_mut().for_each(|x| *x = -*x); |
1180 | |
1181 | assert_eq!(-45, a.into_par_iter().sum::<i32>()); |
1182 | } |
1183 | |
1184 | #[test] |
1185 | fn check_chain() { |
1186 | let mut res = vec![]; |
1187 | |
1188 | // stays indexed in the face of madness |
1189 | Some(0) |
1190 | .into_par_iter() |
1191 | .chain(Ok::<_, ()>(1)) |
1192 | .chain(1..4) |
1193 | .chain(Err("huh?" )) |
1194 | .chain(None) |
1195 | .chain(vec![5, 8, 13]) |
1196 | .map(|x| (x as u8 + b'a' ) as char) |
1197 | .chain(vec!['x' , 'y' , 'z' ]) |
1198 | .zip((0i32..1000).into_par_iter().map(|x| -x)) |
1199 | .enumerate() |
1200 | .map(|(a, (b, c))| (a, b, c)) |
1201 | .chain(None) |
1202 | .collect_into_vec(&mut res); |
1203 | |
1204 | assert_eq!( |
1205 | res, |
1206 | vec![ |
1207 | (0, 'a' , 0), |
1208 | (1, 'b' , -1), |
1209 | (2, 'b' , -2), |
1210 | (3, 'c' , -3), |
1211 | (4, 'd' , -4), |
1212 | (5, 'f' , -5), |
1213 | (6, 'i' , -6), |
1214 | (7, 'n' , -7), |
1215 | (8, 'x' , -8), |
1216 | (9, 'y' , -9), |
1217 | (10, 'z' , -10) |
1218 | ] |
1219 | ); |
1220 | |
1221 | // unindexed is ok too |
1222 | let res: Vec<i32> = Some(1i32) |
1223 | .into_par_iter() |
1224 | .chain( |
1225 | (2i32..4) |
1226 | .into_par_iter() |
1227 | .chain(vec![5, 6, 7, 8, 9]) |
1228 | .chain(Some((10, 100)).into_par_iter().flat_map(|(a, b)| a..b)) |
1229 | .filter(|x| x & 1 == 1), |
1230 | ) |
1231 | .collect(); |
1232 | let other: Vec<i32> = (0..100).filter(|x| x & 1 == 1).collect(); |
1233 | assert_eq!(res, other); |
1234 | |
1235 | // chain collect is ok with the "fake" specialization |
1236 | let res: Vec<i32> = Some(1i32).into_par_iter().chain(None).collect(); |
1237 | assert_eq!(res, &[1]); |
1238 | } |
1239 | |
1240 | #[test] |
1241 | fn check_count() { |
1242 | let c0 = (0_u32..24 * 1024).filter(|i| i % 2 == 0).count(); |
1243 | let c1 = (0_u32..24 * 1024) |
1244 | .into_par_iter() |
1245 | .filter(|i| i % 2 == 0) |
1246 | .count(); |
1247 | assert_eq!(c0, c1); |
1248 | } |
1249 | |
1250 | #[test] |
1251 | fn find_any() { |
1252 | let a: Vec<i32> = (0..1024).collect(); |
1253 | |
1254 | assert!(a.par_iter().find_any(|&&x| x % 42 == 41).is_some()); |
1255 | assert_eq!( |
1256 | a.par_iter().find_any(|&&x| x % 19 == 1 && x % 53 == 0), |
1257 | Some(&742_i32) |
1258 | ); |
1259 | assert_eq!(a.par_iter().find_any(|&&x| x < 0), None); |
1260 | |
1261 | assert!(a.par_iter().position_any(|&x| x % 42 == 41).is_some()); |
1262 | assert_eq!( |
1263 | a.par_iter().position_any(|&x| x % 19 == 1 && x % 53 == 0), |
1264 | Some(742_usize) |
1265 | ); |
1266 | assert_eq!(a.par_iter().position_any(|&x| x < 0), None); |
1267 | |
1268 | assert!(a.par_iter().any(|&x| x > 1000)); |
1269 | assert!(!a.par_iter().any(|&x| x < 0)); |
1270 | |
1271 | assert!(!a.par_iter().all(|&x| x > 1000)); |
1272 | assert!(a.par_iter().all(|&x| x >= 0)); |
1273 | } |
1274 | |
1275 | #[test] |
1276 | fn find_first_or_last() { |
1277 | let a: Vec<i32> = (0..1024).collect(); |
1278 | |
1279 | assert_eq!(a.par_iter().find_first(|&&x| x % 42 == 41), Some(&41_i32)); |
1280 | assert_eq!( |
1281 | a.par_iter().find_first(|&&x| x % 19 == 1 && x % 53 == 0), |
1282 | Some(&742_i32) |
1283 | ); |
1284 | assert_eq!(a.par_iter().find_first(|&&x| x < 0), None); |
1285 | |
1286 | assert_eq!( |
1287 | a.par_iter().position_first(|&x| x % 42 == 41), |
1288 | Some(41_usize) |
1289 | ); |
1290 | assert_eq!( |
1291 | a.par_iter().position_first(|&x| x % 19 == 1 && x % 53 == 0), |
1292 | Some(742_usize) |
1293 | ); |
1294 | assert_eq!(a.par_iter().position_first(|&x| x < 0), None); |
1295 | |
1296 | assert_eq!(a.par_iter().find_last(|&&x| x % 42 == 41), Some(&1007_i32)); |
1297 | assert_eq!( |
1298 | a.par_iter().find_last(|&&x| x % 19 == 1 && x % 53 == 0), |
1299 | Some(&742_i32) |
1300 | ); |
1301 | assert_eq!(a.par_iter().find_last(|&&x| x < 0), None); |
1302 | |
1303 | assert_eq!( |
1304 | a.par_iter().position_last(|&x| x % 42 == 41), |
1305 | Some(1007_usize) |
1306 | ); |
1307 | assert_eq!( |
1308 | a.par_iter().position_last(|&x| x % 19 == 1 && x % 53 == 0), |
1309 | Some(742_usize) |
1310 | ); |
1311 | assert_eq!(a.par_iter().position_last(|&x| x < 0), None); |
1312 | } |
1313 | |
1314 | #[test] |
1315 | fn find_map_first_or_last_or_any() { |
1316 | let mut a: Vec<i32> = vec![]; |
1317 | |
1318 | assert!(a.par_iter().find_map_any(half_if_positive).is_none()); |
1319 | assert!(a.par_iter().find_map_first(half_if_positive).is_none()); |
1320 | assert!(a.par_iter().find_map_last(half_if_positive).is_none()); |
1321 | |
1322 | a = (-1024..-3).collect(); |
1323 | |
1324 | assert!(a.par_iter().find_map_any(half_if_positive).is_none()); |
1325 | assert!(a.par_iter().find_map_first(half_if_positive).is_none()); |
1326 | assert!(a.par_iter().find_map_last(half_if_positive).is_none()); |
1327 | |
1328 | assert!(a.par_iter().find_map_any(half_if_negative).is_some()); |
1329 | assert_eq!( |
1330 | a.par_iter().find_map_first(half_if_negative), |
1331 | Some(-512_i32) |
1332 | ); |
1333 | assert_eq!(a.par_iter().find_map_last(half_if_negative), Some(-2_i32)); |
1334 | |
1335 | a.append(&mut (2..1025).collect()); |
1336 | |
1337 | assert!(a.par_iter().find_map_any(half_if_positive).is_some()); |
1338 | assert_eq!(a.par_iter().find_map_first(half_if_positive), Some(1_i32)); |
1339 | assert_eq!(a.par_iter().find_map_last(half_if_positive), Some(512_i32)); |
1340 | |
1341 | fn half_if_positive(x: &i32) -> Option<i32> { |
1342 | if *x > 0 { |
1343 | Some(x / 2) |
1344 | } else { |
1345 | None |
1346 | } |
1347 | } |
1348 | |
1349 | fn half_if_negative(x: &i32) -> Option<i32> { |
1350 | if *x < 0 { |
1351 | Some(x / 2) |
1352 | } else { |
1353 | None |
1354 | } |
1355 | } |
1356 | } |
1357 | |
1358 | #[test] |
1359 | fn check_find_not_present() { |
1360 | let counter = AtomicUsize::new(0); |
1361 | let value: Option<i32> = (0_i32..2048).into_par_iter().find_any(|&p| { |
1362 | counter.fetch_add(1, Ordering::SeqCst); |
1363 | p >= 2048 |
1364 | }); |
1365 | assert!(value.is_none()); |
1366 | assert!(counter.load(Ordering::SeqCst) == 2048); // should have visited every single one |
1367 | } |
1368 | |
1369 | #[test] |
1370 | fn check_find_is_present() { |
1371 | let counter = AtomicUsize::new(0); |
1372 | let value: Option<i32> = (0_i32..2048).into_par_iter().find_any(|&p| { |
1373 | counter.fetch_add(1, Ordering::SeqCst); |
1374 | (1024..1096).contains(&p) |
1375 | }); |
1376 | let q = value.unwrap(); |
1377 | assert!((1024..1096).contains(&q)); |
1378 | assert!(counter.load(Ordering::SeqCst) < 2048); // should not have visited every single one |
1379 | } |
1380 | |
1381 | #[test] |
1382 | fn check_while_some() { |
1383 | let value = (0_i32..2048).into_par_iter().map(Some).while_some().max(); |
1384 | assert_eq!(value, Some(2047)); |
1385 | |
1386 | let counter = AtomicUsize::new(0); |
1387 | let value = (0_i32..2048) |
1388 | .into_par_iter() |
1389 | .map(|x| { |
1390 | counter.fetch_add(1, Ordering::SeqCst); |
1391 | if x < 1024 { |
1392 | Some(x) |
1393 | } else { |
1394 | None |
1395 | } |
1396 | }) |
1397 | .while_some() |
1398 | .max(); |
1399 | assert!(value < Some(1024)); |
1400 | assert!(counter.load(Ordering::SeqCst) < 2048); // should not have visited every single one |
1401 | } |
1402 | |
1403 | #[test] |
1404 | fn par_iter_collect_option() { |
1405 | let a: Option<Vec<_>> = (0_i32..2048).map(Some).collect(); |
1406 | let b: Option<Vec<_>> = (0_i32..2048).into_par_iter().map(Some).collect(); |
1407 | assert_eq!(a, b); |
1408 | |
1409 | let c: Option<Vec<_>> = (0_i32..2048) |
1410 | .into_par_iter() |
1411 | .map(|x| if x == 1234 { None } else { Some(x) }) |
1412 | .collect(); |
1413 | assert_eq!(c, None); |
1414 | } |
1415 | |
1416 | #[test] |
1417 | fn par_iter_collect_result() { |
1418 | let a: Result<Vec<_>, ()> = (0_i32..2048).map(Ok).collect(); |
1419 | let b: Result<Vec<_>, ()> = (0_i32..2048).into_par_iter().map(Ok).collect(); |
1420 | assert_eq!(a, b); |
1421 | |
1422 | let c: Result<Vec<_>, _> = (0_i32..2048) |
1423 | .into_par_iter() |
1424 | .map(|x| if x == 1234 { Err(x) } else { Ok(x) }) |
1425 | .collect(); |
1426 | assert_eq!(c, Err(1234)); |
1427 | |
1428 | let d: Result<Vec<_>, _> = (0_i32..2048) |
1429 | .into_par_iter() |
1430 | .map(|x| if x % 100 == 99 { Err(x) } else { Ok(x) }) |
1431 | .collect(); |
1432 | assert_eq!(d.map_err(|x| x % 100), Err(99)); |
1433 | } |
1434 | |
1435 | #[test] |
1436 | fn par_iter_collect() { |
1437 | let a: Vec<i32> = (0..1024).collect(); |
1438 | let b: Vec<i32> = a.par_iter().map(|&i| i + 1).collect(); |
1439 | let c: Vec<i32> = (0..1024).map(|i| i + 1).collect(); |
1440 | assert_eq!(b, c); |
1441 | } |
1442 | |
1443 | #[test] |
1444 | fn par_iter_collect_vecdeque() { |
1445 | let a: Vec<i32> = (0..1024).collect(); |
1446 | let b: VecDeque<i32> = a.par_iter().cloned().collect(); |
1447 | let c: VecDeque<i32> = a.iter().cloned().collect(); |
1448 | assert_eq!(b, c); |
1449 | } |
1450 | |
1451 | #[test] |
1452 | fn par_iter_collect_binaryheap() { |
1453 | let a: Vec<i32> = (0..1024).collect(); |
1454 | let mut b: BinaryHeap<i32> = a.par_iter().cloned().collect(); |
1455 | assert_eq!(b.peek(), Some(&1023)); |
1456 | assert_eq!(b.len(), 1024); |
1457 | for n in (0..1024).rev() { |
1458 | assert_eq!(b.pop(), Some(n)); |
1459 | assert_eq!(b.len() as i32, n); |
1460 | } |
1461 | } |
1462 | |
1463 | #[test] |
1464 | fn par_iter_collect_hashmap() { |
1465 | let a: Vec<i32> = (0..1024).collect(); |
1466 | let b: HashMap<i32, String> = a.par_iter().map(|&i| (i, format!("{}" , i))).collect(); |
1467 | assert_eq!(&b[&3], "3" ); |
1468 | assert_eq!(b.len(), 1024); |
1469 | } |
1470 | |
1471 | #[test] |
1472 | fn par_iter_collect_hashset() { |
1473 | let a: Vec<i32> = (0..1024).collect(); |
1474 | let b: HashSet<i32> = a.par_iter().cloned().collect(); |
1475 | assert_eq!(b.len(), 1024); |
1476 | } |
1477 | |
1478 | #[test] |
1479 | fn par_iter_collect_btreemap() { |
1480 | let a: Vec<i32> = (0..1024).collect(); |
1481 | let b: BTreeMap<i32, String> = a.par_iter().map(|&i| (i, format!("{}" , i))).collect(); |
1482 | assert_eq!(&b[&3], "3" ); |
1483 | assert_eq!(b.len(), 1024); |
1484 | } |
1485 | |
1486 | #[test] |
1487 | fn par_iter_collect_btreeset() { |
1488 | let a: Vec<i32> = (0..1024).collect(); |
1489 | let b: BTreeSet<i32> = a.par_iter().cloned().collect(); |
1490 | assert_eq!(b.len(), 1024); |
1491 | } |
1492 | |
1493 | #[test] |
1494 | fn par_iter_collect_linked_list() { |
1495 | let a: Vec<i32> = (0..1024).collect(); |
1496 | let b: LinkedList<_> = a.par_iter().map(|&i| (i, format!("{}" , i))).collect(); |
1497 | let c: LinkedList<_> = a.iter().map(|&i| (i, format!("{}" , i))).collect(); |
1498 | assert_eq!(b, c); |
1499 | } |
1500 | |
1501 | #[test] |
1502 | fn par_iter_collect_linked_list_flat_map_filter() { |
1503 | let b: LinkedList<i32> = (0_i32..1024) |
1504 | .into_par_iter() |
1505 | .flat_map(|i| (0..i)) |
1506 | .filter(|&i| i % 2 == 0) |
1507 | .collect(); |
1508 | let c: LinkedList<i32> = (0_i32..1024) |
1509 | .flat_map(|i| (0..i)) |
1510 | .filter(|&i| i % 2 == 0) |
1511 | .collect(); |
1512 | assert_eq!(b, c); |
1513 | } |
1514 | |
1515 | #[test] |
1516 | fn par_iter_collect_cows() { |
1517 | use std::borrow::Cow; |
1518 | |
1519 | let s = "Fearless Concurrency with Rust" ; |
1520 | |
1521 | // Collects `i32` into a `Vec` |
1522 | let a: Cow<'_, [i32]> = (0..1024).collect(); |
1523 | let b: Cow<'_, [i32]> = a.par_iter().cloned().collect(); |
1524 | assert_eq!(a, b); |
1525 | |
1526 | // Collects `char` into a `String` |
1527 | let a: Cow<'_, str> = s.chars().collect(); |
1528 | let b: Cow<'_, str> = s.par_chars().collect(); |
1529 | assert_eq!(a, b); |
1530 | |
1531 | // Collects `str` into a `String` |
1532 | let a: Cow<'_, str> = s.split_whitespace().collect(); |
1533 | let b: Cow<'_, str> = s.par_split_whitespace().collect(); |
1534 | assert_eq!(a, b); |
1535 | |
1536 | // Collects `String` into a `String` |
1537 | let a: Cow<'_, str> = s.split_whitespace().map(str::to_owned).collect(); |
1538 | let b: Cow<'_, str> = s.par_split_whitespace().map(str::to_owned).collect(); |
1539 | assert_eq!(a, b); |
1540 | } |
1541 | |
1542 | #[test] |
1543 | fn par_iter_unindexed_flat_map() { |
1544 | let b: Vec<i64> = (0_i64..1024).into_par_iter().flat_map(Some).collect(); |
1545 | let c: Vec<i64> = (0_i64..1024).flat_map(Some).collect(); |
1546 | assert_eq!(b, c); |
1547 | } |
1548 | |
1549 | #[test] |
1550 | fn min_max() { |
1551 | let rng = seeded_rng(); |
1552 | let a: Vec<i32> = rng.sample_iter(&Standard).take(1024).collect(); |
1553 | for i in 0..=a.len() { |
1554 | let slice = &a[..i]; |
1555 | assert_eq!(slice.par_iter().min(), slice.iter().min()); |
1556 | assert_eq!(slice.par_iter().max(), slice.iter().max()); |
1557 | } |
1558 | } |
1559 | |
1560 | #[test] |
1561 | fn min_max_by() { |
1562 | let rng = seeded_rng(); |
1563 | // Make sure there are duplicate keys, for testing sort stability |
1564 | let r: Vec<i32> = rng.sample_iter(&Standard).take(512).collect(); |
1565 | let a: Vec<(i32, u16)> = r.iter().chain(&r).cloned().zip(0..).collect(); |
1566 | for i in 0..=a.len() { |
1567 | let slice = &a[..i]; |
1568 | assert_eq!( |
1569 | slice.par_iter().min_by(|x, y| x.0.cmp(&y.0)), |
1570 | slice.iter().min_by(|x, y| x.0.cmp(&y.0)) |
1571 | ); |
1572 | assert_eq!( |
1573 | slice.par_iter().max_by(|x, y| x.0.cmp(&y.0)), |
1574 | slice.iter().max_by(|x, y| x.0.cmp(&y.0)) |
1575 | ); |
1576 | } |
1577 | } |
1578 | |
1579 | #[test] |
1580 | fn min_max_by_key() { |
1581 | let rng = seeded_rng(); |
1582 | // Make sure there are duplicate keys, for testing sort stability |
1583 | let r: Vec<i32> = rng.sample_iter(&Standard).take(512).collect(); |
1584 | let a: Vec<(i32, u16)> = r.iter().chain(&r).cloned().zip(0..).collect(); |
1585 | for i in 0..=a.len() { |
1586 | let slice = &a[..i]; |
1587 | assert_eq!( |
1588 | slice.par_iter().min_by_key(|x| x.0), |
1589 | slice.iter().min_by_key(|x| x.0) |
1590 | ); |
1591 | assert_eq!( |
1592 | slice.par_iter().max_by_key(|x| x.0), |
1593 | slice.iter().max_by_key(|x| x.0) |
1594 | ); |
1595 | } |
1596 | } |
1597 | |
1598 | #[test] |
1599 | fn check_rev() { |
1600 | let a: Vec<usize> = (0..1024).rev().collect(); |
1601 | let b: Vec<usize> = (0..1024).collect(); |
1602 | |
1603 | assert!(a.par_iter().rev().zip(b).all(|(&a, b)| a == b)); |
1604 | } |
1605 | |
1606 | #[test] |
1607 | fn scope_mix() { |
1608 | let counter_p = &AtomicUsize::new(0); |
1609 | scope(|s| { |
1610 | s.spawn(move |s| { |
1611 | divide_and_conquer(s, counter_p, 1024); |
1612 | }); |
1613 | s.spawn(move |_| { |
1614 | let a: Vec<i32> = (0..1024).collect(); |
1615 | let r1 = a.par_iter().map(|&i| i + 1).reduce_with(|i, j| i + j); |
1616 | let r2 = a.iter().map(|&i| i + 1).sum(); |
1617 | assert_eq!(r1.unwrap(), r2); |
1618 | }); |
1619 | }); |
1620 | } |
1621 | |
1622 | fn divide_and_conquer<'scope>(scope: &Scope<'scope>, counter: &'scope AtomicUsize, size: usize) { |
1623 | if size > 1 { |
1624 | scope.spawn(move |scope| divide_and_conquer(scope, counter, size / 2)); |
1625 | scope.spawn(move |scope| divide_and_conquer(scope, counter, size / 2)); |
1626 | } else { |
1627 | // count the leaves |
1628 | counter.fetch_add(1, Ordering::SeqCst); |
1629 | } |
1630 | } |
1631 | |
1632 | #[test] |
1633 | fn check_split() { |
1634 | use std::ops::Range; |
1635 | |
1636 | let a = (0..1024).into_par_iter(); |
1637 | |
1638 | let b = split(0..1024, |Range { start, end }| { |
1639 | let mid = (end - start) / 2; |
1640 | if mid > start { |
1641 | (start..mid, Some(mid..end)) |
1642 | } else { |
1643 | (start..end, None) |
1644 | } |
1645 | }) |
1646 | .flat_map(|range| range); |
1647 | |
1648 | assert_eq!(a.collect::<Vec<_>>(), b.collect::<Vec<_>>()); |
1649 | } |
1650 | |
1651 | #[test] |
1652 | fn check_lengths() { |
1653 | fn check(min: usize, max: usize) { |
1654 | let range = 0..1024 * 1024; |
1655 | |
1656 | // Check against normalized values. |
1657 | let min_check = cmp::min(cmp::max(min, 1), range.len()); |
1658 | let max_check = cmp::max(max, min_check.saturating_add(min_check - 1)); |
1659 | |
1660 | assert!( |
1661 | range |
1662 | .into_par_iter() |
1663 | .with_min_len(min) |
1664 | .with_max_len(max) |
1665 | .fold(|| 0, |count, _| count + 1) |
1666 | .all(|c| c >= min_check && c <= max_check), |
1667 | "check_lengths failed {:?} -> {:?} " , |
1668 | (min, max), |
1669 | (min_check, max_check) |
1670 | ); |
1671 | } |
1672 | |
1673 | let lengths = [0, 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, usize::MAX]; |
1674 | for &min in &lengths { |
1675 | for &max in &lengths { |
1676 | check(min, max); |
1677 | } |
1678 | } |
1679 | } |
1680 | |
1681 | #[test] |
1682 | fn check_map_with() { |
1683 | let (sender, receiver) = mpsc::channel(); |
1684 | let a: HashSet<_> = (0..1024).collect(); |
1685 | |
1686 | a.par_iter() |
1687 | .cloned() |
1688 | .map_with(sender, |s, i| s.send(i).unwrap()) |
1689 | .count(); |
1690 | |
1691 | let b: HashSet<_> = receiver.iter().collect(); |
1692 | assert_eq!(a, b); |
1693 | } |
1694 | |
1695 | #[test] |
1696 | fn check_fold_with() { |
1697 | let (sender, receiver) = mpsc::channel(); |
1698 | let a: HashSet<_> = (0..1024).collect(); |
1699 | |
1700 | a.par_iter() |
1701 | .cloned() |
1702 | .fold_with(sender, |s, i| { |
1703 | s.send(i).unwrap(); |
1704 | s |
1705 | }) |
1706 | .count(); |
1707 | |
1708 | let b: HashSet<_> = receiver.iter().collect(); |
1709 | assert_eq!(a, b); |
1710 | } |
1711 | |
1712 | #[test] |
1713 | fn check_for_each_with() { |
1714 | let (sender, receiver) = mpsc::channel(); |
1715 | let a: HashSet<_> = (0..1024).collect(); |
1716 | |
1717 | a.par_iter() |
1718 | .cloned() |
1719 | .for_each_with(sender, |s, i| s.send(i).unwrap()); |
1720 | |
1721 | let b: HashSet<_> = receiver.iter().collect(); |
1722 | assert_eq!(a, b); |
1723 | } |
1724 | |
1725 | #[test] |
1726 | fn check_extend_items() { |
1727 | fn check<C>() |
1728 | where |
1729 | C: Default |
1730 | + Eq |
1731 | + Debug |
1732 | + Extend<i32> |
1733 | + for<'a> Extend<&'a i32> |
1734 | + ParallelExtend<i32> |
1735 | + for<'a> ParallelExtend<&'a i32>, |
1736 | { |
1737 | let mut serial = C::default(); |
1738 | let mut parallel = C::default(); |
1739 | |
1740 | // extend with references |
1741 | let v: Vec<_> = (0..128).collect(); |
1742 | serial.extend(&v); |
1743 | parallel.par_extend(&v); |
1744 | assert_eq!(serial, parallel); |
1745 | |
1746 | // extend with values |
1747 | serial.extend(-128..0); |
1748 | parallel.par_extend(-128..0); |
1749 | assert_eq!(serial, parallel); |
1750 | } |
1751 | |
1752 | check::<BTreeSet<_>>(); |
1753 | check::<HashSet<_>>(); |
1754 | check::<LinkedList<_>>(); |
1755 | check::<Vec<_>>(); |
1756 | check::<VecDeque<_>>(); |
1757 | } |
1758 | |
1759 | #[test] |
1760 | fn check_extend_heap() { |
1761 | let mut serial: BinaryHeap<_> = Default::default(); |
1762 | let mut parallel: BinaryHeap<_> = Default::default(); |
1763 | |
1764 | // extend with references |
1765 | let v: Vec<_> = (0..128).collect(); |
1766 | serial.extend(&v); |
1767 | parallel.par_extend(&v); |
1768 | assert_eq!( |
1769 | serial.clone().into_sorted_vec(), |
1770 | parallel.clone().into_sorted_vec() |
1771 | ); |
1772 | |
1773 | // extend with values |
1774 | serial.extend(-128..0); |
1775 | parallel.par_extend(-128..0); |
1776 | assert_eq!(serial.into_sorted_vec(), parallel.into_sorted_vec()); |
1777 | } |
1778 | |
1779 | #[test] |
1780 | fn check_extend_pairs() { |
1781 | fn check<C>() |
1782 | where |
1783 | C: Default |
1784 | + Eq |
1785 | + Debug |
1786 | + Extend<(usize, i32)> |
1787 | + for<'a> Extend<(&'a usize, &'a i32)> |
1788 | + ParallelExtend<(usize, i32)> |
1789 | + for<'a> ParallelExtend<(&'a usize, &'a i32)>, |
1790 | { |
1791 | let mut serial = C::default(); |
1792 | let mut parallel = C::default(); |
1793 | |
1794 | // extend with references |
1795 | let m: HashMap<_, _> = (0..128).enumerate().collect(); |
1796 | serial.extend(&m); |
1797 | parallel.par_extend(&m); |
1798 | assert_eq!(serial, parallel); |
1799 | |
1800 | // extend with values |
1801 | let v: Vec<(_, _)> = (-128..0).enumerate().collect(); |
1802 | serial.extend(v.clone()); |
1803 | parallel.par_extend(v); |
1804 | assert_eq!(serial, parallel); |
1805 | } |
1806 | |
1807 | check::<BTreeMap<usize, i32>>(); |
1808 | check::<HashMap<usize, i32>>(); |
1809 | } |
1810 | |
1811 | #[test] |
1812 | fn check_unzip_into_vecs() { |
1813 | let mut a = vec![]; |
1814 | let mut b = vec![]; |
1815 | (0..1024) |
1816 | .into_par_iter() |
1817 | .map(|i| i * i) |
1818 | .enumerate() |
1819 | .unzip_into_vecs(&mut a, &mut b); |
1820 | |
1821 | let (c, d): (Vec<_>, Vec<_>) = (0..1024).map(|i| i * i).enumerate().unzip(); |
1822 | assert_eq!(a, c); |
1823 | assert_eq!(b, d); |
1824 | } |
1825 | |
1826 | #[test] |
1827 | fn check_unzip() { |
1828 | // indexed, unindexed |
1829 | let (a, b): (Vec<_>, HashSet<_>) = (0..1024).into_par_iter().map(|i| i * i).enumerate().unzip(); |
1830 | let (c, d): (Vec<_>, HashSet<_>) = (0..1024).map(|i| i * i).enumerate().unzip(); |
1831 | assert_eq!(a, c); |
1832 | assert_eq!(b, d); |
1833 | |
1834 | // unindexed, indexed |
1835 | let (a, b): (HashSet<_>, Vec<_>) = (0..1024).into_par_iter().map(|i| i * i).enumerate().unzip(); |
1836 | let (c, d): (HashSet<_>, Vec<_>) = (0..1024).map(|i| i * i).enumerate().unzip(); |
1837 | assert_eq!(a, c); |
1838 | assert_eq!(b, d); |
1839 | |
1840 | // indexed, indexed |
1841 | let (a, b): (Vec<_>, Vec<_>) = (0..1024).into_par_iter().map(|i| i * i).enumerate().unzip(); |
1842 | let (c, d): (Vec<_>, Vec<_>) = (0..1024).map(|i| i * i).enumerate().unzip(); |
1843 | assert_eq!(a, c); |
1844 | assert_eq!(b, d); |
1845 | |
1846 | // unindexed producer |
1847 | let (a, b): (Vec<_>, Vec<_>) = (0..1024) |
1848 | .into_par_iter() |
1849 | .filter_map(|i| Some((i, i * i))) |
1850 | .unzip(); |
1851 | let (c, d): (Vec<_>, Vec<_>) = (0..1024).map(|i| (i, i * i)).unzip(); |
1852 | assert_eq!(a, c); |
1853 | assert_eq!(b, d); |
1854 | } |
1855 | |
1856 | #[test] |
1857 | fn check_partition() { |
1858 | let (a, b): (Vec<_>, Vec<_>) = (0..1024).into_par_iter().partition(|&i| i % 3 == 0); |
1859 | let (c, d): (Vec<_>, Vec<_>) = (0..1024).partition(|&i| i % 3 == 0); |
1860 | assert_eq!(a, c); |
1861 | assert_eq!(b, d); |
1862 | } |
1863 | |
1864 | #[test] |
1865 | fn check_partition_map() { |
1866 | let input = "a b c 1 2 3 x y z" ; |
1867 | let (a, b): (Vec<_>, String) = |
1868 | input |
1869 | .par_split_whitespace() |
1870 | .partition_map(|s| match s.parse::<i32>() { |
1871 | Ok(n) => Either::Left(n), |
1872 | Err(_) => Either::Right(s), |
1873 | }); |
1874 | assert_eq!(a, vec![1, 2, 3]); |
1875 | assert_eq!(b, "abcxyz" ); |
1876 | } |
1877 | |
1878 | #[test] |
1879 | fn check_either() { |
1880 | type I = crate::vec::IntoIter<i32>; |
1881 | type E = Either<I, I>; |
1882 | |
1883 | let v: Vec<i32> = (0..1024).collect(); |
1884 | |
1885 | // try iterating the left side |
1886 | let left: E = Either::Left(v.clone().into_par_iter()); |
1887 | assert!(left.eq(v.clone())); |
1888 | |
1889 | // try iterating the right side |
1890 | let right: E = Either::Right(v.clone().into_par_iter()); |
1891 | assert!(right.eq(v.clone())); |
1892 | |
1893 | // try an indexed iterator |
1894 | let left: E = Either::Left(v.clone().into_par_iter()); |
1895 | assert!(left.enumerate().eq(v.into_par_iter().enumerate())); |
1896 | } |
1897 | |
1898 | #[test] |
1899 | fn check_either_extend() { |
1900 | type E = Either<Vec<i32>, HashSet<i32>>; |
1901 | |
1902 | let v: Vec<i32> = (0..1024).collect(); |
1903 | |
1904 | // try extending the left side |
1905 | let mut left: E = Either::Left(vec![]); |
1906 | left.par_extend(v.clone()); |
1907 | assert_eq!(left.as_ref(), Either::Left(&v)); |
1908 | |
1909 | // try extending the right side |
1910 | let mut right: E = Either::Right(HashSet::default()); |
1911 | right.par_extend(v.clone()); |
1912 | assert_eq!(right, Either::Right(v.iter().cloned().collect())); |
1913 | } |
1914 | |
1915 | #[test] |
1916 | fn check_interleave_eq() { |
1917 | let xs: Vec<usize> = (0..10).collect(); |
1918 | let ys: Vec<usize> = (10..20).collect(); |
1919 | |
1920 | let mut actual = vec![]; |
1921 | xs.par_iter() |
1922 | .interleave(&ys) |
1923 | .map(|&i| i) |
1924 | .collect_into_vec(&mut actual); |
1925 | |
1926 | let expected: Vec<usize> = (0..10) |
1927 | .zip(10..20) |
1928 | .flat_map(|(i, j)| vec![i, j].into_iter()) |
1929 | .collect(); |
1930 | assert_eq!(expected, actual); |
1931 | } |
1932 | |
1933 | #[test] |
1934 | fn check_interleave_uneven() { |
1935 | let cases: Vec<(Vec<usize>, Vec<usize>, Vec<usize>)> = vec![ |
1936 | ( |
1937 | (0..9).collect(), |
1938 | vec![10], |
1939 | vec![0, 10, 1, 2, 3, 4, 5, 6, 7, 8], |
1940 | ), |
1941 | ( |
1942 | vec![10], |
1943 | (0..9).collect(), |
1944 | vec![10, 0, 1, 2, 3, 4, 5, 6, 7, 8], |
1945 | ), |
1946 | ( |
1947 | (0..5).collect(), |
1948 | (5..10).collect(), |
1949 | (0..5) |
1950 | .zip(5..10) |
1951 | .flat_map(|(i, j)| vec![i, j].into_iter()) |
1952 | .collect(), |
1953 | ), |
1954 | (vec![], (0..9).collect(), (0..9).collect()), |
1955 | ((0..9).collect(), vec![], (0..9).collect()), |
1956 | ( |
1957 | (0..50).collect(), |
1958 | (50..100).collect(), |
1959 | (0..50) |
1960 | .zip(50..100) |
1961 | .flat_map(|(i, j)| vec![i, j].into_iter()) |
1962 | .collect(), |
1963 | ), |
1964 | ]; |
1965 | |
1966 | for (i, (xs, ys, expected)) in cases.into_iter().enumerate() { |
1967 | let mut res = vec![]; |
1968 | xs.par_iter() |
1969 | .interleave(&ys) |
1970 | .map(|&i| i) |
1971 | .collect_into_vec(&mut res); |
1972 | assert_eq!(expected, res, "Case {} failed" , i); |
1973 | |
1974 | res.truncate(0); |
1975 | xs.par_iter() |
1976 | .interleave(&ys) |
1977 | .rev() |
1978 | .map(|&i| i) |
1979 | .collect_into_vec(&mut res); |
1980 | assert_eq!( |
1981 | expected.into_iter().rev().collect::<Vec<usize>>(), |
1982 | res, |
1983 | "Case {} reversed failed" , |
1984 | i |
1985 | ); |
1986 | } |
1987 | } |
1988 | |
1989 | #[test] |
1990 | fn check_interleave_shortest() { |
1991 | let cases: Vec<(Vec<usize>, Vec<usize>, Vec<usize>)> = vec![ |
1992 | ((0..9).collect(), vec![10], vec![0, 10, 1]), |
1993 | (vec![10], (0..9).collect(), vec![10, 0]), |
1994 | ( |
1995 | (0..5).collect(), |
1996 | (5..10).collect(), |
1997 | (0..5) |
1998 | .zip(5..10) |
1999 | .flat_map(|(i, j)| vec![i, j].into_iter()) |
2000 | .collect(), |
2001 | ), |
2002 | (vec![], (0..9).collect(), vec![]), |
2003 | ((0..9).collect(), vec![], vec![0]), |
2004 | ( |
2005 | (0..50).collect(), |
2006 | (50..100).collect(), |
2007 | (0..50) |
2008 | .zip(50..100) |
2009 | .flat_map(|(i, j)| vec![i, j].into_iter()) |
2010 | .collect(), |
2011 | ), |
2012 | ]; |
2013 | |
2014 | for (i, (xs, ys, expected)) in cases.into_iter().enumerate() { |
2015 | let mut res = vec![]; |
2016 | xs.par_iter() |
2017 | .interleave_shortest(&ys) |
2018 | .map(|&i| i) |
2019 | .collect_into_vec(&mut res); |
2020 | assert_eq!(expected, res, "Case {} failed" , i); |
2021 | |
2022 | res.truncate(0); |
2023 | xs.par_iter() |
2024 | .interleave_shortest(&ys) |
2025 | .rev() |
2026 | .map(|&i| i) |
2027 | .collect_into_vec(&mut res); |
2028 | assert_eq!( |
2029 | expected.into_iter().rev().collect::<Vec<usize>>(), |
2030 | res, |
2031 | "Case {} reversed failed" , |
2032 | i |
2033 | ); |
2034 | } |
2035 | } |
2036 | |
2037 | #[test] |
2038 | #[should_panic (expected = "chunk_size must not be zero" )] |
2039 | fn check_chunks_zero_size() { |
2040 | let _: Vec<Vec<i32>> = vec![1, 2, 3].into_par_iter().chunks(0).collect(); |
2041 | } |
2042 | |
2043 | #[test] |
2044 | fn check_chunks_even_size() { |
2045 | assert_eq!( |
2046 | vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]], |
2047 | (1..10).into_par_iter().chunks(3).collect::<Vec<Vec<i32>>>() |
2048 | ); |
2049 | } |
2050 | |
2051 | #[test] |
2052 | fn check_chunks_empty() { |
2053 | let v: Vec<i32> = vec![]; |
2054 | let expected: Vec<Vec<i32>> = vec![]; |
2055 | assert_eq!( |
2056 | expected, |
2057 | v.into_par_iter().chunks(2).collect::<Vec<Vec<i32>>>() |
2058 | ); |
2059 | } |
2060 | |
2061 | #[test] |
2062 | fn check_chunks_len() { |
2063 | assert_eq!(4, (0..8).into_par_iter().chunks(2).len()); |
2064 | assert_eq!(3, (0..9).into_par_iter().chunks(3).len()); |
2065 | assert_eq!(3, (0..8).into_par_iter().chunks(3).len()); |
2066 | assert_eq!(1, [1].par_iter().chunks(3).len()); |
2067 | assert_eq!(0, (0..0).into_par_iter().chunks(3).len()); |
2068 | } |
2069 | |
2070 | #[test] |
2071 | fn check_chunks_uneven() { |
2072 | let cases: Vec<(Vec<u32>, usize, Vec<Vec<u32>>)> = vec![ |
2073 | ((0..5).collect(), 3, vec![vec![0, 1, 2], vec![3, 4]]), |
2074 | (vec![1], 5, vec![vec![1]]), |
2075 | ((0..4).collect(), 3, vec![vec![0, 1, 2], vec![3]]), |
2076 | ]; |
2077 | |
2078 | for (i, (v, n, expected)) in cases.into_iter().enumerate() { |
2079 | let mut res: Vec<Vec<u32>> = vec![]; |
2080 | v.par_iter() |
2081 | .chunks(n) |
2082 | .map(|v| v.into_iter().cloned().collect()) |
2083 | .collect_into_vec(&mut res); |
2084 | assert_eq!(expected, res, "Case {} failed" , i); |
2085 | |
2086 | res.truncate(0); |
2087 | v.into_par_iter().chunks(n).rev().collect_into_vec(&mut res); |
2088 | assert_eq!( |
2089 | expected.into_iter().rev().collect::<Vec<Vec<u32>>>(), |
2090 | res, |
2091 | "Case {} reversed failed" , |
2092 | i |
2093 | ); |
2094 | } |
2095 | } |
2096 | |
2097 | #[test] |
2098 | #[ignore ] // it's quick enough on optimized 32-bit platforms, but otherwise... ... ... |
2099 | #[should_panic (expected = "overflow" )] |
2100 | #[cfg (debug_assertions)] |
2101 | fn check_repeat_unbounded() { |
2102 | // use just one thread, so we don't get infinite adaptive splitting |
2103 | // (forever stealing and re-splitting jobs that will panic on overflow) |
2104 | let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap(); |
2105 | pool.install(|| { |
2106 | println!("counted {} repeats" , repeat(()).count()); |
2107 | }); |
2108 | } |
2109 | |
2110 | #[test] |
2111 | fn check_repeat_find_any() { |
2112 | let even = repeat(4).find_any(|&x| x % 2 == 0); |
2113 | assert_eq!(even, Some(4)); |
2114 | } |
2115 | |
2116 | #[test] |
2117 | fn check_repeat_take() { |
2118 | let v: Vec<_> = repeat(4).take(4).collect(); |
2119 | assert_eq!(v, [4, 4, 4, 4]); |
2120 | } |
2121 | |
2122 | #[test] |
2123 | fn check_repeat_zip() { |
2124 | let v = vec![4, 4, 4, 4]; |
2125 | let mut fours: Vec<_> = repeat(4).zip(v).collect(); |
2126 | assert_eq!(fours.len(), 4); |
2127 | while let Some(item) = fours.pop() { |
2128 | assert_eq!(item, (4, 4)); |
2129 | } |
2130 | } |
2131 | |
2132 | #[test] |
2133 | fn check_repeatn_zip_left() { |
2134 | let v = vec![4, 4, 4, 4]; |
2135 | let mut fours: Vec<_> = repeatn(4, usize::MAX).zip(v).collect(); |
2136 | assert_eq!(fours.len(), 4); |
2137 | while let Some(item) = fours.pop() { |
2138 | assert_eq!(item, (4, 4)); |
2139 | } |
2140 | } |
2141 | |
2142 | #[test] |
2143 | fn check_repeatn_zip_right() { |
2144 | let v = vec![4, 4, 4, 4]; |
2145 | let mut fours: Vec<_> = v.into_par_iter().zip(repeatn(4, usize::MAX)).collect(); |
2146 | assert_eq!(fours.len(), 4); |
2147 | while let Some(item) = fours.pop() { |
2148 | assert_eq!(item, (4, 4)); |
2149 | } |
2150 | } |
2151 | |
2152 | #[test] |
2153 | fn check_empty() { |
2154 | // drive_unindexed |
2155 | let mut v: Vec<i32> = empty().filter(|_| unreachable!()).collect(); |
2156 | assert!(v.is_empty()); |
2157 | |
2158 | // drive (indexed) |
2159 | empty().collect_into_vec(&mut v); |
2160 | assert!(v.is_empty()); |
2161 | |
2162 | // with_producer |
2163 | let v: Vec<(i32, i32)> = empty().zip(1..10).collect(); |
2164 | assert!(v.is_empty()); |
2165 | } |
2166 | |
2167 | #[test] |
2168 | fn check_once() { |
2169 | // drive_unindexed |
2170 | let mut v: Vec<i32> = once(42).filter(|_| true).collect(); |
2171 | assert_eq!(v, &[42]); |
2172 | |
2173 | // drive (indexed) |
2174 | once(42).collect_into_vec(&mut v); |
2175 | assert_eq!(v, &[42]); |
2176 | |
2177 | // with_producer |
2178 | let v: Vec<(i32, i32)> = once(42).zip(1..10).collect(); |
2179 | assert_eq!(v, &[(42, 1)]); |
2180 | } |
2181 | |
2182 | #[test] |
2183 | fn check_update() { |
2184 | let mut v: Vec<Vec<_>> = vec![vec![1], vec![3, 2, 1]]; |
2185 | v.par_iter_mut().update(|v| v.push(0)).for_each(|_| ()); |
2186 | |
2187 | assert_eq!(v, vec![vec![1, 0], vec![3, 2, 1, 0]]); |
2188 | } |
2189 | |