1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use core::result::Result::{Ok, Err};
15 let b = [1, 2, 3, 5, 5];
16 assert!(b.iter().position(|&v| v == 9) == None);
17 assert!(b.iter().position(|&v| v == 5) == Some(3));
18 assert!(b.iter().position(|&v| v == 3) == Some(2));
19 assert!(b.iter().position(|&v| v == 0) == None);
24 let b = [1, 2, 3, 5, 5];
25 assert!(b.iter().rposition(|&v| v == 9) == None);
26 assert!(b.iter().rposition(|&v| v == 5) == Some(4));
27 assert!(b.iter().rposition(|&v| v == 3) == Some(2));
28 assert!(b.iter().rposition(|&v| v == 0) == None);
32 fn test_binary_search() {
34 assert_eq!(b.binary_search(&5), Err(0));
37 assert_eq!(b.binary_search(&3), Err(0));
38 assert_eq!(b.binary_search(&4), Ok(0));
39 assert_eq!(b.binary_search(&5), Err(1));
41 let b = [1, 2, 4, 6, 8, 9];
42 assert_eq!(b.binary_search(&5), Err(3));
43 assert_eq!(b.binary_search(&6), Ok(3));
44 assert_eq!(b.binary_search(&7), Err(4));
45 assert_eq!(b.binary_search(&8), Ok(4));
47 let b = [1, 2, 4, 5, 6, 8];
48 assert_eq!(b.binary_search(&9), Err(6));
50 let b = [1, 2, 4, 6, 7, 8, 9];
51 assert_eq!(b.binary_search(&6), Ok(3));
52 assert_eq!(b.binary_search(&5), Err(3));
53 assert_eq!(b.binary_search(&8), Ok(5));
55 let b = [1, 2, 4, 5, 6, 8, 9];
56 assert_eq!(b.binary_search(&7), Err(5));
57 assert_eq!(b.binary_search(&0), Err(0));
59 let b = [1, 3, 3, 3, 7];
60 assert_eq!(b.binary_search(&0), Err(0));
61 assert_eq!(b.binary_search(&1), Ok(0));
62 assert_eq!(b.binary_search(&2), Err(1));
63 assert!(match b.binary_search(&3) { Ok(1..=3) => true, _ => false });
64 assert!(match b.binary_search(&3) { Ok(1..=3) => true, _ => false });
65 assert_eq!(b.binary_search(&4), Err(4));
66 assert_eq!(b.binary_search(&5), Err(4));
67 assert_eq!(b.binary_search(&6), Err(4));
68 assert_eq!(b.binary_search(&7), Ok(4));
69 assert_eq!(b.binary_search(&8), Err(5));
73 // Test implementation specific behavior when finding equivalent elements.
74 // It is ok to break this test but when you do a crater run is highly advisable.
75 fn test_binary_search_implementation_details() {
76 let b = [1, 1, 2, 2, 3, 3, 3];
77 assert_eq!(b.binary_search(&1), Ok(1));
78 assert_eq!(b.binary_search(&2), Ok(3));
79 assert_eq!(b.binary_search(&3), Ok(6));
80 let b = [1, 1, 1, 1, 1, 3, 3, 3, 3];
81 assert_eq!(b.binary_search(&1), Ok(4));
82 assert_eq!(b.binary_search(&3), Ok(8));
83 let b = [1, 1, 1, 1, 3, 3, 3, 3, 3];
84 assert_eq!(b.binary_search(&1), Ok(3));
85 assert_eq!(b.binary_search(&3), Ok(8));
89 fn test_iterator_nth() {
90 let v: &[_] = &[0, 1, 2, 3, 4];
92 assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
94 assert_eq!(v.iter().nth(v.len()), None);
96 let mut iter = v.iter();
97 assert_eq!(iter.nth(2).unwrap(), &v[2]);
98 assert_eq!(iter.nth(1).unwrap(), &v[4]);
102 fn test_iterator_last() {
103 let v: &[_] = &[0, 1, 2, 3, 4];
104 assert_eq!(v.iter().last().unwrap(), &4);
105 assert_eq!(v[..1].iter().last().unwrap(), &0);
109 fn test_iterator_count() {
110 let v: &[_] = &[0, 1, 2, 3, 4];
111 assert_eq!(v.iter().count(), 5);
113 let mut iter2 = v.iter();
116 assert_eq!(iter2.count(), 3);
120 fn test_chunks_count() {
121 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
123 assert_eq!(c.count(), 2);
125 let v2: &[i32] = &[0, 1, 2, 3, 4];
126 let c2 = v2.chunks(2);
127 assert_eq!(c2.count(), 3);
129 let v3: &[i32] = &[];
130 let c3 = v3.chunks(2);
131 assert_eq!(c3.count(), 0);
135 fn test_chunks_nth() {
136 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
137 let mut c = v.chunks(2);
138 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
139 assert_eq!(c.next().unwrap(), &[4, 5]);
141 let v2: &[i32] = &[0, 1, 2, 3, 4];
142 let mut c2 = v2.chunks(3);
143 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
144 assert_eq!(c2.next(), None);
148 fn test_chunks_last() {
149 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
151 assert_eq!(c.last().unwrap()[1], 5);
153 let v2: &[i32] = &[0, 1, 2, 3, 4];
154 let c2 = v2.chunks(2);
155 assert_eq!(c2.last().unwrap()[0], 4);
159 fn test_chunks_zip() {
160 let v1: &[i32] = &[0, 1, 2, 3, 4];
161 let v2: &[i32] = &[6, 7, 8, 9, 10];
163 let res = v1.chunks(2)
165 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
166 .collect::<Vec<_>>();
167 assert_eq!(res, vec![14, 22, 14]);
171 fn test_chunks_mut_count() {
172 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
173 let c = v.chunks_mut(3);
174 assert_eq!(c.count(), 2);
176 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
177 let c2 = v2.chunks_mut(2);
178 assert_eq!(c2.count(), 3);
180 let v3: &mut [i32] = &mut [];
181 let c3 = v3.chunks_mut(2);
182 assert_eq!(c3.count(), 0);
186 fn test_chunks_mut_nth() {
187 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
188 let mut c = v.chunks_mut(2);
189 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
190 assert_eq!(c.next().unwrap(), &[4, 5]);
192 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
193 let mut c2 = v2.chunks_mut(3);
194 assert_eq!(c2.nth(1).unwrap(), &[3, 4]);
195 assert_eq!(c2.next(), None);
199 fn test_chunks_mut_last() {
200 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
201 let c = v.chunks_mut(2);
202 assert_eq!(c.last().unwrap(), &[4, 5]);
204 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
205 let c2 = v2.chunks_mut(2);
206 assert_eq!(c2.last().unwrap(), &[4]);
210 fn test_chunks_mut_zip() {
211 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
212 let v2: &[i32] = &[6, 7, 8, 9, 10];
214 for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) {
215 let sum = b.iter().sum::<i32>();
220 assert_eq!(v1, [13, 14, 19, 20, 14]);
224 fn test_exact_chunks_count() {
225 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
226 let c = v.exact_chunks(3);
227 assert_eq!(c.count(), 2);
229 let v2: &[i32] = &[0, 1, 2, 3, 4];
230 let c2 = v2.exact_chunks(2);
231 assert_eq!(c2.count(), 2);
233 let v3: &[i32] = &[];
234 let c3 = v3.exact_chunks(2);
235 assert_eq!(c3.count(), 0);
239 fn test_exact_chunks_nth() {
240 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
241 let mut c = v.exact_chunks(2);
242 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
243 assert_eq!(c.next().unwrap(), &[4, 5]);
245 let v2: &[i32] = &[0, 1, 2, 3, 4, 5, 6];
246 let mut c2 = v2.exact_chunks(3);
247 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
248 assert_eq!(c2.next(), None);
252 fn test_exact_chunks_last() {
253 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
254 let c = v.exact_chunks(2);
255 assert_eq!(c.last().unwrap(), &[4, 5]);
257 let v2: &[i32] = &[0, 1, 2, 3, 4];
258 let c2 = v2.exact_chunks(2);
259 assert_eq!(c2.last().unwrap(), &[2, 3]);
263 fn test_exact_chunks_zip() {
264 let v1: &[i32] = &[0, 1, 2, 3, 4];
265 let v2: &[i32] = &[6, 7, 8, 9, 10];
267 let res = v1.exact_chunks(2)
268 .zip(v2.exact_chunks(2))
269 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
270 .collect::<Vec<_>>();
271 assert_eq!(res, vec![14, 22]);
275 fn test_exact_chunks_mut_count() {
276 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
277 let c = v.exact_chunks_mut(3);
278 assert_eq!(c.count(), 2);
280 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
281 let c2 = v2.exact_chunks_mut(2);
282 assert_eq!(c2.count(), 2);
284 let v3: &mut [i32] = &mut [];
285 let c3 = v3.exact_chunks_mut(2);
286 assert_eq!(c3.count(), 0);
290 fn test_exact_chunks_mut_nth() {
291 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
292 let mut c = v.exact_chunks_mut(2);
293 assert_eq!(c.nth(1).unwrap(), &[2, 3]);
294 assert_eq!(c.next().unwrap(), &[4, 5]);
296 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4, 5, 6];
297 let mut c2 = v2.exact_chunks_mut(3);
298 assert_eq!(c2.nth(1).unwrap(), &[3, 4, 5]);
299 assert_eq!(c2.next(), None);
303 fn test_exact_chunks_mut_last() {
304 let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
305 let c = v.exact_chunks_mut(2);
306 assert_eq!(c.last().unwrap(), &[4, 5]);
308 let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
309 let c2 = v2.exact_chunks_mut(2);
310 assert_eq!(c2.last().unwrap(), &[2, 3]);
314 fn test_exact_chunks_mut_zip() {
315 let v1: &mut [i32] = &mut [0, 1, 2, 3, 4];
316 let v2: &[i32] = &[6, 7, 8, 9, 10];
318 for (a, b) in v1.exact_chunks_mut(2).zip(v2.exact_chunks(2)) {
319 let sum = b.iter().sum::<i32>();
324 assert_eq!(v1, [13, 14, 19, 20, 4]);
328 fn test_windows_count() {
329 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
330 let c = v.windows(3);
331 assert_eq!(c.count(), 4);
333 let v2: &[i32] = &[0, 1, 2, 3, 4];
334 let c2 = v2.windows(6);
335 assert_eq!(c2.count(), 0);
337 let v3: &[i32] = &[];
338 let c3 = v3.windows(2);
339 assert_eq!(c3.count(), 0);
343 fn test_windows_nth() {
344 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
345 let mut c = v.windows(2);
346 assert_eq!(c.nth(2).unwrap()[1], 3);
347 assert_eq!(c.next().unwrap()[0], 3);
349 let v2: &[i32] = &[0, 1, 2, 3, 4];
350 let mut c2 = v2.windows(4);
351 assert_eq!(c2.nth(1).unwrap()[1], 2);
352 assert_eq!(c2.next(), None);
356 fn test_windows_last() {
357 let v: &[i32] = &[0, 1, 2, 3, 4, 5];
358 let c = v.windows(2);
359 assert_eq!(c.last().unwrap()[1], 5);
361 let v2: &[i32] = &[0, 1, 2, 3, 4];
362 let c2 = v2.windows(2);
363 assert_eq!(c2.last().unwrap()[0], 3);
367 fn test_windows_zip() {
368 let v1: &[i32] = &[0, 1, 2, 3, 4];
369 let v2: &[i32] = &[6, 7, 8, 9, 10];
371 let res = v1.windows(2)
373 .map(|(a, b)| a.iter().sum::<i32>() + b.iter().sum::<i32>())
374 .collect::<Vec<_>>();
376 assert_eq!(res, [14, 18, 22, 26]);
379 // The current implementation of SliceIndex fails to handle methods
380 // orthogonally from range types; therefore, it is worth testing
381 // all of the indexing operations on each input.
383 // This checks all six indexing methods, given an input range that
384 // should succeed. (it is NOT suitable for testing invalid inputs)
385 macro_rules! assert_range_eq {
386 ($arr:expr, $range:expr, $expected:expr)
389 let mut expected = $expected;
392 let expected: &[_] = &expected;
394 assert_eq!(&s[$range], expected, "(in assertion for: index)");
395 assert_eq!(s.get($range), Some(expected), "(in assertion for: get)");
398 s.get_unchecked($range), expected,
399 "(in assertion for: get_unchecked)",
404 let s: &mut [_] = &mut arr;
405 let expected: &mut [_] = &mut expected;
408 &mut s[$range], expected,
409 "(in assertion for: index_mut)",
412 s.get_mut($range), Some(&mut expected[..]),
413 "(in assertion for: get_mut)",
417 s.get_unchecked_mut($range), expected,
418 "(in assertion for: get_unchecked_mut)",
425 // Make sure the macro can actually detect bugs,
426 // because if it can't, then what are we even doing here?
428 // (Be aware this only demonstrates the ability to detect bugs
429 // in the FIRST method that panics, as the macro is not designed
430 // to be used in `should_panic`)
432 #[should_panic(expected = "out of range")]
433 fn assert_range_eq_can_fail_by_panic() {
434 assert_range_eq!([0, 1, 2], 0..5, [0, 1, 2]);
437 // (Be aware this only demonstrates the ability to detect bugs
438 // in the FIRST method it calls, as the macro is not designed
439 // to be used in `should_panic`)
441 #[should_panic(expected = "==")]
442 fn assert_range_eq_can_fail_by_inequality() {
443 assert_range_eq!([0, 1, 2], 0..2, [0, 1, 2]);
446 // Test cases for bad index operations.
448 // This generates `should_panic` test cases for Index/IndexMut
449 // and `None` test cases for get/get_mut.
450 macro_rules! panic_cases {
452 // each test case needs a unique name to namespace the tests
453 in mod $case_name:ident {
458 // one or more similar inputs for which data[input] succeeds,
459 // and the corresponding output as an array. This helps validate
460 // "critical points" where an input range straddles the boundary
461 // between valid and invalid.
462 // (such as the input `len..len`, which is just barely valid)
464 good: data[$good:expr] == $output:expr;
467 bad: data[$bad:expr];
468 message: $expect_msg:expr;
476 $( assert_range_eq!($data, $good, $output); )*
480 assert_eq!(v.get($bad), None, "(in None assertion for get)");
484 let v: &mut [_] = &mut v;
485 assert_eq!(v.get_mut($bad), None, "(in None assertion for get_mut)");
490 #[should_panic(expected = $expect_msg)]
498 #[should_panic(expected = $expect_msg)]
499 fn index_mut_fail() {
501 let v: &mut [_] = &mut v;
502 let _v = &mut v[$bad];
510 let v = [0, 1, 2, 3, 4, 5];
512 assert_range_eq!(v, .., [0, 1, 2, 3, 4, 5]);
513 assert_range_eq!(v, ..2, [0, 1]);
514 assert_range_eq!(v, ..=1, [0, 1]);
515 assert_range_eq!(v, 2.., [2, 3, 4, 5]);
516 assert_range_eq!(v, 1..4, [1, 2, 3]);
517 assert_range_eq!(v, 1..=3, [1, 2, 3]);
521 in mod rangefrom_len {
522 data: [0, 1, 2, 3, 4, 5];
524 good: data[6..] == [];
526 message: "but ends at"; // perhaps not ideal
530 data: [0, 1, 2, 3, 4, 5];
532 good: data[..6] == [0, 1, 2, 3, 4, 5];
534 message: "out of range";
537 in mod rangetoinclusive_len {
538 data: [0, 1, 2, 3, 4, 5];
540 good: data[..=5] == [0, 1, 2, 3, 4, 5];
542 message: "out of range";
545 in mod range_len_len {
546 data: [0, 1, 2, 3, 4, 5];
548 good: data[6..6] == [];
550 message: "out of range";
553 in mod rangeinclusive_len_len {
554 data: [0, 1, 2, 3, 4, 5];
556 good: data[6..=5] == [];
558 message: "out of range";
563 in mod range_neg_width {
564 data: [0, 1, 2, 3, 4, 5];
566 good: data[4..4] == [];
568 message: "but ends at";
571 in mod rangeinclusive_neg_width {
572 data: [0, 1, 2, 3, 4, 5];
574 good: data[4..=3] == [];
576 message: "but ends at";
581 in mod rangeinclusive_overflow {
584 // note: using 0 specifically ensures that the result of overflowing is 0..0,
585 // so that `get` doesn't simply return None for the wrong reason.
586 bad: data[0 ..= ::std::usize::MAX];
587 message: "maximum usize";
590 in mod rangetoinclusive_overflow {
593 bad: data[..= ::std::usize::MAX];
594 message: "maximum usize";
600 fn test_find_rfind() {
601 let v = [0, 1, 2, 3, 4, 5];
602 let mut iter = v.iter();
604 while let Some(&elt) = iter.rfind(|_| true) {
606 assert_eq!(elt, v[i]);
609 assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
613 fn test_iter_folds() {
614 let a = [1, 2, 3, 4, 5]; // len>4 so the unroll is used
615 assert_eq!(a.iter().fold(0, |acc, &x| 2*acc + x), 57);
616 assert_eq!(a.iter().rfold(0, |acc, &x| 2*acc + x), 129);
617 let fold = |acc: i32, &x| acc.checked_mul(2)?.checked_add(x);
618 assert_eq!(a.iter().try_fold(0, &fold), Some(57));
619 assert_eq!(a.iter().try_rfold(0, &fold), Some(129));
621 // short-circuiting try_fold, through other methods
622 let a = [0, 1, 2, 3, 5, 5, 5, 7, 8, 9];
623 let mut iter = a.iter();
624 assert_eq!(iter.position(|&x| x == 3), Some(3));
625 assert_eq!(iter.rfind(|&&x| x == 5), Some(&5));
626 assert_eq!(iter.len(), 2);
630 fn test_rotate_left() {
631 const N: usize = 600;
632 let a: &mut [_] = &mut [0; N];
641 assert_eq!(a[(i + k) % N], i);
646 fn test_rotate_right() {
647 const N: usize = 600;
648 let a: &mut [_] = &mut [0; N];
656 assert_eq!(a[(i + 42) % N], i);
661 #[cfg(not(target_arch = "wasm32"))]
663 use core::cmp::Ordering::{Equal, Greater, Less};
664 use core::slice::heapsort;
665 use rand::{Rng, XorShiftRng};
667 let mut v = [0; 600];
668 let mut tmp = [0; 600];
669 let mut rng = XorShiftRng::new_unseeded();
671 for len in (2..25).chain(500..510) {
672 let v = &mut v[0..len];
673 let tmp = &mut tmp[0..len];
675 for &modulus in &[5, 10, 100, 1000] {
678 v[i] = rng.gen::<i32>() % modulus;
681 // Sort in default order.
682 tmp.copy_from_slice(v);
684 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
686 // Sort in ascending order.
687 tmp.copy_from_slice(v);
688 tmp.sort_unstable_by(|a, b| a.cmp(b));
689 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
691 // Sort in descending order.
692 tmp.copy_from_slice(v);
693 tmp.sort_unstable_by(|a, b| b.cmp(a));
694 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
696 // Test heapsort using `<` operator.
697 tmp.copy_from_slice(v);
698 heapsort(tmp, |a, b| a < b);
699 assert!(tmp.windows(2).all(|w| w[0] <= w[1]));
701 // Test heapsort using `>` operator.
702 tmp.copy_from_slice(v);
703 heapsort(tmp, |a, b| a > b);
704 assert!(tmp.windows(2).all(|w| w[0] >= w[1]));
709 // Sort using a completely random comparison function.
710 // This will reorder the elements *somehow*, but won't panic.
711 for i in 0..v.len() {
714 v.sort_unstable_by(|_, _| *rng.choose(&[Less, Equal, Greater]).unwrap());
716 for i in 0..v.len() {
717 assert_eq!(v[i], i as i32);
721 [0i32; 0].sort_unstable();
722 [(); 10].sort_unstable();
723 [(); 100].sort_unstable();
725 let mut v = [0xDEADBEEFu64];
727 assert!(v == [0xDEADBEEF]);
731 use core::slice::memchr::{memchr, memrchr};
733 // test fallback implementations on all platforms
736 assert_eq!(Some(0), memchr(b'a', b"a"));
741 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
746 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
751 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
755 fn matches_past_nul() {
756 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
760 fn no_match_empty() {
761 assert_eq!(None, memchr(b'a', b""));
766 assert_eq!(None, memchr(b'a', b"xyz"));
770 fn matches_one_reversed() {
771 assert_eq!(Some(0), memrchr(b'a', b"a"));
775 fn matches_begin_reversed() {
776 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
780 fn matches_end_reversed() {
781 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
785 fn matches_nul_reversed() {
786 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
790 fn matches_past_nul_reversed() {
791 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
795 fn no_match_empty_reversed() {
796 assert_eq!(None, memrchr(b'a', b""));
800 fn no_match_reversed() {
801 assert_eq!(None, memrchr(b'a', b"xyz"));
805 fn each_alignment_reversed() {
806 let mut data = [1u8; 64];
811 assert_eq!(Some(pos - start), memrchr(needle, &data[start..]));
817 fn test_align_to_simple() {
818 let bytes = [1u8, 2, 3, 4, 5, 6, 7];
819 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<u16>() };
820 assert_eq!(aligned.len(), 3);
821 assert!(prefix == [1] || suffix == [7]);
822 let expect1 = [1 << 8 | 2, 3 << 8 | 4, 5 << 8 | 6];
823 let expect2 = [1 | 2 << 8, 3 | 4 << 8, 5 | 6 << 8];
824 let expect3 = [2 << 8 | 3, 4 << 8 | 5, 6 << 8 | 7];
825 let expect4 = [2 | 3 << 8, 4 | 5 << 8, 6 | 7 << 8];
826 assert!(aligned == expect1 || aligned == expect2 || aligned == expect3 || aligned == expect4,
827 "aligned={:?} expected={:?} || {:?} || {:?} || {:?}",
828 aligned, expect1, expect2, expect3, expect4);
832 fn test_align_to_zst() {
833 let bytes = [1, 2, 3, 4, 5, 6, 7];
834 let (prefix, aligned, suffix) = unsafe { bytes.align_to::<()>() };
835 assert_eq!(aligned.len(), 0);
836 assert!(prefix == [1, 2, 3, 4, 5, 6, 7] || suffix == [1, 2, 3, 4, 5, 6, 7]);
840 fn test_align_to_non_trivial() {
841 #[repr(align(8))] struct U64(u64, u64);
842 #[repr(align(8))] struct U64U64U32(u64, u64, u32);
843 let data = [U64(1, 2), U64(3, 4), U64(5, 6), U64(7, 8), U64(9, 10), U64(11, 12), U64(13, 14),
845 let (prefix, aligned, suffix) = unsafe { data.align_to::<U64U64U32>() };
846 assert_eq!(aligned.len(), 4);
847 assert_eq!(prefix.len() + suffix.len(), 2);