1
Fork 0

Add unit test for zipping slice::{Chunks, ChunksMut, Windows} iterators

For testing if the TrustedRandomAccess implementation works.
This commit is contained in:
Sebastian Dröge 2018-01-03 12:25:18 +02:00
parent 48f2f71185
commit a56a3fc85f

View file

@ -137,6 +137,18 @@ fn test_chunks_last() {
assert_eq!(c2.last().unwrap()[0], 4);
}
#[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];
@ -176,6 +188,20 @@ fn test_chunks_mut_last() {
assert_eq!(c2.last().unwrap()[0], 4);
}
#[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];
@ -215,6 +241,19 @@ fn test_windows_last() {
assert_eq!(c2.last().unwrap()[0], 3);
}
#[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];