]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/type-sizes.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / type-sizes.rs
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.
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 use std::mem::size_of;
12
13 struct t {a: u8, b: i8}
14 struct u {a: u8, b: i8, c: u8}
15 struct v {a: u8, b: i8, c: v2, d: u32}
16 struct v2 {u: char, v: u8}
17 struct w {a: int, b: ()}
18 struct x {a: int, b: (), c: ()}
19 struct y {x: int}
20
21 enum e1 {
22     a(u8, u32), b(u32), c
23 }
24 enum e2 {
25     a(u32), b
26 }
27
28 enum e3 {
29     a([u16; 0], u8), b
30 }
31
32 pub fn main() {
33     assert_eq!(size_of::<u8>(), 1 as uint);
34     assert_eq!(size_of::<u32>(), 4 as uint);
35     assert_eq!(size_of::<char>(), 4 as uint);
36     assert_eq!(size_of::<i8>(), 1 as uint);
37     assert_eq!(size_of::<i32>(), 4 as uint);
38     assert_eq!(size_of::<t>(), 2 as uint);
39     assert_eq!(size_of::<u>(), 3 as uint);
40     // Alignment causes padding before the char and the u32.
41
42     assert!(size_of::<v>() ==
43                 16 as uint);
44     assert_eq!(size_of::<int>(), size_of::<uint>());
45     assert_eq!(size_of::<w>(), size_of::<int>());
46     assert_eq!(size_of::<x>(), size_of::<int>());
47     assert_eq!(size_of::<int>(), size_of::<y>());
48
49     // Make sure enum types are the appropriate size, mostly
50     // around ensuring alignment is handled properly
51
52     assert_eq!(size_of::<e1>(), 8 as uint);
53     assert_eq!(size_of::<e2>(), 8 as uint);
54     assert_eq!(size_of::<e3>(), 4 as uint);
55 }