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_nth_back() {
583 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
584 let mut c = v.windows(2);
585 assert_eq!(c.nth_back(2).unwrap()[0], 2);
586 assert_eq!(c.next_back().unwrap()[1], 2);
588 let v2: &[i32] = &[0, 1, 2, 3, 4];
589 let mut c2 = v2.windows(4);
590 assert_eq!(c2.nth_back(1).unwrap()[1], 1);
591 assert_eq!(c2.next_back(), None);
595 fn test_windows_last() {
596 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
597 let c = v.windows(2);
598 assert_eq!(c.last().unwrap()[1], 5);
600 let v2: &[i32] = &[0, 1, 2, 3, 4];
601 let c2 = v2.windows(2);
602 assert_eq!(c2.last().unwrap()[0], 3);
606 fn test_windows_zip() {
607 let v1: &[i32] = &[0, 1, 2, 3, 4];
608 let v2: &[i32] = &[6, 7, 8, 9, 10];
610 let res = v1.windows(2)
612 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
613 .collect::<Vec<_>>();
615 assert_eq!(res, [14, 18, 22, 26]);
620 fn test_iter_ref_consistency() {
623 fn test<T : Copy + Debug + PartialEq>(x : T) {
624 let v : &[T] = &[x, x, x];
625 let v_ptrs : [*const T; 3] = match v {
626 [ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
633 assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
634 let nth = v.iter().nth(i).unwrap();
635 assert_eq!(nth as *const _, v_ptrs[i]);
637 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
639 // stepping through with nth(0)
641 let mut it = v.iter();
643 let next = it.nth(0).unwrap();
644 assert_eq!(next as *const _, v_ptrs[i]);
646 assert_eq!(it.nth(0), None);
651 let mut it = v.iter();
653 let remaining = len - i;
654 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
656 let next = it.next().unwrap();
657 assert_eq!(next as *const _, v_ptrs[i]);
659 assert_eq!(it.size_hint(), (0, Some(0)));
660 assert_eq!(it.next(), None, "The final call to next() should return None");
665 let mut it = v.iter();
667 let remaining = len - i;
668 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
670 let prev = it.next_back().unwrap();
671 assert_eq!(prev as *const _, v_ptrs[remaining-1]);
673 assert_eq!(it.size_hint(), (0, Some(0)));
674 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
678 fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
679 let v : &mut [T] = &mut [x, x, x];
680 let v_ptrs : [*mut T; 3] = match v {
681 [ref v1, ref v2, ref v3] =>
682 [v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _],
689 assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
690 let nth = v.iter_mut().nth(i).unwrap();
691 assert_eq!(nth as *mut _, v_ptrs[i]);
693 assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
695 // stepping through with nth(0)
697 let mut it = v.iter();
699 let next = it.nth(0).unwrap();
700 assert_eq!(next as *const _, v_ptrs[i]);
702 assert_eq!(it.nth(0), None);
707 let mut it = v.iter_mut();
709 let remaining = len - i;
710 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
712 let next = it.next().unwrap();
713 assert_eq!(next as *mut _, v_ptrs[i]);
715 assert_eq!(it.size_hint(), (0, Some(0)));
716 assert_eq!(it.next(), None, "The final call to next() should return None");
721 let mut it = v.iter_mut();
723 let remaining = len - i;
724 assert_eq!(it.size_hint(), (remaining, Some(remaining)));
726 let prev = it.next_back().unwrap();
727 assert_eq!(prev as *mut _, v_ptrs[remaining-1]);
729 assert_eq!(it.size_hint(), (0, Some(0)));
730 assert_eq!(it.next_back(), None, "The final call to next_back() should return None");
734 // Make sure iterators and slice patterns yield consistent addresses for various types,
738 test([0u32; 0]); // ZST with alignment > 0
741 test_mut([0u32; 0]); // ZST with alignment > 0
744 // The current implementation of SliceIndex fails to handle methods
745 // orthogonally from range types; therefore, it is worth testing
746 // all of the indexing operations on each input.
748 // This checks all six indexing methods, given an input range that
749 // should succeed. (it is NOT suitable for testing invalid inputs)
750 macro_rules! assert_range_eq {
751 ($arr:expr, $range:expr, $expected:expr)
754 let mut expected = $expected;
757 let expected: &[_] = &expected;
759 assert_eq!(&s[$range], expected, "(in assertion for: index)");
760 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
763 s.get_unchecked($range), expected,
764 "(in assertion for: get_unchecked)",
769 let s: &mut [_] = &mut arr;
770 let expected: &mut [_] = &mut expected;
773 &mut s[$range], expected,
774 "(in assertion for: index_mut)",
777 s.get_mut($range), Some(&mut expected[..]),
778 "(in assertion for: get_mut)",
782 s.get_unchecked_mut($range), expected,
783 "(in assertion for: get_unchecked_mut)",
790 // Make sure the macro can actually detect bugs,
791 // because if it can't, then what are we even doing here?
793 // (Be aware this only demonstrates the ability to detect bugs
794 // in the FIRST method that panics, as the macro is not designed
795 // to be used in `should_panic`)
797 #[should_panic(expected = "out of range")]
798 fn assert_range_eq_can_fail_by_panic() {
799 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
802 // (Be aware this only demonstrates the ability to detect bugs
803 // in the FIRST method it calls, as the macro is not designed
804 // to be used in `should_panic`)
806 #[should_panic(expected = "==")]
807 fn assert_range_eq_can_fail_by_inequality() {
808 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
811 // Test cases for bad index operations.
813 // This generates `should_panic` test cases for Index/IndexMut
814 // and `None` test cases for get/get_mut.
815 macro_rules! panic_cases {
817 // each test case needs a unique name to namespace the tests
818 in mod $case_name:ident {
823 // one or more similar inputs for which data[input] succeeds,
824 // and the corresponding output as an array. This helps validate
825 // "critical points" where an input range straddles the boundary
826 // between valid and invalid.
827 // (such as the input `len..len`, which is just barely valid)
829 good: data[$good:expr] == $output:expr;
832 bad: data[$bad:expr];
833 message: $expect_msg:expr;
841 $( assert_range_eq!($data, $good, $output); )*
845 assert_eq!(v.get($bad), None, "(in None assertion for get)");
849 let v: &mut [_] = &mut v;
850 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
855 #[should_panic(expected = $expect_msg)]
863 #[should_panic(expected = $expect_msg)]
864 fn index_mut_fail() {
866 let v: &mut [_] = &mut v;
867 let _v = &mut v[$bad];
875 let v = [0, 1, 2, 3, 4, 5];
877 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
878 assert_range_eq!(v, ..2, [0, 1]);
879 assert_range_eq!(v, ..=1, [0, 1]);
880 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
881 assert_range_eq!(v, 1..4, [1, 2, 3]);
882 assert_range_eq!(v, 1..=3, [1, 2, 3]);
886 in mod rangefrom_len {
887 data: [0, 1, 2, 3, 4, 5];
889 good: data[6..] == [];
891 message: "but ends at"; // perhaps not ideal
895 data: [0, 1, 2, 3, 4, 5];
897 good: data[..6] == [0, 1, 2, 3, 4, 5];
899 message: "out of range";
902 in mod rangetoinclusive_len {
903 data: [0, 1, 2, 3, 4, 5];
905 good: data[..=5] == [0, 1, 2, 3, 4, 5];
907 message: "out of range";
910 in mod range_len_len {
911 data: [0, 1, 2, 3, 4, 5];
913 good: data[6..6] == [];
915 message: "out of range";
918 in mod rangeinclusive_len_len {
919 data: [0, 1, 2, 3, 4, 5];
921 good: data[6..=5] == [];
923 message: "out of range";
928 in mod range_neg_width {
929 data: [0, 1, 2, 3, 4, 5];
931 good: data[4..4] == [];
933 message: "but ends at";
936 in mod rangeinclusive_neg_width {
937 data: [0, 1, 2, 3, 4, 5];
939 good: data[4..=3] == [];
941 message: "but ends at";
946 in mod rangeinclusive_overflow {
949 // note: using 0 specifically ensures that the result of overflowing is 0..0,
950 // so that `get` doesn't simply return None for the wrong reason.
951 bad: data[0 ..= ::std::usize::MAX];
952 message: "maximum usize";
955 in mod rangetoinclusive_overflow {
958 bad: data[..= ::std::usize::MAX];
959 message: "maximum usize";
965 fn test_find_rfind() {
966 let v = [0, 1, 2, 3, 4, 5];
967 let mut iter = v.iter();
969 while let Some(&elt) = iter.rfind(|_| true) {
971 assert_eq!(elt, v[i]);
974 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
978 fn test_iter_folds() {
979 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
980 assert_eq!(a.iter().fold(0, |acc, &x| 2*acc + x), 57);
981 assert_eq!(a.iter().rfold(0, |acc, &x| 2*acc + x), 129);
982 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
983 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
984 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
986 // short-circuiting try_fold, through other methods
987 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
988 let mut iter = a.iter();
989 assert_eq!(iter.position(|&x| x == 3), Some(3));
990 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
991 assert_eq!(iter.len(), 2);
995 fn test_rotate_left() {
996 const N: usize = 600;
997 let a: &mut [_] = &mut [0; N];
1006 assert_eq!(a[(i + k) % N], i);
1011 fn test_rotate_right() {
1012 const N: usize = 600;
1013 let a: &mut [_] = &mut [0; N];
1021 assert_eq!(a[(i + 42) % N], i);
1026 #[cfg(not(target_arch = "wasm32"))]
1027 #[cfg(not(miri))] // Miri does not support entropy
1028 fn sort_unstable() {
1029 use core::cmp::Ordering::{Equal, Greater, Less};
1030 use core::slice::heapsort;
1031 use rand::{FromEntropy, Rng, rngs::SmallRng, seq::SliceRandom};
1033 let mut v = [0; 600];
1034 let mut tmp = [0; 600];
1035 let mut rng = SmallRng::from_entropy();
1037 for len in (2..25).chain(500..510) {
1038 let v = &mut v[0..len];
1039 let tmp = &mut tmp[0..len];
1041 for &modulus in &[5, 10, 100, 1000] {
1044 v[i] = rng.gen::<i32>() % modulus;
1047 // Sort in default order.
1048 tmp.copy_from_slice(v);
1049 tmp.sort_unstable();
1050 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1052 // Sort in ascending order.
1053 tmp.copy_from_slice(v);
1054 tmp.sort_unstable_by(|a, b| a.cmp(b));
1055 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1057 // Sort in descending order.
1058 tmp.copy_from_slice(v);
1059 tmp.sort_unstable_by(|a, b| b.cmp(a));
1060 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1062 // Test heapsort using `<` operator.
1063 tmp.copy_from_slice(v);
1064 heapsort(tmp, |a, b| a < b);
1065 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
1067 // Test heapsort using `>` operator.
1068 tmp.copy_from_slice(v);
1069 heapsort(tmp, |a, b| a > b);
1070 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
1075 // Sort using a completely random comparison function.
1076 // This will reorder the elements *somehow*, but won't panic.
1077 for i in 0..v.len() {
1080 v.sort_unstable_by(|_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
1082 for i in 0..v.len() {
1083 assert_eq!(v[i], i as i32);
1086 // Should not panic.
1087 [0i32; 0].sort_unstable();
1088 [(); 10].sort_unstable();
1089 [(); 100].sort_unstable();
1091 let mut v = [0xDEADBEEFu64];
1093 assert!(v == [0xDEADBEEF]);
1097 use core::slice::memchr::{memchr, memrchr};
1099 // test fallback implementations on all platforms
1102 assert_eq!(Some(0), memchr(b'a', b"a"));
1106 fn matches_begin() {
1107 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
1112 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
1117 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
1121 fn matches_past_nul() {
1122 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
1126 fn no_match_empty() {
1127 assert_eq!(None, memchr(b'a', b""));
1132 assert_eq!(None, memchr(b'a', b"xyz"));
1136 fn matches_one_reversed() {
1137 assert_eq!(Some(0), memrchr(b'a', b"a"));
1141 fn matches_begin_reversed() {
1142 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
1146 fn matches_end_reversed() {
1147 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
1151 fn matches_nul_reversed() {
1152 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
1156 fn matches_past_nul_reversed() {
1157 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
1161 fn no_match_empty_reversed() {
1162 assert_eq!(None, memrchr(b'a', b""));
1166 fn no_match_reversed() {
1167 assert_eq!(None, memrchr(b'a', b"xyz"));
1171 fn each_alignment_reversed() {
1172 let mut data = [1u8; 64];
1176 for start in 0..16 {
1177 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
1183 #[cfg(not(miri))] // Miri cannot compute actual alignment of an allocation
1184 fn test_align_to_simple() {
1185 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
1186 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
1187 assert_eq!(aligned.len(), 3);
1188 assert!(prefix == [1] || suffix == [7]);
1189 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
1190 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
1191 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
1192 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
1193 assert!(aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
1194 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
1195 aligned, expect1, expect2, expect3, expect4);
1199 fn test_align_to_zst() {
1200 let bytes = [1, 2, 3, 4, 5, 6, 7];
1201 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
1202 assert_eq!(aligned.len(), 0);
1203 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
1207 #[cfg(not(miri))] // Miri cannot compute actual alignment of an allocation
1208 fn test_align_to_non_trivial() {
1209 #[repr(align(8))] struct U64(u64, u64);
1210 #[repr(align(8))] struct U64U64U32(u64, u64, u32);
1211 let data = [U64(1, 2), U64(3, 4), U64(5, 6), U64(7, 8), U64(9, 10), U64(11, 12), U64(13, 14),
1213 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
1214 assert_eq!(aligned.len(), 4);
1215 assert_eq!(prefix.len() + suffix.len(), 2);
1219 fn test_align_to_empty_mid() {
1222 // Make sure that we do not create empty unaligned slices for the mid part, even when the
1223 // overall slice is too short to contain an aligned address.
1224 let bytes = [1, 2, 3, 4, 5, 6, 7];
1226 for offset in 0..4 {
1227 let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::<Chunk>() };
1228 assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
1233 fn test_slice_partition_dedup_by() {
1234 let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];
1236 let (dedup, duplicates) = slice.partition_dedup_by(|a, b| a.abs() == b.abs());
1238 assert_eq!(dedup, [1, 2, 3, 1, -5, -2]);
1239 assert_eq!(duplicates, [5, -1, 2]);
1243 fn test_slice_partition_dedup_empty() {
1244 let mut slice: [i32; 0] = [];
1246 let (dedup, duplicates) = slice.partition_dedup();
1248 assert_eq!(dedup, []);
1249 assert_eq!(duplicates, []);
1253 fn test_slice_partition_dedup_one() {
1254 let mut slice = [12];
1256 let (dedup, duplicates) = slice.partition_dedup();
1258 assert_eq!(dedup, [12]);
1259 assert_eq!(duplicates, []);
1263 fn test_slice_partition_dedup_multiple_ident() {
1264 let mut slice = [12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11];
1266 let (dedup, duplicates) = slice.partition_dedup();
1268 assert_eq!(dedup, [12, 11]);
1269 assert_eq!(duplicates, [12, 12, 12, 12, 11, 11, 11, 11, 11]);
1273 fn test_slice_partition_dedup_partialeq() {
1275 struct Foo(i32, i32);
1277 impl PartialEq for Foo {
1278 fn eq(&self, other: &Foo) -> bool {
1283 let mut slice = [Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)];
1285 let (dedup, duplicates) = slice.partition_dedup();
1287 assert_eq!(dedup, [Foo(0, 1), Foo(1, 7)]);
1288 assert_eq!(duplicates, [Foo(0, 5), Foo(1, 9)]);
1292 fn test_copy_within() {
1293 // Start to end, with a RangeTo.
1294 let mut bytes = *b"Hello, World!";
1295 bytes.copy_within(..3, 10);
1296 assert_eq!(&bytes, b"Hello, WorHel");
1298 // End to start, with a RangeFrom.
1299 let mut bytes = *b"Hello, World!";
1300 bytes.copy_within(10.., 0);
1301 assert_eq!(&bytes, b"ld!lo, World!");
1303 // Overlapping, with a RangeInclusive.
1304 let mut bytes = *b"Hello, World!";
1305 bytes.copy_within(0..=11, 1);
1306 assert_eq!(&bytes, b"HHello, World");
1308 // Whole slice, with a RangeFull.
1309 let mut bytes = *b"Hello, World!";
1310 bytes.copy_within(.., 0);
1311 assert_eq!(&bytes, b"Hello, World!");
1315 #[should_panic(expected = "src is out of bounds")]
1316 fn test_copy_within_panics_src_too_long() {
1317 let mut bytes = *b"Hello, World!";
1318 // The length is only 13, so 14 is out of bounds.
1319 bytes.copy_within(10..14, 0);
1323 #[should_panic(expected = "dest is out of bounds")]
1324 fn test_copy_within_panics_dest_too_long() {
1325 let mut bytes = *b"Hello, World!";
1326 // The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1327 bytes.copy_within(0..4, 10);
1330 #[should_panic(expected = "src end is before src start")]
1331 fn test_copy_within_panics_src_inverted() {
1332 let mut bytes = *b"Hello, World!";
1333 // 2 is greater than 1, so this range is invalid.
1334 bytes.copy_within(2..1, 0);
1338 fn test_is_sorted() {
1339 let empty: [i32; 0] = [];
1341 assert!([1, 2, 2, 9].is_sorted());
1342 assert!(![1, 3, 2].is_sorted());
1343 assert!([0].is_sorted());
1344 assert!(empty.is_sorted());
1345 assert!(![0.0, 1.0, std::f32::NAN].is_sorted());
1346 assert!([-2, -1, 0, 3].is_sorted());
1347 assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
1348 assert!(!["c", "bb", "aaa"].is_sorted());
1349 assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));