From 5f4fc8214279f2ffadbf2cc3a4c22b748c17bd15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 11 Jan 2018 12:13:45 +0200 Subject: [PATCH] Add unit tests for exact_chunks/exact_chunks_mut These are basically modified copies of the chunks/chunks_mut tests. --- src/liballoc/tests/lib.rs | 1 + src/liballoc/tests/slice.rs | 56 +++++++++++++++++++ src/libcore/tests/lib.rs | 1 + src/libcore/tests/slice.rs | 104 ++++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index f1e95883b3827..eee229bc6fdfa 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -30,6 +30,7 @@ #![feature(string_retain)] #![feature(unboxed_closures)] #![feature(unicode)] +#![feature(exact_chunks)] extern crate alloc_system; extern crate std_unicode; diff --git a/src/liballoc/tests/slice.rs b/src/liballoc/tests/slice.rs index c2f7522c5f054..5a3e5e6cb780d 100644 --- a/src/liballoc/tests/slice.rs +++ b/src/liballoc/tests/slice.rs @@ -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::>(), chunks); + let chunks: &[&[_]] = &[&[1, 2, 3]]; + assert_eq!(v.exact_chunks(3).collect::>(), chunks); + let chunks: &[&[_]] = &[]; + assert_eq!(v.exact_chunks(6).collect::>(), chunks); + + let chunks: &[&[_]] = &[&[3, 4], &[1, 2]]; + assert_eq!(v.exact_chunks(2).rev().collect::>(), 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]; @@ -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]; diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index c4b85b829812c..2c0009569d75d 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -42,6 +42,7 @@ #![feature(try_from)] #![feature(try_trait)] #![feature(unique)] +#![feature(exact_chunks)] extern crate core; extern crate test; diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index 4f9fc30785c73..a89a88ac0af1e 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -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::() + b.iter().sum::()) + .collect::>(); + 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::(); + 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];