]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rec-align-u32.rs
auto merge of #13600 : brandonw/rust/master, r=brson
[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
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: u32
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_arch = "x86")]
37 #[cfg(target_arch = "arm")]
38 mod m {
39     pub fn align() -> uint { 4u }
40     pub fn size() -> uint { 8u }
41 }
42
43 #[cfg(target_arch = "x86_64")]
44 mod m {
45     pub fn align() -> uint { 4u }
46     pub fn size() -> uint { 8u }
47 }
48
49 pub fn main() {
50     unsafe {
51         let x = Outer {c8: 22u8, t: Inner {c64: 44u32}};
52
53         // Send it through the shape code
54         let y = format!("{:?}", x);
55
56         println!("align inner = {:?}", rusti::min_align_of::<Inner>());
57         println!("size outer = {:?}", mem::size_of::<Outer>());
58         println!("y = {}", y);
59
60         // per clang/gcc the alignment of `inner` is 4 on x86.
61         assert_eq!(rusti::min_align_of::<Inner>(), m::align());
62
63         // per clang/gcc the size of `outer` should be 12
64         // because `inner`s alignment was 4.
65         assert_eq!(mem::size_of::<Outer>(), m::size());
66
67         assert_eq!(y, ~"Outer{c8: 22u8, t: Inner{c64: 44u32}}");
68     }
69 }