2 use core::cmp::Ordering;
3 use core::mem::MaybeUninit;
4 use core::result::Result::{Err, Ok};
9 let b = [1, 2, 3, 5, 5];
10 assert_eq!(b.iter().position(|&v| v == 9), None);
11 assert_eq!(b.iter().position(|&v| v == 5), Some(3));
12 assert_eq!(b.iter().position(|&v| v == 3), Some(2));
13 assert_eq!(b.iter().position(|&v| v == 0), None);
18 let b = [1, 2, 3, 5, 5];
19 assert_eq!(b.iter().rposition(|&v| v == 9), None);
20 assert_eq!(b.iter().rposition(|&v| v == 5), Some(4));
21 assert_eq!(b.iter().rposition(|&v| v == 3), Some(2));
22 assert_eq!(b.iter().rposition(|&v| v == 0), None);
26 fn test_binary_search() {
28 assert_eq!(b.binary_search(&5), Err(0));
31 assert_eq!(b.binary_search(&3), Err(0));
32 assert_eq!(b.binary_search(&4), Ok(0));
33 assert_eq!(b.binary_search(&5), Err(1));
35 let b = [1, 2, 4, 6, 8, 9];
36 assert_eq!(b.binary_search(&5), Err(3));
37 assert_eq!(b.binary_search(&6), Ok(3));
38 assert_eq!(b.binary_search(&7), Err(4));
39 assert_eq!(b.binary_search(&8), Ok(4));
41 let b = [1, 2, 4, 5, 6, 8];
42 assert_eq!(b.binary_search(&9), Err(6));
44 let b = [1, 2, 4, 6, 7, 8, 9];
45 assert_eq!(b.binary_search(&6), Ok(3));
46 assert_eq!(b.binary_search(&5), Err(3));
47 assert_eq!(b.binary_search(&8), Ok(5));
49 let b = [1, 2, 4, 5, 6, 8, 9];
50 assert_eq!(b.binary_search(&7), Err(5));
51 assert_eq!(b.binary_search(&0), Err(0));
53 let b = [1, 3, 3, 3, 7];
54 assert_eq!(b.binary_search(&0), Err(0));
55 assert_eq!(b.binary_search(&1), Ok(0));
56 assert_eq!(b.binary_search(&2), Err(1));
57 assert!(match b.binary_search(&3) {
61 assert!(match b.binary_search(&3) {
65 assert_eq!(b.binary_search(&4), Err(4));
66 assert_eq!(b.binary_search(&5), Err(4));
67 assert_eq!(b.binary_search(&6), Err(4));
68 assert_eq!(b.binary_search(&7), Ok(4));
69 assert_eq!(b.binary_search(&8), Err(5));
71 let b = [(); usize::MAX];
72 assert_eq!(b.binary_search(&()), Ok(usize::MAX / 2));
76 fn test_binary_search_by_overflow() {
77 let b = [(); usize::MAX];
78 assert_eq!(b.binary_search_by(|_| Ordering::Equal), Ok(usize::MAX / 2));
79 assert_eq!(b.binary_search_by(|_| Ordering::Greater), Err(0));
80 assert_eq!(b.binary_search_by(|_| Ordering::Less), Err(usize::MAX));
84 // Test implementation specific behavior when finding equivalent elements.
85 // It is ok to break this test but when you do a crater run is highly advisable.
86 fn test_binary_search_implementation_details() {
87 let b = [1, 1, 2, 2, 3, 3, 3];
88 assert_eq!(b.binary_search(&1), Ok(1));
89 assert_eq!(b.binary_search(&2), Ok(3));
90 assert_eq!(b.binary_search(&3), Ok(5));
91 let b = [1, 1, 1, 1, 1, 3, 3, 3, 3];
92 assert_eq!(b.binary_search(&1), Ok(4));
93 assert_eq!(b.binary_search(&3), Ok(7));
94 let b = [1, 1, 1, 1, 3, 3, 3, 3, 3];
95 assert_eq!(b.binary_search(&1), Ok(2));
96 assert_eq!(b.binary_search(&3), Ok(4));
100 fn test_partition_point() {
101 let b: [i32; 0] = [];
102 assert_eq!(b.partition_point(|&x| x < 5), 0);
105 assert_eq!(b.partition_point(|&x| x < 3), 0);
106 assert_eq!(b.partition_point(|&x| x < 4), 0);
107 assert_eq!(b.partition_point(|&x| x < 5), 1);
109 let b = [1, 2, 4, 6, 8, 9];
110 assert_eq!(b.partition_point(|&x| x < 5), 3);
111 assert_eq!(b.partition_point(|&x| x < 6), 3);
112 assert_eq!(b.partition_point(|&x| x < 7), 4);
113 assert_eq!(b.partition_point(|&x| x < 8), 4);
115 let b = [1, 2, 4, 5, 6, 8];
116 assert_eq!(b.partition_point(|&x| x < 9), 6);
118 let b = [1, 2, 4, 6, 7, 8, 9];
119 assert_eq!(b.partition_point(|&x| x < 6), 3);
120 assert_eq!(b.partition_point(|&x| x < 5), 3);
121 assert_eq!(b.partition_point(|&x| x < 8), 5);
123 let b = [1, 2, 4, 5, 6, 8, 9];
124 assert_eq!(b.partition_point(|&x| x < 7), 5);
125 assert_eq!(b.partition_point(|&x| x < 0), 0);
127 let b = [1, 3, 3, 3, 7];
128 assert_eq!(b.partition_point(|&x| x < 0), 0);
129 assert_eq!(b.partition_point(|&x| x < 1), 0);
130 assert_eq!(b.partition_point(|&x| x < 2), 1);
131 assert_eq!(b.partition_point(|&x| x < 3), 1);
132 assert_eq!(b.partition_point(|&x| x < 4), 4);
133 assert_eq!(b.partition_point(|&x| x < 5), 4);
134 assert_eq!(b.partition_point(|&x| x < 6), 4);
135 assert_eq!(b.partition_point(|&x| x < 7), 4);
136 assert_eq!(b.partition_point(|&x| x < 8), 5);
140 fn test_iterator_advance_by() {
141 let v = &[0, 1, 2, 3, 4];
143 for i in 0..=v.len() {
144 let mut iter = v.iter();
145 iter.advance_by(i).unwrap();
146 assert_eq!(iter.as_slice(), &v[i..]);
149 let mut iter = v.iter();
150 assert_eq!(iter.advance_by(v.len() + 1), Err(v.len()));
151 assert_eq!(iter.as_slice(), &[]);
153 let mut iter = v.iter();
154 iter.advance_by(3).unwrap();
155 assert_eq!(iter.as_slice(), &v[3..]);
156 iter.advance_by(2).unwrap();
157 assert_eq!(iter.as_slice(), &[]);
158 iter.advance_by(0).unwrap();
162 fn test_iterator_advance_back_by() {
163 let v = &[0, 1, 2, 3, 4];
165 for i in 0..=v.len() {
166 let mut iter = v.iter();
167 iter.advance_back_by(i).unwrap();
168 assert_eq!(iter.as_slice(), &v[..v.len() - i]);
171 let mut iter = v.iter();
172 assert_eq!(iter.advance_back_by(v.len() + 1), Err(v.len()));
173 assert_eq!(iter.as_slice(), &[]);
175 let mut iter = v.iter();
176 iter.advance_back_by(3).unwrap();
177 assert_eq!(iter.as_slice(), &v[..v.len() - 3]);
178 iter.advance_back_by(2).unwrap();
179 assert_eq!(iter.as_slice(), &[]);
180 iter.advance_back_by(0).unwrap();
184 fn test_iterator_nth() {
185 let v: &[_] = &[0, 1, 2, 3, 4];
186 for i in 0..v.len() {
187 assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
189 assert_eq!(v.iter().nth(v.len()), None);
191 let mut iter = v.iter();
192 assert_eq!(iter.nth(2).unwrap(), &v[2]);
193 assert_eq!(iter.nth(1).unwrap(), &v[4]);
197 fn test_iterator_nth_back() {
198 let v: &[_] = &[0, 1, 2, 3, 4];
199 for i in 0..v.len() {
200 assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - i - 1]);
202 assert_eq!(v.iter().nth_back(v.len()), None);
204 let mut iter = v.iter();
205 assert_eq!(iter.nth_back(2).unwrap(), &v[2]);
206 assert_eq!(iter.nth_back(1).unwrap(), &v[0]);
210 fn test_iterator_last() {
211 let v: &[_] = &[0, 1, 2, 3, 4];
212 assert_eq!(v.iter().last().unwrap(), &4);
213 assert_eq!(v[..1].iter().last().unwrap(), &0);
217 fn test_iterator_count() {
218 let v: &[_] = &[0, 1, 2, 3, 4];
219 assert_eq!(v.iter().count(), 5);
221 let mut iter2 = v.iter();
224 assert_eq!(iter2.count(), 3);
228 fn test_chunks_count() {
229 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
231 assert_eq!(c.count(), 2);
233 let v2: &[i32] = &[0, 1, 2, 3, 4];
234 let c2 = v2.chunks(2);
235 assert_eq!(c2.count(), 3);
237 let v3: &[i32] = &[];
238 let c3 = v3.chunks(2);
239 assert_eq!(c3.count(), 0);
243 fn test_chunks_nth() {
244 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
245 let mut c = v.chunks(2);
246 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
247 assert_eq!(c.next().unwrap(), &[4, 5]);
249 let v2: &[i32] = &[0, 1, 2, 3, 4];
250 let mut c2 = v2.chunks(3);
251 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
252 assert_eq!(c2.next(), None);
256 fn test_chunks_next() {
257 let v = [0, 1, 2, 3, 4, 5];
258 let mut c = v.chunks(2);
259 assert_eq!(c.next().unwrap(), &[0, 1]);
260 assert_eq!(c.next().unwrap(), &[2, 3]);
261 assert_eq!(c.next().unwrap(), &[4, 5]);
262 assert_eq!(c.next(), None);
264 let v = [0, 1, 2, 3, 4, 5, 6, 7];
265 let mut c = v.chunks(3);
266 assert_eq!(c.next().unwrap(), &[0, 1, 2]);
267 assert_eq!(c.next().unwrap(), &[3, 4, 5]);
268 assert_eq!(c.next().unwrap(), &[6, 7]);
269 assert_eq!(c.next(), None);
273 fn test_chunks_next_back() {
274 let v = [0, 1, 2, 3, 4, 5];
275 let mut c = v.chunks(2);
276 assert_eq!(c.next_back().unwrap(), &[4, 5]);
277 assert_eq!(c.next_back().unwrap(), &[2, 3]);
278 assert_eq!(c.next_back().unwrap(), &[0, 1]);
279 assert_eq!(c.next_back(), None);
281 let v = [0, 1, 2, 3, 4, 5, 6, 7];
282 let mut c = v.chunks(3);
283 assert_eq!(c.next_back().unwrap(), &[6, 7]);
284 assert_eq!(c.next_back().unwrap(), &[3, 4, 5]);
285 assert_eq!(c.next_back().unwrap(), &[0, 1, 2]);
286 assert_eq!(c.next_back(), None);
290 fn test_chunks_nth_back() {
291 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
292 let mut c = v.chunks(2);
293 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
294 assert_eq!(c.next().unwrap(), &[0, 1]);
295 assert_eq!(c.next(), None);
297 let v2: &[i32] = &[0, 1, 2, 3, 4];
298 let mut c2 = v2.chunks(3);
299 assert_eq!(c2.nth_back(1).unwrap(), &[0, 1, 2]);
300 assert_eq!(c2.next(), None);
301 assert_eq!(c2.next_back(), None);
303 let v3: &[i32] = &[0, 1, 2, 3, 4];
304 let mut c3 = v3.chunks(10);
305 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
306 assert_eq!(c3.next(), None);
308 let v4: &[i32] = &[0, 1, 2];
309 let mut c4 = v4.chunks(10);
310 assert_eq!(c4.nth_back(1_000_000_000usize), None);
314 fn test_chunks_last() {
315 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
317 assert_eq!(c.last().unwrap()[1], 5);
319 let v2: &[i32] = &[0, 1, 2, 3, 4];
320 let c2 = v2.chunks(2);
321 assert_eq!(c2.last().unwrap()[0], 4);
325 fn test_chunks_zip() {
326 let v1: &[i32] = &[0, 1, 2, 3, 4];
327 let v2: &[i32] = &[6, 7, 8, 9, 10];
332 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
333 .collect::<Vec<_>>();
334 assert_eq!(res, vec![14, 22, 14]);
338 fn test_chunks_mut_count() {
339 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
340 let c = v.chunks_mut(3);
341 assert_eq!(c.count(), 2);
343 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
344 let c2 = v2.chunks_mut(2);
345 assert_eq!(c2.count(), 3);
347 let v3: &mut [i32] = &mut [];
348 let c3 = v3.chunks_mut(2);
349 assert_eq!(c3.count(), 0);
353 fn test_chunks_mut_nth() {
354 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
355 let mut c = v.chunks_mut(2);
356 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
357 assert_eq!(c.next().unwrap(), &[4, 5]);
359 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
360 let mut c2 = v2.chunks_mut(3);
361 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
362 assert_eq!(c2.next(), None);
366 fn test_chunks_mut_nth_back() {
367 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
368 let mut c = v.chunks_mut(2);
369 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
370 assert_eq!(c.next().unwrap(), &[0, 1]);
372 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
373 let mut c1 = v1.chunks_mut(3);
374 assert_eq!(c1.nth_back(1).unwrap(), &[0, 1, 2]);
375 assert_eq!(c1.next(), None);
377 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
378 let mut c3 = v3.chunks_mut(10);
379 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
380 assert_eq!(c3.next(), None);
382 let v4: &mut [i32] = &mut [0, 1, 2];
383 let mut c4 = v4.chunks_mut(10);
384 assert_eq!(c4.nth_back(1_000_000_000usize), None);
388 fn test_chunks_mut_last() {
389 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
390 let c = v.chunks_mut(2);
391 assert_eq!(c.last().unwrap(), &[4, 5]);
393 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
394 let c2 = v2.chunks_mut(2);
395 assert_eq!(c2.last().unwrap(), &[4]);
399 fn test_chunks_mut_zip() {
400 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
401 let v2: &[i32] = &[6, 7, 8, 9, 10];
403 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
404 let sum = b.iter().sum::<i32>();
409 assert_eq!(v1, [13, 14, 19, 20, 14]);
413 fn test_chunks_exact_count() {
414 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
415 let c = v.chunks_exact(3);
416 assert_eq!(c.count(), 2);
418 let v2: &[i32] = &[0, 1, 2, 3, 4];
419 let c2 = v2.chunks_exact(2);
420 assert_eq!(c2.count(), 2);
422 let v3: &[i32] = &[];
423 let c3 = v3.chunks_exact(2);
424 assert_eq!(c3.count(), 0);
428 fn test_chunks_exact_nth() {
429 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
430 let mut c = v.chunks_exact(2);
431 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
432 assert_eq!(c.next().unwrap(), &[4, 5]);
434 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
435 let mut c2 = v2.chunks_exact(3);
436 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
437 assert_eq!(c2.next(), None);
441 fn test_chunks_exact_nth_back() {
442 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
443 let mut c = v.chunks_exact(2);
444 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
445 assert_eq!(c.next().unwrap(), &[0, 1]);
446 assert_eq!(c.next(), None);
448 let v2: &[i32] = &[0, 1, 2, 3, 4];
449 let mut c2 = v2.chunks_exact(3);
450 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
451 assert_eq!(c2.next(), None);
452 assert_eq!(c2.next_back(), None);
454 let v3: &[i32] = &[0, 1, 2, 3, 4];
455 let mut c3 = v3.chunks_exact(10);
456 assert_eq!(c3.nth_back(0), None);
460 fn test_chunks_exact_last() {
461 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
462 let c = v.chunks_exact(2);
463 assert_eq!(c.last().unwrap(), &[4, 5]);
465 let v2: &[i32] = &[0, 1, 2, 3, 4];
466 let c2 = v2.chunks_exact(2);
467 assert_eq!(c2.last().unwrap(), &[2, 3]);
471 fn test_chunks_exact_remainder() {
472 let v: &[i32] = &[0, 1, 2, 3, 4];
473 let c = v.chunks_exact(2);
474 assert_eq!(c.remainder(), &[4]);
478 fn test_chunks_exact_zip() {
479 let v1: &[i32] = &[0, 1, 2, 3, 4];
480 let v2: &[i32] = &[6, 7, 8, 9, 10];
484 .zip(v2.chunks_exact(2))
485 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
486 .collect::<Vec<_>>();
487 assert_eq!(res, vec![14, 22]);
491 fn test_chunks_exact_mut_count() {
492 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
493 let c = v.chunks_exact_mut(3);
494 assert_eq!(c.count(), 2);
496 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
497 let c2 = v2.chunks_exact_mut(2);
498 assert_eq!(c2.count(), 2);
500 let v3: &mut [i32] = &mut [];
501 let c3 = v3.chunks_exact_mut(2);
502 assert_eq!(c3.count(), 0);
506 fn test_chunks_exact_mut_nth() {
507 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
508 let mut c = v.chunks_exact_mut(2);
509 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
510 assert_eq!(c.next().unwrap(), &[4, 5]);
512 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
513 let mut c2 = v2.chunks_exact_mut(3);
514 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
515 assert_eq!(c2.next(), None);
519 fn test_chunks_exact_mut_nth_back() {
520 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
521 let mut c = v.chunks_exact_mut(2);
522 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
523 assert_eq!(c.next().unwrap(), &[0, 1]);
524 assert_eq!(c.next(), None);
526 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
527 let mut c2 = v2.chunks_exact_mut(3);
528 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
529 assert_eq!(c2.next(), None);
530 assert_eq!(c2.next_back(), None);
532 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
533 let mut c3 = v3.chunks_exact_mut(10);
534 assert_eq!(c3.nth_back(0), None);
538 fn test_chunks_exact_mut_last() {
539 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
540 let c = v.chunks_exact_mut(2);
541 assert_eq!(c.last().unwrap(), &[4, 5]);
543 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
544 let c2 = v2.chunks_exact_mut(2);
545 assert_eq!(c2.last().unwrap(), &[2, 3]);
549 fn test_chunks_exact_mut_remainder() {
550 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
551 let c = v.chunks_exact_mut(2);
552 assert_eq!(c.into_remainder(), &[4]);
556 fn test_chunks_exact_mut_zip() {
557 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
558 let v2: &[i32] = &[6, 7, 8, 9, 10];
560 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
561 let sum = b.iter().sum::<i32>();
566 assert_eq!(v1, [13, 14, 19, 20, 4]);
570 fn test_array_chunks_infer() {
571 let v: &[i32] = &[0, 1, 2, 3, 4, -4];
572 let c = v.array_chunks();
573 for &[a, b, c] in c {
574 assert_eq!(a + b + c, 3);
577 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
578 let total = v2.array_chunks().map(|&[a, b]| a * b).sum::<i32>();
579 assert_eq!(total, 2 * 3 + 4 * 5);
583 fn test_array_chunks_count() {
584 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
585 let c = v.array_chunks::<3>();
586 assert_eq!(c.count(), 2);
588 let v2: &[i32] = &[0, 1, 2, 3, 4];
589 let c2 = v2.array_chunks::<2>();
590 assert_eq!(c2.count(), 2);
592 let v3: &[i32] = &[];
593 let c3 = v3.array_chunks::<2>();
594 assert_eq!(c3.count(), 0);
598 fn test_array_chunks_nth() {
599 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
600 let mut c = v.array_chunks::<2>();
601 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
602 assert_eq!(c.next().unwrap(), &[4, 5]);
604 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
605 let mut c2 = v2.array_chunks::<3>();
606 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
607 assert_eq!(c2.next(), None);
611 fn test_array_chunks_nth_back() {
612 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
613 let mut c = v.array_chunks::<2>();
614 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
615 assert_eq!(c.next().unwrap(), &[0, 1]);
616 assert_eq!(c.next(), None);
618 let v2: &[i32] = &[0, 1, 2, 3, 4];
619 let mut c2 = v2.array_chunks::<3>();
620 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
621 assert_eq!(c2.next(), None);
622 assert_eq!(c2.next_back(), None);
624 let v3: &[i32] = &[0, 1, 2, 3, 4];
625 let mut c3 = v3.array_chunks::<10>();
626 assert_eq!(c3.nth_back(0), None);
630 fn test_array_chunks_last() {
631 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
632 let c = v.array_chunks::<2>();
633 assert_eq!(c.last().unwrap(), &[4, 5]);
635 let v2: &[i32] = &[0, 1, 2, 3, 4];
636 let c2 = v2.array_chunks::<2>();
637 assert_eq!(c2.last().unwrap(), &[2, 3]);
641 fn test_array_chunks_remainder() {
642 let v: &[i32] = &[0, 1, 2, 3, 4];
643 let c = v.array_chunks::<2>();
644 assert_eq!(c.remainder(), &[4]);
648 fn test_array_chunks_zip() {
649 let v1: &[i32] = &[0, 1, 2, 3, 4];
650 let v2: &[i32] = &[6, 7, 8, 9, 10];
654 .zip(v2.array_chunks::<2>())
655 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
656 .collect::<Vec<_>>();
657 assert_eq!(res, vec![14, 22]);
661 fn test_array_chunks_mut_infer() {
662 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
663 for a in v.array_chunks_mut() {
664 let sum = a.iter().sum::<i32>();
667 assert_eq!(v, &[3, 3, 3, 12, 12, 12, 6]);
669 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
670 v2.array_chunks_mut().for_each(|[a, b]| core::mem::swap(a, b));
671 assert_eq!(v2, &[1, 0, 3, 2, 5, 4, 6]);
675 fn test_array_chunks_mut_count() {
676 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
677 let c = v.array_chunks_mut::<3>();
678 assert_eq!(c.count(), 2);
680 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
681 let c2 = v2.array_chunks_mut::<2>();
682 assert_eq!(c2.count(), 2);
684 let v3: &mut [i32] = &mut [];
685 let c3 = v3.array_chunks_mut::<2>();
686 assert_eq!(c3.count(), 0);
690 fn test_array_chunks_mut_nth() {
691 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
692 let mut c = v.array_chunks_mut::<2>();
693 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
694 assert_eq!(c.next().unwrap(), &[4, 5]);
696 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
697 let mut c2 = v2.array_chunks_mut::<3>();
698 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
699 assert_eq!(c2.next(), None);
703 fn test_array_chunks_mut_nth_back() {
704 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
705 let mut c = v.array_chunks_mut::<2>();
706 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
707 assert_eq!(c.next().unwrap(), &[0, 1]);
708 assert_eq!(c.next(), None);
710 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
711 let mut c2 = v2.array_chunks_mut::<3>();
712 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
713 assert_eq!(c2.next(), None);
714 assert_eq!(c2.next_back(), None);
716 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
717 let mut c3 = v3.array_chunks_mut::<10>();
718 assert_eq!(c3.nth_back(0), None);
722 fn test_array_chunks_mut_last() {
723 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
724 let c = v.array_chunks_mut::<2>();
725 assert_eq!(c.last().unwrap(), &[4, 5]);
727 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
728 let c2 = v2.array_chunks_mut::<2>();
729 assert_eq!(c2.last().unwrap(), &[2, 3]);
733 fn test_array_chunks_mut_remainder() {
734 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
735 let c = v.array_chunks_mut::<2>();
736 assert_eq!(c.into_remainder(), &[4]);
740 fn test_array_chunks_mut_zip() {
741 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
742 let v2: &[i32] = &[6, 7, 8, 9, 10];
744 for (a, b) in v1.array_chunks_mut::<2>().zip(v2.array_chunks::<2>()) {
745 let sum = b.iter().sum::<i32>();
750 assert_eq!(v1, [13, 14, 19, 20, 4]);
754 fn test_array_windows_infer() {
755 let v: &[i32] = &[0, 1, 0, 1];
756 assert_eq!(v.array_windows::<2>().count(), 3);
757 let c = v.array_windows();
759 assert_eq!(a + b, 1);
762 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
763 let total = v2.array_windows().map(|&[a, b, c]| a + b + c).sum::<i32>();
764 assert_eq!(total, 3 + 6 + 9 + 12 + 15);
768 fn test_array_windows_count() {
769 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
770 let c = v.array_windows::<3>();
771 assert_eq!(c.count(), 4);
773 let v2: &[i32] = &[0, 1, 2, 3, 4];
774 let c2 = v2.array_windows::<6>();
775 assert_eq!(c2.count(), 0);
777 let v3: &[i32] = &[];
778 let c3 = v3.array_windows::<2>();
779 assert_eq!(c3.count(), 0);
781 let v4: &[()] = &[(); usize::MAX];
782 let c4 = v4.array_windows::<1>();
783 assert_eq!(c4.count(), usize::MAX);
787 fn test_array_windows_nth() {
788 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
789 let snd = v.array_windows::<4>().nth(1);
790 assert_eq!(snd, Some(&[1, 2, 3, 4]));
791 let mut arr_windows = v.array_windows::<2>();
792 assert_ne!(arr_windows.nth(0), arr_windows.nth(0));
793 let last = v.array_windows::<3>().last();
794 assert_eq!(last, Some(&[3, 4, 5]));
798 fn test_array_windows_nth_back() {
799 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
800 let snd = v.array_windows::<4>().nth_back(1);
801 assert_eq!(snd, Some(&[1, 2, 3, 4]));
802 let mut arr_windows = v.array_windows::<2>();
803 assert_ne!(arr_windows.nth_back(0), arr_windows.nth_back(0));
807 fn test_rchunks_count() {
808 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
809 let c = v.rchunks(3);
810 assert_eq!(c.count(), 2);
812 let v2: &[i32] = &[0, 1, 2, 3, 4];
813 let c2 = v2.rchunks(2);
814 assert_eq!(c2.count(), 3);
816 let v3: &[i32] = &[];
817 let c3 = v3.rchunks(2);
818 assert_eq!(c3.count(), 0);
822 fn test_rchunks_nth() {
823 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
824 let mut c = v.rchunks(2);
825 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
826 assert_eq!(c.next().unwrap(), &[0, 1]);
828 let v2: &[i32] = &[0, 1, 2, 3, 4];
829 let mut c2 = v2.rchunks(3);
830 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
831 assert_eq!(c2.next(), None);
835 fn test_rchunks_nth_back() {
836 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
837 let mut c = v.rchunks(2);
838 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
839 assert_eq!(c.next_back().unwrap(), &[4, 5]);
841 let v2: &[i32] = &[0, 1, 2, 3, 4];
842 let mut c2 = v2.rchunks(3);
843 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
844 assert_eq!(c2.next_back(), None);
848 fn test_rchunks_next() {
849 let v = [0, 1, 2, 3, 4, 5];
850 let mut c = v.rchunks(2);
851 assert_eq!(c.next().unwrap(), &[4, 5]);
852 assert_eq!(c.next().unwrap(), &[2, 3]);
853 assert_eq!(c.next().unwrap(), &[0, 1]);
854 assert_eq!(c.next(), None);
856 let v = [0, 1, 2, 3, 4, 5, 6, 7];
857 let mut c = v.rchunks(3);
858 assert_eq!(c.next().unwrap(), &[5, 6, 7]);
859 assert_eq!(c.next().unwrap(), &[2, 3, 4]);
860 assert_eq!(c.next().unwrap(), &[0, 1]);
861 assert_eq!(c.next(), None);
865 fn test_rchunks_next_back() {
866 let v = [0, 1, 2, 3, 4, 5];
867 let mut c = v.rchunks(2);
868 assert_eq!(c.next_back().unwrap(), &[0, 1]);
869 assert_eq!(c.next_back().unwrap(), &[2, 3]);
870 assert_eq!(c.next_back().unwrap(), &[4, 5]);
871 assert_eq!(c.next_back(), None);
873 let v = [0, 1, 2, 3, 4, 5, 6, 7];
874 let mut c = v.rchunks(3);
875 assert_eq!(c.next_back().unwrap(), &[0, 1]);
876 assert_eq!(c.next_back().unwrap(), &[2, 3, 4]);
877 assert_eq!(c.next_back().unwrap(), &[5, 6, 7]);
878 assert_eq!(c.next_back(), None);
882 fn test_rchunks_last() {
883 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
884 let c = v.rchunks(2);
885 assert_eq!(c.last().unwrap()[1], 1);
887 let v2: &[i32] = &[0, 1, 2, 3, 4];
888 let c2 = v2.rchunks(2);
889 assert_eq!(c2.last().unwrap()[0], 0);
893 fn test_rchunks_zip() {
894 let v1: &[i32] = &[0, 1, 2, 3, 4];
895 let v2: &[i32] = &[6, 7, 8, 9, 10];
900 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
901 .collect::<Vec<_>>();
902 assert_eq!(res, vec![26, 18, 6]);
906 fn test_rchunks_mut_count() {
907 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
908 let c = v.rchunks_mut(3);
909 assert_eq!(c.count(), 2);
911 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
912 let c2 = v2.rchunks_mut(2);
913 assert_eq!(c2.count(), 3);
915 let v3: &mut [i32] = &mut [];
916 let c3 = v3.rchunks_mut(2);
917 assert_eq!(c3.count(), 0);
921 fn test_rchunks_mut_nth() {
922 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
923 let mut c = v.rchunks_mut(2);
924 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
925 assert_eq!(c.next().unwrap(), &[0, 1]);
927 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
928 let mut c2 = v2.rchunks_mut(3);
929 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
930 assert_eq!(c2.next(), None);
934 fn test_rchunks_mut_nth_back() {
935 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
936 let mut c = v.rchunks_mut(2);
937 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
938 assert_eq!(c.next_back().unwrap(), &[4, 5]);
940 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
941 let mut c2 = v2.rchunks_mut(3);
942 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
943 assert_eq!(c2.next_back(), None);
947 fn test_rchunks_mut_next() {
948 let mut v = [0, 1, 2, 3, 4, 5];
949 let mut c = v.rchunks_mut(2);
950 assert_eq!(c.next().unwrap(), &mut [4, 5]);
951 assert_eq!(c.next().unwrap(), &mut [2, 3]);
952 assert_eq!(c.next().unwrap(), &mut [0, 1]);
953 assert_eq!(c.next(), None);
955 let mut v = [0, 1, 2, 3, 4, 5, 6, 7];
956 let mut c = v.rchunks_mut(3);
957 assert_eq!(c.next().unwrap(), &mut [5, 6, 7]);
958 assert_eq!(c.next().unwrap(), &mut [2, 3, 4]);
959 assert_eq!(c.next().unwrap(), &mut [0, 1]);
960 assert_eq!(c.next(), None);
964 fn test_rchunks_mut_next_back() {
965 let mut v = [0, 1, 2, 3, 4, 5];
966 let mut c = v.rchunks_mut(2);
967 assert_eq!(c.next_back().unwrap(), &mut [0, 1]);
968 assert_eq!(c.next_back().unwrap(), &mut [2, 3]);
969 assert_eq!(c.next_back().unwrap(), &mut [4, 5]);
970 assert_eq!(c.next_back(), None);
972 let mut v = [0, 1, 2, 3, 4, 5, 6, 7];
973 let mut c = v.rchunks_mut(3);
974 assert_eq!(c.next_back().unwrap(), &mut [0, 1]);
975 assert_eq!(c.next_back().unwrap(), &mut [2, 3, 4]);
976 assert_eq!(c.next_back().unwrap(), &mut [5, 6, 7]);
977 assert_eq!(c.next_back(), None);
981 fn test_rchunks_mut_last() {
982 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
983 let c = v.rchunks_mut(2);
984 assert_eq!(c.last().unwrap(), &[0, 1]);
986 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
987 let c2 = v2.rchunks_mut(2);
988 assert_eq!(c2.last().unwrap(), &[0]);
992 fn test_rchunks_mut_zip() {
993 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
994 let v2: &[i32] = &[6, 7, 8, 9, 10];
996 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
997 let sum = b.iter().sum::<i32>();
1002 assert_eq!(v1, [6, 16, 17, 22, 23]);
1006 fn test_rchunks_exact_count() {
1007 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1008 let c = v.rchunks_exact(3);
1009 assert_eq!(c.count(), 2);
1011 let v2: &[i32] = &[0, 1, 2, 3, 4];
1012 let c2 = v2.rchunks_exact(2);
1013 assert_eq!(c2.count(), 2);
1015 let v3: &[i32] = &[];
1016 let c3 = v3.rchunks_exact(2);
1017 assert_eq!(c3.count(), 0);
1021 fn test_rchunks_exact_nth() {
1022 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1023 let mut c = v.rchunks_exact(2);
1024 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
1025 assert_eq!(c.next().unwrap(), &[0, 1]);
1027 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
1028 let mut c2 = v2.rchunks_exact(3);
1029 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
1030 assert_eq!(c2.next(), None);
1034 fn test_rchunks_exact_nth_back() {
1035 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1036 let mut c = v.rchunks_exact(2);
1037 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
1038 assert_eq!(c.next_back().unwrap(), &[4, 5]);
1040 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
1041 let mut c2 = v2.rchunks_exact(3);
1042 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
1043 assert_eq!(c2.next(), None);
1047 fn test_rchunks_exact_last() {
1048 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1049 let c = v.rchunks_exact(2);
1050 assert_eq!(c.last().unwrap(), &[0, 1]);
1052 let v2: &[i32] = &[0, 1, 2, 3, 4];
1053 let c2 = v2.rchunks_exact(2);
1054 assert_eq!(c2.last().unwrap(), &[1, 2]);
1058 fn test_rchunks_exact_remainder() {
1059 let v: &[i32] = &[0, 1, 2, 3, 4];
1060 let c = v.rchunks_exact(2);
1061 assert_eq!(c.remainder(), &[0]);
1065 fn test_rchunks_exact_zip() {
1066 let v1: &[i32] = &[0, 1, 2, 3, 4];
1067 let v2: &[i32] = &[6, 7, 8, 9, 10];
1071 .zip(v2.rchunks_exact(2))
1072 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
1073 .collect::<Vec<_>>();
1074 assert_eq!(res, vec![26, 18]);
1078 fn test_rchunks_exact_mut_count() {
1079 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
1080 let c = v.rchunks_exact_mut(3);
1081 assert_eq!(c.count(), 2);
1083 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
1084 let c2 = v2.rchunks_exact_mut(2);
1085 assert_eq!(c2.count(), 2);
1087 let v3: &mut [i32] = &mut [];
1088 let c3 = v3.rchunks_exact_mut(2);
1089 assert_eq!(c3.count(), 0);
1093 fn test_rchunks_exact_mut_nth() {
1094 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
1095 let mut c = v.rchunks_exact_mut(2);
1096 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
1097 assert_eq!(c.next().unwrap(), &[0, 1]);
1099 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
1100 let mut c2 = v2.rchunks_exact_mut(3);
1101 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
1102 assert_eq!(c2.next(), None);
1106 fn test_rchunks_exact_mut_nth_back() {
1107 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
1108 let mut c = v.rchunks_exact_mut(2);
1109 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
1110 assert_eq!(c.next_back().unwrap(), &[4, 5]);
1112 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
1113 let mut c2 = v2.rchunks_exact_mut(3);
1114 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
1115 assert_eq!(c2.next(), None);
1119 fn test_rchunks_exact_mut_last() {
1120 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
1121 let c = v.rchunks_exact_mut(2);
1122 assert_eq!(c.last().unwrap(), &[0, 1]);
1124 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
1125 let c2 = v2.rchunks_exact_mut(2);
1126 assert_eq!(c2.last().unwrap(), &[1, 2]);
1130 fn test_rchunks_exact_mut_remainder() {
1131 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
1132 let c = v.rchunks_exact_mut(2);
1133 assert_eq!(c.into_remainder(), &[0]);
1137 fn test_rchunks_exact_mut_zip() {
1138 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
1139 let v2: &[i32] = &[6, 7, 8, 9, 10];
1141 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
1142 let sum = b.iter().sum::<i32>();
1147 assert_eq!(v1, [0, 16, 17, 22, 23]);
1151 fn test_windows_count() {
1152 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1153 let c = v.windows(3);
1154 assert_eq!(c.count(), 4);
1156 let v2: &[i32] = &[0, 1, 2, 3, 4];
1157 let c2 = v2.windows(6);
1158 assert_eq!(c2.count(), 0);
1160 let v3: &[i32] = &[];
1161 let c3 = v3.windows(2);
1162 assert_eq!(c3.count(), 0);
1164 let v4 = &[(); usize::MAX];
1165 let c4 = v4.windows(1);
1166 assert_eq!(c4.count(), usize::MAX);
1170 fn test_windows_nth() {
1171 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1172 let mut c = v.windows(2);
1173 assert_eq!(c.nth(2).unwrap()[1], 3);
1174 assert_eq!(c.next().unwrap()[0], 3);
1176 let v2: &[i32] = &[0, 1, 2, 3, 4];
1177 let mut c2 = v2.windows(4);
1178 assert_eq!(c2.nth(1).unwrap()[1], 2);
1179 assert_eq!(c2.next(), None);
1183 fn test_windows_nth_back() {
1184 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1185 let mut c = v.windows(2);
1186 assert_eq!(c.nth_back(2).unwrap()[0], 2);
1187 assert_eq!(c.next_back().unwrap()[1], 2);
1189 let v2: &[i32] = &[0, 1, 2, 3, 4];
1190 let mut c2 = v2.windows(4);
1191 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
1192 assert_eq!(c2.next_back(), None);
1196 fn test_windows_last() {
1197 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
1198 let c = v.windows(2);
1199 assert_eq!(c.last().unwrap()[1], 5);
1201 let v2: &[i32] = &[0, 1, 2, 3, 4];
1202 let c2 = v2.windows(2);
1203 assert_eq!(c2.last().unwrap()[0], 3);
1207 fn test_windows_zip() {
1208 let v1: &[i32] = &[0, 1, 2, 3, 4];
1209 let v2: &[i32] = &[6, 7, 8, 9, 10];
1214 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
1215 .collect::<Vec<_>>();
1217 assert_eq!(res, [14, 18, 22, 26]);
1222 fn test_iter_ref_consistency() {
1223 use std::fmt::Debug;
1225 fn test<T: Copy + Debug + PartialEq>(x: T) {
1226 let v: &[T] = &[x, x, x];
1227 let v_ptrs: [*const T; 3] = match v {
1228 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
1229 _ => unreachable!(),
1235 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
1236 let nth = v.iter().nth(i).unwrap();
1237 assert_eq!(nth as *const _, v_ptrs[i]);
1239 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
1241 // stepping through with nth(0)
1243 let mut it = v.iter();
1245 let next = it.nth(0).unwrap();
1246 assert_eq!(next as *const _, v_ptrs[i]);
1248 assert_eq!(it.nth(0), None);
1253 let mut it = v.iter();
1255 let remaining = len - i;
1256 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
1258 let next = it.next().unwrap();
1259 assert_eq!(next as *const _, v_ptrs[i]);
1261 assert_eq!(it.size_hint(), (0, Some(0)));
1262 assert_eq!(it.next(), None, "The final call to next() should return None");
1267 let mut it = v.iter();
1269 let remaining = len - i;
1270 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
1272 let prev = it.next_back().unwrap();
1273 assert_eq!(prev as *const _, v_ptrs[remaining - 1]);
1275 assert_eq!(it.size_hint(), (0, Some(0)));
1276 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
1280 fn test_mut<T: Copy + Debug + PartialEq>(x: T) {
1281 let v: &mut [T] = &mut [x, x, x];
1282 let v_ptrs: [*mut T; 3] = match v {
1283 [ref v1, ref v2, ref v3] => {
1284 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _]
1286 _ => unreachable!(),
1292 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
1293 let nth = v.iter_mut().nth(i).unwrap();
1294 assert_eq!(nth as *mut _, v_ptrs[i]);
1296 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
1298 // stepping through with nth(0)
1300 let mut it = v.iter();
1302 let next = it.nth(0).unwrap();
1303 assert_eq!(next as *const _, v_ptrs[i]);
1305 assert_eq!(it.nth(0), None);
1310 let mut it = v.iter_mut();
1312 let remaining = len - i;
1313 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
1315 let next = it.next().unwrap();
1316 assert_eq!(next as *mut _, v_ptrs[i]);
1318 assert_eq!(it.size_hint(), (0, Some(0)));
1319 assert_eq!(it.next(), None, "The final call to next() should return None");
1324 let mut it = v.iter_mut();
1326 let remaining = len - i;
1327 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
1329 let prev = it.next_back().unwrap();
1330 assert_eq!(prev as *mut _, v_ptrs[remaining - 1]);
1332 assert_eq!(it.size_hint(), (0, Some(0)));
1333 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
1337 // Make sure iterators and slice patterns yield consistent addresses for various types,
1341 test([0u32; 0]); // ZST with alignment > 0
1344 test_mut([0u32; 0]); // ZST with alignment > 0
1347 // The current implementation of SliceIndex fails to handle methods
1348 // orthogonally from range types; therefore, it is worth testing
1349 // all of the indexing operations on each input.
1351 // This checks all six indexing methods, given an input range that
1352 // should succeed. (it is NOT suitable for testing invalid inputs)
1353 macro_rules! assert_range_eq {
1354 ($arr:expr, $range:expr, $expected:expr) => {
1356 let mut expected = $expected;
1359 let expected: &[_] = &expected;
1361 assert_eq!(&s[$range], expected, "(in assertion for: index)");
1362 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
1365 s.get_unchecked($range),
1367 "(in assertion for: get_unchecked)",
1372 let s: &mut [_] = &mut arr;
1373 let expected: &mut [_] = &mut expected;
1375 assert_eq!(&mut s[$range], expected, "(in assertion for: index_mut)",);
1378 Some(&mut expected[..]),
1379 "(in assertion for: get_mut)",
1383 s.get_unchecked_mut($range),
1385 "(in assertion for: get_unchecked_mut)",
1392 // Make sure the macro can actually detect bugs,
1393 // because if it can't, then what are we even doing here?
1395 // (Be aware this only demonstrates the ability to detect bugs
1396 // in the FIRST method that panics, as the macro is not designed
1397 // to be used in `should_panic`)
1399 #[should_panic(expected = "out of range")]
1400 fn assert_range_eq_can_fail_by_panic() {
1401 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
1404 // (Be aware this only demonstrates the ability to detect bugs
1405 // in the FIRST method it calls, as the macro is not designed
1406 // to be used in `should_panic`)
1408 #[should_panic(expected = "==")]
1409 fn assert_range_eq_can_fail_by_inequality() {
1410 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
1413 // Test cases for bad index operations.
1415 // This generates `should_panic` test cases for Index/IndexMut
1416 // and `None` test cases for get/get_mut.
1417 macro_rules! panic_cases {
1419 // each test case needs a unique name to namespace the tests
1420 in mod $case_name:ident {
1425 // one or more similar inputs for which data[input] succeeds,
1426 // and the corresponding output as an array. This helps validate
1427 // "critical points" where an input range straddles the boundary
1428 // between valid and invalid.
1429 // (such as the input `len..len`, which is just barely valid)
1431 good: data[$good:expr] == $output:expr;
1434 bad: data[$bad:expr];
1435 message: $expect_msg:expr;
1439 #[allow(unused_imports)]
1440 use core::ops::Bound;
1446 $( assert_range_eq!($data, $good, $output); )*
1450 assert_eq!(v.get($bad), None, "(in None assertion for get)");
1454 let v: &mut [_] = &mut v;
1455 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
1460 #[should_panic(expected = $expect_msg)]
1468 #[should_panic(expected = $expect_msg)]
1469 fn index_mut_fail() {
1471 let v: &mut [_] = &mut v;
1472 let _v = &mut v[$bad];
1480 let v = [0, 1, 2, 3, 4, 5];
1482 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
1483 assert_range_eq!(v, ..2, [0, 1]);
1484 assert_range_eq!(v, ..=1, [0, 1]);
1485 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
1486 assert_range_eq!(v, 1..4, [1, 2, 3]);
1487 assert_range_eq!(v, 1..=3, [1, 2, 3]);
1491 in mod rangefrom_len {
1492 data: [0, 1, 2, 3, 4, 5];
1494 good: data[6..] == [];
1496 message: "out of range";
1499 in mod rangeto_len {
1500 data: [0, 1, 2, 3, 4, 5];
1502 good: data[..6] == [0, 1, 2, 3, 4, 5];
1504 message: "out of range";
1507 in mod rangetoinclusive_len {
1508 data: [0, 1, 2, 3, 4, 5];
1510 good: data[..=5] == [0, 1, 2, 3, 4, 5];
1512 message: "out of range";
1515 in mod rangeinclusive_len {
1516 data: [0, 1, 2, 3, 4, 5];
1518 good: data[0..=5] == [0, 1, 2, 3, 4, 5];
1520 message: "out of range";
1523 in mod range_len_len {
1524 data: [0, 1, 2, 3, 4, 5];
1526 good: data[6..6] == [];
1528 message: "out of range";
1531 in mod rangeinclusive_len_len {
1532 data: [0, 1, 2, 3, 4, 5];
1534 good: data[6..=5] == [];
1536 message: "out of range";
1539 in mod boundpair_len {
1540 data: [0, 1, 2, 3, 4, 5];
1542 good: data[(Bound::Included(6), Bound::Unbounded)] == [];
1543 good: data[(Bound::Unbounded, Bound::Included(5))] == [0, 1, 2, 3, 4, 5];
1544 good: data[(Bound::Unbounded, Bound::Excluded(6))] == [0, 1, 2, 3, 4, 5];
1545 good: data[(Bound::Included(0), Bound::Included(5))] == [0, 1, 2, 3, 4, 5];
1546 good: data[(Bound::Included(0), Bound::Excluded(6))] == [0, 1, 2, 3, 4, 5];
1547 good: data[(Bound::Included(2), Bound::Excluded(4))] == [2, 3];
1548 good: data[(Bound::Excluded(1), Bound::Included(4))] == [2, 3, 4];
1549 good: data[(Bound::Excluded(5), Bound::Excluded(6))] == [];
1550 good: data[(Bound::Included(6), Bound::Excluded(6))] == [];
1551 good: data[(Bound::Excluded(5), Bound::Included(5))] == [];
1552 good: data[(Bound::Included(6), Bound::Included(5))] == [];
1553 bad: data[(Bound::Unbounded, Bound::Included(6))];
1554 message: "out of range";
1559 in mod rangeinclusive_exhausted {
1560 data: [0, 1, 2, 3, 4, 5];
1562 good: data[0..=5] == [0, 1, 2, 3, 4, 5];
1564 let mut iter = 0..=5;
1565 iter.by_ref().count(); // exhaust it
1569 // 0..=6 is out of range before exhaustion, so it
1570 // stands to reason that it still would be after.
1572 let mut iter = 0..=6;
1573 iter.by_ref().count(); // exhaust it
1576 message: "out of range";
1581 in mod range_neg_width {
1582 data: [0, 1, 2, 3, 4, 5];
1584 good: data[4..4] == [];
1586 message: "but ends at";
1589 in mod rangeinclusive_neg_width {
1590 data: [0, 1, 2, 3, 4, 5];
1592 good: data[4..=3] == [];
1594 message: "but ends at";
1597 in mod boundpair_neg_width {
1598 data: [0, 1, 2, 3, 4, 5];
1600 good: data[(Bound::Included(4), Bound::Excluded(4))] == [];
1601 bad: data[(Bound::Included(4), Bound::Excluded(3))];
1602 message: "but ends at";
1607 in mod rangeinclusive_overflow {
1610 // note: using 0 specifically ensures that the result of overflowing is 0..0,
1611 // so that `get` doesn't simply return None for the wrong reason.
1612 bad: data[0 ..= usize::MAX];
1613 message: "maximum usize";
1616 in mod rangetoinclusive_overflow {
1619 bad: data[..= usize::MAX];
1620 message: "maximum usize";
1623 in mod boundpair_overflow_end {
1626 bad: data[(Bound::Unbounded, Bound::Included(usize::MAX))];
1627 message: "maximum usize";
1630 in mod boundpair_overflow_start {
1633 bad: data[(Bound::Excluded(usize::MAX), Bound::Unbounded)];
1634 message: "maximum usize";
1640 fn test_find_rfind() {
1641 let v = [0, 1, 2, 3, 4, 5];
1642 let mut iter = v.iter();
1643 let mut i = v.len();
1644 while let Some(&elt) = iter.rfind(|_| true) {
1646 assert_eq!(elt, v[i]);
1649 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
1653 fn test_iter_folds() {
1654 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
1655 assert_eq!(a.iter().fold(0, |acc, &x| 2 * acc + x), 57);
1656 assert_eq!(a.iter().rfold(0, |acc, &x| 2 * acc + x), 129);
1657 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
1658 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
1659 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
1661 // short-circuiting try_fold, through other methods
1662 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
1663 let mut iter = a.iter();
1664 assert_eq!(iter.position(|&x| x == 3), Some(3));
1665 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
1666 assert_eq!(iter.len(), 2);
1670 fn test_rotate_left() {
1671 const N: usize = 600;
1672 let a: &mut [_] = &mut [0; N];
1681 assert_eq!(a[(i + k) % N], i);
1686 fn test_rotate_right() {
1687 const N: usize = 600;
1688 let a: &mut [_] = &mut [0; N];
1696 assert_eq!(a[(i + 42) % N], i);
1701 #[cfg_attr(miri, ignore)] // Miri is too slow
1702 fn brute_force_rotate_test_0() {
1703 // In case of edge cases involving multiple algorithms
1707 let mut v = Vec::with_capacity(len);
1711 v[..].rotate_right(s);
1712 for i in 0..v.len() {
1713 assert_eq!(v[i], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1720 fn brute_force_rotate_test_1() {
1721 // `ptr_rotate` covers so many kinds of pointer usage, that this is just a good test for
1722 // pointers in general. This uses a `[usize; 4]` to hit all algorithms without overwhelming miri
1726 let mut v: Vec<[usize; 4]> = Vec::with_capacity(len);
1728 v.push([i, 0, 0, 0]);
1730 v[..].rotate_right(s);
1731 for i in 0..v.len() {
1732 assert_eq!(v[i][0], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1739 #[cfg(not(target_arch = "wasm32"))]
1740 fn sort_unstable() {
1741 use core::cmp::Ordering::{Equal, Greater, Less};
1742 use core::slice::heapsort;
1743 use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
1745 // Miri is too slow (but still need to `chain` to make the types match)
1746 let lens = if cfg!(miri) { (2..20).chain(0..0) } else { (2..25).chain(500..510) };
1747 let rounds = if cfg!(miri) { 1 } else { 100 };
1749 let mut v = [0; 600];
1750 let mut tmp = [0; 600];
1751 let mut rng = StdRng::from_entropy();
1754 let v = &mut v[0..len];
1755 let tmp = &mut tmp[0..len];
1757 for &modulus in &[5, 10, 100, 1000] {
1758 for _ in 0..rounds {
1760 v[i] = rng.gen::<i32>() % modulus;
1763 // Sort in default order.
1764 tmp.copy_from_slice(v);
1765 tmp.sort_unstable();
1766 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1768 // Sort in ascending order.
1769 tmp.copy_from_slice(v);
1770 tmp.sort_unstable_by(|a, b| a.cmp(b));
1771 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1773 // Sort in descending order.
1774 tmp.copy_from_slice(v);
1775 tmp.sort_unstable_by(|a, b| b.cmp(a));
1776 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1778 // Test heapsort using `<` operator.
1779 tmp.copy_from_slice(v);
1780 heapsort(tmp, |a, b| a < b);
1781 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1783 // Test heapsort using `>` operator.
1784 tmp.copy_from_slice(v);
1785 heapsort(tmp, |a, b| a > b);
1786 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1791 // Sort using a completely random comparison function.
1792 // This will reorder the elements *somehow*, but won't panic.
1793 for i in 0..v.len() {
1796 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1798 for i in 0..v.len() {
1799 assert_eq!(v[i], i as i32);
1802 // Should not panic.
1803 [0i32; 0].sort_unstable();
1804 [(); 10].sort_unstable();
1805 [(); 100].sort_unstable();
1807 let mut v = [0xDEADBEEFu64];
1809 assert!(v == [0xDEADBEEF]);
1813 #[cfg(not(target_arch = "wasm32"))]
1814 #[cfg_attr(miri, ignore)] // Miri is too slow
1815 fn select_nth_unstable() {
1816 use core::cmp::Ordering::{Equal, Greater, Less};
1817 use rand::rngs::StdRng;
1818 use rand::seq::SliceRandom;
1819 use rand::{Rng, SeedableRng};
1821 let mut rng = StdRng::from_entropy();
1823 for len in (2..21).chain(500..501) {
1824 let mut orig = vec![0; len];
1826 for &modulus in &[5, 10, 1000] {
1829 orig[i] = rng.gen::<i32>() % modulus;
1833 let mut v = orig.clone();
1838 // Sort in default order.
1839 for pivot in 0..len {
1840 let mut v = orig.clone();
1841 v.select_nth_unstable(pivot);
1843 assert_eq!(v_sorted[pivot], v[pivot]);
1845 for j in pivot..len {
1846 assert!(v[i] <= v[j]);
1851 // Sort in ascending order.
1852 for pivot in 0..len {
1853 let mut v = orig.clone();
1854 let (left, pivot, right) = v.select_nth_unstable_by(pivot, |a, b| a.cmp(b));
1856 assert_eq!(left.len() + right.len(), len - 1);
1859 assert!(l <= pivot);
1860 for r in right.iter_mut() {
1862 assert!(pivot <= r);
1867 // Sort in descending order.
1868 let sort_descending_comparator = |a: &i32, b: &i32| b.cmp(a);
1869 let v_sorted_descending = {
1870 let mut v = orig.clone();
1871 v.sort_by(sort_descending_comparator);
1875 for pivot in 0..len {
1876 let mut v = orig.clone();
1877 v.select_nth_unstable_by(pivot, sort_descending_comparator);
1879 assert_eq!(v_sorted_descending[pivot], v[pivot]);
1881 for j in pivot..len {
1882 assert!(v[j] <= v[i]);
1890 // Sort at index using a completely random comparison function.
1891 // This will reorder the elements *somehow*, but won't panic.
1892 let mut v = [0; 500];
1893 for i in 0..v.len() {
1897 for pivot in 0..v.len() {
1898 v.select_nth_unstable_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1900 for i in 0..v.len() {
1901 assert_eq!(v[i], i as i32);
1905 // Should not panic.
1906 [(); 10].select_nth_unstable(0);
1907 [(); 10].select_nth_unstable(5);
1908 [(); 10].select_nth_unstable(9);
1909 [(); 100].select_nth_unstable(0);
1910 [(); 100].select_nth_unstable(50);
1911 [(); 100].select_nth_unstable(99);
1913 let mut v = [0xDEADBEEFu64];
1914 v.select_nth_unstable(0);
1915 assert!(v == [0xDEADBEEF]);
1919 #[should_panic(expected = "index 0 greater than length of slice")]
1920 fn select_nth_unstable_zero_length() {
1921 [0i32; 0].select_nth_unstable(0);
1925 #[should_panic(expected = "index 20 greater than length of slice")]
1926 fn select_nth_unstable_past_length() {
1927 [0i32; 10].select_nth_unstable(20);
1931 use core::slice::memchr::{memchr, memrchr};
1933 // test fallback implementations on all platforms
1936 assert_eq!(Some(0), memchr(b'a', b"a"));
1940 fn matches_begin() {
1941 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1946 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1951 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1955 fn matches_past_nul() {
1956 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1960 fn no_match_empty() {
1961 assert_eq!(None, memchr(b'a', b""));
1966 assert_eq!(None, memchr(b'a', b"xyz"));
1970 fn matches_one_reversed() {
1971 assert_eq!(Some(0), memrchr(b'a', b"a"));
1975 fn matches_begin_reversed() {
1976 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1980 fn matches_end_reversed() {
1981 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1985 fn matches_nul_reversed() {
1986 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1990 fn matches_past_nul_reversed() {
1991 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1995 fn no_match_empty_reversed() {
1996 assert_eq!(None, memrchr(b'a', b""));
2000 fn no_match_reversed() {
2001 assert_eq!(None, memrchr(b'a', b"xyz"));
2005 fn each_alignment_reversed() {
2006 let mut data = [1u8; 64];
2010 for start in 0..16 {
2011 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
2017 fn test_align_to_simple() {
2018 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
2019 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
2020 assert_eq!(aligned.len(), 3);
2021 assert!(prefix == [1] || suffix == [7]);
2022 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
2023 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
2024 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
2025 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
2027 aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
2028 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
2038 fn test_align_to_zst() {
2039 let bytes = [1, 2, 3, 4, 5, 6, 7];
2040 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
2041 assert_eq!(aligned.len(), 0);
2042 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
2046 fn test_align_to_non_trivial() {
2048 struct U64(u64, u64);
2050 struct U64U64U32(u64, u64, u32);
2061 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
2062 assert_eq!(aligned.len(), 4);
2063 assert_eq!(prefix.len() + suffix.len(), 2);
2067 fn test_align_to_empty_mid() {
2070 // Make sure that we do not create empty unaligned slices for the mid part, even when the
2071 // overall slice is too short to contain an aligned address.
2072 let bytes = [1, 2, 3, 4, 5, 6, 7];
2074 for offset in 0..4 {
2075 let (_, mid, _) = unsafe { bytes[offset..offset + 1].align_to::<Chunk>() };
2076 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
2081 fn test_align_to_mut_aliasing() {
2082 let mut val = [1u8, 2, 3, 4, 5];
2083 // `align_to_mut` used to create `mid` in a way that there was some intermediate
2084 // incorrect aliasing, invalidating the resulting `mid` slice.
2085 let (begin, mid, end) = unsafe { val.align_to_mut::<[u8; 2]>() };
2086 assert!(begin.len() == 0);
2087 assert!(end.len() == 1);
2089 assert_eq!(val, [3, 4, 3, 4, 5])
2093 fn test_slice_partition_dedup_by() {
2094 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
2096 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
2098 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
2099 assert_eq!(duplicates, [5, -1, 2]);
2103 fn test_slice_partition_dedup_empty() {
2104 let mut slice: [i32; 0] = [];
2106 let (dedup, duplicates) = slice.partition_dedup();
2108 assert_eq!(dedup, []);
2109 assert_eq!(duplicates, []);
2113 fn test_slice_partition_dedup_one() {
2114 let mut slice = [12];
2116 let (dedup, duplicates) = slice.partition_dedup();
2118 assert_eq!(dedup, [12]);
2119 assert_eq!(duplicates, []);
2123 fn test_slice_partition_dedup_multiple_ident() {
2124 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
2126 let (dedup, duplicates) = slice.partition_dedup();
2128 assert_eq!(dedup, [12, 11]);
2129 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
2133 fn test_slice_partition_dedup_partialeq() {
2135 struct Foo(i32, i32);
2137 impl PartialEq for Foo {
2138 fn eq(&self, other: &Foo) -> bool {
2143 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
2145 let (dedup, duplicates) = slice.partition_dedup();
2147 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
2148 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
2152 fn test_copy_within() {
2153 // Start to end, with a RangeTo.
2154 let mut bytes = *b"Hello, World!";
2155 bytes.copy_within(..3, 10);
2156 assert_eq!(&bytes, b"Hello, WorHel");
2158 // End to start, with a RangeFrom.
2159 let mut bytes = *b"Hello, World!";
2160 bytes.copy_within(10.., 0);
2161 assert_eq!(&bytes, b"ld!lo, World!");
2163 // Overlapping, with a RangeInclusive.
2164 let mut bytes = *b"Hello, World!";
2165 bytes.copy_within(0..=11, 1);
2166 assert_eq!(&bytes, b"HHello, World");
2168 // Whole slice, with a RangeFull.
2169 let mut bytes = *b"Hello, World!";
2170 bytes.copy_within(.., 0);
2171 assert_eq!(&bytes, b"Hello, World!");
2173 // Ensure that copying at the end of slice won't cause UB.
2174 let mut bytes = *b"Hello, World!";
2175 bytes.copy_within(13..13, 5);
2176 assert_eq!(&bytes, b"Hello, World!");
2177 bytes.copy_within(5..5, 13);
2178 assert_eq!(&bytes, b"Hello, World!");
2182 #[should_panic(expected = "range end index 14 out of range for slice of length 13")]
2183 fn test_copy_within_panics_src_too_long() {
2184 let mut bytes = *b"Hello, World!";
2185 // The length is only 13, so 14 is out of bounds.
2186 bytes.copy_within(10..14, 0);
2190 #[should_panic(expected = "dest is out of bounds")]
2191 fn test_copy_within_panics_dest_too_long() {
2192 let mut bytes = *b"Hello, World!";
2193 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
2194 bytes.copy_within(0..4, 10);
2198 #[should_panic(expected = "slice index starts at 2 but ends at 1")]
2199 fn test_copy_within_panics_src_inverted() {
2200 let mut bytes = *b"Hello, World!";
2201 // 2 is greater than 1, so this range is invalid.
2202 bytes.copy_within(2..1, 0);
2205 #[should_panic(expected = "attempted to index slice up to maximum usize")]
2206 fn test_copy_within_panics_src_out_of_bounds() {
2207 let mut bytes = *b"Hello, World!";
2208 // an inclusive range ending at usize::MAX would make src_end overflow
2209 bytes.copy_within(usize::MAX..=usize::MAX, 0);
2213 fn test_is_sorted() {
2214 let empty: [i32; 0] = [];
2216 assert!([1, 2, 2, 9].is_sorted());
2217 assert!(![1, 3, 2].is_sorted());
2218 assert!([0].is_sorted());
2219 assert!(empty.is_sorted());
2220 assert!(![0.0, 1.0, f32::NAN].is_sorted());
2221 assert!([-2, -1, 0, 3].is_sorted());
2222 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
2223 assert!(!["c", "bb", "aaa"].is_sorted());
2224 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
2228 fn test_slice_run_destructors() {
2229 // Make sure that destructors get run on slice literals
2234 impl<'a> Drop for Foo<'a> {
2235 fn drop(&mut self) {
2236 self.x.set(self.x.get() + 1);
2240 fn foo(x: &Cell<isize>) -> Foo<'_> {
2244 let x = &Cell::new(0);
2248 assert_eq!(l[0].x.get(), 0);
2251 assert_eq!(x.get(), 1);
2255 fn test_const_from_ref() {
2256 const VALUE: &i32 = &1;
2257 const SLICE: &[i32] = core::slice::from_ref(VALUE);
2259 assert!(core::ptr::eq(VALUE, &SLICE[0]))
2263 fn test_slice_fill_with_uninit() {
2264 // This should not UB. See #87891
2265 let mut a = [MaybeUninit::<u8>::uninit(); 10];
2266 a.fill(MaybeUninit::uninit());
2271 let mut x = ["a", "b", "c", "d"];
2273 assert_eq!(x, ["a", "d", "c", "b"]);
2275 assert_eq!(x, ["b", "d", "c", "a"]);
2280 #[should_panic(expected = "index out of bounds: the len is 4 but the index is 4")]
2281 fn index_a_equals_len() {
2282 let mut x = ["a", "b", "c", "d"];
2287 #[should_panic(expected = "index out of bounds: the len is 4 but the index is 4")]
2288 fn index_b_equals_len() {
2289 let mut x = ["a", "b", "c", "d"];
2294 #[should_panic(expected = "index out of bounds: the len is 4 but the index is 5")]
2295 fn index_a_greater_than_len() {
2296 let mut x = ["a", "b", "c", "d"];
2301 #[should_panic(expected = "index out of bounds: the len is 4 but the index is 5")]
2302 fn index_b_greater_than_len() {
2303 let mut x = ["a", "b", "c", "d"];
2309 fn slice_split_array_mut() {
2310 let v = &mut [1, 2, 3, 4, 5, 6][..];
2313 let (left, right) = v.split_array_mut::<0>();
2314 assert_eq!(left, &mut []);
2315 assert_eq!(right, [1, 2, 3, 4, 5, 6]);
2319 let (left, right) = v.split_array_mut::<6>();
2320 assert_eq!(left, &mut [1, 2, 3, 4, 5, 6]);
2321 assert_eq!(right, []);
2326 fn slice_rsplit_array_mut() {
2327 let v = &mut [1, 2, 3, 4, 5, 6][..];
2330 let (left, right) = v.rsplit_array_mut::<0>();
2331 assert_eq!(left, [1, 2, 3, 4, 5, 6]);
2332 assert_eq!(right, &mut []);
2336 let (left, right) = v.rsplit_array_mut::<6>();
2337 assert_eq!(left, []);
2338 assert_eq!(right, &mut [1, 2, 3, 4, 5, 6]);
2344 fn slice_split_array_ref_out_of_bounds() {
2345 let v = &[1, 2, 3, 4, 5, 6][..];
2347 let _ = v.split_array_ref::<7>();
2352 fn slice_split_array_mut_out_of_bounds() {
2353 let v = &mut [1, 2, 3, 4, 5, 6][..];
2355 let _ = v.split_array_mut::<7>();
2360 fn slice_rsplit_array_ref_out_of_bounds() {
2361 let v = &[1, 2, 3, 4, 5, 6][..];
2363 let _ = v.rsplit_array_ref::<7>();
2368 fn slice_rsplit_array_mut_out_of_bounds() {
2369 let v = &mut [1, 2, 3, 4, 5, 6][..];
2371 let _ = v.rsplit_array_mut::<7>();
2374 macro_rules! take_tests {
2375 (slice: &[], $($tts:tt)*) => {
2376 take_tests!(ty: &[()], slice: &[], $($tts)*);
2378 (slice: &mut [], $($tts:tt)*) => {
2379 take_tests!(ty: &mut [()], slice: &mut [], $($tts)*);
2381 (slice: &$slice:expr, $($tts:tt)*) => {
2382 take_tests!(ty: &[_], slice: &$slice, $($tts)*);
2384 (slice: &mut $slice:expr, $($tts:tt)*) => {
2385 take_tests!(ty: &mut [_], slice: &mut $slice, $($tts)*);
2387 (ty: $ty:ty, slice: $slice:expr, method: $method:ident, $(($test_name:ident, ($($args:expr),*), $output:expr, $remaining:expr),)*) => {
2391 let mut slice: $ty = $slice;
2392 assert_eq!($output, slice.$method($($args)*));
2393 let remaining: $ty = $remaining;
2394 assert_eq!(remaining, slice);
2401 slice: &[0, 1, 2, 3], method: take,
2402 (take_in_bounds_range_to, (..1), Some(&[0] as _), &[1, 2, 3]),
2403 (take_in_bounds_range_to_inclusive, (..=0), Some(&[0] as _), &[1, 2, 3]),
2404 (take_in_bounds_range_from, (2..), Some(&[2, 3] as _), &[0, 1]),
2405 (take_oob_range_to, (..5), None, &[0, 1, 2, 3]),
2406 (take_oob_range_to_inclusive, (..=4), None, &[0, 1, 2, 3]),
2407 (take_oob_range_from, (5..), None, &[0, 1, 2, 3]),
2411 slice: &mut [0, 1, 2, 3], method: take_mut,
2412 (take_mut_in_bounds_range_to, (..1), Some(&mut [0] as _), &mut [1, 2, 3]),
2413 (take_mut_in_bounds_range_to_inclusive, (..=0), Some(&mut [0] as _), &mut [1, 2, 3]),
2414 (take_mut_in_bounds_range_from, (2..), Some(&mut [2, 3] as _), &mut [0, 1]),
2415 (take_mut_oob_range_to, (..5), None, &mut [0, 1, 2, 3]),
2416 (take_mut_oob_range_to_inclusive, (..=4), None, &mut [0, 1, 2, 3]),
2417 (take_mut_oob_range_from, (5..), None, &mut [0, 1, 2, 3]),
2421 slice: &[1, 2], method: take_first,
2422 (take_first_nonempty, (), Some(&1), &[2]),
2426 slice: &mut [1, 2], method: take_first_mut,
2427 (take_first_mut_nonempty, (), Some(&mut 1), &mut [2]),
2431 slice: &[1, 2], method: take_last,
2432 (take_last_nonempty, (), Some(&2), &[1]),
2436 slice: &mut [1, 2], method: take_last_mut,
2437 (take_last_mut_nonempty, (), Some(&mut 2), &mut [1]),
2441 slice: &[], method: take_first,
2442 (take_first_empty, (), None, &[]),
2446 slice: &mut [], method: take_first_mut,
2447 (take_first_mut_empty, (), None, &mut []),
2451 slice: &[], method: take_last,
2452 (take_last_empty, (), None, &[]),
2456 slice: &mut [], method: take_last_mut,
2457 (take_last_mut_empty, (), None, &mut []),
2460 #[cfg(not(miri))] // unused in Miri
2461 const EMPTY_MAX: &'static [()] = &[(); usize::MAX];
2463 // can't be a constant due to const mutability rules
2464 #[cfg(not(miri))] // unused in Miri
2465 macro_rules! empty_max_mut {
2467 &mut [(); usize::MAX] as _
2471 #[cfg(not(miri))] // Comparing usize::MAX many elements takes forever in Miri (and in rustc without optimizations)
2473 slice: &[(); usize::MAX], method: take,
2474 (take_in_bounds_max_range_to, (..usize::MAX), Some(EMPTY_MAX), &[(); 0]),
2475 (take_oob_max_range_to_inclusive, (..=usize::MAX), None, EMPTY_MAX),
2476 (take_in_bounds_max_range_from, (usize::MAX..), Some(&[] as _), EMPTY_MAX),
2479 #[cfg(not(miri))] // Comparing usize::MAX many elements takes forever in Miri (and in rustc without optimizations)
2481 slice: &mut [(); usize::MAX], method: take_mut,
2482 (take_mut_in_bounds_max_range_to, (..usize::MAX), Some(empty_max_mut!()), &mut [(); 0]),
2483 (take_mut_oob_max_range_to_inclusive, (..=usize::MAX), None, empty_max_mut!()),
2484 (take_mut_in_bounds_max_range_from, (usize::MAX..), Some(&mut [] as _), empty_max_mut!()),
2488 fn test_slice_from_ptr_range() {
2489 let arr = ["foo".to_owned(), "bar".to_owned()];
2490 let range = arr.as_ptr_range();
2492 assert_eq!(slice::from_ptr_range(range), &arr);
2495 let mut arr = [1, 2, 3];
2496 let range = arr.as_mut_ptr_range();
2498 assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
2501 let arr: [Vec<String>; 0] = [];
2502 let range = arr.as_ptr_range();
2504 assert_eq!(slice::from_ptr_range(range), &arr);
2509 #[cfg(not(bootstrap))]
2510 #[should_panic = "slice len overflow"]
2511 fn test_flatten_size_overflow() {
2512 let x = &[[(); usize::MAX]; 2][..];
2513 let _ = x.flatten();
2517 #[cfg(not(bootstrap))]
2518 #[should_panic = "slice len overflow"]
2519 fn test_flatten_mut_size_overflow() {
2520 let x = &mut [[(); usize::MAX]; 2][..];
2521 let _ = x.flatten_mut();