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