1 // Copyright 2012 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.
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.
12 use std::mem::size_of;
14 struct t {a: u8, b: i8}
15 struct u {a: u8, b: i8, c: u8}
16 struct v {a: u8, b: i8, c: v2, d: u32}
17 struct v2 {u: char, v: u8}
18 struct w {a: isize, b: ()}
19 struct x {a: isize, b: (), c: ()}
34 // Test struct field reordering to make sure it actually reorders.
35 struct WillOptimize1(u8, u16, u8);
36 struct WillOptimize2 { a: u8, b: u16, c: u8}
39 assert_eq!(size_of::<u8>(), 1 as usize);
40 assert_eq!(size_of::<u32>(), 4 as usize);
41 assert_eq!(size_of::<char>(), 4 as usize);
42 assert_eq!(size_of::<i8>(), 1 as usize);
43 assert_eq!(size_of::<i32>(), 4 as usize);
44 assert_eq!(size_of::<t>(), 2 as usize);
45 assert_eq!(size_of::<u>(), 3 as usize);
46 // Alignment causes padding before the char and the u32.
48 assert_eq!(size_of::<v>(),
50 assert_eq!(size_of::<isize>(), size_of::<usize>());
51 assert_eq!(size_of::<w>(), size_of::<isize>());
52 assert_eq!(size_of::<x>(), size_of::<isize>());
53 assert_eq!(size_of::<isize>(), size_of::<y>());
55 // Make sure enum types are the appropriate size, mostly
56 // around ensuring alignment is handled properly
58 assert_eq!(size_of::<e1>(), 8 as usize);
59 assert_eq!(size_of::<e2>(), 8 as usize);
60 assert_eq!(size_of::<e3>(), 4 as usize);
62 assert_eq!(size_of::<WillOptimize1>(), 4);
63 assert_eq!(size_of::<WillOptimize2>(), 4);