From 5eb99e04adb72d4bac5bde421424be3ce3285b57 Mon Sep 17 00:00:00 2001 From: fqwert Date: Mon, 26 Jun 2023 11:30:00 +0800 Subject: [PATCH] pariter increase test coverage Signed-off-by: fqwert --- ylong_runtime/src/iter/parallel/slice.rs | 12 +++++++-- ylong_runtime/tests/par_iter.rs | 34 ++++++++++++++++++------ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ylong_runtime/src/iter/parallel/slice.rs b/ylong_runtime/src/iter/parallel/slice.rs index a8a10ac..36d014d 100644 --- a/ylong_runtime/src/iter/parallel/slice.rs +++ b/ylong_runtime/src/iter/parallel/slice.rs @@ -26,7 +26,11 @@ impl ParSplit for &[T] { fn split(self) -> (Self, Option) { let idx = self.len() >> 1; let (left, right) = self.split_at(idx); - (left, Some(right)) + if right.is_empty(){ + (left,None) + }else{ + (left, Some(right)) + } } } @@ -42,6 +46,10 @@ impl<'a, T> ParSplit for &'a mut [T] { fn split(self) -> (Self, Option) { let idx = self.len() >> 1; let (left, right) = self.split_at_mut(idx); - (left, Some(right)) + if right.is_empty(){ + (left,None) + }else{ + (left, Some(right)) + } } } diff --git a/ylong_runtime/tests/par_iter.rs b/ylong_runtime/tests/par_iter.rs index 99d77ad..d16ef5f 100644 --- a/ylong_runtime/tests/par_iter.rs +++ b/ylong_runtime/tests/par_iter.rs @@ -23,10 +23,11 @@ use std::collections::{HashMap, HashSet}; use ylong_runtime::iter::prelude::*; #[test] fn sdv_par_iter_test() { - let fut = async { + ylong_runtime::block_on(async { let a = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let b = vec![2, 3, 4, 5]; + let mut b = vec![1, 2, 3, 4]; + b.par_iter_mut().for_each(|x|*x+=1).await.unwrap(); let sum = a .par_iter() .map(|x| *x + 1) @@ -38,16 +39,16 @@ fn sdv_par_iter_test() { .unwrap(); assert_eq!(sum, 29); - let s = a.iter().copied().collect::>(); - let sum = s.into_par_iter().sum().await.unwrap(); + let hash_set = a.iter().copied().collect::>(); + let sum = hash_set.into_par_iter().sum().await.unwrap(); assert_eq!(sum, 55); - let m = a + let hash_map = a .iter() .zip(b.iter()) .map(|x| (*x.0, *x.1)) .collect::>(); - let sum = m.into_par_iter().map(|x| x.0 * x.1).sum().await.unwrap(); + let sum = hash_map.into_par_iter().map(|x| x.0 * x.1).sum().await.unwrap(); assert_eq!(sum, 40); let sum = a @@ -60,6 +61,23 @@ fn sdv_par_iter_test() { .await .unwrap(); assert_eq!(sum, 29); - }; - ylong_runtime::block_on(fut); + + let mut a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let b = [2, 3, 4, 5]; + let mut c = [1]; + let mut d = [1,2,3,4]; + a.par_iter_mut().for_each(|x|*x+=1).await.unwrap(); + + let sum = a + .par_iter() + .filter(|x| **x < 5) + .zip(b.par_iter()) + .zip(c.par_iter_mut()) + .zip(d.par_iter_mut()) + .map(|x| {*x.1=2;*x.0.1*=2;x.0.0.0 * (*x.0.0.1)+*x.0.1*(*x.1)}) + .sum() + .await + .unwrap(); + assert_eq!(sum, 8); + }); } -- Gitee