]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/mir_raw_fat_ptr.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / test / run-pass / mir_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 #![feature(rustc_attrs)]
12
13 // check raw fat pointer ops in mir
14 // FIXME: please improve this when we get monomorphization support
15
16 use std::mem;
17
18 #[derive(Debug, PartialEq, Eq)]
19 struct ComparisonResults {
20 lt: bool,
21 le: bool,
22 gt: bool,
23 ge: bool,
24 eq: bool,
25 ne: bool
26 }
27
28 const LT: ComparisonResults = ComparisonResults {
29 lt: true,
30 le: true,
31 gt: false,
32 ge: false,
33 eq: false,
34 ne: true
35 };
36
37 const EQ: ComparisonResults = ComparisonResults {
38 lt: false,
39 le: true,
40 gt: false,
41 ge: true,
42 eq: true,
43 ne: false
44 };
45
46 const GT: ComparisonResults = ComparisonResults {
47 lt: false,
48 le: false,
49 gt: true,
50 ge: true,
51 eq: false,
52 ne: true
53 };
54
55 #[rustc_mir]
56 fn compare_su8(a: *const S<[u8]>, b: *const S<[u8]>) -> ComparisonResults {
57 ComparisonResults {
58 lt: a < b,
59 le: a <= b,
60 gt: a > b,
61 ge: a >= b,
62 eq: a == b,
63 ne: a != b
64 }
65 }
66
67 #[rustc_mir]
68 fn compare_au8(a: *const [u8], b: *const [u8]) -> ComparisonResults {
69 ComparisonResults {
70 lt: a < b,
71 le: a <= b,
72 gt: a > b,
73 ge: a >= b,
74 eq: a == b,
75 ne: a != b
76 }
77 }
78
79 #[rustc_mir]
80 fn compare_foo<'a>(a: *const (Foo+'a), b: *const (Foo+'a)) -> ComparisonResults {
81 ComparisonResults {
82 lt: a < b,
83 le: a <= b,
84 gt: a > b,
85 ge: a >= b,
86 eq: a == b,
87 ne: a != b
88 }
89 }
90
91 #[rustc_mir]
92 fn simple_eq<'a>(a: *const (Foo+'a), b: *const (Foo+'a)) -> bool {
93 let result = a == b;
94 result
95 }
96
97 fn assert_inorder<T: Copy>(a: &[T],
98 compare: fn(T, T) -> ComparisonResults) {
99 for i in 0..a.len() {
100 for j in 0..a.len() {
101 let cres = compare(a[i], a[j]);
102 if i < j {
103 assert_eq!(cres, LT);
104 } else if i == j {
105 assert_eq!(cres, EQ);
106 } else {
107 assert_eq!(cres, GT);
108 }
109 }
110 }
111 }
112
113 trait Foo { fn foo(&self) -> usize; }
114 impl<T> Foo for T {
115 fn foo(&self) -> usize {
116 mem::size_of::<T>()
117 }
118 }
119
120 struct S<T:?Sized>(u32, T);
121
122 fn main() {
123 let array = [0,1,2,3,4];
124 let array2 = [5,6,7,8,9];
125
126 // fat ptr comparison: addr then extra
127
128 // check ordering for arrays
129 let mut ptrs: Vec<*const [u8]> = vec![
130 &array[0..0], &array[0..1], &array, &array[1..]
131 ];
132
133 let array_addr = &array as *const [u8] as *const u8 as usize;
134 let array2_addr = &array2 as *const [u8] as *const u8 as usize;
135 if array2_addr < array_addr {
136 ptrs.insert(0, &array2);
137 } else {
138 ptrs.push(&array2);
139 }
140 assert_inorder(&ptrs, compare_au8);
141
142 let u8_ = (0u8, 1u8);
143 let u32_ = (4u32, 5u32);
144
145 // check ordering for ptrs
146 let buf: &mut [*const Foo] = &mut [
147 &u8_, &u8_.0,
148 &u32_, &u32_.0,
149 ];
150 buf.sort_by(|u,v| {
151 let u : [*const (); 2] = unsafe { mem::transmute(*u) };
152 let v : [*const (); 2] = unsafe { mem::transmute(*v) };
153 u.cmp(&v)
154 });
155 assert_inorder(buf, compare_foo);
156
157 // check ordering for structs containing arrays
158 let ss: (S<[u8; 2]>,
159 S<[u8; 3]>,
160 S<[u8; 2]>) = (
161 S(7, [8, 9]),
162 S(10, [11, 12, 13]),
163 S(4, [5, 6])
164 );
165 assert_inorder(&[
166 &ss.0 as *const S<[u8]>,
167 &ss.1 as *const S<[u8]>,
168 &ss.2 as *const S<[u8]>
169 ], compare_su8);
170
171 assert!(simple_eq(&0u8 as *const _, &0u8 as *const _));
172 assert!(!simple_eq(&0u8 as *const _, &1u8 as *const _));
173 }