]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rec-align-u64.rs
Add a few more derivings to AST types
[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(target_os = "linux")]
40 #[cfg(target_os = "macos")]
41 #[cfg(target_os = "freebsd")]
42 mod m {
43     #[cfg(target_arch = "x86")]
44     pub mod m {
45         pub fn align() -> uint { 4u }
46         pub fn size() -> uint { 12u }
47     }
48
49     #[cfg(target_arch = "x86_64")]
50     pub mod m {
51         pub fn align() -> uint { 8u }
52         pub fn size() -> uint { 16u }
53     }
54 }
55
56 #[cfg(target_os = "win32")]
57 mod m {
58     #[cfg(target_arch = "x86")]
59     pub mod m {
60         pub fn align() -> uint { 8u }
61         pub fn size() -> uint { 16u }
62     }
63 }
64
65 #[cfg(target_os = "android")]
66 mod m {
67     #[cfg(target_arch = "arm")]
68     pub mod m {
69         pub fn align() -> uint { 8u }
70         pub fn size() -> uint { 16u }
71     }
72 }
73
74 pub fn main() {
75     unsafe {
76         let x = Outer {c8: 22u8, t: Inner {c64: 44u64}};
77
78         // Send it through the shape code
79         let y = format!("{:?}", x);
80
81         println!("align inner = {}", rusti::min_align_of::<Inner>());
82         println!("size outer = {}", mem::size_of::<Outer>());
83         println!("y = {}", y);
84
85         // per clang/gcc the alignment of `Inner` is 4 on x86.
86         assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
87
88         // per clang/gcc the size of `Outer` should be 12
89         // because `Inner`s alignment was 4.
90         assert_eq!(mem::size_of::<Outer>(), m::m::size());
91
92         assert_eq!(y, "Outer{c8: 22u8, t: Inner{c64: 44u64}}".to_string());
93     }
94 }