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