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_last() {
139 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
141 assert_eq!(c.last().unwrap()[1], 5);
143 let v2: &[i32] = &[0, 1, 2, 3, 4];
144 let c2 = v2.chunks(2);
145 assert_eq!(c2.last().unwrap()[0], 4);
149 fn test_chunks_zip() {
150 let v1: &[i32] = &[0, 1, 2, 3, 4];
151 let v2: &[i32] = &[6, 7, 8, 9, 10];
153 let res = v1.chunks(2)
155 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
156 .collect::<Vec<_>>();
157 assert_eq!(res, vec![14, 22, 14]);
161 fn test_chunks_mut_count() {
162 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
163 let c = v.chunks_mut(3);
164 assert_eq!(c.count(), 2);
166 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
167 let c2 = v2.chunks_mut(2);
168 assert_eq!(c2.count(), 3);
170 let v3: &mut [i32] = &mut [];
171 let c3 = v3.chunks_mut(2);
172 assert_eq!(c3.count(), 0);
176 fn test_chunks_mut_nth() {
177 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
178 let mut c = v.chunks_mut(2);
179 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
180 assert_eq!(c.next().unwrap(), &[4, 5]);
182 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
183 let mut c2 = v2.chunks_mut(3);
184 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
185 assert_eq!(c2.next(), None);
189 fn test_chunks_mut_last() {
190 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
191 let c = v.chunks_mut(2);
192 assert_eq!(c.last().unwrap(), &[4, 5]);
194 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
195 let c2 = v2.chunks_mut(2);
196 assert_eq!(c2.last().unwrap(), &[4]);
200 fn test_chunks_mut_zip() {
201 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
202 let v2: &[i32] = &[6, 7, 8, 9, 10];
204 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
205 let sum = b.iter().sum::<i32>();
210 assert_eq!(v1, [13, 14, 19, 20, 14]);
214 fn test_chunks_exact_count() {
215 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
216 let c = v.chunks_exact(3);
217 assert_eq!(c.count(), 2);
219 let v2: &[i32] = &[0, 1, 2, 3, 4];
220 let c2 = v2.chunks_exact(2);
221 assert_eq!(c2.count(), 2);
223 let v3: &[i32] = &[];
224 let c3 = v3.chunks_exact(2);
225 assert_eq!(c3.count(), 0);
229 fn test_chunks_exact_nth() {
230 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
231 let mut c = v.chunks_exact(2);
232 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
233 assert_eq!(c.next().unwrap(), &[4, 5]);
235 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
236 let mut c2 = v2.chunks_exact(3);
237 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
238 assert_eq!(c2.next(), None);
242 fn test_chunks_exact_last() {
243 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
244 let c = v.chunks_exact(2);
245 assert_eq!(c.last().unwrap(), &[4, 5]);
247 let v2: &[i32] = &[0, 1, 2, 3, 4];
248 let c2 = v2.chunks_exact(2);
249 assert_eq!(c2.last().unwrap(), &[2, 3]);
253 fn test_chunks_exact_remainder() {
254 let v: &[i32] = &[0, 1, 2, 3, 4];
255 let c = v.chunks_exact(2);
256 assert_eq!(c.remainder(), &[4]);
260 fn test_chunks_exact_zip() {
261 let v1: &[i32] = &[0, 1, 2, 3, 4];
262 let v2: &[i32] = &[6, 7, 8, 9, 10];
264 let res = v1.chunks_exact(2)
265 .zip(v2.chunks_exact(2))
266 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
267 .collect::<Vec<_>>();
268 assert_eq!(res, vec![14, 22]);
272 fn test_chunks_exact_mut_count() {
273 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
274 let c = v.chunks_exact_mut(3);
275 assert_eq!(c.count(), 2);
277 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
278 let c2 = v2.chunks_exact_mut(2);
279 assert_eq!(c2.count(), 2);
281 let v3: &mut [i32] = &mut [];
282 let c3 = v3.chunks_exact_mut(2);
283 assert_eq!(c3.count(), 0);
287 fn test_chunks_exact_mut_nth() {
288 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
289 let mut c = v.chunks_exact_mut(2);
290 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
291 assert_eq!(c.next().unwrap(), &[4, 5]);
293 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
294 let mut c2 = v2.chunks_exact_mut(3);
295 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
296 assert_eq!(c2.next(), None);
300 fn test_chunks_exact_mut_last() {
301 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
302 let c = v.chunks_exact_mut(2);
303 assert_eq!(c.last().unwrap(), &[4, 5]);
305 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
306 let c2 = v2.chunks_exact_mut(2);
307 assert_eq!(c2.last().unwrap(), &[2, 3]);
311 fn test_chunks_exact_mut_remainder() {
312 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
313 let c = v.chunks_exact_mut(2);
314 assert_eq!(c.into_remainder(), &[4]);
318 fn test_chunks_exact_mut_zip() {
319 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
320 let v2: &[i32] = &[6, 7, 8, 9, 10];
322 for (a, b) in v1.chunks_exact_mut(2).zip(v2.chunks_exact(2)) {
323 let sum = b.iter().sum::<i32>();
328 assert_eq!(v1, [13, 14, 19, 20, 4]);
332 fn test_rchunks_count() {
333 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
334 let c = v.rchunks(3);
335 assert_eq!(c.count(), 2);
337 let v2: &[i32] = &[0, 1, 2, 3, 4];
338 let c2 = v2.rchunks(2);
339 assert_eq!(c2.count(), 3);
341 let v3: &[i32] = &[];
342 let c3 = v3.rchunks(2);
343 assert_eq!(c3.count(), 0);
347 fn test_rchunks_nth() {
348 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
349 let mut c = v.rchunks(2);
350 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
351 assert_eq!(c.next().unwrap(), &[0, 1]);
353 let v2: &[i32] = &[0, 1, 2, 3, 4];
354 let mut c2 = v2.rchunks(3);
355 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
356 assert_eq!(c2.next(), None);
360 fn test_rchunks_last() {
361 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
362 let c = v.rchunks(2);
363 assert_eq!(c.last().unwrap()[1], 1);
365 let v2: &[i32] = &[0, 1, 2, 3, 4];
366 let c2 = v2.rchunks(2);
367 assert_eq!(c2.last().unwrap()[0], 0);
371 fn test_rchunks_zip() {
372 let v1: &[i32] = &[0, 1, 2, 3, 4];
373 let v2: &[i32] = &[6, 7, 8, 9, 10];
375 let res = v1.rchunks(2)
377 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
378 .collect::<Vec<_>>();
379 assert_eq!(res, vec![26, 18, 6]);
383 fn test_rchunks_mut_count() {
384 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
385 let c = v.rchunks_mut(3);
386 assert_eq!(c.count(), 2);
388 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
389 let c2 = v2.rchunks_mut(2);
390 assert_eq!(c2.count(), 3);
392 let v3: &mut [i32] = &mut [];
393 let c3 = v3.rchunks_mut(2);
394 assert_eq!(c3.count(), 0);
398 fn test_rchunks_mut_nth() {
399 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
400 let mut c = v.rchunks_mut(2);
401 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
402 assert_eq!(c.next().unwrap(), &[0, 1]);
404 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
405 let mut c2 = v2.rchunks_mut(3);
406 assert_eq!(c2.nth(1).unwrap(), &[0, 1]);
407 assert_eq!(c2.next(), None);
411 fn test_rchunks_mut_last() {
412 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
413 let c = v.rchunks_mut(2);
414 assert_eq!(c.last().unwrap(), &[0, 1]);
416 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
417 let c2 = v2.rchunks_mut(2);
418 assert_eq!(c2.last().unwrap(), &[0]);
422 fn test_rchunks_mut_zip() {
423 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
424 let v2: &[i32] = &[6, 7, 8, 9, 10];
426 for (a, b) in v1.rchunks_mut(2).zip(v2.rchunks(2)) {
427 let sum = b.iter().sum::<i32>();
432 assert_eq!(v1, [6, 16, 17, 22, 23]);
436 fn test_rchunks_exact_count() {
437 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
438 let c = v.rchunks_exact(3);
439 assert_eq!(c.count(), 2);
441 let v2: &[i32] = &[0, 1, 2, 3, 4];
442 let c2 = v2.rchunks_exact(2);
443 assert_eq!(c2.count(), 2);
445 let v3: &[i32] = &[];
446 let c3 = v3.rchunks_exact(2);
447 assert_eq!(c3.count(), 0);
451 fn test_rchunks_exact_nth() {
452 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
453 let mut c = v.rchunks_exact(2);
454 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
455 assert_eq!(c.next().unwrap(), &[0, 1]);
457 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
458 let mut c2 = v2.rchunks_exact(3);
459 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
460 assert_eq!(c2.next(), None);
464 fn test_rchunks_exact_last() {
465 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
466 let c = v.rchunks_exact(2);
467 assert_eq!(c.last().unwrap(), &[0, 1]);
469 let v2: &[i32] = &[0, 1, 2, 3, 4];
470 let c2 = v2.rchunks_exact(2);
471 assert_eq!(c2.last().unwrap(), &[1, 2]);
475 fn test_rchunks_exact_remainder() {
476 let v: &[i32] = &[0, 1, 2, 3, 4];
477 let c = v.rchunks_exact(2);
478 assert_eq!(c.remainder(), &[0]);
482 fn test_rchunks_exact_zip() {
483 let v1: &[i32] = &[0, 1, 2, 3, 4];
484 let v2: &[i32] = &[6, 7, 8, 9, 10];
486 let res = v1.rchunks_exact(2)
487 .zip(v2.rchunks_exact(2))
488 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
489 .collect::<Vec<_>>();
490 assert_eq!(res, vec![26, 18]);
494 fn test_rchunks_exact_mut_count() {
495 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
496 let c = v.rchunks_exact_mut(3);
497 assert_eq!(c.count(), 2);
499 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
500 let c2 = v2.rchunks_exact_mut(2);
501 assert_eq!(c2.count(), 2);
503 let v3: &mut [i32] = &mut [];
504 let c3 = v3.rchunks_exact_mut(2);
505 assert_eq!(c3.count(), 0);
509 fn test_rchunks_exact_mut_nth() {
510 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
511 let mut c = v.rchunks_exact_mut(2);
512 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
513 assert_eq!(c.next().unwrap(), &[0, 1]);
515 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
516 let mut c2 = v2.rchunks_exact_mut(3);
517 assert_eq!(c2.nth(1).unwrap(), &[1, 2, 3]);
518 assert_eq!(c2.next(), None);
522 fn test_rchunks_exact_mut_last() {
523 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
524 let c = v.rchunks_exact_mut(2);
525 assert_eq!(c.last().unwrap(), &[0, 1]);
527 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
528 let c2 = v2.rchunks_exact_mut(2);
529 assert_eq!(c2.last().unwrap(), &[1, 2]);
533 fn test_rchunks_exact_mut_remainder() {
534 let v: &mut [i32] = &mut [0, 1, 2, 3, 4];
535 let c = v.rchunks_exact_mut(2);
536 assert_eq!(c.into_remainder(), &[0]);
540 fn test_rchunks_exact_mut_zip() {
541 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
542 let v2: &[i32] = &[6, 7, 8, 9, 10];
544 for (a, b) in v1.rchunks_exact_mut(2).zip(v2.rchunks_exact(2)) {
545 let sum = b.iter().sum::<i32>();
550 assert_eq!(v1, [0, 16, 17, 22, 23]);
554 fn test_windows_count() {
555 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
556 let c = v.windows(3);
557 assert_eq!(c.count(), 4);
559 let v2: &[i32] = &[0, 1, 2, 3, 4];
560 let c2 = v2.windows(6);
561 assert_eq!(c2.count(), 0);
563 let v3: &[i32] = &[];
564 let c3 = v3.windows(2);
565 assert_eq!(c3.count(), 0);
569 fn test_windows_nth() {
570 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
571 let mut c = v.windows(2);
572 assert_eq!(c.nth(2).unwrap()[1], 3);
573 assert_eq!(c.next().unwrap()[0], 3);
575 let v2: &[i32] = &[0, 1, 2, 3, 4];
576 let mut c2 = v2.windows(4);
577 assert_eq!(c2.nth(1).unwrap()[1], 2);
578 assert_eq!(c2.next(), None);
582 fn test_windows_last() {
583 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
584 let c = v.windows(2);
585 assert_eq!(c.last().unwrap()[1], 5);
587 let v2: &[i32] = &[0, 1, 2, 3, 4];
588 let c2 = v2.windows(2);
589 assert_eq!(c2.last().unwrap()[0], 3);
593 fn test_windows_zip() {
594 let v1: &[i32] = &[0, 1, 2, 3, 4];
595 let v2: &[i32] = &[6, 7, 8, 9, 10];
597 let res = v1.windows(2)
599 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
600 .collect::<Vec<_>>();
602 assert_eq!(res, [14, 18, 22, 26]);
607 fn test_iter_ref_consistency() {
610 fn test<T : Copy + Debug + PartialEq>(x : T) {
611 let v : &[T] = &[x, x, x];
612 let v_ptrs : [*const T; 3] = match v {
613 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
620 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
621 let nth = v.iter().nth(i).unwrap();
622 assert_eq!(nth as *const _, v_ptrs[i]);
624 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
626 // stepping through with nth(0)
628 let mut it = v.iter();
630 let next = it.nth(0).unwrap();
631 assert_eq!(next as *const _, v_ptrs[i]);
633 assert_eq!(it.nth(0), None);
638 let mut it = v.iter();
640 let remaining = len - i;
641 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
643 let next = it.next().unwrap();
644 assert_eq!(next as *const _, v_ptrs[i]);
646 assert_eq!(it.size_hint(), (0, Some(0)));
647 assert_eq!(it.next(), None, "The final call to next() should return None");
652 let mut it = v.iter();
654 let remaining = len - i;
655 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
657 let prev = it.next_back().unwrap();
658 assert_eq!(prev as *const _, v_ptrs[remaining-1]);
660 assert_eq!(it.size_hint(), (0, Some(0)));
661 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
665 fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
666 let v : &mut [T] = &mut [x, x, x];
667 let v_ptrs : [*mut T; 3] = match v {
668 [ref v1, ref v2, ref v3] =>
669 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _],
676 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
677 let nth = v.iter_mut().nth(i).unwrap();
678 assert_eq!(nth as *mut _, v_ptrs[i]);
680 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
682 // stepping through with nth(0)
684 let mut it = v.iter();
686 let next = it.nth(0).unwrap();
687 assert_eq!(next as *const _, v_ptrs[i]);
689 assert_eq!(it.nth(0), None);
694 let mut it = v.iter_mut();
696 let remaining = len - i;
697 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
699 let next = it.next().unwrap();
700 assert_eq!(next as *mut _, v_ptrs[i]);
702 assert_eq!(it.size_hint(), (0, Some(0)));
703 assert_eq!(it.next(), None, "The final call to next() should return None");
708 let mut it = v.iter_mut();
710 let remaining = len - i;
711 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
713 let prev = it.next_back().unwrap();
714 assert_eq!(prev as *mut _, v_ptrs[remaining-1]);
716 assert_eq!(it.size_hint(), (0, Some(0)));
717 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
721 // Make sure iterators and slice patterns yield consistent addresses for various types,
725 test([0u32; 0]); // ZST with alignment > 0
728 test_mut([0u32; 0]); // ZST with alignment > 0
731 // The current implementation of SliceIndex fails to handle methods
732 // orthogonally from range types; therefore, it is worth testing
733 // all of the indexing operations on each input.
735 // This checks all six indexing methods, given an input range that
736 // should succeed. (it is NOT suitable for testing invalid inputs)
737 macro_rules! assert_range_eq {
738 ($arr:expr, $range:expr, $expected:expr)
741 let mut expected = $expected;
744 let expected: &[_] = &expected;
746 assert_eq!(&s[$range], expected, "(in assertion for: index)");
747 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
750 s.get_unchecked($range), expected,
751 "(in assertion for: get_unchecked)",
756 let s: &mut [_] = &mut arr;
757 let expected: &mut [_] = &mut expected;
760 &mut s[$range], expected,
761 "(in assertion for: index_mut)",
764 s.get_mut($range), Some(&mut expected[..]),
765 "(in assertion for: get_mut)",
769 s.get_unchecked_mut($range), expected,
770 "(in assertion for: get_unchecked_mut)",
777 // Make sure the macro can actually detect bugs,
778 // because if it can't, then what are we even doing here?
780 // (Be aware this only demonstrates the ability to detect bugs
781 // in the FIRST method that panics, as the macro is not designed
782 // to be used in `should_panic`)
784 #[should_panic(expected = "out of range")]
786 fn assert_range_eq_can_fail_by_panic() {
787 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
790 // (Be aware this only demonstrates the ability to detect bugs
791 // in the FIRST method it calls, as the macro is not designed
792 // to be used in `should_panic`)
794 #[should_panic(expected = "==")]
796 fn assert_range_eq_can_fail_by_inequality() {
797 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
800 // Test cases for bad index operations.
802 // This generates `should_panic` test cases for Index/IndexMut
803 // and `None` test cases for get/get_mut.
804 macro_rules! panic_cases {
806 // each test case needs a unique name to namespace the tests
807 in mod $case_name:ident {
812 // one or more similar inputs for which data[input] succeeds,
813 // and the corresponding output as an array. This helps validate
814 // "critical points" where an input range straddles the boundary
815 // between valid and invalid.
816 // (such as the input `len..len`, which is just barely valid)
818 good: data[$good:expr] == $output:expr;
821 bad: data[$bad:expr];
822 message: $expect_msg:expr;
830 $( assert_range_eq!($data, $good, $output); )*
834 assert_eq!(v.get($bad), None, "(in None assertion for get)");
838 let v: &mut [_] = &mut v;
839 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
844 #[should_panic(expected = $expect_msg)]
853 #[should_panic(expected = $expect_msg)]
855 fn index_mut_fail() {
857 let v: &mut [_] = &mut v;
858 let _v = &mut v[$bad];
866 let v = [0, 1, 2, 3, 4, 5];
868 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
869 assert_range_eq!(v, ..2, [0, 1]);
870 assert_range_eq!(v, ..=1, [0, 1]);
871 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
872 assert_range_eq!(v, 1..4, [1, 2, 3]);
873 assert_range_eq!(v, 1..=3, [1, 2, 3]);
877 in mod rangefrom_len {
878 data: [0, 1, 2, 3, 4, 5];
880 good: data[6..] == [];
882 message: "but ends at"; // perhaps not ideal
886 data: [0, 1, 2, 3, 4, 5];
888 good: data[..6] == [0, 1, 2, 3, 4, 5];
890 message: "out of range";
893 in mod rangetoinclusive_len {
894 data: [0, 1, 2, 3, 4, 5];
896 good: data[..=5] == [0, 1, 2, 3, 4, 5];
898 message: "out of range";
901 in mod range_len_len {
902 data: [0, 1, 2, 3, 4, 5];
904 good: data[6..6] == [];
906 message: "out of range";
909 in mod rangeinclusive_len_len {
910 data: [0, 1, 2, 3, 4, 5];
912 good: data[6..=5] == [];
914 message: "out of range";
919 in mod range_neg_width {
920 data: [0, 1, 2, 3, 4, 5];
922 good: data[4..4] == [];
924 message: "but ends at";
927 in mod rangeinclusive_neg_width {
928 data: [0, 1, 2, 3, 4, 5];
930 good: data[4..=3] == [];
932 message: "but ends at";
937 in mod rangeinclusive_overflow {
940 // note: using 0 specifically ensures that the result of overflowing is 0..0,
941 // so that `get` doesn't simply return None for the wrong reason.
942 bad: data[0 ..= ::std::usize::MAX];
943 message: "maximum usize";
946 in mod rangetoinclusive_overflow {
949 bad: data[..= ::std::usize::MAX];
950 message: "maximum usize";
956 fn test_find_rfind() {
957 let v = [0, 1, 2, 3, 4, 5];
958 let mut iter = v.iter();
960 while let Some(&elt) = iter.rfind(|_| true) {
962 assert_eq!(elt, v[i]);
965 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
969 fn test_iter_folds() {
970 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
971 assert_eq!(a.iter().fold(0, |acc, &x| 2*acc + x), 57);
972 assert_eq!(a.iter().rfold(0, |acc, &x| 2*acc + x), 129);
973 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
974 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
975 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
977 // short-circuiting try_fold, through other methods
978 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
979 let mut iter = a.iter();
980 assert_eq!(iter.position(|&x| x == 3), Some(3));
981 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
982 assert_eq!(iter.len(), 2);
986 fn test_rotate_left() {
987 const N: usize = 600;
988 let a: &mut [_] = &mut [0; N];
997 assert_eq!(a[(i + k) % N], i);
1002 fn test_rotate_right() {
1003 const N: usize = 600;
1004 let a: &mut [_] = &mut [0; N];
1012 assert_eq!(a[(i + 42) % N], i);
1017 #[cfg(not(target_arch = "wasm32"))]
1019 fn sort_unstable() {
1020 use core::cmp::Ordering::{Equal, Greater, Less};
1021 use core::slice::heapsort;
1022 use rand::{FromEntropy, Rng, rngs::SmallRng, seq::SliceRandom};
1024 let mut v = [0; 600];
1025 let mut tmp = [0; 600];
1026 let mut rng = SmallRng::from_entropy();
1028 for len in (2..25).chain(500..510) {
1029 let v = &mut v[0..len];
1030 let tmp = &mut tmp[0..len];
1032 for &modulus in &[5, 10, 100, 1000] {
1035 v[i] = rng.gen::<i32>() % modulus;
1038 // Sort in default order.
1039 tmp.copy_from_slice(v);
1040 tmp.sort_unstable();
1041 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1043 // Sort in ascending order.
1044 tmp.copy_from_slice(v);
1045 tmp.sort_unstable_by(|a, b| a.cmp(b));
1046 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1048 // Sort in descending order.
1049 tmp.copy_from_slice(v);
1050 tmp.sort_unstable_by(|a, b| b.cmp(a));
1051 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1053 // Test heapsort using `<` operator.
1054 tmp.copy_from_slice(v);
1055 heapsort(tmp, |a, b| a < b);
1056 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1058 // Test heapsort using `>` operator.
1059 tmp.copy_from_slice(v);
1060 heapsort(tmp, |a, b| a > b);
1061 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1066 // Sort using a completely random comparison function.
1067 // This will reorder the elements *somehow*, but won't panic.
1068 for i in 0..v.len() {
1071 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1073 for i in 0..v.len() {
1074 assert_eq!(v[i], i as i32);
1077 // Should not panic.
1078 [0i32; 0].sort_unstable();
1079 [(); 10].sort_unstable();
1080 [(); 100].sort_unstable();
1082 let mut v = [0xDEADBEEFu64];
1084 assert!(v == [0xDEADBEEF]);
1088 use core::slice::memchr::{memchr, memrchr};
1090 // test fallback implementations on all platforms
1093 assert_eq!(Some(0), memchr(b'a', b"a"));
1097 fn matches_begin() {
1098 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1103 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1108 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1112 fn matches_past_nul() {
1113 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1117 fn no_match_empty() {
1118 assert_eq!(None, memchr(b'a', b""));
1123 assert_eq!(None, memchr(b'a', b"xyz"));
1127 fn matches_one_reversed() {
1128 assert_eq!(Some(0), memrchr(b'a', b"a"));
1132 fn matches_begin_reversed() {
1133 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1137 fn matches_end_reversed() {
1138 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1142 fn matches_nul_reversed() {
1143 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1147 fn matches_past_nul_reversed() {
1148 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1152 fn no_match_empty_reversed() {
1153 assert_eq!(None, memrchr(b'a', b""));
1157 fn no_match_reversed() {
1158 assert_eq!(None, memrchr(b'a', b"xyz"));
1162 fn each_alignment_reversed() {
1163 let mut data = [1u8; 64];
1167 for start in 0..16 {
1168 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1175 fn test_align_to_simple() {
1176 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1177 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1178 assert_eq!(aligned.len(), 3);
1179 assert!(prefix == [1] || suffix == [7]);
1180 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1181 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1182 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1183 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1184 assert!(aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1185 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1186 aligned, expect1, expect2, expect3, expect4);
1191 fn test_align_to_zst() {
1192 let bytes = [1, 2, 3, 4, 5, 6, 7];
1193 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1194 assert_eq!(aligned.len(), 0);
1195 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1200 fn test_align_to_non_trivial() {
1201 #[repr(align(8))] struct U64(u64, u64);
1202 #[repr(align(8))] struct U64U64U32(u64, u64, u32);
1203 let data = [U64(1, 2), U64(3, 4), U64(5, 6), U64(7, 8), U64(9, 10), U64(11, 12), U64(13, 14),
1205 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1206 assert_eq!(aligned.len(), 4);
1207 assert_eq!(prefix.len() + suffix.len(), 2);
1212 fn test_align_to_empty_mid() {
1215 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1216 // overall slice is too short to contain an aligned address.
1217 let bytes = [1, 2, 3, 4, 5, 6, 7];
1219 for offset in 0..4 {
1220 let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::<Chunk>() };
1221 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1226 fn test_slice_partition_dedup_by() {
1227 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1229 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1231 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1232 assert_eq!(duplicates, [5, -1, 2]);
1236 fn test_slice_partition_dedup_empty() {
1237 let mut slice: [i32; 0] = [];
1239 let (dedup, duplicates) = slice.partition_dedup();
1241 assert_eq!(dedup, []);
1242 assert_eq!(duplicates, []);
1246 fn test_slice_partition_dedup_one() {
1247 let mut slice = [12];
1249 let (dedup, duplicates) = slice.partition_dedup();
1251 assert_eq!(dedup, [12]);
1252 assert_eq!(duplicates, []);
1256 fn test_slice_partition_dedup_multiple_ident() {
1257 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1259 let (dedup, duplicates) = slice.partition_dedup();
1261 assert_eq!(dedup, [12, 11]);
1262 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1266 fn test_slice_partition_dedup_partialeq() {
1268 struct Foo(i32, i32);
1270 impl PartialEq for Foo {
1271 fn eq(&self, other: &Foo) -> bool {
1276 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1278 let (dedup, duplicates) = slice.partition_dedup();
1280 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1281 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1285 fn test_copy_within() {
1286 // Start to end, with a RangeTo.
1287 let mut bytes = *b"Hello, World!";
1288 bytes.copy_within(..3, 10);
1289 assert_eq!(&bytes, b"Hello, WorHel");
1291 // End to start, with a RangeFrom.
1292 let mut bytes = *b"Hello, World!";
1293 bytes.copy_within(10.., 0);
1294 assert_eq!(&bytes, b"ld!lo, World!");
1296 // Overlapping, with a RangeInclusive.
1297 let mut bytes = *b"Hello, World!";
1298 bytes.copy_within(0..=11, 1);
1299 assert_eq!(&bytes, b"HHello, World");
1301 // Whole slice, with a RangeFull.
1302 let mut bytes = *b"Hello, World!";
1303 bytes.copy_within(.., 0);
1304 assert_eq!(&bytes, b"Hello, World!");
1308 #[should_panic(expected = "src is out of bounds")]
1310 fn test_copy_within_panics_src_too_long() {
1311 let mut bytes = *b"Hello, World!";
1312 // The length is only 13, so 14 is out of bounds.
1313 bytes.copy_within(10..14, 0);
1317 #[should_panic(expected = "dest is out of bounds")]
1319 fn test_copy_within_panics_dest_too_long() {
1320 let mut bytes = *b"Hello, World!";
1321 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1322 bytes.copy_within(0..4, 10);
1325 #[should_panic(expected = "src end is before src start")]
1327 fn test_copy_within_panics_src_inverted() {
1328 let mut bytes = *b"Hello, World!";
1329 // 2 is greater than 1, so this range is invalid.
1330 bytes.copy_within(2..1, 0);
1334 fn test_is_sorted() {
1335 let empty: [i32; 0] = [];
1337 assert!([1, 2, 2, 9].is_sorted());
1338 assert!(![1, 3, 2].is_sorted());
1339 assert!([0].is_sorted());
1340 assert!(empty.is_sorted());
1341 assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
1342 assert!([-2, -1, 0, 3].is_sorted());
1343 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1344 assert!(!["c", "bb", "aaa"].is_sorted());
1345 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));