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_iterator_nth() {
86 let v: &[_] = &[0, 1, 2, 3, 4];
88 assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
90 assert_eq!(v.iter().nth(v.len()), None);
92 let mut iter = v.iter();
93 assert_eq!(iter.nth(2).unwrap(), &v[2]);
94 assert_eq!(iter.nth(1).unwrap(), &v[4]);
98 fn test_iterator_nth_back() {
99 let v: &[_] = &[0, 1, 2, 3, 4];
100 for i in 0..v.len() {
101 assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - i - 1]);
103 assert_eq!(v.iter().nth_back(v.len()), None);
105 let mut iter = v.iter();
106 assert_eq!(iter.nth_back(2).unwrap(), &v[2]);
107 assert_eq!(iter.nth_back(1).unwrap(), &v[0]);
111 fn test_iterator_last() {
112 let v: &[_] = &[0, 1, 2, 3, 4];
113 assert_eq!(v.iter().last().unwrap(), &4);
114 assert_eq!(v[..1].iter().last().unwrap(), &0);
118 fn test_iterator_count() {
119 let v: &[_] = &[0, 1, 2, 3, 4];
120 assert_eq!(v.iter().count(), 5);
122 let mut iter2 = v.iter();
125 assert_eq!(iter2.count(), 3);
129 fn test_chunks_count() {
130 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
132 assert_eq!(c.count(), 2);
134 let v2: &[i32] = &[0, 1, 2, 3, 4];
135 let c2 = v2.chunks(2);
136 assert_eq!(c2.count(), 3);
138 let v3: &[i32] = &[];
139 let c3 = v3.chunks(2);
140 assert_eq!(c3.count(), 0);
144 fn test_chunks_nth() {
145 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
146 let mut c = v.chunks(2);
147 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
148 assert_eq!(c.next().unwrap(), &[4, 5]);
150 let v2: &[i32] = &[0, 1, 2, 3, 4];
151 let mut c2 = v2.chunks(3);
152 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
153 assert_eq!(c2.next(), None);
157 fn test_chunks_nth_back() {
158 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
159 let mut c = v.chunks(2);
160 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
161 assert_eq!(c.next().unwrap(), &[0, 1]);
162 assert_eq!(c.next(), None);
164 let v2: &[i32] = &[0, 1, 2, 3, 4];
165 let mut c2 = v2.chunks(3);
166 assert_eq!(c2.nth_back(1).unwrap(), &[0, 1, 2]);
167 assert_eq!(c2.next(), None);
168 assert_eq!(c2.next_back(), None);
170 let v3: &[i32] = &[0, 1, 2, 3, 4];
171 let mut c3 = v3.chunks(10);
172 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
173 assert_eq!(c3.next(), None);
175 let v4: &[i32] = &[0, 1, 2];
176 let mut c4 = v4.chunks(10);
177 assert_eq!(c4.nth_back(1_000_000_000usize), None);
181 fn test_chunks_last() {
182 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
184 assert_eq!(c.last().unwrap()[1], 5);
186 let v2: &[i32] = &[0, 1, 2, 3, 4];
187 let c2 = v2.chunks(2);
188 assert_eq!(c2.last().unwrap()[0], 4);
192 fn test_chunks_zip() {
193 let v1: &[i32] = &[0, 1, 2, 3, 4];
194 let v2: &[i32] = &[6, 7, 8, 9, 10];
199 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
200 .collect::<Vec<_>>();
201 assert_eq!(res, vec![14, 22, 14]);
205 fn test_chunks_mut_count() {
206 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
207 let c = v.chunks_mut(3);
208 assert_eq!(c.count(), 2);
210 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
211 let c2 = v2.chunks_mut(2);
212 assert_eq!(c2.count(), 3);
214 let v3: &mut [i32] = &mut [];
215 let c3 = v3.chunks_mut(2);
216 assert_eq!(c3.count(), 0);
220 fn test_chunks_mut_nth() {
221 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
222 let mut c = v.chunks_mut(2);
223 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
224 assert_eq!(c.next().unwrap(), &[4, 5]);
226 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
227 let mut c2 = v2.chunks_mut(3);
228 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
229 assert_eq!(c2.next(), None);
233 fn test_chunks_mut_nth_back() {
234 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
235 let mut c = v.chunks_mut(2);
236 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
237 assert_eq!(c.next().unwrap(), &[0, 1]);
239 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
240 let mut c1 = v1.chunks_mut(3);
241 assert_eq!(c1.nth_back(1).unwrap(), &[0, 1, 2]);
242 assert_eq!(c1.next(), None);
244 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
245 let mut c3 = v3.chunks_mut(10);
246 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
247 assert_eq!(c3.next(), None);
249 let v4: &mut [i32] = &mut [0, 1, 2];
250 let mut c4 = v4.chunks_mut(10);
251 assert_eq!(c4.nth_back(1_000_000_000usize), None);
255 fn test_chunks_mut_last() {
256 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
257 let c = v.chunks_mut(2);
258 assert_eq!(c.last().unwrap(), &[4, 5]);
260 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
261 let c2 = v2.chunks_mut(2);
262 assert_eq!(c2.last().unwrap(), &[4]);
266 fn test_chunks_mut_zip() {
267 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
268 let v2: &[i32] = &[6, 7, 8, 9, 10];
270 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
271 let sum = b.iter().sum::<i32>();
276 assert_eq!(v1, [13, 14, 19, 20, 14]);
280 fn test_chunks_exact_count() {
281 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
282 let c = v.chunks_exact(3);
283 assert_eq!(c.count(), 2);
285 let v2: &[i32] = &[0, 1, 2, 3, 4];
286 let c2 = v2.chunks_exact(2);
287 assert_eq!(c2.count(), 2);
289 let v3: &[i32] = &[];
290 let c3 = v3.chunks_exact(2);
291 assert_eq!(c3.count(), 0);
295 fn test_chunks_exact_nth() {
296 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
297 let mut c = v.chunks_exact(2);
298 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
299 assert_eq!(c.next().unwrap(), &[4, 5]);
301 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
302 let mut c2 = v2.chunks_exact(3);
303 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
304 assert_eq!(c2.next(), None);
308 fn test_chunks_exact_nth_back() {
309 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
310 let mut c = v.chunks_exact(2);
311 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
312 assert_eq!(c.next().unwrap(), &[0, 1]);
313 assert_eq!(c.next(), None);
315 let v2: &[i32] = &[0, 1, 2, 3, 4];
316 let mut c2 = v2.chunks_exact(3);
317 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
318 assert_eq!(c2.next(), None);
319 assert_eq!(c2.next_back(), None);
321 let v3: &[i32] = &[0, 1, 2, 3, 4];
322 let mut c3 = v3.chunks_exact(10);
323 assert_eq!(c3.nth_back(0), None);
327 fn test_chunks_exact_last() {
328 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
329 let c = v.chunks_exact(2);
330 assert_eq!(c.last().unwrap(), &[4, 5]);
332 let v2: &[i32] = &[0, 1, 2, 3, 4];
333 let c2 = v2.chunks_exact(2);
334 assert_eq!(c2.last().unwrap(), &[2, 3]);
338 fn test_chunks_exact_remainder() {
339 let v: &[i32] = &[0, 1, 2, 3, 4];
340 let c = v.chunks_exact(2);
341 assert_eq!(c.remainder(), &[4]);
345 fn test_chunks_exact_zip() {
346 let v1: &[i32] = &[0, 1, 2, 3, 4];
347 let v2: &[i32] = &[6, 7, 8, 9, 10];
351 .zip(v2.chunks_exact(2))
352 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
353 .collect::<Vec<_>>();
354 assert_eq!(res, vec![14, 22]);
358 fn test_chunks_exact_mut_count() {
359 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
360 let c = v.chunks_exact_mut(3);
361 assert_eq!(c.count(), 2);
363 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
364 let c2 = v2.chunks_exact_mut(2);
365 assert_eq!(c2.count(), 2);
367 let v3: &mut [i32] = &mut [];
368 let c3 = v3.chunks_exact_mut(2);
369 assert_eq!(c3.count(), 0);
373 fn test_chunks_exact_mut_nth() {
374 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
375 let mut c = v.chunks_exact_mut(2);
376 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
377 assert_eq!(c.next().unwrap(), &[4, 5]);
379 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
380 let mut c2 = v2.chunks_exact_mut(3);
381 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
382 assert_eq!(c2.next(), None);
386 fn test_chunks_exact_mut_nth_back() {
387 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
388 let mut c = v.chunks_exact_mut(2);
389 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
390 assert_eq!(c.next().unwrap(), &[0, 1]);
391 assert_eq!(c.next(), None);
393 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
394 let mut c2 = v2.chunks_exact_mut(3);
395 assert_eq!(c2.nth_back(0).unwrap(), &[0, 1, 2]);
396 assert_eq!(c2.next(), None);
397 assert_eq!(c2.next_back(), None);
399 let v3: &mut [i32] = &mut [0, 1, 2, 3, 4];
400 let mut c3 = v3.chunks_exact_mut(10);
401 assert_eq!(c3.nth_back(0), None);
405 fn test_chunks_exact_mut_last() {
406 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
407 let c = v.chunks_exact_mut(2);
408 assert_eq!(c.last().unwrap(), &[4, 5]);
410 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
411 let c2 = v2.chunks_exact_mut(2);
412 assert_eq!(c2.last().unwrap(), &[2, 3]);
416 fn test_chunks_exact_mut_remainder() {
417 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
418 let c = v.chunks_exact_mut(2);
419 assert_eq!(c.into_remainder(), &[4]);
423 fn test_chunks_exact_mut_zip() {
424 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
425 let v2: &[i32] = &[6, 7, 8, 9, 10];
427 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
428 let sum = b.iter().sum::<i32>();
433 assert_eq!(v1, [13, 14, 19, 20, 4]);
437 fn test_rchunks_count() {
438 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
439 let c = v.rchunks(3);
440 assert_eq!(c.count(), 2);
442 let v2: &[i32] = &[0, 1, 2, 3, 4];
443 let c2 = v2.rchunks(2);
444 assert_eq!(c2.count(), 3);
446 let v3: &[i32] = &[];
447 let c3 = v3.rchunks(2);
448 assert_eq!(c3.count(), 0);
452 fn test_rchunks_nth() {
453 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
454 let mut c = v.rchunks(2);
455 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
456 assert_eq!(c.next().unwrap(), &[0, 1]);
458 let v2: &[i32] = &[0, 1, 2, 3, 4];
459 let mut c2 = v2.rchunks(3);
460 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
461 assert_eq!(c2.next(), None);
465 fn test_rchunks_nth_back() {
466 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
467 let mut c = v.rchunks(2);
468 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
469 assert_eq!(c.next_back().unwrap(), &[4, 5]);
471 let v2: &[i32] = &[0, 1, 2, 3, 4];
472 let mut c2 = v2.rchunks(3);
473 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
474 assert_eq!(c2.next_back(), None);
478 fn test_rchunks_last() {
479 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
480 let c = v.rchunks(2);
481 assert_eq!(c.last().unwrap()[1], 1);
483 let v2: &[i32] = &[0, 1, 2, 3, 4];
484 let c2 = v2.rchunks(2);
485 assert_eq!(c2.last().unwrap()[0], 0);
489 fn test_rchunks_zip() {
490 let v1: &[i32] = &[0, 1, 2, 3, 4];
491 let v2: &[i32] = &[6, 7, 8, 9, 10];
496 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
497 .collect::<Vec<_>>();
498 assert_eq!(res, vec![26, 18, 6]);
502 fn test_rchunks_mut_count() {
503 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
504 let c = v.rchunks_mut(3);
505 assert_eq!(c.count(), 2);
507 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
508 let c2 = v2.rchunks_mut(2);
509 assert_eq!(c2.count(), 3);
511 let v3: &mut [i32] = &mut [];
512 let c3 = v3.rchunks_mut(2);
513 assert_eq!(c3.count(), 0);
517 fn test_rchunks_mut_nth() {
518 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
519 let mut c = v.rchunks_mut(2);
520 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
521 assert_eq!(c.next().unwrap(), &[0, 1]);
523 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
524 let mut c2 = v2.rchunks_mut(3);
525 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
526 assert_eq!(c2.next(), None);
530 fn test_rchunks_mut_nth_back() {
531 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
532 let mut c = v.rchunks_mut(2);
533 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
534 assert_eq!(c.next_back().unwrap(), &[4, 5]);
536 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
537 let mut c2 = v2.rchunks_mut(3);
538 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
539 assert_eq!(c2.next_back(), None);
543 fn test_rchunks_mut_last() {
544 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
545 let c = v.rchunks_mut(2);
546 assert_eq!(c.last().unwrap(), &[0, 1]);
548 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
549 let c2 = v2.rchunks_mut(2);
550 assert_eq!(c2.last().unwrap(), &[0]);
554 fn test_rchunks_mut_zip() {
555 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
556 let v2: &[i32] = &[6, 7, 8, 9, 10];
558 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
559 let sum = b.iter().sum::<i32>();
564 assert_eq!(v1, [6, 16, 17, 22, 23]);
568 fn test_rchunks_exact_count() {
569 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
570 let c = v.rchunks_exact(3);
571 assert_eq!(c.count(), 2);
573 let v2: &[i32] = &[0, 1, 2, 3, 4];
574 let c2 = v2.rchunks_exact(2);
575 assert_eq!(c2.count(), 2);
577 let v3: &[i32] = &[];
578 let c3 = v3.rchunks_exact(2);
579 assert_eq!(c3.count(), 0);
583 fn test_rchunks_exact_nth() {
584 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
585 let mut c = v.rchunks_exact(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, 5, 6];
590 let mut c2 = v2.rchunks_exact(3);
591 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
592 assert_eq!(c2.next(), None);
596 fn test_rchunks_exact_nth_back() {
597 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
598 let mut c = v.rchunks_exact(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, 5, 6];
603 let mut c2 = v2.rchunks_exact(3);
604 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
605 assert_eq!(c2.next(), None);
609 fn test_rchunks_exact_last() {
610 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
611 let c = v.rchunks_exact(2);
612 assert_eq!(c.last().unwrap(), &[0, 1]);
614 let v2: &[i32] = &[0, 1, 2, 3, 4];
615 let c2 = v2.rchunks_exact(2);
616 assert_eq!(c2.last().unwrap(), &[1, 2]);
620 fn test_rchunks_exact_remainder() {
621 let v: &[i32] = &[0, 1, 2, 3, 4];
622 let c = v.rchunks_exact(2);
623 assert_eq!(c.remainder(), &[0]);
627 fn test_rchunks_exact_zip() {
628 let v1: &[i32] = &[0, 1, 2, 3, 4];
629 let v2: &[i32] = &[6, 7, 8, 9, 10];
633 .zip(v2.rchunks_exact(2))
634 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
635 .collect::<Vec<_>>();
636 assert_eq!(res, vec![26, 18]);
640 fn test_rchunks_exact_mut_count() {
641 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
642 let c = v.rchunks_exact_mut(3);
643 assert_eq!(c.count(), 2);
645 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
646 let c2 = v2.rchunks_exact_mut(2);
647 assert_eq!(c2.count(), 2);
649 let v3: &mut [i32] = &mut [];
650 let c3 = v3.rchunks_exact_mut(2);
651 assert_eq!(c3.count(), 0);
655 fn test_rchunks_exact_mut_nth() {
656 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
657 let mut c = v.rchunks_exact_mut(2);
658 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
659 assert_eq!(c.next().unwrap(), &[0, 1]);
661 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
662 let mut c2 = v2.rchunks_exact_mut(3);
663 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
664 assert_eq!(c2.next(), None);
668 fn test_rchunks_exact_mut_nth_back() {
669 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
670 let mut c = v.rchunks_exact_mut(2);
671 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
672 assert_eq!(c.next_back().unwrap(), &[4, 5]);
674 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
675 let mut c2 = v2.rchunks_exact_mut(3);
676 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
677 assert_eq!(c2.next(), None);
681 fn test_rchunks_exact_mut_last() {
682 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
683 let c = v.rchunks_exact_mut(2);
684 assert_eq!(c.last().unwrap(), &[0, 1]);
686 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
687 let c2 = v2.rchunks_exact_mut(2);
688 assert_eq!(c2.last().unwrap(), &[1, 2]);
692 fn test_rchunks_exact_mut_remainder() {
693 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
694 let c = v.rchunks_exact_mut(2);
695 assert_eq!(c.into_remainder(), &[0]);
699 fn test_rchunks_exact_mut_zip() {
700 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
701 let v2: &[i32] = &[6, 7, 8, 9, 10];
703 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
704 let sum = b.iter().sum::<i32>();
709 assert_eq!(v1, [0, 16, 17, 22, 23]);
713 fn test_windows_count() {
714 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
715 let c = v.windows(3);
716 assert_eq!(c.count(), 4);
718 let v2: &[i32] = &[0, 1, 2, 3, 4];
719 let c2 = v2.windows(6);
720 assert_eq!(c2.count(), 0);
722 let v3: &[i32] = &[];
723 let c3 = v3.windows(2);
724 assert_eq!(c3.count(), 0);
728 fn test_windows_nth() {
729 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
730 let mut c = v.windows(2);
731 assert_eq!(c.nth(2).unwrap()[1], 3);
732 assert_eq!(c.next().unwrap()[0], 3);
734 let v2: &[i32] = &[0, 1, 2, 3, 4];
735 let mut c2 = v2.windows(4);
736 assert_eq!(c2.nth(1).unwrap()[1], 2);
737 assert_eq!(c2.next(), None);
741 fn test_windows_nth_back() {
742 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
743 let mut c = v.windows(2);
744 assert_eq!(c.nth_back(2).unwrap()[0], 2);
745 assert_eq!(c.next_back().unwrap()[1], 2);
747 let v2: &[i32] = &[0, 1, 2, 3, 4];
748 let mut c2 = v2.windows(4);
749 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
750 assert_eq!(c2.next_back(), None);
754 fn test_windows_last() {
755 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
756 let c = v.windows(2);
757 assert_eq!(c.last().unwrap()[1], 5);
759 let v2: &[i32] = &[0, 1, 2, 3, 4];
760 let c2 = v2.windows(2);
761 assert_eq!(c2.last().unwrap()[0], 3);
765 fn test_windows_zip() {
766 let v1: &[i32] = &[0, 1, 2, 3, 4];
767 let v2: &[i32] = &[6, 7, 8, 9, 10];
772 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
773 .collect::<Vec<_>>();
775 assert_eq!(res, [14, 18, 22, 26]);
780 fn test_iter_ref_consistency() {
783 fn test<T: Copy + Debug + PartialEq>(x: T) {
784 let v: &[T] = &[x, x, x];
785 let v_ptrs: [*const T; 3] = match v {
786 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
793 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
794 let nth = v.iter().nth(i).unwrap();
795 assert_eq!(nth as *const _, v_ptrs[i]);
797 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
799 // stepping through with nth(0)
801 let mut it = v.iter();
803 let next = it.nth(0).unwrap();
804 assert_eq!(next as *const _, v_ptrs[i]);
806 assert_eq!(it.nth(0), None);
811 let mut it = v.iter();
813 let remaining = len - i;
814 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
816 let next = it.next().unwrap();
817 assert_eq!(next as *const _, v_ptrs[i]);
819 assert_eq!(it.size_hint(), (0, Some(0)));
820 assert_eq!(it.next(), None, "The final call to next() should return None");
825 let mut it = v.iter();
827 let remaining = len - i;
828 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
830 let prev = it.next_back().unwrap();
831 assert_eq!(prev as *const _, v_ptrs[remaining - 1]);
833 assert_eq!(it.size_hint(), (0, Some(0)));
834 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
838 fn test_mut<T: Copy + Debug + PartialEq>(x: T) {
839 let v: &mut [T] = &mut [x, x, x];
840 let v_ptrs: [*mut T; 3] = match v {
841 [ref v1, ref v2, ref v3] => {
842 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _]
850 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
851 let nth = v.iter_mut().nth(i).unwrap();
852 assert_eq!(nth as *mut _, v_ptrs[i]);
854 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
856 // stepping through with nth(0)
858 let mut it = v.iter();
860 let next = it.nth(0).unwrap();
861 assert_eq!(next as *const _, v_ptrs[i]);
863 assert_eq!(it.nth(0), None);
868 let mut it = v.iter_mut();
870 let remaining = len - i;
871 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
873 let next = it.next().unwrap();
874 assert_eq!(next as *mut _, v_ptrs[i]);
876 assert_eq!(it.size_hint(), (0, Some(0)));
877 assert_eq!(it.next(), None, "The final call to next() should return None");
882 let mut it = v.iter_mut();
884 let remaining = len - i;
885 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
887 let prev = it.next_back().unwrap();
888 assert_eq!(prev as *mut _, v_ptrs[remaining - 1]);
890 assert_eq!(it.size_hint(), (0, Some(0)));
891 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
895 // Make sure iterators and slice patterns yield consistent addresses for various types,
899 test([0u32; 0]); // ZST with alignment > 0
902 test_mut([0u32; 0]); // ZST with alignment > 0
905 // The current implementation of SliceIndex fails to handle methods
906 // orthogonally from range types; therefore, it is worth testing
907 // all of the indexing operations on each input.
909 // This checks all six indexing methods, given an input range that
910 // should succeed. (it is NOT suitable for testing invalid inputs)
911 macro_rules! assert_range_eq {
912 ($arr:expr, $range:expr, $expected:expr) => {
914 let mut expected = $expected;
917 let expected: &[_] = &expected;
919 assert_eq!(&s[$range], expected, "(in assertion for: index)");
920 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
923 s.get_unchecked($range),
925 "(in assertion for: get_unchecked)",
930 let s: &mut [_] = &mut arr;
931 let expected: &mut [_] = &mut expected;
933 assert_eq!(&mut s[$range], expected, "(in assertion for: index_mut)",);
936 Some(&mut expected[..]),
937 "(in assertion for: get_mut)",
941 s.get_unchecked_mut($range),
943 "(in assertion for: get_unchecked_mut)",
950 // Make sure the macro can actually detect bugs,
951 // because if it can't, then what are we even doing here?
953 // (Be aware this only demonstrates the ability to detect bugs
954 // in the FIRST method that panics, as the macro is not designed
955 // to be used in `should_panic`)
957 #[should_panic(expected = "out of range")]
958 fn assert_range_eq_can_fail_by_panic() {
959 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
962 // (Be aware this only demonstrates the ability to detect bugs
963 // in the FIRST method it calls, as the macro is not designed
964 // to be used in `should_panic`)
966 #[should_panic(expected = "==")]
967 fn assert_range_eq_can_fail_by_inequality() {
968 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
971 // Test cases for bad index operations.
973 // This generates `should_panic` test cases for Index/IndexMut
974 // and `None` test cases for get/get_mut.
975 macro_rules! panic_cases {
977 // each test case needs a unique name to namespace the tests
978 in mod $case_name:ident {
983 // one or more similar inputs for which data[input] succeeds,
984 // and the corresponding output as an array. This helps validate
985 // "critical points" where an input range straddles the boundary
986 // between valid and invalid.
987 // (such as the input `len..len`, which is just barely valid)
989 good: data[$good:expr] == $output:expr;
992 bad: data[$bad:expr];
993 message: $expect_msg:expr;
1001 $( assert_range_eq!($data, $good, $output); )*
1005 assert_eq!(v.get($bad), None, "(in None assertion for get)");
1009 let v: &mut [_] = &mut v;
1010 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
1015 #[should_panic(expected = $expect_msg)]
1023 #[should_panic(expected = $expect_msg)]
1024 fn index_mut_fail() {
1026 let v: &mut [_] = &mut v;
1027 let _v = &mut v[$bad];
1035 let v = [0, 1, 2, 3, 4, 5];
1037 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
1038 assert_range_eq!(v, ..2, [0, 1]);
1039 assert_range_eq!(v, ..=1, [0, 1]);
1040 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
1041 assert_range_eq!(v, 1..4, [1, 2, 3]);
1042 assert_range_eq!(v, 1..=3, [1, 2, 3]);
1046 in mod rangefrom_len {
1047 data: [0, 1, 2, 3, 4, 5];
1049 good: data[6..] == [];
1051 message: "but ends at"; // perhaps not ideal
1054 in mod rangeto_len {
1055 data: [0, 1, 2, 3, 4, 5];
1057 good: data[..6] == [0, 1, 2, 3, 4, 5];
1059 message: "out of range";
1062 in mod rangetoinclusive_len {
1063 data: [0, 1, 2, 3, 4, 5];
1065 good: data[..=5] == [0, 1, 2, 3, 4, 5];
1067 message: "out of range";
1070 in mod range_len_len {
1071 data: [0, 1, 2, 3, 4, 5];
1073 good: data[6..6] == [];
1075 message: "out of range";
1078 in mod rangeinclusive_len_len {
1079 data: [0, 1, 2, 3, 4, 5];
1081 good: data[6..=5] == [];
1083 message: "out of range";
1088 in mod range_neg_width {
1089 data: [0, 1, 2, 3, 4, 5];
1091 good: data[4..4] == [];
1093 message: "but ends at";
1096 in mod rangeinclusive_neg_width {
1097 data: [0, 1, 2, 3, 4, 5];
1099 good: data[4..=3] == [];
1101 message: "but ends at";
1106 in mod rangeinclusive_overflow {
1109 // note: using 0 specifically ensures that the result of overflowing is 0..0,
1110 // so that `get` doesn't simply return None for the wrong reason.
1111 bad: data[0 ..= ::std::usize::MAX];
1112 message: "maximum usize";
1115 in mod rangetoinclusive_overflow {
1118 bad: data[..= ::std::usize::MAX];
1119 message: "maximum usize";
1125 fn test_find_rfind() {
1126 let v = [0, 1, 2, 3, 4, 5];
1127 let mut iter = v.iter();
1128 let mut i = v.len();
1129 while let Some(&elt) = iter.rfind(|_| true) {
1131 assert_eq!(elt, v[i]);
1134 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
1138 fn test_iter_folds() {
1139 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
1140 assert_eq!(a.iter().fold(0, |acc, &x| 2 * acc + x), 57);
1141 assert_eq!(a.iter().rfold(0, |acc, &x| 2 * acc + x), 129);
1142 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
1143 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
1144 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
1146 // short-circuiting try_fold, through other methods
1147 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
1148 let mut iter = a.iter();
1149 assert_eq!(iter.position(|&x| x == 3), Some(3));
1150 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
1151 assert_eq!(iter.len(), 2);
1155 fn test_rotate_left() {
1156 const N: usize = 600;
1157 let a: &mut [_] = &mut [0; N];
1166 assert_eq!(a[(i + k) % N], i);
1171 fn test_rotate_right() {
1172 const N: usize = 600;
1173 let a: &mut [_] = &mut [0; N];
1181 assert_eq!(a[(i + 42) % N], i);
1186 #[cfg_attr(miri, ignore)] // Miri is too slow
1187 fn brute_force_rotate_test_0() {
1188 // In case of edge cases involving multiple algorithms
1192 let mut v = Vec::with_capacity(len);
1196 v[..].rotate_right(s);
1197 for i in 0..v.len() {
1198 assert_eq!(v[i], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1205 fn brute_force_rotate_test_1() {
1206 // `ptr_rotate` covers so many kinds of pointer usage, that this is just a good test for
1207 // pointers in general. This uses a `[usize; 4]` to hit all algorithms without overwhelming miri
1211 let mut v: Vec<[usize; 4]> = Vec::with_capacity(len);
1213 v.push([i, 0, 0, 0]);
1215 v[..].rotate_right(s);
1216 for i in 0..v.len() {
1217 assert_eq!(v[i][0], v.len().wrapping_add(i.wrapping_sub(s)) % v.len());
1224 #[cfg(not(target_arch = "wasm32"))]
1225 fn sort_unstable() {
1226 use core::cmp::Ordering::{Equal, Greater, Less};
1227 use core::slice::heapsort;
1228 use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
1230 #[cfg(not(miri))] // Miri is too slow
1231 let large_range = 500..510;
1232 #[cfg(not(miri))] // Miri is too slow
1236 let large_range = 0..0; // empty range
1240 let mut v = [0; 600];
1241 let mut tmp = [0; 600];
1242 let mut rng = StdRng::from_entropy();
1244 for len in (2..25).chain(large_range) {
1245 let v = &mut v[0..len];
1246 let tmp = &mut tmp[0..len];
1248 for &modulus in &[5, 10, 100, 1000] {
1249 for _ in 0..rounds {
1251 v[i] = rng.gen::<i32>() % modulus;
1254 // Sort in default order.
1255 tmp.copy_from_slice(v);
1256 tmp.sort_unstable();
1257 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1259 // Sort in ascending order.
1260 tmp.copy_from_slice(v);
1261 tmp.sort_unstable_by(|a, b| a.cmp(b));
1262 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1264 // Sort in descending order.
1265 tmp.copy_from_slice(v);
1266 tmp.sort_unstable_by(|a, b| b.cmp(a));
1267 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1269 // Test heapsort using `<` operator.
1270 tmp.copy_from_slice(v);
1271 heapsort(tmp, |a, b| a < b);
1272 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1274 // Test heapsort using `>` operator.
1275 tmp.copy_from_slice(v);
1276 heapsort(tmp, |a, b| a > b);
1277 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1282 // Sort using a completely random comparison function.
1283 // This will reorder the elements *somehow*, but won't panic.
1284 for i in 0..v.len() {
1287 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1289 for i in 0..v.len() {
1290 assert_eq!(v[i], i as i32);
1293 // Should not panic.
1294 [0i32; 0].sort_unstable();
1295 [(); 10].sort_unstable();
1296 [(); 100].sort_unstable();
1298 let mut v = [0xDEADBEEFu64];
1300 assert!(v == [0xDEADBEEF]);
1304 #[cfg(not(target_arch = "wasm32"))]
1305 #[cfg_attr(miri, ignore)] // Miri is too slow
1306 fn partition_at_index() {
1307 use core::cmp::Ordering::{Equal, Greater, Less};
1308 use rand::rngs::StdRng;
1309 use rand::seq::SliceRandom;
1310 use rand::{Rng, SeedableRng};
1312 let mut rng = StdRng::from_entropy();
1314 for len in (2..21).chain(500..501) {
1315 let mut orig = vec![0; len];
1317 for &modulus in &[5, 10, 1000] {
1320 orig[i] = rng.gen::<i32>() % modulus;
1324 let mut v = orig.clone();
1329 // Sort in default order.
1330 for pivot in 0..len {
1331 let mut v = orig.clone();
1332 v.partition_at_index(pivot);
1334 assert_eq!(v_sorted[pivot], v[pivot]);
1336 for j in pivot..len {
1337 assert!(v[i] <= v[j]);
1342 // Sort in ascending order.
1343 for pivot in 0..len {
1344 let mut v = orig.clone();
1345 let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
1347 assert_eq!(left.len() + right.len(), len - 1);
1350 assert!(l <= pivot);
1351 for r in right.iter_mut() {
1353 assert!(pivot <= r);
1358 // Sort in descending order.
1359 let sort_descending_comparator = |a: &i32, b: &i32| b.cmp(a);
1360 let v_sorted_descending = {
1361 let mut v = orig.clone();
1362 v.sort_by(sort_descending_comparator);
1366 for pivot in 0..len {
1367 let mut v = orig.clone();
1368 v.partition_at_index_by(pivot, sort_descending_comparator);
1370 assert_eq!(v_sorted_descending[pivot], v[pivot]);
1372 for j in pivot..len {
1373 assert!(v[j] <= v[i]);
1381 // Sort at index using a completely random comparison function.
1382 // This will reorder the elements *somehow*, but won't panic.
1383 let mut v = [0; 500];
1384 for i in 0..v.len() {
1388 for pivot in 0..v.len() {
1389 v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1391 for i in 0..v.len() {
1392 assert_eq!(v[i], i as i32);
1396 // Should not panic.
1397 [(); 10].partition_at_index(0);
1398 [(); 10].partition_at_index(5);
1399 [(); 10].partition_at_index(9);
1400 [(); 100].partition_at_index(0);
1401 [(); 100].partition_at_index(50);
1402 [(); 100].partition_at_index(99);
1404 let mut v = [0xDEADBEEFu64];
1405 v.partition_at_index(0);
1406 assert!(v == [0xDEADBEEF]);
1410 #[should_panic(expected = "index 0 greater than length of slice")]
1411 fn partition_at_index_zero_length() {
1412 [0i32; 0].partition_at_index(0);
1416 #[should_panic(expected = "index 20 greater than length of slice")]
1417 fn partition_at_index_past_length() {
1418 [0i32; 10].partition_at_index(20);
1422 use core::slice::memchr::{memchr, memrchr};
1424 // test fallback implementations on all platforms
1427 assert_eq!(Some(0), memchr(b'a', b"a"));
1431 fn matches_begin() {
1432 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1437 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1442 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1446 fn matches_past_nul() {
1447 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1451 fn no_match_empty() {
1452 assert_eq!(None, memchr(b'a', b""));
1457 assert_eq!(None, memchr(b'a', b"xyz"));
1461 fn matches_one_reversed() {
1462 assert_eq!(Some(0), memrchr(b'a', b"a"));
1466 fn matches_begin_reversed() {
1467 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1471 fn matches_end_reversed() {
1472 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1476 fn matches_nul_reversed() {
1477 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1481 fn matches_past_nul_reversed() {
1482 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1486 fn no_match_empty_reversed() {
1487 assert_eq!(None, memrchr(b'a', b""));
1491 fn no_match_reversed() {
1492 assert_eq!(None, memrchr(b'a', b"xyz"));
1496 fn each_alignment_reversed() {
1497 let mut data = [1u8; 64];
1501 for start in 0..16 {
1502 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1508 #[cfg_attr(miri, ignore)] // Miri does not compute a maximal `mid` for `align_offset`
1509 fn test_align_to_simple() {
1510 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1511 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1512 assert_eq!(aligned.len(), 3);
1513 assert!(prefix == [1] || suffix == [7]);
1514 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1515 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1516 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1517 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1519 aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1520 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1530 fn test_align_to_zst() {
1531 let bytes = [1, 2, 3, 4, 5, 6, 7];
1532 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1533 assert_eq!(aligned.len(), 0);
1534 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1538 #[cfg_attr(miri, ignore)] // Miri does not compute a maximal `mid` for `align_offset`
1539 fn test_align_to_non_trivial() {
1541 struct U64(u64, u64);
1543 struct U64U64U32(u64, u64, u32);
1554 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1555 assert_eq!(aligned.len(), 4);
1556 assert_eq!(prefix.len() + suffix.len(), 2);
1560 fn test_align_to_empty_mid() {
1563 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1564 // overall slice is too short to contain an aligned address.
1565 let bytes = [1, 2, 3, 4, 5, 6, 7];
1567 for offset in 0..4 {
1568 let (_, mid, _) = unsafe { bytes[offset..offset + 1].align_to::<Chunk>() };
1569 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1574 fn test_slice_partition_dedup_by() {
1575 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1577 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1579 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1580 assert_eq!(duplicates, [5, -1, 2]);
1584 fn test_slice_partition_dedup_empty() {
1585 let mut slice: [i32; 0] = [];
1587 let (dedup, duplicates) = slice.partition_dedup();
1589 assert_eq!(dedup, []);
1590 assert_eq!(duplicates, []);
1594 fn test_slice_partition_dedup_one() {
1595 let mut slice = [12];
1597 let (dedup, duplicates) = slice.partition_dedup();
1599 assert_eq!(dedup, [12]);
1600 assert_eq!(duplicates, []);
1604 fn test_slice_partition_dedup_multiple_ident() {
1605 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1607 let (dedup, duplicates) = slice.partition_dedup();
1609 assert_eq!(dedup, [12, 11]);
1610 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1614 fn test_slice_partition_dedup_partialeq() {
1616 struct Foo(i32, i32);
1618 impl PartialEq for Foo {
1619 fn eq(&self, other: &Foo) -> bool {
1624 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1626 let (dedup, duplicates) = slice.partition_dedup();
1628 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1629 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1633 fn test_copy_within() {
1634 // Start to end, with a RangeTo.
1635 let mut bytes = *b"Hello, World!";
1636 bytes.copy_within(..3, 10);
1637 assert_eq!(&bytes, b"Hello, WorHel");
1639 // End to start, with a RangeFrom.
1640 let mut bytes = *b"Hello, World!";
1641 bytes.copy_within(10.., 0);
1642 assert_eq!(&bytes, b"ld!lo, World!");
1644 // Overlapping, with a RangeInclusive.
1645 let mut bytes = *b"Hello, World!";
1646 bytes.copy_within(0..=11, 1);
1647 assert_eq!(&bytes, b"HHello, World");
1649 // Whole slice, with a RangeFull.
1650 let mut bytes = *b"Hello, World!";
1651 bytes.copy_within(.., 0);
1652 assert_eq!(&bytes, b"Hello, World!");
1654 // Ensure that copying at the end of slice won't cause UB.
1655 let mut bytes = *b"Hello, World!";
1656 bytes.copy_within(13..13, 5);
1657 assert_eq!(&bytes, b"Hello, World!");
1658 bytes.copy_within(5..5, 13);
1659 assert_eq!(&bytes, b"Hello, World!");
1663 #[should_panic(expected = "src is out of bounds")]
1664 fn test_copy_within_panics_src_too_long() {
1665 let mut bytes = *b"Hello, World!";
1666 // The length is only 13, so 14 is out of bounds.
1667 bytes.copy_within(10..14, 0);
1671 #[should_panic(expected = "dest is out of bounds")]
1672 fn test_copy_within_panics_dest_too_long() {
1673 let mut bytes = *b"Hello, World!";
1674 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1675 bytes.copy_within(0..4, 10);
1678 #[should_panic(expected = "src end is before src start")]
1679 fn test_copy_within_panics_src_inverted() {
1680 let mut bytes = *b"Hello, World!";
1681 // 2 is greater than 1, so this range is invalid.
1682 bytes.copy_within(2..1, 0);
1685 #[should_panic(expected = "attempted to index slice up to maximum usize")]
1686 fn test_copy_within_panics_src_out_of_bounds() {
1687 let mut bytes = *b"Hello, World!";
1688 // an inclusive range ending at usize::max_value() would make src_end overflow
1689 bytes.copy_within(usize::max_value()..=usize::max_value(), 0);
1693 fn test_is_sorted() {
1694 let empty: [i32; 0] = [];
1696 assert!([1, 2, 2, 9].is_sorted());
1697 assert!(![1, 3, 2].is_sorted());
1698 assert!([0].is_sorted());
1699 assert!(empty.is_sorted());
1700 assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
1701 assert!([-2, -1, 0, 3].is_sorted());
1702 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1703 assert!(!["c", "bb", "aaa"].is_sorted());
1704 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));