Skip to content

Commit a56a3fc

Browse files
committed
Add unit test for zipping slice::{Chunks, ChunksMut, Windows} iterators
For testing if the TrustedRandomAccess implementation works.
1 parent 48f2f71 commit a56a3fc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/libcore/tests/slice.rs

+39
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ fn test_chunks_last() {
137137
assert_eq!(c2.last().unwrap()[0], 4);
138138
}
139139

140+
#[test]
141+
fn test_chunks_zip() {
142+
let v1: &[i32] = &[0, 1, 2, 3, 4];
143+
let v2: &[i32] = &[6, 7, 8, 9, 10];
144+
145+
let res = v1.chunks(2)
146+
.zip(v2.chunks(2))
147+
.map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
148+
.collect::<Vec<_>>();
149+
assert_eq!(res, vec![14, 22, 14]);
150+
}
151+
140152
#[test]
141153
fn test_chunks_mut_count() {
142154
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
@@ -176,6 +188,20 @@ fn test_chunks_mut_last() {
176188
assert_eq!(c2.last().unwrap()[0], 4);
177189
}
178190

191+
#[test]
192+
fn test_chunks_mut_zip() {
193+
let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
194+
let v2: &[i32] = &[6, 7, 8, 9, 10];
195+
196+
for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
197+
let sum = b.iter().sum::<i32>();
198+
for v in a {
199+
*v += sum;
200+
}
201+
}
202+
assert_eq!(v1, [13, 14, 19, 20, 14]);
203+
}
204+
179205
#[test]
180206
fn test_windows_count() {
181207
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
@@ -215,6 +241,19 @@ fn test_windows_last() {
215241
assert_eq!(c2.last().unwrap()[0], 3);
216242
}
217243

244+
#[test]
245+
fn test_windows_zip() {
246+
let v1: &[i32] = &[0, 1, 2, 3, 4];
247+
let v2: &[i32] = &[6, 7, 8, 9, 10];
248+
249+
let res = v1.windows(2)
250+
.zip(v2.windows(2))
251+
.map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
252+
.collect::<Vec<_>>();
253+
254+
assert_eq!(res, [14, 18, 22, 26]);
255+
}
256+
218257
#[test]
219258
fn get_range() {
220259
let v: &[i32] = &[0, 1, 2, 3, 4, 5];

0 commit comments

Comments
 (0)