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_last() {
93 let v: &[_] = &[0, 1, 2, 3, 4];
94 assert_eq!(v.iter().last().unwrap(), &4);
95 assert_eq!(v[..1].iter().last().unwrap(), &0);
99 fn test_iterator_count() {
100 let v: &[_] = &[0, 1, 2, 3, 4];
101 assert_eq!(v.iter().count(), 5);
103 let mut iter2 = v.iter();
106 assert_eq!(iter2.count(), 3);
110 fn test_chunks_count() {
111 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
113 assert_eq!(c.count(), 2);
115 let v2: &[i32] = &[0, 1, 2, 3, 4];
116 let c2 = v2.chunks(2);
117 assert_eq!(c2.count(), 3);
119 let v3: &[i32] = &[];
120 let c3 = v3.chunks(2);
121 assert_eq!(c3.count(), 0);
125 fn test_chunks_nth() {
126 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
127 let mut c = v.chunks(2);
128 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
129 assert_eq!(c.next().unwrap(), &[4, 5]);
131 let v2: &[i32] = &[0, 1, 2, 3, 4];
132 let mut c2 = v2.chunks(3);
133 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
134 assert_eq!(c2.next(), None);
138 fn test_chunks_nth_back() {
139 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
140 let mut c = v.chunks(2);
141 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
142 assert_eq!(c.next().unwrap(), &[0, 1]);
143 assert_eq!(c.next(), None);
145 let v2: &[i32] = &[0, 1, 2, 3, 4];
146 let mut c2 = v2.chunks(3);
147 assert_eq!(c2.nth_back(1).unwrap(), &[0, 1, 2]);
148 assert_eq!(c2.next(), None);
149 assert_eq!(c2.next_back(), None);
151 let v3: &[i32] = &[0, 1, 2, 3, 4];
152 let mut c3 = v3.chunks(10);
153 assert_eq!(c3.nth_back(0).unwrap(), &[0, 1, 2, 3, 4]);
154 assert_eq!(c3.next(), None);
156 let v4: &[i32] = &[0, 1, 2];
157 let mut c4 = v4.chunks(10);
158 assert_eq!(c4.nth_back(1_000_000_000usize), None);
162 fn test_chunks_last() {
163 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
165 assert_eq!(c.last().unwrap()[1], 5);
167 let v2: &[i32] = &[0, 1, 2, 3, 4];
168 let c2 = v2.chunks(2);
169 assert_eq!(c2.last().unwrap()[0], 4);
173 fn test_chunks_zip() {
174 let v1: &[i32] = &[0, 1, 2, 3, 4];
175 let v2: &[i32] = &[6, 7, 8, 9, 10];
177 let res = v1.chunks(2)
179 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
180 .collect::<Vec<_>>();
181 assert_eq!(res, vec![14, 22, 14]);
185 fn test_chunks_mut_count() {
186 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
187 let c = v.chunks_mut(3);
188 assert_eq!(c.count(), 2);
190 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
191 let c2 = v2.chunks_mut(2);
192 assert_eq!(c2.count(), 3);
194 let v3: &mut [i32] = &mut [];
195 let c3 = v3.chunks_mut(2);
196 assert_eq!(c3.count(), 0);
200 fn test_chunks_mut_nth() {
201 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
202 let mut c = v.chunks_mut(2);
203 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
204 assert_eq!(c.next().unwrap(), &[4, 5]);
206 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
207 let mut c2 = v2.chunks_mut(3);
208 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
209 assert_eq!(c2.next(), None);
213 fn test_chunks_mut_last() {
214 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
215 let c = v.chunks_mut(2);
216 assert_eq!(c.last().unwrap(), &[4, 5]);
218 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
219 let c2 = v2.chunks_mut(2);
220 assert_eq!(c2.last().unwrap(), &[4]);
224 fn test_chunks_mut_zip() {
225 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
226 let v2: &[i32] = &[6, 7, 8, 9, 10];
228 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
229 let sum = b.iter().sum::<i32>();
234 assert_eq!(v1, [13, 14, 19, 20, 14]);
238 fn test_chunks_exact_count() {
239 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
240 let c = v.chunks_exact(3);
241 assert_eq!(c.count(), 2);
243 let v2: &[i32] = &[0, 1, 2, 3, 4];
244 let c2 = v2.chunks_exact(2);
245 assert_eq!(c2.count(), 2);
247 let v3: &[i32] = &[];
248 let c3 = v3.chunks_exact(2);
249 assert_eq!(c3.count(), 0);
253 fn test_chunks_exact_nth() {
254 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
255 let mut c = v.chunks_exact(2);
256 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
257 assert_eq!(c.next().unwrap(), &[4, 5]);
259 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
260 let mut c2 = v2.chunks_exact(3);
261 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
262 assert_eq!(c2.next(), None);
266 fn test_chunks_exact_last() {
267 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
268 let c = v.chunks_exact(2);
269 assert_eq!(c.last().unwrap(), &[4, 5]);
271 let v2: &[i32] = &[0, 1, 2, 3, 4];
272 let c2 = v2.chunks_exact(2);
273 assert_eq!(c2.last().unwrap(), &[2, 3]);
277 fn test_chunks_exact_remainder() {
278 let v: &[i32] = &[0, 1, 2, 3, 4];
279 let c = v.chunks_exact(2);
280 assert_eq!(c.remainder(), &[4]);
284 fn test_chunks_exact_zip() {
285 let v1: &[i32] = &[0, 1, 2, 3, 4];
286 let v2: &[i32] = &[6, 7, 8, 9, 10];
288 let res = v1.chunks_exact(2)
289 .zip(v2.chunks_exact(2))
290 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
291 .collect::<Vec<_>>();
292 assert_eq!(res, vec![14, 22]);
296 fn test_chunks_exact_mut_count() {
297 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
298 let c = v.chunks_exact_mut(3);
299 assert_eq!(c.count(), 2);
301 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
302 let c2 = v2.chunks_exact_mut(2);
303 assert_eq!(c2.count(), 2);
305 let v3: &mut [i32] = &mut [];
306 let c3 = v3.chunks_exact_mut(2);
307 assert_eq!(c3.count(), 0);
311 fn test_chunks_exact_mut_nth() {
312 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
313 let mut c = v.chunks_exact_mut(2);
314 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
315 assert_eq!(c.next().unwrap(), &[4, 5]);
317 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
318 let mut c2 = v2.chunks_exact_mut(3);
319 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
320 assert_eq!(c2.next(), None);
324 fn test_chunks_exact_mut_last() {
325 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
326 let c = v.chunks_exact_mut(2);
327 assert_eq!(c.last().unwrap(), &[4, 5]);
329 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
330 let c2 = v2.chunks_exact_mut(2);
331 assert_eq!(c2.last().unwrap(), &[2, 3]);
335 fn test_chunks_exact_mut_remainder() {
336 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
337 let c = v.chunks_exact_mut(2);
338 assert_eq!(c.into_remainder(), &[4]);
342 fn test_chunks_exact_mut_zip() {
343 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
344 let v2: &[i32] = &[6, 7, 8, 9, 10];
346 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
347 let sum = b.iter().sum::<i32>();
352 assert_eq!(v1, [13, 14, 19, 20, 4]);
356 fn test_rchunks_count() {
357 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
358 let c = v.rchunks(3);
359 assert_eq!(c.count(), 2);
361 let v2: &[i32] = &[0, 1, 2, 3, 4];
362 let c2 = v2.rchunks(2);
363 assert_eq!(c2.count(), 3);
365 let v3: &[i32] = &[];
366 let c3 = v3.rchunks(2);
367 assert_eq!(c3.count(), 0);
371 fn test_rchunks_nth() {
372 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
373 let mut c = v.rchunks(2);
374 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
375 assert_eq!(c.next().unwrap(), &[0, 1]);
377 let v2: &[i32] = &[0, 1, 2, 3, 4];
378 let mut c2 = v2.rchunks(3);
379 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
380 assert_eq!(c2.next(), None);
384 fn test_rchunks_nth_back() {
385 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
386 let mut c = v.rchunks(2);
387 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
388 assert_eq!(c.next_back().unwrap(), &[4, 5]);
390 let v2: &[i32] = &[0, 1, 2, 3, 4];
391 let mut c2 = v2.rchunks(3);
392 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
393 assert_eq!(c2.next_back(), None);
397 fn test_rchunks_last() {
398 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
399 let c = v.rchunks(2);
400 assert_eq!(c.last().unwrap()[1], 1);
402 let v2: &[i32] = &[0, 1, 2, 3, 4];
403 let c2 = v2.rchunks(2);
404 assert_eq!(c2.last().unwrap()[0], 0);
408 fn test_rchunks_zip() {
409 let v1: &[i32] = &[0, 1, 2, 3, 4];
410 let v2: &[i32] = &[6, 7, 8, 9, 10];
412 let res = v1.rchunks(2)
414 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
415 .collect::<Vec<_>>();
416 assert_eq!(res, vec![26, 18, 6]);
420 fn test_rchunks_mut_count() {
421 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
422 let c = v.rchunks_mut(3);
423 assert_eq!(c.count(), 2);
425 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
426 let c2 = v2.rchunks_mut(2);
427 assert_eq!(c2.count(), 3);
429 let v3: &mut [i32] = &mut [];
430 let c3 = v3.rchunks_mut(2);
431 assert_eq!(c3.count(), 0);
435 fn test_rchunks_mut_nth() {
436 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
437 let mut c = v.rchunks_mut(2);
438 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
439 assert_eq!(c.next().unwrap(), &[0, 1]);
441 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
442 let mut c2 = v2.rchunks_mut(3);
443 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
444 assert_eq!(c2.next(), None);
448 fn test_rchunks_mut_nth_back() {
449 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
450 let mut c = v.rchunks_mut(2);
451 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
452 assert_eq!(c.next_back().unwrap(), &[4, 5]);
454 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
455 let mut c2 = v2.rchunks_mut(3);
456 assert_eq!(c2.nth_back(1).unwrap(), &[2, 3, 4]);
457 assert_eq!(c2.next_back(), None);
461 fn test_rchunks_mut_last() {
462 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
463 let c = v.rchunks_mut(2);
464 assert_eq!(c.last().unwrap(), &[0, 1]);
466 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
467 let c2 = v2.rchunks_mut(2);
468 assert_eq!(c2.last().unwrap(), &[0]);
472 fn test_rchunks_mut_zip() {
473 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
474 let v2: &[i32] = &[6, 7, 8, 9, 10];
476 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
477 let sum = b.iter().sum::<i32>();
482 assert_eq!(v1, [6, 16, 17, 22, 23]);
486 fn test_rchunks_exact_count() {
487 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
488 let c = v.rchunks_exact(3);
489 assert_eq!(c.count(), 2);
491 let v2: &[i32] = &[0, 1, 2, 3, 4];
492 let c2 = v2.rchunks_exact(2);
493 assert_eq!(c2.count(), 2);
495 let v3: &[i32] = &[];
496 let c3 = v3.rchunks_exact(2);
497 assert_eq!(c3.count(), 0);
501 fn test_rchunks_exact_nth() {
502 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
503 let mut c = v.rchunks_exact(2);
504 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
505 assert_eq!(c.next().unwrap(), &[0, 1]);
507 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
508 let mut c2 = v2.rchunks_exact(3);
509 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
510 assert_eq!(c2.next(), None);
514 fn test_rchunks_exact_nth_back() {
515 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
516 let mut c = v.rchunks_exact(2);
517 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
518 assert_eq!(c.next_back().unwrap(), &[4, 5]);
520 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
521 let mut c2 = v2.rchunks_exact(3);
522 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
523 assert_eq!(c2.next(), None);
527 fn test_rchunks_exact_last() {
528 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
529 let c = v.rchunks_exact(2);
530 assert_eq!(c.last().unwrap(), &[0, 1]);
532 let v2: &[i32] = &[0, 1, 2, 3, 4];
533 let c2 = v2.rchunks_exact(2);
534 assert_eq!(c2.last().unwrap(), &[1, 2]);
538 fn test_rchunks_exact_remainder() {
539 let v: &[i32] = &[0, 1, 2, 3, 4];
540 let c = v.rchunks_exact(2);
541 assert_eq!(c.remainder(), &[0]);
545 fn test_rchunks_exact_zip() {
546 let v1: &[i32] = &[0, 1, 2, 3, 4];
547 let v2: &[i32] = &[6, 7, 8, 9, 10];
549 let res = v1.rchunks_exact(2)
550 .zip(v2.rchunks_exact(2))
551 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
552 .collect::<Vec<_>>();
553 assert_eq!(res, vec![26, 18]);
557 fn test_rchunks_exact_mut_count() {
558 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
559 let c = v.rchunks_exact_mut(3);
560 assert_eq!(c.count(), 2);
562 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
563 let c2 = v2.rchunks_exact_mut(2);
564 assert_eq!(c2.count(), 2);
566 let v3: &mut [i32] = &mut [];
567 let c3 = v3.rchunks_exact_mut(2);
568 assert_eq!(c3.count(), 0);
572 fn test_rchunks_exact_mut_nth() {
573 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
574 let mut c = v.rchunks_exact_mut(2);
575 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
576 assert_eq!(c.next().unwrap(), &[0, 1]);
578 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
579 let mut c2 = v2.rchunks_exact_mut(3);
580 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
581 assert_eq!(c2.next(), None);
585 fn test_rchunks_exact_mut_nth_back() {
586 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
587 let mut c = v.rchunks_exact_mut(2);
588 assert_eq!(c.nth_back(1).unwrap(), &[2, 3]);
589 assert_eq!(c.next_back().unwrap(), &[4, 5]);
591 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
592 let mut c2 = v2.rchunks_exact_mut(3);
593 assert_eq!(c2.nth_back(1).unwrap(), &[4, 5, 6]);
594 assert_eq!(c2.next(), None);
598 fn test_rchunks_exact_mut_last() {
599 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
600 let c = v.rchunks_exact_mut(2);
601 assert_eq!(c.last().unwrap(), &[0, 1]);
603 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
604 let c2 = v2.rchunks_exact_mut(2);
605 assert_eq!(c2.last().unwrap(), &[1, 2]);
609 fn test_rchunks_exact_mut_remainder() {
610 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
611 let c = v.rchunks_exact_mut(2);
612 assert_eq!(c.into_remainder(), &[0]);
616 fn test_rchunks_exact_mut_zip() {
617 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
618 let v2: &[i32] = &[6, 7, 8, 9, 10];
620 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
621 let sum = b.iter().sum::<i32>();
626 assert_eq!(v1, [0, 16, 17, 22, 23]);
630 fn test_windows_count() {
631 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
632 let c = v.windows(3);
633 assert_eq!(c.count(), 4);
635 let v2: &[i32] = &[0, 1, 2, 3, 4];
636 let c2 = v2.windows(6);
637 assert_eq!(c2.count(), 0);
639 let v3: &[i32] = &[];
640 let c3 = v3.windows(2);
641 assert_eq!(c3.count(), 0);
645 fn test_windows_nth() {
646 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
647 let mut c = v.windows(2);
648 assert_eq!(c.nth(2).unwrap()[1], 3);
649 assert_eq!(c.next().unwrap()[0], 3);
651 let v2: &[i32] = &[0, 1, 2, 3, 4];
652 let mut c2 = v2.windows(4);
653 assert_eq!(c2.nth(1).unwrap()[1], 2);
654 assert_eq!(c2.next(), None);
658 fn test_windows_nth_back() {
659 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
660 let mut c = v.windows(2);
661 assert_eq!(c.nth_back(2).unwrap()[0], 2);
662 assert_eq!(c.next_back().unwrap()[1], 2);
664 let v2: &[i32] = &[0, 1, 2, 3, 4];
665 let mut c2 = v2.windows(4);
666 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
667 assert_eq!(c2.next_back(), None);
671 fn test_windows_last() {
672 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
673 let c = v.windows(2);
674 assert_eq!(c.last().unwrap()[1], 5);
676 let v2: &[i32] = &[0, 1, 2, 3, 4];
677 let c2 = v2.windows(2);
678 assert_eq!(c2.last().unwrap()[0], 3);
682 fn test_windows_zip() {
683 let v1: &[i32] = &[0, 1, 2, 3, 4];
684 let v2: &[i32] = &[6, 7, 8, 9, 10];
686 let res = v1.windows(2)
688 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
689 .collect::<Vec<_>>();
691 assert_eq!(res, [14, 18, 22, 26]);
696 fn test_iter_ref_consistency() {
699 fn test<T : Copy + Debug + PartialEq>(x : T) {
700 let v : &[T] = &[x, x, x];
701 let v_ptrs : [*const T; 3] = match v {
702 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
709 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
710 let nth = v.iter().nth(i).unwrap();
711 assert_eq!(nth as *const _, v_ptrs[i]);
713 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
715 // stepping through with nth(0)
717 let mut it = v.iter();
719 let next = it.nth(0).unwrap();
720 assert_eq!(next as *const _, v_ptrs[i]);
722 assert_eq!(it.nth(0), None);
727 let mut it = v.iter();
729 let remaining = len - i;
730 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
732 let next = it.next().unwrap();
733 assert_eq!(next as *const _, v_ptrs[i]);
735 assert_eq!(it.size_hint(), (0, Some(0)));
736 assert_eq!(it.next(), None, "The final call to next() should return None");
741 let mut it = v.iter();
743 let remaining = len - i;
744 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
746 let prev = it.next_back().unwrap();
747 assert_eq!(prev as *const _, v_ptrs[remaining-1]);
749 assert_eq!(it.size_hint(), (0, Some(0)));
750 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
754 fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
755 let v : &mut [T] = &mut [x, x, x];
756 let v_ptrs : [*mut T; 3] = match v {
757 [ref v1, ref v2, ref v3] =>
758 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _],
765 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
766 let nth = v.iter_mut().nth(i).unwrap();
767 assert_eq!(nth as *mut _, v_ptrs[i]);
769 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
771 // stepping through with nth(0)
773 let mut it = v.iter();
775 let next = it.nth(0).unwrap();
776 assert_eq!(next as *const _, v_ptrs[i]);
778 assert_eq!(it.nth(0), None);
783 let mut it = v.iter_mut();
785 let remaining = len - i;
786 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
788 let next = it.next().unwrap();
789 assert_eq!(next as *mut _, v_ptrs[i]);
791 assert_eq!(it.size_hint(), (0, Some(0)));
792 assert_eq!(it.next(), None, "The final call to next() should return None");
797 let mut it = v.iter_mut();
799 let remaining = len - i;
800 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
802 let prev = it.next_back().unwrap();
803 assert_eq!(prev as *mut _, v_ptrs[remaining-1]);
805 assert_eq!(it.size_hint(), (0, Some(0)));
806 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
810 // Make sure iterators and slice patterns yield consistent addresses for various types,
814 test([0u32; 0]); // ZST with alignment > 0
817 test_mut([0u32; 0]); // ZST with alignment > 0
820 // The current implementation of SliceIndex fails to handle methods
821 // orthogonally from range types; therefore, it is worth testing
822 // all of the indexing operations on each input.
824 // This checks all six indexing methods, given an input range that
825 // should succeed. (it is NOT suitable for testing invalid inputs)
826 macro_rules! assert_range_eq {
827 ($arr:expr, $range:expr, $expected:expr)
830 let mut expected = $expected;
833 let expected: &[_] = &expected;
835 assert_eq!(&s[$range], expected, "(in assertion for: index)");
836 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
839 s.get_unchecked($range), expected,
840 "(in assertion for: get_unchecked)",
845 let s: &mut [_] = &mut arr;
846 let expected: &mut [_] = &mut expected;
849 &mut s[$range], expected,
850 "(in assertion for: index_mut)",
853 s.get_mut($range), Some(&mut expected[..]),
854 "(in assertion for: get_mut)",
858 s.get_unchecked_mut($range), expected,
859 "(in assertion for: get_unchecked_mut)",
866 // Make sure the macro can actually detect bugs,
867 // because if it can't, then what are we even doing here?
869 // (Be aware this only demonstrates the ability to detect bugs
870 // in the FIRST method that panics, as the macro is not designed
871 // to be used in `should_panic`)
873 #[should_panic(expected = "out of range")]
874 fn assert_range_eq_can_fail_by_panic() {
875 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
878 // (Be aware this only demonstrates the ability to detect bugs
879 // in the FIRST method it calls, as the macro is not designed
880 // to be used in `should_panic`)
882 #[should_panic(expected = "==")]
883 fn assert_range_eq_can_fail_by_inequality() {
884 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
887 // Test cases for bad index operations.
889 // This generates `should_panic` test cases for Index/IndexMut
890 // and `None` test cases for get/get_mut.
891 macro_rules! panic_cases {
893 // each test case needs a unique name to namespace the tests
894 in mod $case_name:ident {
899 // one or more similar inputs for which data[input] succeeds,
900 // and the corresponding output as an array. This helps validate
901 // "critical points" where an input range straddles the boundary
902 // between valid and invalid.
903 // (such as the input `len..len`, which is just barely valid)
905 good: data[$good:expr] == $output:expr;
908 bad: data[$bad:expr];
909 message: $expect_msg:expr;
917 $( assert_range_eq!($data, $good, $output); )*
921 assert_eq!(v.get($bad), None, "(in None assertion for get)");
925 let v: &mut [_] = &mut v;
926 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
931 #[should_panic(expected = $expect_msg)]
939 #[should_panic(expected = $expect_msg)]
940 fn index_mut_fail() {
942 let v: &mut [_] = &mut v;
943 let _v = &mut v[$bad];
951 let v = [0, 1, 2, 3, 4, 5];
953 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
954 assert_range_eq!(v, ..2, [0, 1]);
955 assert_range_eq!(v, ..=1, [0, 1]);
956 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
957 assert_range_eq!(v, 1..4, [1, 2, 3]);
958 assert_range_eq!(v, 1..=3, [1, 2, 3]);
962 in mod rangefrom_len {
963 data: [0, 1, 2, 3, 4, 5];
965 good: data[6..] == [];
967 message: "but ends at"; // perhaps not ideal
971 data: [0, 1, 2, 3, 4, 5];
973 good: data[..6] == [0, 1, 2, 3, 4, 5];
975 message: "out of range";
978 in mod rangetoinclusive_len {
979 data: [0, 1, 2, 3, 4, 5];
981 good: data[..=5] == [0, 1, 2, 3, 4, 5];
983 message: "out of range";
986 in mod range_len_len {
987 data: [0, 1, 2, 3, 4, 5];
989 good: data[6..6] == [];
991 message: "out of range";
994 in mod rangeinclusive_len_len {
995 data: [0, 1, 2, 3, 4, 5];
997 good: data[6..=5] == [];
999 message: "out of range";
1004 in mod range_neg_width {
1005 data: [0, 1, 2, 3, 4, 5];
1007 good: data[4..4] == [];
1009 message: "but ends at";
1012 in mod rangeinclusive_neg_width {
1013 data: [0, 1, 2, 3, 4, 5];
1015 good: data[4..=3] == [];
1017 message: "but ends at";
1022 in mod rangeinclusive_overflow {
1025 // note: using 0 specifically ensures that the result of overflowing is 0..0,
1026 // so that `get` doesn't simply return None for the wrong reason.
1027 bad: data[0 ..= ::std::usize::MAX];
1028 message: "maximum usize";
1031 in mod rangetoinclusive_overflow {
1034 bad: data[..= ::std::usize::MAX];
1035 message: "maximum usize";
1041 fn test_find_rfind() {
1042 let v = [0, 1, 2, 3, 4, 5];
1043 let mut iter = v.iter();
1044 let mut i = v.len();
1045 while let Some(&elt) = iter.rfind(|_| true) {
1047 assert_eq!(elt, v[i]);
1050 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
1054 fn test_iter_folds() {
1055 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
1056 assert_eq!(a.iter().fold(0, |acc, &x| 2*acc + x), 57);
1057 assert_eq!(a.iter().rfold(0, |acc, &x| 2*acc + x), 129);
1058 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
1059 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
1060 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
1062 // short-circuiting try_fold, through other methods
1063 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
1064 let mut iter = a.iter();
1065 assert_eq!(iter.position(|&x| x == 3), Some(3));
1066 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
1067 assert_eq!(iter.len(), 2);
1071 fn test_rotate_left() {
1072 const N: usize = 600;
1073 let a: &mut [_] = &mut [0; N];
1082 assert_eq!(a[(i + k) % N], i);
1087 fn test_rotate_right() {
1088 const N: usize = 600;
1089 let a: &mut [_] = &mut [0; N];
1097 assert_eq!(a[(i + 42) % N], i);
1102 #[cfg(not(target_arch = "wasm32"))]
1103 fn sort_unstable() {
1104 use core::cmp::Ordering::{Equal, Greater, Less};
1105 use core::slice::heapsort;
1106 use rand::{FromEntropy, Rng, rngs::SmallRng, seq::SliceRandom};
1108 #[cfg(not(miri))] // Miri is too slow
1109 let large_range = 500..510;
1110 #[cfg(not(miri))] // Miri is too slow
1114 let large_range = 0..0; // empty range
1118 let mut v = [0; 600];
1119 let mut tmp = [0; 600];
1120 let mut rng = SmallRng::from_entropy();
1122 for len in (2..25).chain(large_range) {
1123 let v = &mut v[0..len];
1124 let tmp = &mut tmp[0..len];
1126 for &modulus in &[5, 10, 100, 1000] {
1127 for _ in 0..rounds {
1129 v[i] = rng.gen::<i32>() % modulus;
1132 // Sort in default order.
1133 tmp.copy_from_slice(v);
1134 tmp.sort_unstable();
1135 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1137 // Sort in ascending order.
1138 tmp.copy_from_slice(v);
1139 tmp.sort_unstable_by(|a, b| a.cmp(b));
1140 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1142 // Sort in descending order.
1143 tmp.copy_from_slice(v);
1144 tmp.sort_unstable_by(|a, b| b.cmp(a));
1145 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1147 // Test heapsort using `<` operator.
1148 tmp.copy_from_slice(v);
1149 heapsort(tmp, |a, b| a < b);
1150 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1152 // Test heapsort using `>` operator.
1153 tmp.copy_from_slice(v);
1154 heapsort(tmp, |a, b| a > b);
1155 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1160 // Sort using a completely random comparison function.
1161 // This will reorder the elements *somehow*, but won't panic.
1162 for i in 0..v.len() {
1165 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1167 for i in 0..v.len() {
1168 assert_eq!(v[i], i as i32);
1171 // Should not panic.
1172 [0i32; 0].sort_unstable();
1173 [(); 10].sort_unstable();
1174 [(); 100].sort_unstable();
1176 let mut v = [0xDEADBEEFu64];
1178 assert!(v == [0xDEADBEEF]);
1182 #[cfg(not(target_arch = "wasm32"))]
1183 #[cfg(not(miri))] // Miri is too slow
1184 fn partition_at_index() {
1185 use core::cmp::Ordering::{Equal, Greater, Less};
1186 use rand::rngs::SmallRng;
1187 use rand::seq::SliceRandom;
1188 use rand::{FromEntropy, Rng};
1190 let mut rng = SmallRng::from_entropy();
1192 for len in (2..21).chain(500..501) {
1193 let mut orig = vec![0; len];
1195 for &modulus in &[5, 10, 1000] {
1198 orig[i] = rng.gen::<i32>() % modulus;
1202 let mut v = orig.clone();
1207 // Sort in default order.
1208 for pivot in 0..len {
1209 let mut v = orig.clone();
1210 v.partition_at_index(pivot);
1212 assert_eq!(v_sorted[pivot], v[pivot]);
1214 for j in pivot..len {
1215 assert!(v[i] <= v[j]);
1220 // Sort in ascending order.
1221 for pivot in 0..len {
1222 let mut v = orig.clone();
1223 let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
1225 assert_eq!(left.len() + right.len(), len - 1);
1228 assert!(l <= pivot);
1229 for r in right.iter_mut() {
1231 assert!(pivot <= r);
1236 // Sort in descending order.
1237 let sort_descending_comparator = |a: &i32, b: &i32| b.cmp(a);
1238 let v_sorted_descending = {
1239 let mut v = orig.clone();
1240 v.sort_by(sort_descending_comparator);
1244 for pivot in 0..len {
1245 let mut v = orig.clone();
1246 v.partition_at_index_by(pivot, sort_descending_comparator);
1248 assert_eq!(v_sorted_descending[pivot], v[pivot]);
1250 for j in pivot..len {
1251 assert!(v[j] <= v[i]);
1259 // Sort at index using a completely random comparison function.
1260 // This will reorder the elements *somehow*, but won't panic.
1261 let mut v = [0; 500];
1262 for i in 0..v.len() {
1266 for pivot in 0..v.len() {
1267 v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1269 for i in 0..v.len() {
1270 assert_eq!(v[i], i as i32);
1274 // Should not panic.
1275 [(); 10].partition_at_index(0);
1276 [(); 10].partition_at_index(5);
1277 [(); 10].partition_at_index(9);
1278 [(); 100].partition_at_index(0);
1279 [(); 100].partition_at_index(50);
1280 [(); 100].partition_at_index(99);
1282 let mut v = [0xDEADBEEFu64];
1283 v.partition_at_index(0);
1284 assert!(v == [0xDEADBEEF]);
1288 #[should_panic(expected = "index 0 greater than length of slice")]
1289 fn partition_at_index_zero_length() {
1290 [0i32; 0].partition_at_index(0);
1294 #[should_panic(expected = "index 20 greater than length of slice")]
1295 fn partition_at_index_past_length() {
1296 [0i32; 10].partition_at_index(20);
1300 use core::slice::memchr::{memchr, memrchr};
1302 // test fallback implementations on all platforms
1305 assert_eq!(Some(0), memchr(b'a', b"a"));
1309 fn matches_begin() {
1310 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1315 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1320 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1324 fn matches_past_nul() {
1325 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1329 fn no_match_empty() {
1330 assert_eq!(None, memchr(b'a', b""));
1335 assert_eq!(None, memchr(b'a', b"xyz"));
1339 fn matches_one_reversed() {
1340 assert_eq!(Some(0), memrchr(b'a', b"a"));
1344 fn matches_begin_reversed() {
1345 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1349 fn matches_end_reversed() {
1350 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1354 fn matches_nul_reversed() {
1355 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1359 fn matches_past_nul_reversed() {
1360 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1364 fn no_match_empty_reversed() {
1365 assert_eq!(None, memrchr(b'a', b""));
1369 fn no_match_reversed() {
1370 assert_eq!(None, memrchr(b'a', b"xyz"));
1374 fn each_alignment_reversed() {
1375 let mut data = [1u8; 64];
1379 for start in 0..16 {
1380 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1386 #[cfg(not(miri))] // Miri cannot compute actual alignment of an allocation
1387 fn test_align_to_simple() {
1388 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1389 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1390 assert_eq!(aligned.len(), 3);
1391 assert!(prefix == [1] || suffix == [7]);
1392 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1393 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1394 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1395 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1396 assert!(aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1397 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1398 aligned, expect1, expect2, expect3, expect4);
1402 fn test_align_to_zst() {
1403 let bytes = [1, 2, 3, 4, 5, 6, 7];
1404 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1405 assert_eq!(aligned.len(), 0);
1406 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1410 #[cfg(not(miri))] // Miri cannot compute actual alignment of an allocation
1411 fn test_align_to_non_trivial() {
1412 #[repr(align(8))] struct U64(u64, u64);
1413 #[repr(align(8))] struct U64U64U32(u64, u64, u32);
1414 let data = [U64(1, 2), U64(3, 4), U64(5, 6), U64(7, 8), U64(9, 10), U64(11, 12), U64(13, 14),
1416 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1417 assert_eq!(aligned.len(), 4);
1418 assert_eq!(prefix.len() + suffix.len(), 2);
1422 fn test_align_to_empty_mid() {
1425 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1426 // overall slice is too short to contain an aligned address.
1427 let bytes = [1, 2, 3, 4, 5, 6, 7];
1429 for offset in 0..4 {
1430 let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::<Chunk>() };
1431 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1436 fn test_slice_partition_dedup_by() {
1437 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1439 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1441 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1442 assert_eq!(duplicates, [5, -1, 2]);
1446 fn test_slice_partition_dedup_empty() {
1447 let mut slice: [i32; 0] = [];
1449 let (dedup, duplicates) = slice.partition_dedup();
1451 assert_eq!(dedup, []);
1452 assert_eq!(duplicates, []);
1456 fn test_slice_partition_dedup_one() {
1457 let mut slice = [12];
1459 let (dedup, duplicates) = slice.partition_dedup();
1461 assert_eq!(dedup, [12]);
1462 assert_eq!(duplicates, []);
1466 fn test_slice_partition_dedup_multiple_ident() {
1467 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1469 let (dedup, duplicates) = slice.partition_dedup();
1471 assert_eq!(dedup, [12, 11]);
1472 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1476 fn test_slice_partition_dedup_partialeq() {
1478 struct Foo(i32, i32);
1480 impl PartialEq for Foo {
1481 fn eq(&self, other: &Foo) -> bool {
1486 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1488 let (dedup, duplicates) = slice.partition_dedup();
1490 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1491 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1495 fn test_copy_within() {
1496 // Start to end, with a RangeTo.
1497 let mut bytes = *b"Hello, World!";
1498 bytes.copy_within(..3, 10);
1499 assert_eq!(&bytes, b"Hello, WorHel");
1501 // End to start, with a RangeFrom.
1502 let mut bytes = *b"Hello, World!";
1503 bytes.copy_within(10.., 0);
1504 assert_eq!(&bytes, b"ld!lo, World!");
1506 // Overlapping, with a RangeInclusive.
1507 let mut bytes = *b"Hello, World!";
1508 bytes.copy_within(0..=11, 1);
1509 assert_eq!(&bytes, b"HHello, World");
1511 // Whole slice, with a RangeFull.
1512 let mut bytes = *b"Hello, World!";
1513 bytes.copy_within(.., 0);
1514 assert_eq!(&bytes, b"Hello, World!");
1516 // Ensure that copying at the end of slice won't cause UB.
1517 let mut bytes = *b"Hello, World!";
1518 bytes.copy_within(13..13, 5);
1519 assert_eq!(&bytes, b"Hello, World!");
1520 bytes.copy_within(5..5, 13);
1521 assert_eq!(&bytes, b"Hello, World!");
1525 #[should_panic(expected = "src is out of bounds")]
1526 fn test_copy_within_panics_src_too_long() {
1527 let mut bytes = *b"Hello, World!";
1528 // The length is only 13, so 14 is out of bounds.
1529 bytes.copy_within(10..14, 0);
1533 #[should_panic(expected = "dest is out of bounds")]
1534 fn test_copy_within_panics_dest_too_long() {
1535 let mut bytes = *b"Hello, World!";
1536 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1537 bytes.copy_within(0..4, 10);
1540 #[should_panic(expected = "src end is before src start")]
1541 fn test_copy_within_panics_src_inverted() {
1542 let mut bytes = *b"Hello, World!";
1543 // 2 is greater than 1, so this range is invalid.
1544 bytes.copy_within(2..1, 0);
1547 #[should_panic(expected = "attempted to index slice up to maximum usize")]
1548 fn test_copy_within_panics_src_out_of_bounds() {
1549 let mut bytes = *b"Hello, World!";
1550 // an inclusive range ending at usize::max_value() would make src_end overflow
1551 bytes.copy_within(usize::max_value()..=usize::max_value(), 0);
1555 fn test_is_sorted() {
1556 let empty: [i32; 0] = [];
1558 assert!([1, 2, 2, 9].is_sorted());
1559 assert!(![1, 3, 2].is_sorted());
1560 assert!([0].is_sorted());
1561 assert!(empty.is_sorted());
1562 assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
1563 assert!([-2, -1, 0, 3].is_sorted());
1564 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1565 assert!(!["c", "bb", "aaa"].is_sorted());
1566 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));