]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rec-align-u64.rs
auto merge of #17888 : gmfawcett/rust/patch-1, r=alexcrichton
[rust.git] / src / test / run-pass / rec-align-u64.rs
1 // Copyright 2012-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.
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 // Issue #2303
12
13 #![feature(intrinsics)]
14
15 extern crate debug;
16
17 use std::mem;
18
19 mod rusti {
20     extern "rust-intrinsic" {
21         pub fn pref_align_of<T>() -> uint;
22         pub fn min_align_of<T>() -> uint;
23     }
24 }
25
26 // This is the type with the questionable alignment
27 struct Inner {
28     c64: u64
29 }
30
31 // This is the type that contains the type with the
32 // questionable alignment, for testing
33 struct Outer {
34     c8: u8,
35     t: Inner
36 }
37
38
39 #[cfg(any(target_os = "linux",
40           target_os = "macos",
41           target_os = "freebsd",
42           target_os = "dragonfly"))]
43 mod m {
44     #[cfg(target_arch = "x86")]
45     pub mod m {
46         pub fn align() -> uint { 4u }
47         pub fn size() -> uint { 12u }
48     }
49
50     #[cfg(any(target_arch = "x86_64", target_arch = "arm"))]
51     pub mod m {
52         pub fn align() -> uint { 8u }
53         pub fn size() -> uint { 16u }
54     }
55 }
56
57 #[cfg(target_os = "windows")]
58 mod m {
59     #[cfg(target_arch = "x86")]
60     pub mod m {
61         pub fn align() -> uint { 8u }
62         pub fn size() -> uint { 16u }
63     }
64
65     #[cfg(target_arch = "x86_64")]
66     pub mod m {
67         pub fn align() -> uint { 8u }
68         pub fn size() -> uint { 16u }
69     }
70 }
71
72 #[cfg(target_os = "android")]
73 mod m {
74     #[cfg(target_arch = "arm")]
75     pub mod m {
76         pub fn align() -> uint { 8u }
77         pub fn size() -> uint { 16u }
78     }
79 }
80
81 pub fn main() {
82     unsafe {
83         let x = Outer {c8: 22u8, t: Inner {c64: 44u64}};
84
85         // Send it through the shape code
86         let y = format!("{:?}", x);
87
88         println!("align inner = {}", rusti::min_align_of::<Inner>());
89         println!("size outer = {}", mem::size_of::<Outer>());
90         println!("y = {}", y);
91
92         // per clang/gcc the alignment of `Inner` is 4 on x86.
93         assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
94
95         // per clang/gcc the size of `Outer` should be 12
96         // because `Inner`s alignment was 4.
97         assert_eq!(mem::size_of::<Outer>(), m::m::size());
98
99         assert_eq!(y, "Outer{c8: 22u8, t: Inner{c64: 44u64}}".to_string());
100     }
101 }