diff options
| author | Sebastian Dröge <sebastian@centricular.com> | 2018-01-03 12:25:18 +0200 |
|---|---|---|
| committer | Sebastian Dröge <sebastian@centricular.com> | 2018-01-03 15:05:18 +0200 |
| commit | a56a3fc85f466c8488dccdfb78cfcd05740e0b36 (patch) | |
| tree | 6bd9cfb9296c0efbb8c4af8641f36f29cb7edb54 /src/libcore/tests | |
| parent | 48f2f711857c3797978f1a4e300a1375672cef84 (diff) | |
| download | rust-a56a3fc85f466c8488dccdfb78cfcd05740e0b36.tar.gz rust-a56a3fc85f466c8488dccdfb78cfcd05740e0b36.zip | |
Add unit test for zipping slice::{Chunks, ChunksMut, Windows} iterators
For testing if the TrustedRandomAccess implementation works.
Diffstat (limited to 'src/libcore/tests')
| -rw-r--r-- | src/libcore/tests/slice.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index fa4c2e9b373..d6230e93f99 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -138,6 +138,18 @@ fn test_chunks_last() { } #[test] +fn test_chunks_zip() { + let v1: &[i32] = &[0, 1, 2, 3, 4]; + let v2: &[i32] = &[6, 7, 8, 9, 10]; + + let res = v1.chunks(2) + .zip(v2.chunks(2)) + .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>()) + .collect::<Vec<_>>(); + assert_eq!(res, vec![14, 22, 14]); +} + +#[test] fn test_chunks_mut_count() { let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; let c = v.chunks_mut(3); @@ -177,6 +189,20 @@ fn test_chunks_mut_last() { } #[test] +fn test_chunks_mut_zip() { + let v1: &mut [i32] = &mut [0, 1, 2, 3, 4]; + let v2: &[i32] = &[6, 7, 8, 9, 10]; + + for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) { + let sum = b.iter().sum::<i32>(); + for v in a { + *v += sum; + } + } + assert_eq!(v1, [13, 14, 19, 20, 14]); +} + +#[test] fn test_windows_count() { let v: &[i32] = &[0, 1, 2, 3, 4, 5]; let c = v.windows(3); @@ -216,6 +242,19 @@ fn test_windows_last() { } #[test] +fn test_windows_zip() { + let v1: &[i32] = &[0, 1, 2, 3, 4]; + let v2: &[i32] = &[6, 7, 8, 9, 10]; + + let res = v1.windows(2) + .zip(v2.windows(2)) + .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>()) + .collect::<Vec<_>>(); + + assert_eq!(res, [14, 18, 22, 26]); +} + +#[test] fn get_range() { let v: &[i32] = &[0, 1, 2, 3, 4, 5]; assert_eq!(v.get(..), Some(&[0, 1, 2, 3, 4, 5][..])); |
