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