1 use core::result::Result::{Ok, Err};
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) { 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_nth_back() {
227 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
228 let mut c = v.chunks_mut(2);
229 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
230 assert_eq!(c.next().unwrap(), &[0, 1]);
232 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
233 let mut c1 = v1.chunks_mut(3);
234 assert_eq!(c1.nth_back(1).unwrap(), &[0, 1, 2]);
235 assert_eq!(c1.next(), None);
237 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
238 let mut c3 = v3.chunks_mut(10);
239 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
240 assert_eq!(c3.next(), None);
242 let v4: &mut [i32] = &mut [0, 1, 2];
243 let mut c4 = v4.chunks_mut(10);
244 assert_eq!(c4.nth_back(1_000_000_000usize), None);
248 fn test_chunks_mut_last() {
249 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
250 let c = v.chunks_mut(2);
251 assert_eq!(c.last().unwrap(), &[4, 5]);
253 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
254 let c2 = v2.chunks_mut(2);
255 assert_eq!(c2.last().unwrap(), &[4]);
259 fn test_chunks_mut_zip() {
260 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
261 let v2: &[i32] = &[6, 7, 8, 9, 10];
263 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
264 let sum = b.iter().sum::<i32>();
269 assert_eq!(v1, [13, 14, 19, 20, 14]);
273 fn test_chunks_exact_count() {
274 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
275 let c = v.chunks_exact(3);
276 assert_eq!(c.count(), 2);
278 let v2: &[i32] = &[0, 1, 2, 3, 4];
279 let c2 = v2.chunks_exact(2);
280 assert_eq!(c2.count(), 2);
282 let v3: &[i32] = &[];
283 let c3 = v3.chunks_exact(2);
284 assert_eq!(c3.count(), 0);
288 fn test_chunks_exact_nth() {
289 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
290 let mut c = v.chunks_exact(2);
291 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
292 assert_eq!(c.next().unwrap(), &[4, 5]);
294 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
295 let mut c2 = v2.chunks_exact(3);
296 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
297 assert_eq!(c2.next(), None);
301 fn test_chunks_exact_nth_back() {
302 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
303 let mut c = v.chunks_exact(2);
304 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
305 assert_eq!(c.next().unwrap(), &[0, 1]);
306 assert_eq!(c.next(), None);
308 let v2: &[i32] = &[0, 1, 2, 3, 4];
309 let mut c2 = v2.chunks_exact(3);
310 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
311 assert_eq!(c2.next(), None);
312 assert_eq!(c2.next_back(), None);
314 let v3: &[i32] = &[0, 1, 2, 3, 4];
315 let mut c3 = v3.chunks_exact(10);
316 assert_eq!(c3.nth_back(0), None);
320 fn test_chunks_exact_last() {
321 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
322 let c = v.chunks_exact(2);
323 assert_eq!(c.last().unwrap(), &[4, 5]);
325 let v2: &[i32] = &[0, 1, 2, 3, 4];
326 let c2 = v2.chunks_exact(2);
327 assert_eq!(c2.last().unwrap(), &[2, 3]);
331 fn test_chunks_exact_remainder() {
332 let v: &[i32] = &[0, 1, 2, 3, 4];
333 let c = v.chunks_exact(2);
334 assert_eq!(c.remainder(), &[4]);
338 fn test_chunks_exact_zip() {
339 let v1: &[i32] = &[0, 1, 2, 3, 4];
340 let v2: &[i32] = &[6, 7, 8, 9, 10];
342 let res = v1.chunks_exact(2)
343 .zip(v2.chunks_exact(2))
344 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
345 .collect::<Vec<_>>();
346 assert_eq!(res, vec![14, 22]);
350 fn test_chunks_exact_mut_count() {
351 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
352 let c = v.chunks_exact_mut(3);
353 assert_eq!(c.count(), 2);
355 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
356 let c2 = v2.chunks_exact_mut(2);
357 assert_eq!(c2.count(), 2);
359 let v3: &mut [i32] = &mut [];
360 let c3 = v3.chunks_exact_mut(2);
361 assert_eq!(c3.count(), 0);
365 fn test_chunks_exact_mut_nth() {
366 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
367 let mut c = v.chunks_exact_mut(2);
368 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
369 assert_eq!(c.next().unwrap(), &[4, 5]);
371 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
372 let mut c2 = v2.chunks_exact_mut(3);
373 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
374 assert_eq!(c2.next(), None);
378 fn test_chunks_exact_mut_nth_back() {
379 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
380 let mut c = v.chunks_exact_mut(2);
381 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
382 assert_eq!(c.next().unwrap(), &[0, 1]);
383 assert_eq!(c.next(), None);
385 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
386 let mut c2 = v2.chunks_exact_mut(3);
387 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
388 assert_eq!(c2.next(), None);
389 assert_eq!(c2.next_back(), None);
391 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
392 let mut c3 = v3.chunks_exact_mut(10);
393 assert_eq!(c3.nth_back(0), None);
397 fn test_chunks_exact_mut_last() {
398 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
399 let c = v.chunks_exact_mut(2);
400 assert_eq!(c.last().unwrap(), &[4, 5]);
402 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
403 let c2 = v2.chunks_exact_mut(2);
404 assert_eq!(c2.last().unwrap(), &[2, 3]);
408 fn test_chunks_exact_mut_remainder() {
409 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
410 let c = v.chunks_exact_mut(2);
411 assert_eq!(c.into_remainder(), &[4]);
415 fn test_chunks_exact_mut_zip() {
416 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
417 let v2: &[i32] = &[6, 7, 8, 9, 10];
419 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
420 let sum = b.iter().sum::<i32>();
425 assert_eq!(v1, [13, 14, 19, 20, 4]);
429 fn test_rchunks_count() {
430 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
431 let c = v.rchunks(3);
432 assert_eq!(c.count(), 2);
434 let v2: &[i32] = &[0, 1, 2, 3, 4];
435 let c2 = v2.rchunks(2);
436 assert_eq!(c2.count(), 3);
438 let v3: &[i32] = &[];
439 let c3 = v3.rchunks(2);
440 assert_eq!(c3.count(), 0);
444 fn test_rchunks_nth() {
445 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
446 let mut c = v.rchunks(2);
447 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
448 assert_eq!(c.next().unwrap(), &[0, 1]);
450 let v2: &[i32] = &[0, 1, 2, 3, 4];
451 let mut c2 = v2.rchunks(3);
452 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
453 assert_eq!(c2.next(), None);
457 fn test_rchunks_nth_back() {
458 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
459 let mut c = v.rchunks(2);
460 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
461 assert_eq!(c.next_back().unwrap(), &[4, 5]);
463 let v2: &[i32] = &[0, 1, 2, 3, 4];
464 let mut c2 = v2.rchunks(3);
465 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
466 assert_eq!(c2.next_back(), None);
470 fn test_rchunks_last() {
471 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
472 let c = v.rchunks(2);
473 assert_eq!(c.last().unwrap()[1], 1);
475 let v2: &[i32] = &[0, 1, 2, 3, 4];
476 let c2 = v2.rchunks(2);
477 assert_eq!(c2.last().unwrap()[0], 0);
481 fn test_rchunks_zip() {
482 let v1: &[i32] = &[0, 1, 2, 3, 4];
483 let v2: &[i32] = &[6, 7, 8, 9, 10];
485 let res = v1.rchunks(2)
487 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
488 .collect::<Vec<_>>();
489 assert_eq!(res, vec![26, 18, 6]);
493 fn test_rchunks_mut_count() {
494 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
495 let c = v.rchunks_mut(3);
496 assert_eq!(c.count(), 2);
498 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
499 let c2 = v2.rchunks_mut(2);
500 assert_eq!(c2.count(), 3);
502 let v3: &mut [i32] = &mut [];
503 let c3 = v3.rchunks_mut(2);
504 assert_eq!(c3.count(), 0);
508 fn test_rchunks_mut_nth() {
509 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
510 let mut c = v.rchunks_mut(2);
511 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
512 assert_eq!(c.next().unwrap(), &[0, 1]);
514 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
515 let mut c2 = v2.rchunks_mut(3);
516 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
517 assert_eq!(c2.next(), None);
521 fn test_rchunks_mut_nth_back() {
522 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
523 let mut c = v.rchunks_mut(2);
524 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
525 assert_eq!(c.next_back().unwrap(), &[4, 5]);
527 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
528 let mut c2 = v2.rchunks_mut(3);
529 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
530 assert_eq!(c2.next_back(), None);
534 fn test_rchunks_mut_last() {
535 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
536 let c = v.rchunks_mut(2);
537 assert_eq!(c.last().unwrap(), &[0, 1]);
539 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
540 let c2 = v2.rchunks_mut(2);
541 assert_eq!(c2.last().unwrap(), &[0]);
545 fn test_rchunks_mut_zip() {
546 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
547 let v2: &[i32] = &[6, 7, 8, 9, 10];
549 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
550 let sum = b.iter().sum::<i32>();
555 assert_eq!(v1, [6, 16, 17, 22, 23]);
559 fn test_rchunks_exact_count() {
560 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
561 let c = v.rchunks_exact(3);
562 assert_eq!(c.count(), 2);
564 let v2: &[i32] = &[0, 1, 2, 3, 4];
565 let c2 = v2.rchunks_exact(2);
566 assert_eq!(c2.count(), 2);
568 let v3: &[i32] = &[];
569 let c3 = v3.rchunks_exact(2);
570 assert_eq!(c3.count(), 0);
574 fn test_rchunks_exact_nth() {
575 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
576 let mut c = v.rchunks_exact(2);
577 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
578 assert_eq!(c.next().unwrap(), &[0, 1]);
580 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
581 let mut c2 = v2.rchunks_exact(3);
582 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
583 assert_eq!(c2.next(), None);
587 fn test_rchunks_exact_nth_back() {
588 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
589 let mut c = v.rchunks_exact(2);
590 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
591 assert_eq!(c.next_back().unwrap(), &[4, 5]);
593 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
594 let mut c2 = v2.rchunks_exact(3);
595 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
596 assert_eq!(c2.next(), None);
600 fn test_rchunks_exact_last() {
601 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
602 let c = v.rchunks_exact(2);
603 assert_eq!(c.last().unwrap(), &[0, 1]);
605 let v2: &[i32] = &[0, 1, 2, 3, 4];
606 let c2 = v2.rchunks_exact(2);
607 assert_eq!(c2.last().unwrap(), &[1, 2]);
611 fn test_rchunks_exact_remainder() {
612 let v: &[i32] = &[0, 1, 2, 3, 4];
613 let c = v.rchunks_exact(2);
614 assert_eq!(c.remainder(), &[0]);
618 fn test_rchunks_exact_zip() {
619 let v1: &[i32] = &[0, 1, 2, 3, 4];
620 let v2: &[i32] = &[6, 7, 8, 9, 10];
622 let res = v1.rchunks_exact(2)
623 .zip(v2.rchunks_exact(2))
624 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
625 .collect::<Vec<_>>();
626 assert_eq!(res, vec![26, 18]);
630 fn test_rchunks_exact_mut_count() {
631 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
632 let c = v.rchunks_exact_mut(3);
633 assert_eq!(c.count(), 2);
635 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
636 let c2 = v2.rchunks_exact_mut(2);
637 assert_eq!(c2.count(), 2);
639 let v3: &mut [i32] = &mut [];
640 let c3 = v3.rchunks_exact_mut(2);
641 assert_eq!(c3.count(), 0);
645 fn test_rchunks_exact_mut_nth() {
646 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
647 let mut c = v.rchunks_exact_mut(2);
648 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
649 assert_eq!(c.next().unwrap(), &[0, 1]);
651 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
652 let mut c2 = v2.rchunks_exact_mut(3);
653 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
654 assert_eq!(c2.next(), None);
658 fn test_rchunks_exact_mut_nth_back() {
659 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
660 let mut c = v.rchunks_exact_mut(2);
661 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
662 assert_eq!(c.next_back().unwrap(), &[4, 5]);
664 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
665 let mut c2 = v2.rchunks_exact_mut(3);
666 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
667 assert_eq!(c2.next(), None);
671 fn test_rchunks_exact_mut_last() {
672 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
673 let c = v.rchunks_exact_mut(2);
674 assert_eq!(c.last().unwrap(), &[0, 1]);
676 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
677 let c2 = v2.rchunks_exact_mut(2);
678 assert_eq!(c2.last().unwrap(), &[1, 2]);
682 fn test_rchunks_exact_mut_remainder() {
683 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
684 let c = v.rchunks_exact_mut(2);
685 assert_eq!(c.into_remainder(), &[0]);
689 fn test_rchunks_exact_mut_zip() {
690 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
691 let v2: &[i32] = &[6, 7, 8, 9, 10];
693 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
694 let sum = b.iter().sum::<i32>();
699 assert_eq!(v1, [0, 16, 17, 22, 23]);
703 fn test_windows_count() {
704 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
705 let c = v.windows(3);
706 assert_eq!(c.count(), 4);
708 let v2: &[i32] = &[0, 1, 2, 3, 4];
709 let c2 = v2.windows(6);
710 assert_eq!(c2.count(), 0);
712 let v3: &[i32] = &[];
713 let c3 = v3.windows(2);
714 assert_eq!(c3.count(), 0);
718 fn test_windows_nth() {
719 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
720 let mut c = v.windows(2);
721 assert_eq!(c.nth(2).unwrap()[1], 3);
722 assert_eq!(c.next().unwrap()[0], 3);
724 let v2: &[i32] = &[0, 1, 2, 3, 4];
725 let mut c2 = v2.windows(4);
726 assert_eq!(c2.nth(1).unwrap()[1], 2);
727 assert_eq!(c2.next(), None);
731 fn test_windows_nth_back() {
732 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
733 let mut c = v.windows(2);
734 assert_eq!(c.nth_back(2).unwrap()[0], 2);
735 assert_eq!(c.next_back().unwrap()[1], 2);
737 let v2: &[i32] = &[0, 1, 2, 3, 4];
738 let mut c2 = v2.windows(4);
739 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
740 assert_eq!(c2.next_back(), None);
744 fn test_windows_last() {
745 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
746 let c = v.windows(2);
747 assert_eq!(c.last().unwrap()[1], 5);
749 let v2: &[i32] = &[0, 1, 2, 3, 4];
750 let c2 = v2.windows(2);
751 assert_eq!(c2.last().unwrap()[0], 3);
755 fn test_windows_zip() {
756 let v1: &[i32] = &[0, 1, 2, 3, 4];
757 let v2: &[i32] = &[6, 7, 8, 9, 10];
759 let res = v1.windows(2)
761 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
762 .collect::<Vec<_>>();
764 assert_eq!(res, [14, 18, 22, 26]);
769 fn test_iter_ref_consistency() {
772 fn test<T : Copy + Debug + PartialEq>(x : T) {
773 let v : &[T] = &[x, x, x];
774 let v_ptrs : [*const T; 3] = match v {
775 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
782 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
783 let nth = v.iter().nth(i).unwrap();
784 assert_eq!(nth as *const _, v_ptrs[i]);
786 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
788 // stepping through with nth(0)
790 let mut it = v.iter();
792 let next = it.nth(0).unwrap();
793 assert_eq!(next as *const _, v_ptrs[i]);
795 assert_eq!(it.nth(0), None);
800 let mut it = v.iter();
802 let remaining = len - i;
803 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
805 let next = it.next().unwrap();
806 assert_eq!(next as *const _, v_ptrs[i]);
808 assert_eq!(it.size_hint(), (0, Some(0)));
809 assert_eq!(it.next(), None, "The final call to next() should return None");
814 let mut it = v.iter();
816 let remaining = len - i;
817 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
819 let prev = it.next_back().unwrap();
820 assert_eq!(prev as *const _, v_ptrs[remaining-1]);
822 assert_eq!(it.size_hint(), (0, Some(0)));
823 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
827 fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
828 let v : &mut [T] = &mut [x, x, x];
829 let v_ptrs : [*mut T; 3] = match v {
830 [ref v1, ref v2, ref v3] =>
831 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _],
838 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
839 let nth = v.iter_mut().nth(i).unwrap();
840 assert_eq!(nth as *mut _, v_ptrs[i]);
842 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
844 // stepping through with nth(0)
846 let mut it = v.iter();
848 let next = it.nth(0).unwrap();
849 assert_eq!(next as *const _, v_ptrs[i]);
851 assert_eq!(it.nth(0), None);
856 let mut it = v.iter_mut();
858 let remaining = len - i;
859 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
861 let next = it.next().unwrap();
862 assert_eq!(next as *mut _, v_ptrs[i]);
864 assert_eq!(it.size_hint(), (0, Some(0)));
865 assert_eq!(it.next(), None, "The final call to next() should return None");
870 let mut it = v.iter_mut();
872 let remaining = len - i;
873 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
875 let prev = it.next_back().unwrap();
876 assert_eq!(prev as *mut _, v_ptrs[remaining-1]);
878 assert_eq!(it.size_hint(), (0, Some(0)));
879 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
883 // Make sure iterators and slice patterns yield consistent addresses for various types,
887 test([0u32; 0]); // ZST with alignment > 0
890 test_mut([0u32; 0]); // ZST with alignment > 0
893 // The current implementation of SliceIndex fails to handle methods
894 // orthogonally from range types; therefore, it is worth testing
895 // all of the indexing operations on each input.
897 // This checks all six indexing methods, given an input range that
898 // should succeed. (it is NOT suitable for testing invalid inputs)
899 macro_rules! assert_range_eq {
900 ($arr:expr, $range:expr, $expected:expr)
903 let mut expected = $expected;
906 let expected: &[_] = &expected;
908 assert_eq!(&s[$range], expected, "(in assertion for: index)");
909 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
912 s.get_unchecked($range), expected,
913 "(in assertion for: get_unchecked)",
918 let s: &mut [_] = &mut arr;
919 let expected: &mut [_] = &mut expected;
922 &mut s[$range], expected,
923 "(in assertion for: index_mut)",
926 s.get_mut($range), Some(&mut expected[..]),
927 "(in assertion for: get_mut)",
931 s.get_unchecked_mut($range), expected,
932 "(in assertion for: get_unchecked_mut)",
939 // Make sure the macro can actually detect bugs,
940 // because if it can't, then what are we even doing here?
942 // (Be aware this only demonstrates the ability to detect bugs
943 // in the FIRST method that panics, as the macro is not designed
944 // to be used in `should_panic`)
946 #[should_panic(expected = "out of range")]
947 fn assert_range_eq_can_fail_by_panic() {
948 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
951 // (Be aware this only demonstrates the ability to detect bugs
952 // in the FIRST method it calls, as the macro is not designed
953 // to be used in `should_panic`)
955 #[should_panic(expected = "==")]
956 fn assert_range_eq_can_fail_by_inequality() {
957 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
960 // Test cases for bad index operations.
962 // This generates `should_panic` test cases for Index/IndexMut
963 // and `None` test cases for get/get_mut.
964 macro_rules! panic_cases {
966 // each test case needs a unique name to namespace the tests
967 in mod $case_name:ident {
972 // one or more similar inputs for which data[input] succeeds,
973 // and the corresponding output as an array. This helps validate
974 // "critical points" where an input range straddles the boundary
975 // between valid and invalid.
976 // (such as the input `len..len`, which is just barely valid)
978 good: data[$good:expr] == $output:expr;
981 bad: data[$bad:expr];
982 message: $expect_msg:expr;
990 $( assert_range_eq!($data, $good, $output); )*
994 assert_eq!(v.get($bad), None, "(in None assertion for get)");
998 let v: &mut [_] = &mut v;
999 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
1004 #[should_panic(expected = $expect_msg)]
1012 #[should_panic(expected = $expect_msg)]
1013 fn index_mut_fail() {
1015 let v: &mut [_] = &mut v;
1016 let _v = &mut v[$bad];
1024 let v = [0, 1, 2, 3, 4, 5];
1026 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
1027 assert_range_eq!(v, ..2, [0, 1]);
1028 assert_range_eq!(v, ..=1, [0, 1]);
1029 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
1030 assert_range_eq!(v, 1..4, [1, 2, 3]);
1031 assert_range_eq!(v, 1..=3, [1, 2, 3]);
1035 in mod rangefrom_len {
1036 data: [0, 1, 2, 3, 4, 5];
1038 good: data[6..] == [];
1040 message: "but ends at"; // perhaps not ideal
1043 in mod rangeto_len {
1044 data: [0, 1, 2, 3, 4, 5];
1046 good: data[..6] == [0, 1, 2, 3, 4, 5];
1048 message: "out of range";
1051 in mod rangetoinclusive_len {
1052 data: [0, 1, 2, 3, 4, 5];
1054 good: data[..=5] == [0, 1, 2, 3, 4, 5];
1056 message: "out of range";
1059 in mod range_len_len {
1060 data: [0, 1, 2, 3, 4, 5];
1062 good: data[6..6] == [];
1064 message: "out of range";
1067 in mod rangeinclusive_len_len {
1068 data: [0, 1, 2, 3, 4, 5];
1070 good: data[6..=5] == [];
1072 message: "out of range";
1077 in mod range_neg_width {
1078 data: [0, 1, 2, 3, 4, 5];
1080 good: data[4..4] == [];
1082 message: "but ends at";
1085 in mod rangeinclusive_neg_width {
1086 data: [0, 1, 2, 3, 4, 5];
1088 good: data[4..=3] == [];
1090 message: "but ends at";
1095 in mod rangeinclusive_overflow {
1098 // note: using 0 specifically ensures that the result of overflowing is 0..0,
1099 // so that `get` doesn't simply return None for the wrong reason.
1100 bad: data[0 ..= ::std::usize::MAX];
1101 message: "maximum usize";
1104 in mod rangetoinclusive_overflow {
1107 bad: data[..= ::std::usize::MAX];
1108 message: "maximum usize";
1114 fn test_find_rfind() {
1115 let v = [0, 1, 2, 3, 4, 5];
1116 let mut iter = v.iter();
1117 let mut i = v.len();
1118 while let Some(&elt) = iter.rfind(|_| true) {
1120 assert_eq!(elt, v[i]);
1123 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
1127 fn test_iter_folds() {
1128 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
1129 assert_eq!(a.iter().fold(0, |acc, &x| 2*acc + x), 57);
1130 assert_eq!(a.iter().rfold(0, |acc, &x| 2*acc + x), 129);
1131 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
1132 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
1133 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
1135 // short-circuiting try_fold, through other methods
1136 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
1137 let mut iter = a.iter();
1138 assert_eq!(iter.position(|&x| x == 3), Some(3));
1139 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
1140 assert_eq!(iter.len(), 2);
1144 fn test_rotate_left() {
1145 const N: usize = 600;
1146 let a: &mut [_] = &mut [0; N];
1155 assert_eq!(a[(i + k) % N], i);
1160 fn test_rotate_right() {
1161 const N: usize = 600;
1162 let a: &mut [_] = &mut [0; N];
1170 assert_eq!(a[(i + 42) % N], i);
1175 #[cfg(not(miri))] // Miri is too slow
1176 fn brute_force_rotate_test_0() {
1177 // In case of edge cases involving multiple algorithms
1181 let mut v = Vec::with_capacity(len);
1185 v[..].rotate_right(s);
1186 for i in 0..v.len() {
1187 assert_eq!(v[i], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1194 fn brute_force_rotate_test_1() {
1195 // `ptr_rotate` covers so many kinds of pointer usage, that this is just a good test for
1196 // pointers in general. This uses a `[usize; 4]` to hit all algorithms without overwhelming miri
1200 let mut v: Vec<[usize; 4]> = Vec::with_capacity(len);
1202 v.push([i, 0, 0, 0]);
1204 v[..].rotate_right(s);
1205 for i in 0..v.len() {
1206 assert_eq!(v[i][0], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1213 #[cfg(not(target_arch = "wasm32"))]
1214 fn sort_unstable() {
1215 use core::cmp::Ordering::{Equal, Greater, Less};
1216 use core::slice::heapsort;
1217 use rand::{SeedableRng, Rng, rngs::StdRng, seq::SliceRandom};
1219 #[cfg(not(miri))] // Miri is too slow
1220 let large_range = 500..510;
1221 #[cfg(not(miri))] // Miri is too slow
1225 let large_range = 0..0; // empty range
1229 let mut v = [0; 600];
1230 let mut tmp = [0; 600];
1231 let mut rng = StdRng::from_entropy();
1233 for len in (2..25).chain(large_range) {
1234 let v = &mut v[0..len];
1235 let tmp = &mut tmp[0..len];
1237 for &modulus in &[5, 10, 100, 1000] {
1238 for _ in 0..rounds {
1240 v[i] = rng.gen::<i32>() % modulus;
1243 // Sort in default order.
1244 tmp.copy_from_slice(v);
1245 tmp.sort_unstable();
1246 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1248 // Sort in ascending order.
1249 tmp.copy_from_slice(v);
1250 tmp.sort_unstable_by(|a, b| a.cmp(b));
1251 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1253 // Sort in descending order.
1254 tmp.copy_from_slice(v);
1255 tmp.sort_unstable_by(|a, b| b.cmp(a));
1256 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1258 // Test heapsort using `<` operator.
1259 tmp.copy_from_slice(v);
1260 heapsort(tmp, |a, b| a < b);
1261 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1263 // Test heapsort using `>` operator.
1264 tmp.copy_from_slice(v);
1265 heapsort(tmp, |a, b| a > b);
1266 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1271 // Sort using a completely random comparison function.
1272 // This will reorder the elements *somehow*, but won't panic.
1273 for i in 0..v.len() {
1276 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1278 for i in 0..v.len() {
1279 assert_eq!(v[i], i as i32);
1282 // Should not panic.
1283 [0i32; 0].sort_unstable();
1284 [(); 10].sort_unstable();
1285 [(); 100].sort_unstable();
1287 let mut v = [0xDEADBEEFu64];
1289 assert!(v == [0xDEADBEEF]);
1293 #[cfg(not(target_arch = "wasm32"))]
1294 #[cfg(not(miri))] // Miri is too slow
1295 fn partition_at_index() {
1296 use core::cmp::Ordering::{Equal, Greater, Less};
1297 use rand::rngs::StdRng;
1298 use rand::seq::SliceRandom;
1299 use rand::{SeedableRng, Rng};
1301 let mut rng = StdRng::from_entropy();
1303 for len in (2..21).chain(500..501) {
1304 let mut orig = vec![0; len];
1306 for &modulus in &[5, 10, 1000] {
1309 orig[i] = rng.gen::<i32>() % modulus;
1313 let mut v = orig.clone();
1318 // Sort in default order.
1319 for pivot in 0..len {
1320 let mut v = orig.clone();
1321 v.partition_at_index(pivot);
1323 assert_eq!(v_sorted[pivot], v[pivot]);
1325 for j in pivot..len {
1326 assert!(v[i] <= v[j]);
1331 // Sort in ascending order.
1332 for pivot in 0..len {
1333 let mut v = orig.clone();
1334 let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
1336 assert_eq!(left.len() + right.len(), len - 1);
1339 assert!(l <= pivot);
1340 for r in right.iter_mut() {
1342 assert!(pivot <= r);
1347 // Sort in descending order.
1348 let sort_descending_comparator = |a: &i32, b: &i32| b.cmp(a);
1349 let v_sorted_descending = {
1350 let mut v = orig.clone();
1351 v.sort_by(sort_descending_comparator);
1355 for pivot in 0..len {
1356 let mut v = orig.clone();
1357 v.partition_at_index_by(pivot, sort_descending_comparator);
1359 assert_eq!(v_sorted_descending[pivot], v[pivot]);
1361 for j in pivot..len {
1362 assert!(v[j] <= v[i]);
1370 // Sort at index using a completely random comparison function.
1371 // This will reorder the elements *somehow*, but won't panic.
1372 let mut v = [0; 500];
1373 for i in 0..v.len() {
1377 for pivot in 0..v.len() {
1378 v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1380 for i in 0..v.len() {
1381 assert_eq!(v[i], i as i32);
1385 // Should not panic.
1386 [(); 10].partition_at_index(0);
1387 [(); 10].partition_at_index(5);
1388 [(); 10].partition_at_index(9);
1389 [(); 100].partition_at_index(0);
1390 [(); 100].partition_at_index(50);
1391 [(); 100].partition_at_index(99);
1393 let mut v = [0xDEADBEEFu64];
1394 v.partition_at_index(0);
1395 assert!(v == [0xDEADBEEF]);
1399 #[should_panic(expected = "index 0 greater than length of slice")]
1400 fn partition_at_index_zero_length() {
1401 [0i32; 0].partition_at_index(0);
1405 #[should_panic(expected = "index 20 greater than length of slice")]
1406 fn partition_at_index_past_length() {
1407 [0i32; 10].partition_at_index(20);
1411 use core::slice::memchr::{memchr, memrchr};
1413 // test fallback implementations on all platforms
1416 assert_eq!(Some(0), memchr(b'a', b"a"));
1420 fn matches_begin() {
1421 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1426 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1431 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1435 fn matches_past_nul() {
1436 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1440 fn no_match_empty() {
1441 assert_eq!(None, memchr(b'a', b""));
1446 assert_eq!(None, memchr(b'a', b"xyz"));
1450 fn matches_one_reversed() {
1451 assert_eq!(Some(0), memrchr(b'a', b"a"));
1455 fn matches_begin_reversed() {
1456 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1460 fn matches_end_reversed() {
1461 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1465 fn matches_nul_reversed() {
1466 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1470 fn matches_past_nul_reversed() {
1471 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1475 fn no_match_empty_reversed() {
1476 assert_eq!(None, memrchr(b'a', b""));
1480 fn no_match_reversed() {
1481 assert_eq!(None, memrchr(b'a', b"xyz"));
1485 fn each_alignment_reversed() {
1486 let mut data = [1u8; 64];
1490 for start in 0..16 {
1491 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1497 #[cfg(not(miri))] // Miri does not compute a maximal `mid` for `align_offset`
1498 fn test_align_to_simple() {
1499 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1500 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1501 assert_eq!(aligned.len(), 3);
1502 assert!(prefix == [1] || suffix == [7]);
1503 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1504 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1505 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1506 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1507 assert!(aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1508 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1509 aligned, expect1, expect2, expect3, expect4);
1513 fn test_align_to_zst() {
1514 let bytes = [1, 2, 3, 4, 5, 6, 7];
1515 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1516 assert_eq!(aligned.len(), 0);
1517 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1521 #[cfg(not(miri))] // Miri does not compute a maximal `mid` for `align_offset`
1522 fn test_align_to_non_trivial() {
1523 #[repr(align(8))] struct U64(u64, u64);
1524 #[repr(align(8))] struct U64U64U32(u64, u64, u32);
1525 let data = [U64(1, 2), U64(3, 4), U64(5, 6), U64(7, 8), U64(9, 10), U64(11, 12), U64(13, 14),
1527 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1528 assert_eq!(aligned.len(), 4);
1529 assert_eq!(prefix.len() + suffix.len(), 2);
1533 fn test_align_to_empty_mid() {
1536 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1537 // overall slice is too short to contain an aligned address.
1538 let bytes = [1, 2, 3, 4, 5, 6, 7];
1540 for offset in 0..4 {
1541 let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::<Chunk>() };
1542 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1547 fn test_slice_partition_dedup_by() {
1548 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1550 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1552 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1553 assert_eq!(duplicates, [5, -1, 2]);
1557 fn test_slice_partition_dedup_empty() {
1558 let mut slice: [i32; 0] = [];
1560 let (dedup, duplicates) = slice.partition_dedup();
1562 assert_eq!(dedup, []);
1563 assert_eq!(duplicates, []);
1567 fn test_slice_partition_dedup_one() {
1568 let mut slice = [12];
1570 let (dedup, duplicates) = slice.partition_dedup();
1572 assert_eq!(dedup, [12]);
1573 assert_eq!(duplicates, []);
1577 fn test_slice_partition_dedup_multiple_ident() {
1578 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1580 let (dedup, duplicates) = slice.partition_dedup();
1582 assert_eq!(dedup, [12, 11]);
1583 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1587 fn test_slice_partition_dedup_partialeq() {
1589 struct Foo(i32, i32);
1591 impl PartialEq for Foo {
1592 fn eq(&self, other: &Foo) -> bool {
1597 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1599 let (dedup, duplicates) = slice.partition_dedup();
1601 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1602 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1606 fn test_copy_within() {
1607 // Start to end, with a RangeTo.
1608 let mut bytes = *b"Hello, World!";
1609 bytes.copy_within(..3, 10);
1610 assert_eq!(&bytes, b"Hello, WorHel");
1612 // End to start, with a RangeFrom.
1613 let mut bytes = *b"Hello, World!";
1614 bytes.copy_within(10.., 0);
1615 assert_eq!(&bytes, b"ld!lo, World!");
1617 // Overlapping, with a RangeInclusive.
1618 let mut bytes = *b"Hello, World!";
1619 bytes.copy_within(0..=11, 1);
1620 assert_eq!(&bytes, b"HHello, World");
1622 // Whole slice, with a RangeFull.
1623 let mut bytes = *b"Hello, World!";
1624 bytes.copy_within(.., 0);
1625 assert_eq!(&bytes, b"Hello, World!");
1627 // Ensure that copying at the end of slice won't cause UB.
1628 let mut bytes = *b"Hello, World!";
1629 bytes.copy_within(13..13, 5);
1630 assert_eq!(&bytes, b"Hello, World!");
1631 bytes.copy_within(5..5, 13);
1632 assert_eq!(&bytes, b"Hello, World!");
1636 #[should_panic(expected = "src is out of bounds")]
1637 fn test_copy_within_panics_src_too_long() {
1638 let mut bytes = *b"Hello, World!";
1639 // The length is only 13, so 14 is out of bounds.
1640 bytes.copy_within(10..14, 0);
1644 #[should_panic(expected = "dest is out of bounds")]
1645 fn test_copy_within_panics_dest_too_long() {
1646 let mut bytes = *b"Hello, World!";
1647 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1648 bytes.copy_within(0..4, 10);
1651 #[should_panic(expected = "src end is before src start")]
1652 fn test_copy_within_panics_src_inverted() {
1653 let mut bytes = *b"Hello, World!";
1654 // 2 is greater than 1, so this range is invalid.
1655 bytes.copy_within(2..1, 0);
1658 #[should_panic(expected = "attempted to index slice up to maximum usize")]
1659 fn test_copy_within_panics_src_out_of_bounds() {
1660 let mut bytes = *b"Hello, World!";
1661 // an inclusive range ending at usize::max_value() would make src_end overflow
1662 bytes.copy_within(usize::max_value()..=usize::max_value(), 0);
1666 fn test_is_sorted() {
1667 let empty: [i32; 0] = [];
1669 assert!([1, 2, 2, 9].is_sorted());
1670 assert!(![1, 3, 2].is_sorted());
1671 assert!([0].is_sorted());
1672 assert!(empty.is_sorted());
1673 assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
1674 assert!([-2, -1, 0, 3].is_sorted());
1675 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1676 assert!(!["c", "bb", "aaa"].is_sorted());
1677 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));