Skip to content

Commit

Permalink
Add unit tests for exact_chunks/exact_chunks_mut
Browse files Browse the repository at this point in the history
These are basically modified copies of the chunks/chunks_mut tests.
  • Loading branch information
sdroege committed Jan 13, 2018
1 parent ed77483 commit 5f4fc82
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#![feature(string_retain)]
#![feature(unboxed_closures)]
#![feature(unicode)]
#![feature(exact_chunks)]

extern crate alloc_system;
extern crate std_unicode;
Expand Down
56 changes: 56 additions & 0 deletions src/liballoc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,30 @@ fn test_chunksator_0() {
let _it = v.chunks(0);
}

#[test]
fn test_exact_chunksator() {
let v = &[1, 2, 3, 4, 5];

assert_eq!(v.exact_chunks(2).len(), 2);

let chunks: &[&[_]] = &[&[1, 2], &[3, 4]];
assert_eq!(v.exact_chunks(2).collect::<Vec<_>>(), chunks);
let chunks: &[&[_]] = &[&[1, 2, 3]];
assert_eq!(v.exact_chunks(3).collect::<Vec<_>>(), chunks);
let chunks: &[&[_]] = &[];
assert_eq!(v.exact_chunks(6).collect::<Vec<_>>(), chunks);

let chunks: &[&[_]] = &[&[3, 4], &[1, 2]];
assert_eq!(v.exact_chunks(2).rev().collect::<Vec<_>>(), chunks);
}

#[test]
#[should_panic]
fn test_exact_chunksator_0() {
let v = &[1, 2, 3, 4];
let _it = v.exact_chunks(0);
}

#[test]
fn test_reverse_part() {
let mut values = [1, 2, 3, 4, 5];
Expand Down Expand Up @@ -1146,6 +1170,38 @@ fn test_mut_chunks_0() {
let _it = v.chunks_mut(0);
}

#[test]
fn test_mut_exact_chunks() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
assert_eq!(v.exact_chunks_mut(2).len(), 3);
for (i, chunk) in v.exact_chunks_mut(3).enumerate() {
for x in chunk {
*x = i as u8;
}
}
let result = [0, 0, 0, 1, 1, 1, 6];
assert_eq!(v, result);
}

#[test]
fn test_mut_exact_chunks_rev() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
for (i, chunk) in v.exact_chunks_mut(3).rev().enumerate() {
for x in chunk {
*x = i as u8;
}
}
let result = [1, 1, 1, 0, 0, 0, 6];
assert_eq!(v, result);
}

#[test]
#[should_panic]
fn test_mut_exact_chunks_0() {
let mut v = [1, 2, 3, 4];
let _it = v.exact_chunks_mut(0);
}

#[test]
fn test_mut_last() {
let mut x = [1, 2, 3, 4, 5];
Expand Down
1 change: 1 addition & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#![feature(try_from)]
#![feature(try_trait)]
#![feature(unique)]
#![feature(exact_chunks)]

extern crate core;
extern crate test;
Expand Down
104 changes: 104 additions & 0 deletions src/libcore/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,110 @@ fn test_chunks_mut_zip() {
assert_eq!(v1, [13, 14, 19, 20, 14]);
}

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

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

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

#[test]
fn test_exact_chunks_nth() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
let mut c = v.exact_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.exact_chunks(3);
assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
assert_eq!(c2.next(), None);
}

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

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

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

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

#[test]
fn test_exact_chunks_mut_count() {
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
let c = v.exact_chunks_mut(3);
assert_eq!(c.count(), 2);

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

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

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

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

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

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

#[test]
fn test_exact_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.exact_chunks_mut(2).zip(v2.exact_chunks(2)) {
let sum = b.iter().sum::<i32>();
for v in a {
*v += sum;
}
}
assert_eq!(v1, [13, 14, 19, 20, 4]);
}

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

0 comments on commit 5f4fc82

Please sign in to comment.