]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rec-align-u32.rs
auto merge of #19648 : mquandalle/rust/patch-1, r=alexcrichton
[rust.git] / src / test / run-pass / rec-align-u32.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 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 #[deriving(Show)]
26 struct Inner {
27     c64: u32
28 }
29
30 // This is the type that contains the type with the
31 // questionable alignment, for testing
32 #[deriving(Show)]
33 struct Outer {
34     c8: u8,
35     t: Inner
36 }
37
38
39 #[cfg(any(target_arch = "x86", target_arch = "arm"))]
40 mod m {
41     pub fn align() -> uint { 4u }
42     pub fn size() -> uint { 8u }
43 }
44
45 #[cfg(target_arch = "x86_64")]
46 mod m {
47     pub fn align() -> uint { 4u }
48     pub fn size() -> uint { 8u }
49 }
50
51 pub fn main() {
52     unsafe {
53         let x = Outer {c8: 22u8, t: Inner {c64: 44u32}};
54
55         // Send it through the shape code
56         let y = format!("{}", x);
57
58         println!("align inner = {}", rusti::min_align_of::<Inner>());
59         println!("size outer = {}", mem::size_of::<Outer>());
60         println!("y = {}", y);
61
62         // per clang/gcc the alignment of `inner` is 4 on x86.
63         assert_eq!(rusti::min_align_of::<Inner>(), m::align());
64
65         // per clang/gcc the size of `outer` should be 12
66         // because `inner`s alignment was 4.
67         assert_eq!(mem::size_of::<Outer>(), m::size());
68
69         assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
70     }
71 }