1 use core::result::Result::{Ok, Err};
5 let b = [1, 2, 3, 5, 5];
6 assert!(b.iter().position(|&v| v == 9) == None);
7 assert!(b.iter().position(|&v| v == 5) == Some(3));
8 assert!(b.iter().position(|&v| v == 3) == Some(2));
9 assert!(b.iter().position(|&v| v == 0) == None);
14 let b = [1, 2, 3, 5, 5];
15 assert!(b.iter().rposition(|&v| v == 9) == None);
16 assert!(b.iter().rposition(|&v| v == 5) == Some(4));
17 assert!(b.iter().rposition(|&v| v == 3) == Some(2));
18 assert!(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) { Ok(1..=3) => true, _ => false });
54 assert!(match b.binary_search(&3) { Ok(1..=3) => true, _ => false });
55 assert_eq!(b.binary_search(&4), Err(4));
56 assert_eq!(b.binary_search(&5), Err(4));
57 assert_eq!(b.binary_search(&6), Err(4));
58 assert_eq!(b.binary_search(&7), Ok(4));
59 assert_eq!(b.binary_search(&8), Err(5));
63 // Test implementation specific behavior when finding equivalent elements.
64 // It is ok to break this test but when you do a crater run is highly advisable.
65 fn test_binary_search_implementation_details() {
66 let b = [1, 1, 2, 2, 3, 3, 3];
67 assert_eq!(b.binary_search(&1), Ok(1));
68 assert_eq!(b.binary_search(&2), Ok(3));
69 assert_eq!(b.binary_search(&3), Ok(6));
70 let b = [1, 1, 1, 1, 1, 3, 3, 3, 3];
71 assert_eq!(b.binary_search(&1), Ok(4));
72 assert_eq!(b.binary_search(&3), Ok(8));
73 let b = [1, 1, 1, 1, 3, 3, 3, 3, 3];
74 assert_eq!(b.binary_search(&1), Ok(3));
75 assert_eq!(b.binary_search(&3), Ok(8));
79 fn test_iterator_nth() {
80 let v: &[_] = &[0, 1, 2, 3, 4];
82 assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
84 assert_eq!(v.iter().nth(v.len()), None);
86 let mut iter = v.iter();
87 assert_eq!(iter.nth(2).unwrap(), &v[2]);
88 assert_eq!(iter.nth(1).unwrap(), &v[4]);
92 fn test_iterator_nth_back() {
93 let v: &[_] = &[0, 1, 2, 3, 4];
95 assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - i - 1]);
97 assert_eq!(v.iter().nth_back(v.len()), None);
99 let mut iter = v.iter();
100 assert_eq!(iter.nth_back(2).unwrap(), &v[2]);
101 assert_eq!(iter.nth_back(1).unwrap(), &v[0]);
105 fn test_iterator_last() {
106 let v: &[_] = &[0, 1, 2, 3, 4];
107 assert_eq!(v.iter().last().unwrap(), &4);
108 assert_eq!(v[..1].iter().last().unwrap(), &0);
112 fn test_iterator_count() {
113 let v: &[_] = &[0, 1, 2, 3, 4];
114 assert_eq!(v.iter().count(), 5);
116 let mut iter2 = v.iter();
119 assert_eq!(iter2.count(), 3);
123 fn test_chunks_count() {
124 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
126 assert_eq!(c.count(), 2);
128 let v2: &[i32] = &[0, 1, 2, 3, 4];
129 let c2 = v2.chunks(2);
130 assert_eq!(c2.count(), 3);
132 let v3: &[i32] = &[];
133 let c3 = v3.chunks(2);
134 assert_eq!(c3.count(), 0);
138 fn test_chunks_nth() {
139 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
140 let mut c = v.chunks(2);
141 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
142 assert_eq!(c.next().unwrap(), &[4, 5]);
144 let v2: &[i32] = &[0, 1, 2, 3, 4];
145 let mut c2 = v2.chunks(3);
146 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
147 assert_eq!(c2.next(), None);
151 fn test_chunks_nth_back() {
152 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
153 let mut c = v.chunks(2);
154 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
155 assert_eq!(c.next().unwrap(), &[0, 1]);
156 assert_eq!(c.next(), None);
158 let v2: &[i32] = &[0, 1, 2, 3, 4];
159 let mut c2 = v2.chunks(3);
160 assert_eq!(c2.nth_back(1).unwrap(), &[0, 1, 2]);
161 assert_eq!(c2.next(), None);
162 assert_eq!(c2.next_back(), None);
164 let v3: &[i32] = &[0, 1, 2, 3, 4];
165 let mut c3 = v3.chunks(10);
166 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
167 assert_eq!(c3.next(), None);
169 let v4: &[i32] = &[0, 1, 2];
170 let mut c4 = v4.chunks(10);
171 assert_eq!(c4.nth_back(1_000_000_000usize), None);
175 fn test_chunks_last() {
176 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
178 assert_eq!(c.last().unwrap()[1], 5);
180 let v2: &[i32] = &[0, 1, 2, 3, 4];
181 let c2 = v2.chunks(2);
182 assert_eq!(c2.last().unwrap()[0], 4);
186 fn test_chunks_zip() {
187 let v1: &[i32] = &[0, 1, 2, 3, 4];
188 let v2: &[i32] = &[6, 7, 8, 9, 10];
190 let res = v1.chunks(2)
192 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
193 .collect::<Vec<_>>();
194 assert_eq!(res, vec![14, 22, 14]);
198 fn test_chunks_mut_count() {
199 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
200 let c = v.chunks_mut(3);
201 assert_eq!(c.count(), 2);
203 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
204 let c2 = v2.chunks_mut(2);
205 assert_eq!(c2.count(), 3);
207 let v3: &mut [i32] = &mut [];
208 let c3 = v3.chunks_mut(2);
209 assert_eq!(c3.count(), 0);
213 fn test_chunks_mut_nth() {
214 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
215 let mut c = v.chunks_mut(2);
216 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
217 assert_eq!(c.next().unwrap(), &[4, 5]);
219 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
220 let mut c2 = v2.chunks_mut(3);
221 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
222 assert_eq!(c2.next(), None);
226 fn test_chunks_mut_last() {
227 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
228 let c = v.chunks_mut(2);
229 assert_eq!(c.last().unwrap(), &[4, 5]);
231 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
232 let c2 = v2.chunks_mut(2);
233 assert_eq!(c2.last().unwrap(), &[4]);
237 fn test_chunks_mut_zip() {
238 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
239 let v2: &[i32] = &[6, 7, 8, 9, 10];
241 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
242 let sum = b.iter().sum::<i32>();
247 assert_eq!(v1, [13, 14, 19, 20, 14]);
251 fn test_chunks_exact_count() {
252 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
253 let c = v.chunks_exact(3);
254 assert_eq!(c.count(), 2);
256 let v2: &[i32] = &[0, 1, 2, 3, 4];
257 let c2 = v2.chunks_exact(2);
258 assert_eq!(c2.count(), 2);
260 let v3: &[i32] = &[];
261 let c3 = v3.chunks_exact(2);
262 assert_eq!(c3.count(), 0);
266 fn test_chunks_exact_nth() {
267 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
268 let mut c = v.chunks_exact(2);
269 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
270 assert_eq!(c.next().unwrap(), &[4, 5]);
272 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
273 let mut c2 = v2.chunks_exact(3);
274 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
275 assert_eq!(c2.next(), None);
279 fn test_chunks_exact_nth_back() {
280 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
281 let mut c = v.chunks_exact(2);
282 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
283 assert_eq!(c.next().unwrap(), &[0, 1]);
284 assert_eq!(c.next(), None);
286 let v2: &[i32] = &[0, 1, 2, 3, 4];
287 let mut c2 = v2.chunks_exact(3);
288 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
289 assert_eq!(c2.next(), None);
290 assert_eq!(c2.next_back(), None);
292 let v3: &[i32] = &[0, 1, 2, 3, 4];
293 let mut c3 = v3.chunks_exact(10);
294 assert_eq!(c3.nth_back(0), None);
298 fn test_chunks_exact_last() {
299 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
300 let c = v.chunks_exact(2);
301 assert_eq!(c.last().unwrap(), &[4, 5]);
303 let v2: &[i32] = &[0, 1, 2, 3, 4];
304 let c2 = v2.chunks_exact(2);
305 assert_eq!(c2.last().unwrap(), &[2, 3]);
309 fn test_chunks_exact_remainder() {
310 let v: &[i32] = &[0, 1, 2, 3, 4];
311 let c = v.chunks_exact(2);
312 assert_eq!(c.remainder(), &[4]);
316 fn test_chunks_exact_zip() {
317 let v1: &[i32] = &[0, 1, 2, 3, 4];
318 let v2: &[i32] = &[6, 7, 8, 9, 10];
320 let res = v1.chunks_exact(2)
321 .zip(v2.chunks_exact(2))
322 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
323 .collect::<Vec<_>>();
324 assert_eq!(res, vec![14, 22]);
328 fn test_chunks_exact_mut_count() {
329 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
330 let c = v.chunks_exact_mut(3);
331 assert_eq!(c.count(), 2);
333 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
334 let c2 = v2.chunks_exact_mut(2);
335 assert_eq!(c2.count(), 2);
337 let v3: &mut [i32] = &mut [];
338 let c3 = v3.chunks_exact_mut(2);
339 assert_eq!(c3.count(), 0);
343 fn test_chunks_exact_mut_nth() {
344 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
345 let mut c = v.chunks_exact_mut(2);
346 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
347 assert_eq!(c.next().unwrap(), &[4, 5]);
349 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
350 let mut c2 = v2.chunks_exact_mut(3);
351 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
352 assert_eq!(c2.next(), None);
356 fn test_chunks_exact_mut_last() {
357 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
358 let c = v.chunks_exact_mut(2);
359 assert_eq!(c.last().unwrap(), &[4, 5]);
361 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
362 let c2 = v2.chunks_exact_mut(2);
363 assert_eq!(c2.last().unwrap(), &[2, 3]);
367 fn test_chunks_exact_mut_remainder() {
368 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
369 let c = v.chunks_exact_mut(2);
370 assert_eq!(c.into_remainder(), &[4]);
374 fn test_chunks_exact_mut_zip() {
375 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
376 let v2: &[i32] = &[6, 7, 8, 9, 10];
378 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
379 let sum = b.iter().sum::<i32>();
384 assert_eq!(v1, [13, 14, 19, 20, 4]);
388 fn test_rchunks_count() {
389 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
390 let c = v.rchunks(3);
391 assert_eq!(c.count(), 2);
393 let v2: &[i32] = &[0, 1, 2, 3, 4];
394 let c2 = v2.rchunks(2);
395 assert_eq!(c2.count(), 3);
397 let v3: &[i32] = &[];
398 let c3 = v3.rchunks(2);
399 assert_eq!(c3.count(), 0);
403 fn test_rchunks_nth() {
404 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
405 let mut c = v.rchunks(2);
406 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
407 assert_eq!(c.next().unwrap(), &[0, 1]);
409 let v2: &[i32] = &[0, 1, 2, 3, 4];
410 let mut c2 = v2.rchunks(3);
411 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
412 assert_eq!(c2.next(), None);
416 fn test_rchunks_nth_back() {
417 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
418 let mut c = v.rchunks(2);
419 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
420 assert_eq!(c.next_back().unwrap(), &[4, 5]);
422 let v2: &[i32] = &[0, 1, 2, 3, 4];
423 let mut c2 = v2.rchunks(3);
424 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
425 assert_eq!(c2.next_back(), None);
429 fn test_rchunks_last() {
430 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
431 let c = v.rchunks(2);
432 assert_eq!(c.last().unwrap()[1], 1);
434 let v2: &[i32] = &[0, 1, 2, 3, 4];
435 let c2 = v2.rchunks(2);
436 assert_eq!(c2.last().unwrap()[0], 0);
440 fn test_rchunks_zip() {
441 let v1: &[i32] = &[0, 1, 2, 3, 4];
442 let v2: &[i32] = &[6, 7, 8, 9, 10];
444 let res = v1.rchunks(2)
446 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
447 .collect::<Vec<_>>();
448 assert_eq!(res, vec![26, 18, 6]);
452 fn test_rchunks_mut_count() {
453 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
454 let c = v.rchunks_mut(3);
455 assert_eq!(c.count(), 2);
457 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
458 let c2 = v2.rchunks_mut(2);
459 assert_eq!(c2.count(), 3);
461 let v3: &mut [i32] = &mut [];
462 let c3 = v3.rchunks_mut(2);
463 assert_eq!(c3.count(), 0);
467 fn test_rchunks_mut_nth() {
468 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
469 let mut c = v.rchunks_mut(2);
470 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
471 assert_eq!(c.next().unwrap(), &[0, 1]);
473 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
474 let mut c2 = v2.rchunks_mut(3);
475 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
476 assert_eq!(c2.next(), None);
480 fn test_rchunks_mut_nth_back() {
481 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
482 let mut c = v.rchunks_mut(2);
483 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
484 assert_eq!(c.next_back().unwrap(), &[4, 5]);
486 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
487 let mut c2 = v2.rchunks_mut(3);
488 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
489 assert_eq!(c2.next_back(), None);
493 fn test_rchunks_mut_last() {
494 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
495 let c = v.rchunks_mut(2);
496 assert_eq!(c.last().unwrap(), &[0, 1]);
498 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
499 let c2 = v2.rchunks_mut(2);
500 assert_eq!(c2.last().unwrap(), &[0]);
504 fn test_rchunks_mut_zip() {
505 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
506 let v2: &[i32] = &[6, 7, 8, 9, 10];
508 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
509 let sum = b.iter().sum::<i32>();
514 assert_eq!(v1, [6, 16, 17, 22, 23]);
518 fn test_rchunks_exact_count() {
519 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
520 let c = v.rchunks_exact(3);
521 assert_eq!(c.count(), 2);
523 let v2: &[i32] = &[0, 1, 2, 3, 4];
524 let c2 = v2.rchunks_exact(2);
525 assert_eq!(c2.count(), 2);
527 let v3: &[i32] = &[];
528 let c3 = v3.rchunks_exact(2);
529 assert_eq!(c3.count(), 0);
533 fn test_rchunks_exact_nth() {
534 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
535 let mut c = v.rchunks_exact(2);
536 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
537 assert_eq!(c.next().unwrap(), &[0, 1]);
539 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
540 let mut c2 = v2.rchunks_exact(3);
541 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
542 assert_eq!(c2.next(), None);
546 fn test_rchunks_exact_nth_back() {
547 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
548 let mut c = v.rchunks_exact(2);
549 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
550 assert_eq!(c.next_back().unwrap(), &[4, 5]);
552 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
553 let mut c2 = v2.rchunks_exact(3);
554 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
555 assert_eq!(c2.next(), None);
559 fn test_rchunks_exact_last() {
560 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
561 let c = v.rchunks_exact(2);
562 assert_eq!(c.last().unwrap(), &[0, 1]);
564 let v2: &[i32] = &[0, 1, 2, 3, 4];
565 let c2 = v2.rchunks_exact(2);
566 assert_eq!(c2.last().unwrap(), &[1, 2]);
570 fn test_rchunks_exact_remainder() {
571 let v: &[i32] = &[0, 1, 2, 3, 4];
572 let c = v.rchunks_exact(2);
573 assert_eq!(c.remainder(), &[0]);
577 fn test_rchunks_exact_zip() {
578 let v1: &[i32] = &[0, 1, 2, 3, 4];
579 let v2: &[i32] = &[6, 7, 8, 9, 10];
581 let res = v1.rchunks_exact(2)
582 .zip(v2.rchunks_exact(2))
583 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
584 .collect::<Vec<_>>();
585 assert_eq!(res, vec![26, 18]);
589 fn test_rchunks_exact_mut_count() {
590 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
591 let c = v.rchunks_exact_mut(3);
592 assert_eq!(c.count(), 2);
594 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
595 let c2 = v2.rchunks_exact_mut(2);
596 assert_eq!(c2.count(), 2);
598 let v3: &mut [i32] = &mut [];
599 let c3 = v3.rchunks_exact_mut(2);
600 assert_eq!(c3.count(), 0);
604 fn test_rchunks_exact_mut_nth() {
605 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
606 let mut c = v.rchunks_exact_mut(2);
607 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
608 assert_eq!(c.next().unwrap(), &[0, 1]);
610 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
611 let mut c2 = v2.rchunks_exact_mut(3);
612 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
613 assert_eq!(c2.next(), None);
617 fn test_rchunks_exact_mut_nth_back() {
618 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
619 let mut c = v.rchunks_exact_mut(2);
620 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
621 assert_eq!(c.next_back().unwrap(), &[4, 5]);
623 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
624 let mut c2 = v2.rchunks_exact_mut(3);
625 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
626 assert_eq!(c2.next(), None);
630 fn test_rchunks_exact_mut_last() {
631 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
632 let c = v.rchunks_exact_mut(2);
633 assert_eq!(c.last().unwrap(), &[0, 1]);
635 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
636 let c2 = v2.rchunks_exact_mut(2);
637 assert_eq!(c2.last().unwrap(), &[1, 2]);
641 fn test_rchunks_exact_mut_remainder() {
642 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
643 let c = v.rchunks_exact_mut(2);
644 assert_eq!(c.into_remainder(), &[0]);
648 fn test_rchunks_exact_mut_zip() {
649 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
650 let v2: &[i32] = &[6, 7, 8, 9, 10];
652 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
653 let sum = b.iter().sum::<i32>();
658 assert_eq!(v1, [0, 16, 17, 22, 23]);
662 fn test_windows_count() {
663 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
664 let c = v.windows(3);
665 assert_eq!(c.count(), 4);
667 let v2: &[i32] = &[0, 1, 2, 3, 4];
668 let c2 = v2.windows(6);
669 assert_eq!(c2.count(), 0);
671 let v3: &[i32] = &[];
672 let c3 = v3.windows(2);
673 assert_eq!(c3.count(), 0);
677 fn test_windows_nth() {
678 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
679 let mut c = v.windows(2);
680 assert_eq!(c.nth(2).unwrap()[1], 3);
681 assert_eq!(c.next().unwrap()[0], 3);
683 let v2: &[i32] = &[0, 1, 2, 3, 4];
684 let mut c2 = v2.windows(4);
685 assert_eq!(c2.nth(1).unwrap()[1], 2);
686 assert_eq!(c2.next(), None);
690 fn test_windows_nth_back() {
691 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
692 let mut c = v.windows(2);
693 assert_eq!(c.nth_back(2).unwrap()[0], 2);
694 assert_eq!(c.next_back().unwrap()[1], 2);
696 let v2: &[i32] = &[0, 1, 2, 3, 4];
697 let mut c2 = v2.windows(4);
698 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
699 assert_eq!(c2.next_back(), None);
703 fn test_windows_last() {
704 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
705 let c = v.windows(2);
706 assert_eq!(c.last().unwrap()[1], 5);
708 let v2: &[i32] = &[0, 1, 2, 3, 4];
709 let c2 = v2.windows(2);
710 assert_eq!(c2.last().unwrap()[0], 3);
714 fn test_windows_zip() {
715 let v1: &[i32] = &[0, 1, 2, 3, 4];
716 let v2: &[i32] = &[6, 7, 8, 9, 10];
718 let res = v1.windows(2)
720 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
721 .collect::<Vec<_>>();
723 assert_eq!(res, [14, 18, 22, 26]);
728 fn test_iter_ref_consistency() {
731 fn test<T : Copy + Debug + PartialEq>(x : T) {
732 let v : &[T] = &[x, x, x];
733 let v_ptrs : [*const T; 3] = match v {
734 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
741 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
742 let nth = v.iter().nth(i).unwrap();
743 assert_eq!(nth as *const _, v_ptrs[i]);
745 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
747 // stepping through with nth(0)
749 let mut it = v.iter();
751 let next = it.nth(0).unwrap();
752 assert_eq!(next as *const _, v_ptrs[i]);
754 assert_eq!(it.nth(0), None);
759 let mut it = v.iter();
761 let remaining = len - i;
762 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
764 let next = it.next().unwrap();
765 assert_eq!(next as *const _, v_ptrs[i]);
767 assert_eq!(it.size_hint(), (0, Some(0)));
768 assert_eq!(it.next(), None, "The final call to next() should return None");
773 let mut it = v.iter();
775 let remaining = len - i;
776 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
778 let prev = it.next_back().unwrap();
779 assert_eq!(prev as *const _, v_ptrs[remaining-1]);
781 assert_eq!(it.size_hint(), (0, Some(0)));
782 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
786 fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
787 let v : &mut [T] = &mut [x, x, x];
788 let v_ptrs : [*mut T; 3] = match v {
789 [ref v1, ref v2, ref v3] =>
790 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _],
797 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
798 let nth = v.iter_mut().nth(i).unwrap();
799 assert_eq!(nth as *mut _, v_ptrs[i]);
801 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
803 // stepping through with nth(0)
805 let mut it = v.iter();
807 let next = it.nth(0).unwrap();
808 assert_eq!(next as *const _, v_ptrs[i]);
810 assert_eq!(it.nth(0), None);
815 let mut it = v.iter_mut();
817 let remaining = len - i;
818 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
820 let next = it.next().unwrap();
821 assert_eq!(next as *mut _, v_ptrs[i]);
823 assert_eq!(it.size_hint(), (0, Some(0)));
824 assert_eq!(it.next(), None, "The final call to next() should return None");
829 let mut it = v.iter_mut();
831 let remaining = len - i;
832 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
834 let prev = it.next_back().unwrap();
835 assert_eq!(prev as *mut _, v_ptrs[remaining-1]);
837 assert_eq!(it.size_hint(), (0, Some(0)));
838 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
842 // Make sure iterators and slice patterns yield consistent addresses for various types,
846 test([0u32; 0]); // ZST with alignment > 0
849 test_mut([0u32; 0]); // ZST with alignment > 0
852 // The current implementation of SliceIndex fails to handle methods
853 // orthogonally from range types; therefore, it is worth testing
854 // all of the indexing operations on each input.
856 // This checks all six indexing methods, given an input range that
857 // should succeed. (it is NOT suitable for testing invalid inputs)
858 macro_rules! assert_range_eq {
859 ($arr:expr, $range:expr, $expected:expr)
862 let mut expected = $expected;
865 let expected: &[_] = &expected;
867 assert_eq!(&s[$range], expected, "(in assertion for: index)");
868 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
871 s.get_unchecked($range), expected,
872 "(in assertion for: get_unchecked)",
877 let s: &mut [_] = &mut arr;
878 let expected: &mut [_] = &mut expected;
881 &mut s[$range], expected,
882 "(in assertion for: index_mut)",
885 s.get_mut($range), Some(&mut expected[..]),
886 "(in assertion for: get_mut)",
890 s.get_unchecked_mut($range), expected,
891 "(in assertion for: get_unchecked_mut)",
898 // Make sure the macro can actually detect bugs,
899 // because if it can't, then what are we even doing here?
901 // (Be aware this only demonstrates the ability to detect bugs
902 // in the FIRST method that panics, as the macro is not designed
903 // to be used in `should_panic`)
905 #[should_panic(expected = "out of range")]
906 fn assert_range_eq_can_fail_by_panic() {
907 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
910 // (Be aware this only demonstrates the ability to detect bugs
911 // in the FIRST method it calls, as the macro is not designed
912 // to be used in `should_panic`)
914 #[should_panic(expected = "==")]
915 fn assert_range_eq_can_fail_by_inequality() {
916 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
919 // Test cases for bad index operations.
921 // This generates `should_panic` test cases for Index/IndexMut
922 // and `None` test cases for get/get_mut.
923 macro_rules! panic_cases {
925 // each test case needs a unique name to namespace the tests
926 in mod $case_name:ident {
931 // one or more similar inputs for which data[input] succeeds,
932 // and the corresponding output as an array. This helps validate
933 // "critical points" where an input range straddles the boundary
934 // between valid and invalid.
935 // (such as the input `len..len`, which is just barely valid)
937 good: data[$good:expr] == $output:expr;
940 bad: data[$bad:expr];
941 message: $expect_msg:expr;
949 $( assert_range_eq!($data, $good, $output); )*
953 assert_eq!(v.get($bad), None, "(in None assertion for get)");
957 let v: &mut [_] = &mut v;
958 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
963 #[should_panic(expected = $expect_msg)]
971 #[should_panic(expected = $expect_msg)]
972 fn index_mut_fail() {
974 let v: &mut [_] = &mut v;
975 let _v = &mut v[$bad];
983 let v = [0, 1, 2, 3, 4, 5];
985 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
986 assert_range_eq!(v, ..2, [0, 1]);
987 assert_range_eq!(v, ..=1, [0, 1]);
988 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
989 assert_range_eq!(v, 1..4, [1, 2, 3]);
990 assert_range_eq!(v, 1..=3, [1, 2, 3]);
994 in mod rangefrom_len {
995 data: [0, 1, 2, 3, 4, 5];
997 good: data[6..] == [];
999 message: "but ends at"; // perhaps not ideal
1002 in mod rangeto_len {
1003 data: [0, 1, 2, 3, 4, 5];
1005 good: data[..6] == [0, 1, 2, 3, 4, 5];
1007 message: "out of range";
1010 in mod rangetoinclusive_len {
1011 data: [0, 1, 2, 3, 4, 5];
1013 good: data[..=5] == [0, 1, 2, 3, 4, 5];
1015 message: "out of range";
1018 in mod range_len_len {
1019 data: [0, 1, 2, 3, 4, 5];
1021 good: data[6..6] == [];
1023 message: "out of range";
1026 in mod rangeinclusive_len_len {
1027 data: [0, 1, 2, 3, 4, 5];
1029 good: data[6..=5] == [];
1031 message: "out of range";
1036 in mod range_neg_width {
1037 data: [0, 1, 2, 3, 4, 5];
1039 good: data[4..4] == [];
1041 message: "but ends at";
1044 in mod rangeinclusive_neg_width {
1045 data: [0, 1, 2, 3, 4, 5];
1047 good: data[4..=3] == [];
1049 message: "but ends at";
1054 in mod rangeinclusive_overflow {
1057 // note: using 0 specifically ensures that the result of overflowing is 0..0,
1058 // so that `get` doesn't simply return None for the wrong reason.
1059 bad: data[0 ..= ::std::usize::MAX];
1060 message: "maximum usize";
1063 in mod rangetoinclusive_overflow {
1066 bad: data[..= ::std::usize::MAX];
1067 message: "maximum usize";
1073 fn test_find_rfind() {
1074 let v = [0, 1, 2, 3, 4, 5];
1075 let mut iter = v.iter();
1076 let mut i = v.len();
1077 while let Some(&elt) = iter.rfind(|_| true) {
1079 assert_eq!(elt, v[i]);
1082 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
1086 fn test_iter_folds() {
1087 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
1088 assert_eq!(a.iter().fold(0, |acc, &x| 2*acc + x), 57);
1089 assert_eq!(a.iter().rfold(0, |acc, &x| 2*acc + x), 129);
1090 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
1091 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
1092 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
1094 // short-circuiting try_fold, through other methods
1095 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
1096 let mut iter = a.iter();
1097 assert_eq!(iter.position(|&x| x == 3), Some(3));
1098 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
1099 assert_eq!(iter.len(), 2);
1103 fn test_rotate_left() {
1104 const N: usize = 600;
1105 let a: &mut [_] = &mut [0; N];
1114 assert_eq!(a[(i + k) % N], i);
1119 fn test_rotate_right() {
1120 const N: usize = 600;
1121 let a: &mut [_] = &mut [0; N];
1129 assert_eq!(a[(i + 42) % N], i);
1134 #[cfg(not(target_arch = "wasm32"))]
1135 fn sort_unstable() {
1136 use core::cmp::Ordering::{Equal, Greater, Less};
1137 use core::slice::heapsort;
1138 use rand::{FromEntropy, Rng, rngs::SmallRng, seq::SliceRandom};
1140 #[cfg(not(miri))] // Miri is too slow
1141 let large_range = 500..510;
1142 #[cfg(not(miri))] // Miri is too slow
1146 let large_range = 0..0; // empty range
1150 let mut v = [0; 600];
1151 let mut tmp = [0; 600];
1152 let mut rng = SmallRng::from_entropy();
1154 for len in (2..25).chain(large_range) {
1155 let v = &mut v[0..len];
1156 let tmp = &mut tmp[0..len];
1158 for &modulus in &[5, 10, 100, 1000] {
1159 for _ in 0..rounds {
1161 v[i] = rng.gen::<i32>() % modulus;
1164 // Sort in default order.
1165 tmp.copy_from_slice(v);
1166 tmp.sort_unstable();
1167 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1169 // Sort in ascending order.
1170 tmp.copy_from_slice(v);
1171 tmp.sort_unstable_by(|a, b| a.cmp(b));
1172 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1174 // Sort in descending order.
1175 tmp.copy_from_slice(v);
1176 tmp.sort_unstable_by(|a, b| b.cmp(a));
1177 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1179 // Test heapsort using `<` operator.
1180 tmp.copy_from_slice(v);
1181 heapsort(tmp, |a, b| a < b);
1182 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1184 // Test heapsort using `>` operator.
1185 tmp.copy_from_slice(v);
1186 heapsort(tmp, |a, b| a > b);
1187 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1192 // Sort using a completely random comparison function.
1193 // This will reorder the elements *somehow*, but won't panic.
1194 for i in 0..v.len() {
1197 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1199 for i in 0..v.len() {
1200 assert_eq!(v[i], i as i32);
1203 // Should not panic.
1204 [0i32; 0].sort_unstable();
1205 [(); 10].sort_unstable();
1206 [(); 100].sort_unstable();
1208 let mut v = [0xDEADBEEFu64];
1210 assert!(v == [0xDEADBEEF]);
1214 #[cfg(not(target_arch = "wasm32"))]
1215 #[cfg(not(miri))] // Miri is too slow
1216 fn partition_at_index() {
1217 use core::cmp::Ordering::{Equal, Greater, Less};
1218 use rand::rngs::SmallRng;
1219 use rand::seq::SliceRandom;
1220 use rand::{FromEntropy, Rng};
1222 let mut rng = SmallRng::from_entropy();
1224 for len in (2..21).chain(500..501) {
1225 let mut orig = vec![0; len];
1227 for &modulus in &[5, 10, 1000] {
1230 orig[i] = rng.gen::<i32>() % modulus;
1234 let mut v = orig.clone();
1239 // Sort in default order.
1240 for pivot in 0..len {
1241 let mut v = orig.clone();
1242 v.partition_at_index(pivot);
1244 assert_eq!(v_sorted[pivot], v[pivot]);
1246 for j in pivot..len {
1247 assert!(v[i] <= v[j]);
1252 // Sort in ascending order.
1253 for pivot in 0..len {
1254 let mut v = orig.clone();
1255 let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
1257 assert_eq!(left.len() + right.len(), len - 1);
1260 assert!(l <= pivot);
1261 for r in right.iter_mut() {
1263 assert!(pivot <= r);
1268 // Sort in descending order.
1269 let sort_descending_comparator = |a: &i32, b: &i32| b.cmp(a);
1270 let v_sorted_descending = {
1271 let mut v = orig.clone();
1272 v.sort_by(sort_descending_comparator);
1276 for pivot in 0..len {
1277 let mut v = orig.clone();
1278 v.partition_at_index_by(pivot, sort_descending_comparator);
1280 assert_eq!(v_sorted_descending[pivot], v[pivot]);
1282 for j in pivot..len {
1283 assert!(v[j] <= v[i]);
1291 // Sort at index using a completely random comparison function.
1292 // This will reorder the elements *somehow*, but won't panic.
1293 let mut v = [0; 500];
1294 for i in 0..v.len() {
1298 for pivot in 0..v.len() {
1299 v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1301 for i in 0..v.len() {
1302 assert_eq!(v[i], i as i32);
1306 // Should not panic.
1307 [(); 10].partition_at_index(0);
1308 [(); 10].partition_at_index(5);
1309 [(); 10].partition_at_index(9);
1310 [(); 100].partition_at_index(0);
1311 [(); 100].partition_at_index(50);
1312 [(); 100].partition_at_index(99);
1314 let mut v = [0xDEADBEEFu64];
1315 v.partition_at_index(0);
1316 assert!(v == [0xDEADBEEF]);
1320 #[should_panic(expected = "index 0 greater than length of slice")]
1321 fn partition_at_index_zero_length() {
1322 [0i32; 0].partition_at_index(0);
1326 #[should_panic(expected = "index 20 greater than length of slice")]
1327 fn partition_at_index_past_length() {
1328 [0i32; 10].partition_at_index(20);
1332 use core::slice::memchr::{memchr, memrchr};
1334 // test fallback implementations on all platforms
1337 assert_eq!(Some(0), memchr(b'a', b"a"));
1341 fn matches_begin() {
1342 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1347 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1352 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1356 fn matches_past_nul() {
1357 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1361 fn no_match_empty() {
1362 assert_eq!(None, memchr(b'a', b""));
1367 assert_eq!(None, memchr(b'a', b"xyz"));
1371 fn matches_one_reversed() {
1372 assert_eq!(Some(0), memrchr(b'a', b"a"));
1376 fn matches_begin_reversed() {
1377 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1381 fn matches_end_reversed() {
1382 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1386 fn matches_nul_reversed() {
1387 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1391 fn matches_past_nul_reversed() {
1392 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1396 fn no_match_empty_reversed() {
1397 assert_eq!(None, memrchr(b'a', b""));
1401 fn no_match_reversed() {
1402 assert_eq!(None, memrchr(b'a', b"xyz"));
1406 fn each_alignment_reversed() {
1407 let mut data = [1u8; 64];
1411 for start in 0..16 {
1412 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1418 #[cfg(not(miri))] // Miri does not compute a maximal `mid` for `align_offset`
1419 fn test_align_to_simple() {
1420 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1421 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1422 assert_eq!(aligned.len(), 3);
1423 assert!(prefix == [1] || suffix == [7]);
1424 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1425 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1426 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1427 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1428 assert!(aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1429 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1430 aligned, expect1, expect2, expect3, expect4);
1434 fn test_align_to_zst() {
1435 let bytes = [1, 2, 3, 4, 5, 6, 7];
1436 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1437 assert_eq!(aligned.len(), 0);
1438 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1442 #[cfg(not(miri))] // Miri does not compute a maximal `mid` for `align_offset`
1443 fn test_align_to_non_trivial() {
1444 #[repr(align(8))] struct U64(u64, u64);
1445 #[repr(align(8))] struct U64U64U32(u64, u64, u32);
1446 let data = [U64(1, 2), U64(3, 4), U64(5, 6), U64(7, 8), U64(9, 10), U64(11, 12), U64(13, 14),
1448 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1449 assert_eq!(aligned.len(), 4);
1450 assert_eq!(prefix.len() + suffix.len(), 2);
1454 fn test_align_to_empty_mid() {
1457 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1458 // overall slice is too short to contain an aligned address.
1459 let bytes = [1, 2, 3, 4, 5, 6, 7];
1461 for offset in 0..4 {
1462 let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::<Chunk>() };
1463 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1468 fn test_slice_partition_dedup_by() {
1469 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1471 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1473 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1474 assert_eq!(duplicates, [5, -1, 2]);
1478 fn test_slice_partition_dedup_empty() {
1479 let mut slice: [i32; 0] = [];
1481 let (dedup, duplicates) = slice.partition_dedup();
1483 assert_eq!(dedup, []);
1484 assert_eq!(duplicates, []);
1488 fn test_slice_partition_dedup_one() {
1489 let mut slice = [12];
1491 let (dedup, duplicates) = slice.partition_dedup();
1493 assert_eq!(dedup, [12]);
1494 assert_eq!(duplicates, []);
1498 fn test_slice_partition_dedup_multiple_ident() {
1499 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1501 let (dedup, duplicates) = slice.partition_dedup();
1503 assert_eq!(dedup, [12, 11]);
1504 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1508 fn test_slice_partition_dedup_partialeq() {
1510 struct Foo(i32, i32);
1512 impl PartialEq for Foo {
1513 fn eq(&self, other: &Foo) -> bool {
1518 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1520 let (dedup, duplicates) = slice.partition_dedup();
1522 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1523 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1527 fn test_copy_within() {
1528 // Start to end, with a RangeTo.
1529 let mut bytes = *b"Hello, World!";
1530 bytes.copy_within(..3, 10);
1531 assert_eq!(&bytes, b"Hello, WorHel");
1533 // End to start, with a RangeFrom.
1534 let mut bytes = *b"Hello, World!";
1535 bytes.copy_within(10.., 0);
1536 assert_eq!(&bytes, b"ld!lo, World!");
1538 // Overlapping, with a RangeInclusive.
1539 let mut bytes = *b"Hello, World!";
1540 bytes.copy_within(0..=11, 1);
1541 assert_eq!(&bytes, b"HHello, World");
1543 // Whole slice, with a RangeFull.
1544 let mut bytes = *b"Hello, World!";
1545 bytes.copy_within(.., 0);
1546 assert_eq!(&bytes, b"Hello, World!");
1548 // Ensure that copying at the end of slice won't cause UB.
1549 let mut bytes = *b"Hello, World!";
1550 bytes.copy_within(13..13, 5);
1551 assert_eq!(&bytes, b"Hello, World!");
1552 bytes.copy_within(5..5, 13);
1553 assert_eq!(&bytes, b"Hello, World!");
1557 #[should_panic(expected = "src is out of bounds")]
1558 fn test_copy_within_panics_src_too_long() {
1559 let mut bytes = *b"Hello, World!";
1560 // The length is only 13, so 14 is out of bounds.
1561 bytes.copy_within(10..14, 0);
1565 #[should_panic(expected = "dest is out of bounds")]
1566 fn test_copy_within_panics_dest_too_long() {
1567 let mut bytes = *b"Hello, World!";
1568 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1569 bytes.copy_within(0..4, 10);
1572 #[should_panic(expected = "src end is before src start")]
1573 fn test_copy_within_panics_src_inverted() {
1574 let mut bytes = *b"Hello, World!";
1575 // 2 is greater than 1, so this range is invalid.
1576 bytes.copy_within(2..1, 0);
1579 #[should_panic(expected = "attempted to index slice up to maximum usize")]
1580 fn test_copy_within_panics_src_out_of_bounds() {
1581 let mut bytes = *b"Hello, World!";
1582 // an inclusive range ending at usize::max_value() would make src_end overflow
1583 bytes.copy_within(usize::max_value()..=usize::max_value(), 0);
1587 fn test_is_sorted() {
1588 let empty: [i32; 0] = [];
1590 assert!([1, 2, 2, 9].is_sorted());
1591 assert!(![1, 3, 2].is_sorted());
1592 assert!([0].is_sorted());
1593 assert!(empty.is_sorted());
1594 assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
1595 assert!([-2, -1, 0, 3].is_sorted());
1596 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1597 assert!(!["c", "bb", "aaa"].is_sorted());
1598 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));