1 use core::result::Result::{Err, Ok};
5 let b = [1, 2, 3, 5, 5];
6 assert_eq!(b.iter().position(|&v| v == 9), None);
7 assert_eq!(b.iter().position(|&v| v == 5), Some(3));
8 assert_eq!(b.iter().position(|&v| v == 3), Some(2));
9 assert_eq!(b.iter().position(|&v| v == 0), None);
14 let b = [1, 2, 3, 5, 5];
15 assert_eq!(b.iter().rposition(|&v| v == 9), None);
16 assert_eq!(b.iter().rposition(|&v| v == 5), Some(4));
17 assert_eq!(b.iter().rposition(|&v| v == 3), Some(2));
18 assert_eq!(b.iter().rposition(|&v| v == 0), None);
22 fn test_binary_search() {
24 assert_eq!(b.binary_search(&5), Err(0));
27 assert_eq!(b.binary_search(&3), Err(0));
28 assert_eq!(b.binary_search(&4), Ok(0));
29 assert_eq!(b.binary_search(&5), Err(1));
31 let b = [1, 2, 4, 6, 8, 9];
32 assert_eq!(b.binary_search(&5), Err(3));
33 assert_eq!(b.binary_search(&6), Ok(3));
34 assert_eq!(b.binary_search(&7), Err(4));
35 assert_eq!(b.binary_search(&8), Ok(4));
37 let b = [1, 2, 4, 5, 6, 8];
38 assert_eq!(b.binary_search(&9), Err(6));
40 let b = [1, 2, 4, 6, 7, 8, 9];
41 assert_eq!(b.binary_search(&6), Ok(3));
42 assert_eq!(b.binary_search(&5), Err(3));
43 assert_eq!(b.binary_search(&8), Ok(5));
45 let b = [1, 2, 4, 5, 6, 8, 9];
46 assert_eq!(b.binary_search(&7), Err(5));
47 assert_eq!(b.binary_search(&0), Err(0));
49 let b = [1, 3, 3, 3, 7];
50 assert_eq!(b.binary_search(&0), Err(0));
51 assert_eq!(b.binary_search(&1), Ok(0));
52 assert_eq!(b.binary_search(&2), Err(1));
53 assert!(match b.binary_search(&3) {
57 assert!(match b.binary_search(&3) {
61 assert_eq!(b.binary_search(&4), Err(4));
62 assert_eq!(b.binary_search(&5), Err(4));
63 assert_eq!(b.binary_search(&6), Err(4));
64 assert_eq!(b.binary_search(&7), Ok(4));
65 assert_eq!(b.binary_search(&8), Err(5));
69 // Test implementation specific behavior when finding equivalent elements.
70 // It is ok to break this test but when you do a crater run is highly advisable.
71 fn test_binary_search_implementation_details() {
72 let b = [1, 1, 2, 2, 3, 3, 3];
73 assert_eq!(b.binary_search(&1), Ok(1));
74 assert_eq!(b.binary_search(&2), Ok(3));
75 assert_eq!(b.binary_search(&3), Ok(6));
76 let b = [1, 1, 1, 1, 1, 3, 3, 3, 3];
77 assert_eq!(b.binary_search(&1), Ok(4));
78 assert_eq!(b.binary_search(&3), Ok(8));
79 let b = [1, 1, 1, 1, 3, 3, 3, 3, 3];
80 assert_eq!(b.binary_search(&1), Ok(3));
81 assert_eq!(b.binary_search(&3), Ok(8));
85 fn test_partition_point() {
87 assert_eq!(b.partition_point(|&x| x < 5), 0);
90 assert_eq!(b.partition_point(|&x| x < 3), 0);
91 assert_eq!(b.partition_point(|&x| x < 4), 0);
92 assert_eq!(b.partition_point(|&x| x < 5), 1);
94 let b = [1, 2, 4, 6, 8, 9];
95 assert_eq!(b.partition_point(|&x| x < 5), 3);
96 assert_eq!(b.partition_point(|&x| x < 6), 3);
97 assert_eq!(b.partition_point(|&x| x < 7), 4);
98 assert_eq!(b.partition_point(|&x| x < 8), 4);
100 let b = [1, 2, 4, 5, 6, 8];
101 assert_eq!(b.partition_point(|&x| x < 9), 6);
103 let b = [1, 2, 4, 6, 7, 8, 9];
104 assert_eq!(b.partition_point(|&x| x < 6), 3);
105 assert_eq!(b.partition_point(|&x| x < 5), 3);
106 assert_eq!(b.partition_point(|&x| x < 8), 5);
108 let b = [1, 2, 4, 5, 6, 8, 9];
109 assert_eq!(b.partition_point(|&x| x < 7), 5);
110 assert_eq!(b.partition_point(|&x| x < 0), 0);
112 let b = [1, 3, 3, 3, 7];
113 assert_eq!(b.partition_point(|&x| x < 0), 0);
114 assert_eq!(b.partition_point(|&x| x < 1), 0);
115 assert_eq!(b.partition_point(|&x| x < 2), 1);
116 assert_eq!(b.partition_point(|&x| x < 3), 1);
117 assert_eq!(b.partition_point(|&x| x < 4), 4);
118 assert_eq!(b.partition_point(|&x| x < 5), 4);
119 assert_eq!(b.partition_point(|&x| x < 6), 4);
120 assert_eq!(b.partition_point(|&x| x < 7), 4);
121 assert_eq!(b.partition_point(|&x| x < 8), 5);
125 fn test_iterator_nth() {
126 let v: &[_] = &[0, 1, 2, 3, 4];
127 for i in 0..v.len() {
128 assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
130 assert_eq!(v.iter().nth(v.len()), None);
132 let mut iter = v.iter();
133 assert_eq!(iter.nth(2).unwrap(), &v[2]);
134 assert_eq!(iter.nth(1).unwrap(), &v[4]);
138 fn test_iterator_nth_back() {
139 let v: &[_] = &[0, 1, 2, 3, 4];
140 for i in 0..v.len() {
141 assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - i - 1]);
143 assert_eq!(v.iter().nth_back(v.len()), None);
145 let mut iter = v.iter();
146 assert_eq!(iter.nth_back(2).unwrap(), &v[2]);
147 assert_eq!(iter.nth_back(1).unwrap(), &v[0]);
151 fn test_iterator_last() {
152 let v: &[_] = &[0, 1, 2, 3, 4];
153 assert_eq!(v.iter().last().unwrap(), &4);
154 assert_eq!(v[..1].iter().last().unwrap(), &0);
158 fn test_iterator_count() {
159 let v: &[_] = &[0, 1, 2, 3, 4];
160 assert_eq!(v.iter().count(), 5);
162 let mut iter2 = v.iter();
165 assert_eq!(iter2.count(), 3);
169 fn test_chunks_count() {
170 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
172 assert_eq!(c.count(), 2);
174 let v2: &[i32] = &[0, 1, 2, 3, 4];
175 let c2 = v2.chunks(2);
176 assert_eq!(c2.count(), 3);
178 let v3: &[i32] = &[];
179 let c3 = v3.chunks(2);
180 assert_eq!(c3.count(), 0);
184 fn test_chunks_nth() {
185 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
186 let mut c = v.chunks(2);
187 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
188 assert_eq!(c.next().unwrap(), &[4, 5]);
190 let v2: &[i32] = &[0, 1, 2, 3, 4];
191 let mut c2 = v2.chunks(3);
192 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
193 assert_eq!(c2.next(), None);
197 fn test_chunks_nth_back() {
198 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
199 let mut c = v.chunks(2);
200 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
201 assert_eq!(c.next().unwrap(), &[0, 1]);
202 assert_eq!(c.next(), None);
204 let v2: &[i32] = &[0, 1, 2, 3, 4];
205 let mut c2 = v2.chunks(3);
206 assert_eq!(c2.nth_back(1).unwrap(), &[0, 1, 2]);
207 assert_eq!(c2.next(), None);
208 assert_eq!(c2.next_back(), None);
210 let v3: &[i32] = &[0, 1, 2, 3, 4];
211 let mut c3 = v3.chunks(10);
212 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
213 assert_eq!(c3.next(), None);
215 let v4: &[i32] = &[0, 1, 2];
216 let mut c4 = v4.chunks(10);
217 assert_eq!(c4.nth_back(1_000_000_000usize), None);
221 fn test_chunks_last() {
222 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
224 assert_eq!(c.last().unwrap()[1], 5);
226 let v2: &[i32] = &[0, 1, 2, 3, 4];
227 let c2 = v2.chunks(2);
228 assert_eq!(c2.last().unwrap()[0], 4);
232 fn test_chunks_zip() {
233 let v1: &[i32] = &[0, 1, 2, 3, 4];
234 let v2: &[i32] = &[6, 7, 8, 9, 10];
239 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
240 .collect::<Vec<_>>();
241 assert_eq!(res, vec![14, 22, 14]);
245 fn test_chunks_mut_count() {
246 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
247 let c = v.chunks_mut(3);
248 assert_eq!(c.count(), 2);
250 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
251 let c2 = v2.chunks_mut(2);
252 assert_eq!(c2.count(), 3);
254 let v3: &mut [i32] = &mut [];
255 let c3 = v3.chunks_mut(2);
256 assert_eq!(c3.count(), 0);
260 fn test_chunks_mut_nth() {
261 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
262 let mut c = v.chunks_mut(2);
263 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
264 assert_eq!(c.next().unwrap(), &[4, 5]);
266 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
267 let mut c2 = v2.chunks_mut(3);
268 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
269 assert_eq!(c2.next(), None);
273 fn test_chunks_mut_nth_back() {
274 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
275 let mut c = v.chunks_mut(2);
276 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
277 assert_eq!(c.next().unwrap(), &[0, 1]);
279 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
280 let mut c1 = v1.chunks_mut(3);
281 assert_eq!(c1.nth_back(1).unwrap(), &[0, 1, 2]);
282 assert_eq!(c1.next(), None);
284 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
285 let mut c3 = v3.chunks_mut(10);
286 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
287 assert_eq!(c3.next(), None);
289 let v4: &mut [i32] = &mut [0, 1, 2];
290 let mut c4 = v4.chunks_mut(10);
291 assert_eq!(c4.nth_back(1_000_000_000usize), None);
295 fn test_chunks_mut_last() {
296 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
297 let c = v.chunks_mut(2);
298 assert_eq!(c.last().unwrap(), &[4, 5]);
300 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
301 let c2 = v2.chunks_mut(2);
302 assert_eq!(c2.last().unwrap(), &[4]);
306 fn test_chunks_mut_zip() {
307 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
308 let v2: &[i32] = &[6, 7, 8, 9, 10];
310 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
311 let sum = b.iter().sum::<i32>();
316 assert_eq!(v1, [13, 14, 19, 20, 14]);
320 fn test_chunks_exact_count() {
321 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
322 let c = v.chunks_exact(3);
323 assert_eq!(c.count(), 2);
325 let v2: &[i32] = &[0, 1, 2, 3, 4];
326 let c2 = v2.chunks_exact(2);
327 assert_eq!(c2.count(), 2);
329 let v3: &[i32] = &[];
330 let c3 = v3.chunks_exact(2);
331 assert_eq!(c3.count(), 0);
335 fn test_chunks_exact_nth() {
336 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
337 let mut c = v.chunks_exact(2);
338 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
339 assert_eq!(c.next().unwrap(), &[4, 5]);
341 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
342 let mut c2 = v2.chunks_exact(3);
343 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
344 assert_eq!(c2.next(), None);
348 fn test_chunks_exact_nth_back() {
349 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
350 let mut c = v.chunks_exact(2);
351 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
352 assert_eq!(c.next().unwrap(), &[0, 1]);
353 assert_eq!(c.next(), None);
355 let v2: &[i32] = &[0, 1, 2, 3, 4];
356 let mut c2 = v2.chunks_exact(3);
357 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
358 assert_eq!(c2.next(), None);
359 assert_eq!(c2.next_back(), None);
361 let v3: &[i32] = &[0, 1, 2, 3, 4];
362 let mut c3 = v3.chunks_exact(10);
363 assert_eq!(c3.nth_back(0), None);
367 fn test_chunks_exact_last() {
368 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
369 let c = v.chunks_exact(2);
370 assert_eq!(c.last().unwrap(), &[4, 5]);
372 let v2: &[i32] = &[0, 1, 2, 3, 4];
373 let c2 = v2.chunks_exact(2);
374 assert_eq!(c2.last().unwrap(), &[2, 3]);
378 fn test_chunks_exact_remainder() {
379 let v: &[i32] = &[0, 1, 2, 3, 4];
380 let c = v.chunks_exact(2);
381 assert_eq!(c.remainder(), &[4]);
385 fn test_chunks_exact_zip() {
386 let v1: &[i32] = &[0, 1, 2, 3, 4];
387 let v2: &[i32] = &[6, 7, 8, 9, 10];
391 .zip(v2.chunks_exact(2))
392 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
393 .collect::<Vec<_>>();
394 assert_eq!(res, vec![14, 22]);
398 fn test_chunks_exact_mut_count() {
399 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
400 let c = v.chunks_exact_mut(3);
401 assert_eq!(c.count(), 2);
403 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
404 let c2 = v2.chunks_exact_mut(2);
405 assert_eq!(c2.count(), 2);
407 let v3: &mut [i32] = &mut [];
408 let c3 = v3.chunks_exact_mut(2);
409 assert_eq!(c3.count(), 0);
413 fn test_chunks_exact_mut_nth() {
414 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
415 let mut c = v.chunks_exact_mut(2);
416 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
417 assert_eq!(c.next().unwrap(), &[4, 5]);
419 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
420 let mut c2 = v2.chunks_exact_mut(3);
421 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
422 assert_eq!(c2.next(), None);
426 fn test_chunks_exact_mut_nth_back() {
427 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
428 let mut c = v.chunks_exact_mut(2);
429 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
430 assert_eq!(c.next().unwrap(), &[0, 1]);
431 assert_eq!(c.next(), None);
433 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
434 let mut c2 = v2.chunks_exact_mut(3);
435 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
436 assert_eq!(c2.next(), None);
437 assert_eq!(c2.next_back(), None);
439 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
440 let mut c3 = v3.chunks_exact_mut(10);
441 assert_eq!(c3.nth_back(0), None);
445 fn test_chunks_exact_mut_last() {
446 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
447 let c = v.chunks_exact_mut(2);
448 assert_eq!(c.last().unwrap(), &[4, 5]);
450 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
451 let c2 = v2.chunks_exact_mut(2);
452 assert_eq!(c2.last().unwrap(), &[2, 3]);
456 fn test_chunks_exact_mut_remainder() {
457 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
458 let c = v.chunks_exact_mut(2);
459 assert_eq!(c.into_remainder(), &[4]);
463 fn test_chunks_exact_mut_zip() {
464 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
465 let v2: &[i32] = &[6, 7, 8, 9, 10];
467 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
468 let sum = b.iter().sum::<i32>();
473 assert_eq!(v1, [13, 14, 19, 20, 4]);
477 fn test_array_chunks_infer() {
478 let v: &[i32] = &[0, 1, 2, 3, 4, -4];
479 let c = v.array_chunks();
480 for &[a, b, c] in c {
481 assert_eq!(a + b + c, 3);
484 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
485 let total = v2.array_chunks().map(|&[a, b]| a * b).sum::<i32>();
486 assert_eq!(total, 2 * 3 + 4 * 5);
490 fn test_array_chunks_count() {
491 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
492 let c = v.array_chunks::<3>();
493 assert_eq!(c.count(), 2);
495 let v2: &[i32] = &[0, 1, 2, 3, 4];
496 let c2 = v2.array_chunks::<2>();
497 assert_eq!(c2.count(), 2);
499 let v3: &[i32] = &[];
500 let c3 = v3.array_chunks::<2>();
501 assert_eq!(c3.count(), 0);
505 fn test_array_chunks_nth() {
506 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
507 let mut c = v.array_chunks::<2>();
508 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
509 assert_eq!(c.next().unwrap(), &[4, 5]);
511 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
512 let mut c2 = v2.array_chunks::<3>();
513 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
514 assert_eq!(c2.next(), None);
518 fn test_array_chunks_nth_back() {
519 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
520 let mut c = v.array_chunks::<2>();
521 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
522 assert_eq!(c.next().unwrap(), &[0, 1]);
523 assert_eq!(c.next(), None);
525 let v2: &[i32] = &[0, 1, 2, 3, 4];
526 let mut c2 = v2.array_chunks::<3>();
527 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
528 assert_eq!(c2.next(), None);
529 assert_eq!(c2.next_back(), None);
531 let v3: &[i32] = &[0, 1, 2, 3, 4];
532 let mut c3 = v3.array_chunks::<10>();
533 assert_eq!(c3.nth_back(0), None);
537 fn test_array_chunks_last() {
538 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
539 let c = v.array_chunks::<2>();
540 assert_eq!(c.last().unwrap(), &[4, 5]);
542 let v2: &[i32] = &[0, 1, 2, 3, 4];
543 let c2 = v2.array_chunks::<2>();
544 assert_eq!(c2.last().unwrap(), &[2, 3]);
548 fn test_array_chunks_remainder() {
549 let v: &[i32] = &[0, 1, 2, 3, 4];
550 let c = v.array_chunks::<2>();
551 assert_eq!(c.remainder(), &[4]);
555 fn test_array_chunks_zip() {
556 let v1: &[i32] = &[0, 1, 2, 3, 4];
557 let v2: &[i32] = &[6, 7, 8, 9, 10];
561 .zip(v2.array_chunks::<2>())
562 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
563 .collect::<Vec<_>>();
564 assert_eq!(res, vec![14, 22]);
568 fn test_rchunks_count() {
569 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
570 let c = v.rchunks(3);
571 assert_eq!(c.count(), 2);
573 let v2: &[i32] = &[0, 1, 2, 3, 4];
574 let c2 = v2.rchunks(2);
575 assert_eq!(c2.count(), 3);
577 let v3: &[i32] = &[];
578 let c3 = v3.rchunks(2);
579 assert_eq!(c3.count(), 0);
583 fn test_rchunks_nth() {
584 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
585 let mut c = v.rchunks(2);
586 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
587 assert_eq!(c.next().unwrap(), &[0, 1]);
589 let v2: &[i32] = &[0, 1, 2, 3, 4];
590 let mut c2 = v2.rchunks(3);
591 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
592 assert_eq!(c2.next(), None);
596 fn test_rchunks_nth_back() {
597 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
598 let mut c = v.rchunks(2);
599 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
600 assert_eq!(c.next_back().unwrap(), &[4, 5]);
602 let v2: &[i32] = &[0, 1, 2, 3, 4];
603 let mut c2 = v2.rchunks(3);
604 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
605 assert_eq!(c2.next_back(), None);
609 fn test_rchunks_last() {
610 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
611 let c = v.rchunks(2);
612 assert_eq!(c.last().unwrap()[1], 1);
614 let v2: &[i32] = &[0, 1, 2, 3, 4];
615 let c2 = v2.rchunks(2);
616 assert_eq!(c2.last().unwrap()[0], 0);
620 fn test_rchunks_zip() {
621 let v1: &[i32] = &[0, 1, 2, 3, 4];
622 let v2: &[i32] = &[6, 7, 8, 9, 10];
627 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
628 .collect::<Vec<_>>();
629 assert_eq!(res, vec![26, 18, 6]);
633 fn test_rchunks_mut_count() {
634 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
635 let c = v.rchunks_mut(3);
636 assert_eq!(c.count(), 2);
638 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
639 let c2 = v2.rchunks_mut(2);
640 assert_eq!(c2.count(), 3);
642 let v3: &mut [i32] = &mut [];
643 let c3 = v3.rchunks_mut(2);
644 assert_eq!(c3.count(), 0);
648 fn test_rchunks_mut_nth() {
649 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
650 let mut c = v.rchunks_mut(2);
651 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
652 assert_eq!(c.next().unwrap(), &[0, 1]);
654 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
655 let mut c2 = v2.rchunks_mut(3);
656 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
657 assert_eq!(c2.next(), None);
661 fn test_rchunks_mut_nth_back() {
662 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
663 let mut c = v.rchunks_mut(2);
664 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
665 assert_eq!(c.next_back().unwrap(), &[4, 5]);
667 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
668 let mut c2 = v2.rchunks_mut(3);
669 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
670 assert_eq!(c2.next_back(), None);
674 fn test_rchunks_mut_last() {
675 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
676 let c = v.rchunks_mut(2);
677 assert_eq!(c.last().unwrap(), &[0, 1]);
679 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
680 let c2 = v2.rchunks_mut(2);
681 assert_eq!(c2.last().unwrap(), &[0]);
685 fn test_rchunks_mut_zip() {
686 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
687 let v2: &[i32] = &[6, 7, 8, 9, 10];
689 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
690 let sum = b.iter().sum::<i32>();
695 assert_eq!(v1, [6, 16, 17, 22, 23]);
699 fn test_rchunks_exact_count() {
700 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
701 let c = v.rchunks_exact(3);
702 assert_eq!(c.count(), 2);
704 let v2: &[i32] = &[0, 1, 2, 3, 4];
705 let c2 = v2.rchunks_exact(2);
706 assert_eq!(c2.count(), 2);
708 let v3: &[i32] = &[];
709 let c3 = v3.rchunks_exact(2);
710 assert_eq!(c3.count(), 0);
714 fn test_rchunks_exact_nth() {
715 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
716 let mut c = v.rchunks_exact(2);
717 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
718 assert_eq!(c.next().unwrap(), &[0, 1]);
720 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
721 let mut c2 = v2.rchunks_exact(3);
722 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
723 assert_eq!(c2.next(), None);
727 fn test_rchunks_exact_nth_back() {
728 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
729 let mut c = v.rchunks_exact(2);
730 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
731 assert_eq!(c.next_back().unwrap(), &[4, 5]);
733 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
734 let mut c2 = v2.rchunks_exact(3);
735 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
736 assert_eq!(c2.next(), None);
740 fn test_rchunks_exact_last() {
741 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
742 let c = v.rchunks_exact(2);
743 assert_eq!(c.last().unwrap(), &[0, 1]);
745 let v2: &[i32] = &[0, 1, 2, 3, 4];
746 let c2 = v2.rchunks_exact(2);
747 assert_eq!(c2.last().unwrap(), &[1, 2]);
751 fn test_rchunks_exact_remainder() {
752 let v: &[i32] = &[0, 1, 2, 3, 4];
753 let c = v.rchunks_exact(2);
754 assert_eq!(c.remainder(), &[0]);
758 fn test_rchunks_exact_zip() {
759 let v1: &[i32] = &[0, 1, 2, 3, 4];
760 let v2: &[i32] = &[6, 7, 8, 9, 10];
764 .zip(v2.rchunks_exact(2))
765 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
766 .collect::<Vec<_>>();
767 assert_eq!(res, vec![26, 18]);
771 fn test_rchunks_exact_mut_count() {
772 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
773 let c = v.rchunks_exact_mut(3);
774 assert_eq!(c.count(), 2);
776 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
777 let c2 = v2.rchunks_exact_mut(2);
778 assert_eq!(c2.count(), 2);
780 let v3: &mut [i32] = &mut [];
781 let c3 = v3.rchunks_exact_mut(2);
782 assert_eq!(c3.count(), 0);
786 fn test_rchunks_exact_mut_nth() {
787 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
788 let mut c = v.rchunks_exact_mut(2);
789 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
790 assert_eq!(c.next().unwrap(), &[0, 1]);
792 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
793 let mut c2 = v2.rchunks_exact_mut(3);
794 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
795 assert_eq!(c2.next(), None);
799 fn test_rchunks_exact_mut_nth_back() {
800 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
801 let mut c = v.rchunks_exact_mut(2);
802 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
803 assert_eq!(c.next_back().unwrap(), &[4, 5]);
805 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
806 let mut c2 = v2.rchunks_exact_mut(3);
807 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
808 assert_eq!(c2.next(), None);
812 fn test_rchunks_exact_mut_last() {
813 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
814 let c = v.rchunks_exact_mut(2);
815 assert_eq!(c.last().unwrap(), &[0, 1]);
817 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
818 let c2 = v2.rchunks_exact_mut(2);
819 assert_eq!(c2.last().unwrap(), &[1, 2]);
823 fn test_rchunks_exact_mut_remainder() {
824 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
825 let c = v.rchunks_exact_mut(2);
826 assert_eq!(c.into_remainder(), &[0]);
830 fn test_rchunks_exact_mut_zip() {
831 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
832 let v2: &[i32] = &[6, 7, 8, 9, 10];
834 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
835 let sum = b.iter().sum::<i32>();
840 assert_eq!(v1, [0, 16, 17, 22, 23]);
844 fn test_windows_count() {
845 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
846 let c = v.windows(3);
847 assert_eq!(c.count(), 4);
849 let v2: &[i32] = &[0, 1, 2, 3, 4];
850 let c2 = v2.windows(6);
851 assert_eq!(c2.count(), 0);
853 let v3: &[i32] = &[];
854 let c3 = v3.windows(2);
855 assert_eq!(c3.count(), 0);
859 fn test_windows_nth() {
860 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
861 let mut c = v.windows(2);
862 assert_eq!(c.nth(2).unwrap()[1], 3);
863 assert_eq!(c.next().unwrap()[0], 3);
865 let v2: &[i32] = &[0, 1, 2, 3, 4];
866 let mut c2 = v2.windows(4);
867 assert_eq!(c2.nth(1).unwrap()[1], 2);
868 assert_eq!(c2.next(), None);
872 fn test_windows_nth_back() {
873 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
874 let mut c = v.windows(2);
875 assert_eq!(c.nth_back(2).unwrap()[0], 2);
876 assert_eq!(c.next_back().unwrap()[1], 2);
878 let v2: &[i32] = &[0, 1, 2, 3, 4];
879 let mut c2 = v2.windows(4);
880 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
881 assert_eq!(c2.next_back(), None);
885 fn test_windows_last() {
886 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
887 let c = v.windows(2);
888 assert_eq!(c.last().unwrap()[1], 5);
890 let v2: &[i32] = &[0, 1, 2, 3, 4];
891 let c2 = v2.windows(2);
892 assert_eq!(c2.last().unwrap()[0], 3);
896 fn test_windows_zip() {
897 let v1: &[i32] = &[0, 1, 2, 3, 4];
898 let v2: &[i32] = &[6, 7, 8, 9, 10];
903 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
904 .collect::<Vec<_>>();
906 assert_eq!(res, [14, 18, 22, 26]);
911 fn test_iter_ref_consistency() {
914 fn test<T: Copy + Debug + PartialEq>(x: T) {
915 let v: &[T] = &[x, x, x];
916 let v_ptrs: [*const T; 3] = match v {
917 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
924 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
925 let nth = v.iter().nth(i).unwrap();
926 assert_eq!(nth as *const _, v_ptrs[i]);
928 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
930 // stepping through with nth(0)
932 let mut it = v.iter();
934 let next = it.nth(0).unwrap();
935 assert_eq!(next as *const _, v_ptrs[i]);
937 assert_eq!(it.nth(0), None);
942 let mut it = v.iter();
944 let remaining = len - i;
945 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
947 let next = it.next().unwrap();
948 assert_eq!(next as *const _, v_ptrs[i]);
950 assert_eq!(it.size_hint(), (0, Some(0)));
951 assert_eq!(it.next(), None, "The final call to next() should return None");
956 let mut it = v.iter();
958 let remaining = len - i;
959 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
961 let prev = it.next_back().unwrap();
962 assert_eq!(prev as *const _, v_ptrs[remaining - 1]);
964 assert_eq!(it.size_hint(), (0, Some(0)));
965 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
969 fn test_mut<T: Copy + Debug + PartialEq>(x: T) {
970 let v: &mut [T] = &mut [x, x, x];
971 let v_ptrs: [*mut T; 3] = match v {
972 [ref v1, ref v2, ref v3] => {
973 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _]
981 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
982 let nth = v.iter_mut().nth(i).unwrap();
983 assert_eq!(nth as *mut _, v_ptrs[i]);
985 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
987 // stepping through with nth(0)
989 let mut it = v.iter();
991 let next = it.nth(0).unwrap();
992 assert_eq!(next as *const _, v_ptrs[i]);
994 assert_eq!(it.nth(0), None);
999 let mut it = v.iter_mut();
1001 let remaining = len - i;
1002 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
1004 let next = it.next().unwrap();
1005 assert_eq!(next as *mut _, v_ptrs[i]);
1007 assert_eq!(it.size_hint(), (0, Some(0)));
1008 assert_eq!(it.next(), None, "The final call to next() should return None");
1013 let mut it = v.iter_mut();
1015 let remaining = len - i;
1016 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
1018 let prev = it.next_back().unwrap();
1019 assert_eq!(prev as *mut _, v_ptrs[remaining - 1]);
1021 assert_eq!(it.size_hint(), (0, Some(0)));
1022 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
1026 // Make sure iterators and slice patterns yield consistent addresses for various types,
1030 test([0u32; 0]); // ZST with alignment > 0
1033 test_mut([0u32; 0]); // ZST with alignment > 0
1036 // The current implementation of SliceIndex fails to handle methods
1037 // orthogonally from range types; therefore, it is worth testing
1038 // all of the indexing operations on each input.
1040 // This checks all six indexing methods, given an input range that
1041 // should succeed. (it is NOT suitable for testing invalid inputs)
1042 macro_rules! assert_range_eq {
1043 ($arr:expr, $range:expr, $expected:expr) => {
1045 let mut expected = $expected;
1048 let expected: &[_] = &expected;
1050 assert_eq!(&s[$range], expected, "(in assertion for: index)");
1051 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
1054 s.get_unchecked($range),
1056 "(in assertion for: get_unchecked)",
1061 let s: &mut [_] = &mut arr;
1062 let expected: &mut [_] = &mut expected;
1064 assert_eq!(&mut s[$range], expected, "(in assertion for: index_mut)",);
1067 Some(&mut expected[..]),
1068 "(in assertion for: get_mut)",
1072 s.get_unchecked_mut($range),
1074 "(in assertion for: get_unchecked_mut)",
1081 // Make sure the macro can actually detect bugs,
1082 // because if it can't, then what are we even doing here?
1084 // (Be aware this only demonstrates the ability to detect bugs
1085 // in the FIRST method that panics, as the macro is not designed
1086 // to be used in `should_panic`)
1088 #[should_panic(expected = "out of range")]
1089 fn assert_range_eq_can_fail_by_panic() {
1090 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
1093 // (Be aware this only demonstrates the ability to detect bugs
1094 // in the FIRST method it calls, as the macro is not designed
1095 // to be used in `should_panic`)
1097 #[should_panic(expected = "==")]
1098 fn assert_range_eq_can_fail_by_inequality() {
1099 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
1102 // Test cases for bad index operations.
1104 // This generates `should_panic` test cases for Index/IndexMut
1105 // and `None` test cases for get/get_mut.
1106 macro_rules! panic_cases {
1108 // each test case needs a unique name to namespace the tests
1109 in mod $case_name:ident {
1114 // one or more similar inputs for which data[input] succeeds,
1115 // and the corresponding output as an array. This helps validate
1116 // "critical points" where an input range straddles the boundary
1117 // between valid and invalid.
1118 // (such as the input `len..len`, which is just barely valid)
1120 good: data[$good:expr] == $output:expr;
1123 bad: data[$bad:expr];
1124 message: $expect_msg:expr;
1132 $( assert_range_eq!($data, $good, $output); )*
1136 assert_eq!(v.get($bad), None, "(in None assertion for get)");
1140 let v: &mut [_] = &mut v;
1141 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
1146 #[should_panic(expected = $expect_msg)]
1154 #[should_panic(expected = $expect_msg)]
1155 fn index_mut_fail() {
1157 let v: &mut [_] = &mut v;
1158 let _v = &mut v[$bad];
1166 let v = [0, 1, 2, 3, 4, 5];
1168 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
1169 assert_range_eq!(v, ..2, [0, 1]);
1170 assert_range_eq!(v, ..=1, [0, 1]);
1171 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
1172 assert_range_eq!(v, 1..4, [1, 2, 3]);
1173 assert_range_eq!(v, 1..=3, [1, 2, 3]);
1177 in mod rangefrom_len {
1178 data: [0, 1, 2, 3, 4, 5];
1180 good: data[6..] == [];
1182 message: "out of range";
1185 in mod rangeto_len {
1186 data: [0, 1, 2, 3, 4, 5];
1188 good: data[..6] == [0, 1, 2, 3, 4, 5];
1190 message: "out of range";
1193 in mod rangetoinclusive_len {
1194 data: [0, 1, 2, 3, 4, 5];
1196 good: data[..=5] == [0, 1, 2, 3, 4, 5];
1198 message: "out of range";
1201 in mod range_len_len {
1202 data: [0, 1, 2, 3, 4, 5];
1204 good: data[6..6] == [];
1206 message: "out of range";
1209 in mod rangeinclusive_len_len {
1210 data: [0, 1, 2, 3, 4, 5];
1212 good: data[6..=5] == [];
1214 message: "out of range";
1219 in mod range_neg_width {
1220 data: [0, 1, 2, 3, 4, 5];
1222 good: data[4..4] == [];
1224 message: "but ends at";
1227 in mod rangeinclusive_neg_width {
1228 data: [0, 1, 2, 3, 4, 5];
1230 good: data[4..=3] == [];
1232 message: "but ends at";
1237 in mod rangeinclusive_overflow {
1240 // note: using 0 specifically ensures that the result of overflowing is 0..0,
1241 // so that `get` doesn't simply return None for the wrong reason.
1242 bad: data[0 ..= usize::MAX];
1243 message: "maximum usize";
1246 in mod rangetoinclusive_overflow {
1249 bad: data[..= usize::MAX];
1250 message: "maximum usize";
1256 fn test_find_rfind() {
1257 let v = [0, 1, 2, 3, 4, 5];
1258 let mut iter = v.iter();
1259 let mut i = v.len();
1260 while let Some(&elt) = iter.rfind(|_| true) {
1262 assert_eq!(elt, v[i]);
1265 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
1269 fn test_iter_folds() {
1270 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
1271 assert_eq!(a.iter().fold(0, |acc, &x| 2 * acc + x), 57);
1272 assert_eq!(a.iter().rfold(0, |acc, &x| 2 * acc + x), 129);
1273 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
1274 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
1275 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
1277 // short-circuiting try_fold, through other methods
1278 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
1279 let mut iter = a.iter();
1280 assert_eq!(iter.position(|&x| x == 3), Some(3));
1281 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
1282 assert_eq!(iter.len(), 2);
1286 fn test_rotate_left() {
1287 const N: usize = 600;
1288 let a: &mut [_] = &mut [0; N];
1297 assert_eq!(a[(i + k) % N], i);
1302 fn test_rotate_right() {
1303 const N: usize = 600;
1304 let a: &mut [_] = &mut [0; N];
1312 assert_eq!(a[(i + 42) % N], i);
1317 #[cfg_attr(miri, ignore)] // Miri is too slow
1318 fn brute_force_rotate_test_0() {
1319 // In case of edge cases involving multiple algorithms
1323 let mut v = Vec::with_capacity(len);
1327 v[..].rotate_right(s);
1328 for i in 0..v.len() {
1329 assert_eq!(v[i], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1336 fn brute_force_rotate_test_1() {
1337 // `ptr_rotate` covers so many kinds of pointer usage, that this is just a good test for
1338 // pointers in general. This uses a `[usize; 4]` to hit all algorithms without overwhelming miri
1342 let mut v: Vec<[usize; 4]> = Vec::with_capacity(len);
1344 v.push([i, 0, 0, 0]);
1346 v[..].rotate_right(s);
1347 for i in 0..v.len() {
1348 assert_eq!(v[i][0], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1355 #[cfg(not(target_arch = "wasm32"))]
1356 fn sort_unstable() {
1357 use core::cmp::Ordering::{Equal, Greater, Less};
1358 use core::slice::heapsort;
1359 use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
1361 // Miri is too slow (but still need to `chain` to make the types match)
1362 let lens = if cfg!(miri) { (2..20).chain(0..0) } else { (2..25).chain(500..510) };
1363 let rounds = if cfg!(miri) { 1 } else { 100 };
1365 let mut v = [0; 600];
1366 let mut tmp = [0; 600];
1367 let mut rng = StdRng::from_entropy();
1370 let v = &mut v[0..len];
1371 let tmp = &mut tmp[0..len];
1373 for &modulus in &[5, 10, 100, 1000] {
1374 for _ in 0..rounds {
1376 v[i] = rng.gen::<i32>() % modulus;
1379 // Sort in default order.
1380 tmp.copy_from_slice(v);
1381 tmp.sort_unstable();
1382 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1384 // Sort in ascending order.
1385 tmp.copy_from_slice(v);
1386 tmp.sort_unstable_by(|a, b| a.cmp(b));
1387 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1389 // Sort in descending order.
1390 tmp.copy_from_slice(v);
1391 tmp.sort_unstable_by(|a, b| b.cmp(a));
1392 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1394 // Test heapsort using `<` operator.
1395 tmp.copy_from_slice(v);
1396 heapsort(tmp, |a, b| a < b);
1397 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1399 // Test heapsort using `>` operator.
1400 tmp.copy_from_slice(v);
1401 heapsort(tmp, |a, b| a > b);
1402 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1407 // Sort using a completely random comparison function.
1408 // This will reorder the elements *somehow*, but won't panic.
1409 for i in 0..v.len() {
1412 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1414 for i in 0..v.len() {
1415 assert_eq!(v[i], i as i32);
1418 // Should not panic.
1419 [0i32; 0].sort_unstable();
1420 [(); 10].sort_unstable();
1421 [(); 100].sort_unstable();
1423 let mut v = [0xDEADBEEFu64];
1425 assert!(v == [0xDEADBEEF]);
1429 #[cfg(not(target_arch = "wasm32"))]
1430 #[cfg_attr(miri, ignore)] // Miri is too slow
1431 fn partition_at_index() {
1432 use core::cmp::Ordering::{Equal, Greater, Less};
1433 use rand::rngs::StdRng;
1434 use rand::seq::SliceRandom;
1435 use rand::{Rng, SeedableRng};
1437 let mut rng = StdRng::from_entropy();
1439 for len in (2..21).chain(500..501) {
1440 let mut orig = vec![0; len];
1442 for &modulus in &[5, 10, 1000] {
1445 orig[i] = rng.gen::<i32>() % modulus;
1449 let mut v = orig.clone();
1454 // Sort in default order.
1455 for pivot in 0..len {
1456 let mut v = orig.clone();
1457 v.partition_at_index(pivot);
1459 assert_eq!(v_sorted[pivot], v[pivot]);
1461 for j in pivot..len {
1462 assert!(v[i] <= v[j]);
1467 // Sort in ascending order.
1468 for pivot in 0..len {
1469 let mut v = orig.clone();
1470 let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
1472 assert_eq!(left.len() + right.len(), len - 1);
1475 assert!(l <= pivot);
1476 for r in right.iter_mut() {
1478 assert!(pivot <= r);
1483 // Sort in descending order.
1484 let sort_descending_comparator = |a: &i32, b: &i32| b.cmp(a);
1485 let v_sorted_descending = {
1486 let mut v = orig.clone();
1487 v.sort_by(sort_descending_comparator);
1491 for pivot in 0..len {
1492 let mut v = orig.clone();
1493 v.partition_at_index_by(pivot, sort_descending_comparator);
1495 assert_eq!(v_sorted_descending[pivot], v[pivot]);
1497 for j in pivot..len {
1498 assert!(v[j] <= v[i]);
1506 // Sort at index using a completely random comparison function.
1507 // This will reorder the elements *somehow*, but won't panic.
1508 let mut v = [0; 500];
1509 for i in 0..v.len() {
1513 for pivot in 0..v.len() {
1514 v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1516 for i in 0..v.len() {
1517 assert_eq!(v[i], i as i32);
1521 // Should not panic.
1522 [(); 10].partition_at_index(0);
1523 [(); 10].partition_at_index(5);
1524 [(); 10].partition_at_index(9);
1525 [(); 100].partition_at_index(0);
1526 [(); 100].partition_at_index(50);
1527 [(); 100].partition_at_index(99);
1529 let mut v = [0xDEADBEEFu64];
1530 v.partition_at_index(0);
1531 assert!(v == [0xDEADBEEF]);
1535 #[should_panic(expected = "index 0 greater than length of slice")]
1536 fn partition_at_index_zero_length() {
1537 [0i32; 0].partition_at_index(0);
1541 #[should_panic(expected = "index 20 greater than length of slice")]
1542 fn partition_at_index_past_length() {
1543 [0i32; 10].partition_at_index(20);
1547 use core::slice::memchr::{memchr, memrchr};
1549 // test fallback implementations on all platforms
1552 assert_eq!(Some(0), memchr(b'a', b"a"));
1556 fn matches_begin() {
1557 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1562 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1567 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1571 fn matches_past_nul() {
1572 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1576 fn no_match_empty() {
1577 assert_eq!(None, memchr(b'a', b""));
1582 assert_eq!(None, memchr(b'a', b"xyz"));
1586 fn matches_one_reversed() {
1587 assert_eq!(Some(0), memrchr(b'a', b"a"));
1591 fn matches_begin_reversed() {
1592 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1596 fn matches_end_reversed() {
1597 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1601 fn matches_nul_reversed() {
1602 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1606 fn matches_past_nul_reversed() {
1607 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1611 fn no_match_empty_reversed() {
1612 assert_eq!(None, memrchr(b'a', b""));
1616 fn no_match_reversed() {
1617 assert_eq!(None, memrchr(b'a', b"xyz"));
1621 fn each_alignment_reversed() {
1622 let mut data = [1u8; 64];
1626 for start in 0..16 {
1627 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1633 #[cfg_attr(miri, ignore)] // Miri does not compute a maximal `mid` for `align_offset`
1634 fn test_align_to_simple() {
1635 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1636 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1637 assert_eq!(aligned.len(), 3);
1638 assert!(prefix == [1] || suffix == [7]);
1639 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1640 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1641 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1642 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1644 aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1645 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1655 fn test_align_to_zst() {
1656 let bytes = [1, 2, 3, 4, 5, 6, 7];
1657 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1658 assert_eq!(aligned.len(), 0);
1659 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1663 #[cfg_attr(miri, ignore)] // Miri does not compute a maximal `mid` for `align_offset`
1664 fn test_align_to_non_trivial() {
1666 struct U64(u64, u64);
1668 struct U64U64U32(u64, u64, u32);
1679 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1680 assert_eq!(aligned.len(), 4);
1681 assert_eq!(prefix.len() + suffix.len(), 2);
1685 fn test_align_to_empty_mid() {
1688 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1689 // overall slice is too short to contain an aligned address.
1690 let bytes = [1, 2, 3, 4, 5, 6, 7];
1692 for offset in 0..4 {
1693 let (_, mid, _) = unsafe { bytes[offset..offset + 1].align_to::<Chunk>() };
1694 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1699 fn test_align_to_mut_aliasing() {
1700 let mut val = [1u8, 2, 3, 4, 5];
1701 // `align_to_mut` used to create `mid` in a way that there was some intermediate
1702 // incorrect aliasing, invalidating the resulting `mid` slice.
1703 let (begin, mid, end) = unsafe { val.align_to_mut::<[u8; 2]>() };
1704 assert!(begin.len() == 0);
1705 assert!(end.len() == 1);
1707 assert_eq!(val, [3, 4, 3, 4, 5])
1711 fn test_slice_partition_dedup_by() {
1712 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1714 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1716 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1717 assert_eq!(duplicates, [5, -1, 2]);
1721 fn test_slice_partition_dedup_empty() {
1722 let mut slice: [i32; 0] = [];
1724 let (dedup, duplicates) = slice.partition_dedup();
1726 assert_eq!(dedup, []);
1727 assert_eq!(duplicates, []);
1731 fn test_slice_partition_dedup_one() {
1732 let mut slice = [12];
1734 let (dedup, duplicates) = slice.partition_dedup();
1736 assert_eq!(dedup, [12]);
1737 assert_eq!(duplicates, []);
1741 fn test_slice_partition_dedup_multiple_ident() {
1742 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1744 let (dedup, duplicates) = slice.partition_dedup();
1746 assert_eq!(dedup, [12, 11]);
1747 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1751 fn test_slice_partition_dedup_partialeq() {
1753 struct Foo(i32, i32);
1755 impl PartialEq for Foo {
1756 fn eq(&self, other: &Foo) -> bool {
1761 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1763 let (dedup, duplicates) = slice.partition_dedup();
1765 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1766 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1770 fn test_copy_within() {
1771 // Start to end, with a RangeTo.
1772 let mut bytes = *b"Hello, World!";
1773 bytes.copy_within(..3, 10);
1774 assert_eq!(&bytes, b"Hello, WorHel");
1776 // End to start, with a RangeFrom.
1777 let mut bytes = *b"Hello, World!";
1778 bytes.copy_within(10.., 0);
1779 assert_eq!(&bytes, b"ld!lo, World!");
1781 // Overlapping, with a RangeInclusive.
1782 let mut bytes = *b"Hello, World!";
1783 bytes.copy_within(0..=11, 1);
1784 assert_eq!(&bytes, b"HHello, World");
1786 // Whole slice, with a RangeFull.
1787 let mut bytes = *b"Hello, World!";
1788 bytes.copy_within(.., 0);
1789 assert_eq!(&bytes, b"Hello, World!");
1791 // Ensure that copying at the end of slice won't cause UB.
1792 let mut bytes = *b"Hello, World!";
1793 bytes.copy_within(13..13, 5);
1794 assert_eq!(&bytes, b"Hello, World!");
1795 bytes.copy_within(5..5, 13);
1796 assert_eq!(&bytes, b"Hello, World!");
1800 #[should_panic(expected = "src is out of bounds")]
1801 fn test_copy_within_panics_src_too_long() {
1802 let mut bytes = *b"Hello, World!";
1803 // The length is only 13, so 14 is out of bounds.
1804 bytes.copy_within(10..14, 0);
1808 #[should_panic(expected = "dest is out of bounds")]
1809 fn test_copy_within_panics_dest_too_long() {
1810 let mut bytes = *b"Hello, World!";
1811 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1812 bytes.copy_within(0..4, 10);
1815 #[should_panic(expected = "src end is before src start")]
1816 fn test_copy_within_panics_src_inverted() {
1817 let mut bytes = *b"Hello, World!";
1818 // 2 is greater than 1, so this range is invalid.
1819 bytes.copy_within(2..1, 0);
1822 #[should_panic(expected = "attempted to index slice up to maximum usize")]
1823 fn test_copy_within_panics_src_out_of_bounds() {
1824 let mut bytes = *b"Hello, World!";
1825 // an inclusive range ending at usize::MAX would make src_end overflow
1826 bytes.copy_within(usize::MAX..=usize::MAX, 0);
1830 fn test_is_sorted() {
1831 let empty: [i32; 0] = [];
1833 assert!([1, 2, 2, 9].is_sorted());
1834 assert!(![1, 3, 2].is_sorted());
1835 assert!([0].is_sorted());
1836 assert!(empty.is_sorted());
1837 assert!(![0.0, 1.0, f32::NAN].is_sorted());
1838 assert!([-2, -1, 0, 3].is_sorted());
1839 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1840 assert!(!["c", "bb", "aaa"].is_sorted());
1841 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));