]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rec-align-u64.rs
std: Rename Show/String to Debug/Display
[rust.git] / src / test / run-pass / rec-align-u64.rs
1 // Copyright 2012-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 // Issue #2303
12
13 #![feature(intrinsics)]
14
15 use std::mem;
16
17 mod rusti {
18     extern "rust-intrinsic" {
19         pub fn pref_align_of<T>() -> uint;
20         pub fn min_align_of<T>() -> uint;
21     }
22 }
23
24 // This is the type with the questionable alignment
25 #[derive(Debug)]
26 struct Inner {
27     c64: u64
28 }
29
30 // This is the type that contains the type with the
31 // questionable alignment, for testing
32 #[derive(Debug)]
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", target_arch = "aarch64"))]
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         let y = format!("{:?}", x);
86
87         println!("align inner = {:?}", rusti::min_align_of::<Inner>());
88         println!("size outer = {:?}", mem::size_of::<Outer>());
89         println!("y = {:?}", y);
90
91         // per clang/gcc the alignment of `Inner` is 4 on x86.
92         assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
93
94         // per clang/gcc the size of `Outer` should be 12
95         // because `Inner`s alignment was 4.
96         assert_eq!(mem::size_of::<Outer>(), m::m::size());
97
98         assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
99     }
100 }