]> git.proxmox.com Git - rustc.git/blob - src/test/ui/stdlib-unit-tests/raw-fat-ptr.rs
Update upstream source from tag 'upstream/1.64.0+dfsg1'
[rustc.git] / src / test / ui / stdlib-unit-tests / raw-fat-ptr.rs
1 // run-pass
2 // check raw fat pointer ops
3
4 use std::mem;
5
6 fn assert_inorder<T: PartialEq + PartialOrd>(a: &[T]) {
7 for i in 0..a.len() {
8 for j in 0..a.len() {
9 if i < j {
10 assert!(a[i] < a[j]);
11 assert!(a[i] <= a[j]);
12 assert!(!(a[i] == a[j]));
13 assert!(a[i] != a[j]);
14 assert!(!(a[i] >= a[j]));
15 assert!(!(a[i] > a[j]));
16 } else if i == j {
17 assert!(!(a[i] < a[j]));
18 assert!(a[i] <= a[j]);
19 assert!(a[i] == a[j]);
20 assert!(!(a[i] != a[j]));
21 assert!(a[i] >= a[j]);
22 assert!(!(a[i] > a[j]));
23 } else {
24 assert!(!(a[i] < a[j]));
25 assert!(!(a[i] <= a[j]));
26 assert!(!(a[i] == a[j]));
27 assert!(a[i] != a[j]);
28 assert!(a[i] >= a[j]);
29 assert!(a[i] > a[j]);
30 }
31 }
32 }
33 }
34
35 trait Foo { fn foo(&self) -> usize; }
36 impl<T> Foo for T {
37 fn foo(&self) -> usize {
38 mem::size_of::<T>()
39 }
40 }
41
42 #[allow(unused_tuple_struct_fields)]
43 struct S<T:?Sized>(u32, T);
44
45 fn main() {
46 let mut array = [0,1,2,3,4];
47 let mut array2 = [5,6,7,8,9];
48
49 // fat ptr comparison: addr then extra
50
51 // check ordering for arrays
52 let mut ptrs: Vec<*const [u8]> = vec![
53 &array[0..0], &array[0..1], &array, &array[1..]
54 ];
55
56 let array_addr = &array as *const [u8] as *const u8 as usize;
57 let array2_addr = &array2 as *const [u8] as *const u8 as usize;
58 if array2_addr < array_addr {
59 ptrs.insert(0, &array2);
60 } else {
61 ptrs.push(&array2);
62 }
63 assert_inorder(&ptrs);
64
65 // check ordering for mut arrays
66 let mut ptrs: Vec<*mut [u8]> = vec![
67 &mut array[0..0], &mut array[0..1], &mut array, &mut array[1..]
68 ];
69
70 let array_addr = &mut array as *mut [u8] as *mut u8 as usize;
71 let array2_addr = &mut array2 as *mut [u8] as *mut u8 as usize;
72 if array2_addr < array_addr {
73 ptrs.insert(0, &mut array2);
74 } else {
75 ptrs.push(&mut array2);
76 }
77 assert_inorder(&ptrs);
78
79 let mut u8_ = (0u8, 1u8);
80 let mut u32_ = (4u32, 5u32);
81
82 // check ordering for ptrs
83 let buf: &mut [*const dyn Foo] = &mut [
84 &u8_, &u8_.0,
85 &u32_, &u32_.0,
86 ];
87 buf.sort_by(|u,v| {
88 let u : [*const (); 2] = unsafe { mem::transmute(*u) };
89 let v : [*const (); 2] = unsafe { mem::transmute(*v) };
90 u.cmp(&v)
91 });
92 assert_inorder(buf);
93
94 // check ordering for mut ptrs
95 let buf: &mut [*mut dyn Foo] = &mut [
96 &mut u8_, &mut u8_.0,
97 &mut u32_, &mut u32_.0,
98 ];
99 buf.sort_by(|u,v| {
100 let u : [*const (); 2] = unsafe { mem::transmute(*u) };
101 let v : [*const (); 2] = unsafe { mem::transmute(*v) };
102 u.cmp(&v)
103 });
104 assert_inorder(buf);
105
106 // check ordering for structs containing arrays
107 let ss: (S<[u8; 2]>,
108 S<[u8; 3]>,
109 S<[u8; 2]>) = (
110 S(7, [8, 9]),
111 S(10, [11, 12, 13]),
112 S(4, [5, 6])
113 );
114 assert_inorder(&[
115 &ss.0 as *const S<[u8]>,
116 &ss.1 as *const S<[u8]>,
117 &ss.2 as *const S<[u8]>
118 ]);
119 }