Skip to content

Commit

Permalink
add tests for array_chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Jul 30, 2020
1 parent d405347 commit d27007f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
1 change: 0 additions & 1 deletion library/core/src/slice/mod.rs
Expand Up @@ -865,7 +865,6 @@ impl<T> [T] {
/// ```
///
/// [`chunks`]: #method.chunks
/// [`rchunks_exact`]: #method.rchunks_exact
#[unstable(feature = "array_chunks", issue = "none")]
#[inline]
pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
@@ -1,4 +1,5 @@
#![feature(alloc_layout_extra)]
#![feature(array_chunks)]
#![feature(bool_to_option)]
#![feature(bound_cloned)]
#![feature(box_syntax)]
Expand Down
91 changes: 91 additions & 0 deletions library/core/tests/slice.rs
Expand Up @@ -473,6 +473,97 @@ fn test_chunks_exact_mut_zip() {
assert_eq!(v1, [13, 14, 19, 20, 4]);
}

#[test]
fn test_array_chunks_infer() {
let v: &[i32] = &[0, 1, 2, 3, 4, -4];
let c = v.array_chunks();
for &[a, b, c] in c {
assert_eq!(a + b + c, 3);
}

let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
let total = v2.array_chunks().map(|&[a, b]| a * b).sum::<i32>();
assert_eq!(total, 2 * 3 + 4 * 5);
}

#[test]
fn test_array_chunks_count() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
let c = v.array_chunks::<3>();
assert_eq!(c.count(), 2);

let v2: &[i32] = &[0, 1, 2, 3, 4];
let c2 = v2.array_chunks::<2>();
assert_eq!(c2.count(), 2);

let v3: &[i32] = &[];
let c3 = v3.array_chunks::<2>();
assert_eq!(c3.count(), 0);
}

#[test]
fn test_array_chunks_nth() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
let mut c = v.array_chunks::<2>();
assert_eq!(c.nth(1).unwrap(), &[2, 3]);
assert_eq!(c.next().unwrap(), &[4, 5]);

let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
let mut c2 = v2.array_chunks::<3>();
assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
assert_eq!(c2.next(), None);
}

#[test]
fn test_array_chunks_nth_back() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
let mut c = v.array_chunks::<2>();
assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
assert_eq!(c.next().unwrap(), &[0, 1]);
assert_eq!(c.next(), None);

let v2: &[i32] = &[0, 1, 2, 3, 4];
let mut c2 = v2.array_chunks::<3>();
assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
assert_eq!(c2.next(), None);
assert_eq!(c2.next_back(), None);

let v3: &[i32] = &[0, 1, 2, 3, 4];
let mut c3 = v3.array_chunks::<10>();
assert_eq!(c3.nth_back(0), None);
}

#[test]
fn test_array_chunks_last() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
let c = v.array_chunks::<2>();
assert_eq!(c.last().unwrap(), &[4, 5]);

let v2: &[i32] = &[0, 1, 2, 3, 4];
let c2 = v2.array_chunks::<2>();
assert_eq!(c2.last().unwrap(), &[2, 3]);
}

#[test]
fn test_array_chunks_remainder() {
let v: &[i32] = &[0, 1, 2, 3, 4];
let c = v.array_chunks::<2>();
assert_eq!(c.remainder(), &[4]);
}

#[test]
fn test_array_chunks_zip() {
let v1: &[i32] = &[0, 1, 2, 3, 4];
let v2: &[i32] = &[6, 7, 8, 9, 10];

let res = v1
.array_chunks::<2>()
.zip(v2.array_chunks::<2>())
.map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
.collect::<Vec<_>>();
assert_eq!(res, vec![14, 22]);
}

#[test]
fn test_rchunks_count() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
Expand Down

0 comments on commit d27007f

Please sign in to comment.