]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/enum-discrim-manual-sizing.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass / enum-discrim-manual-sizing.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2013 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
c34b1796 11
54a0048b 12use std::mem::{size_of, align_of};
1a4d82fc
JJ
13
14#[repr(i8)]
15enum Ei8 {
16 Ai8 = 0,
17 Bi8 = 1
18}
19
20#[repr(u8)]
21enum Eu8 {
22 Au8 = 0,
23 Bu8 = 1
24}
25
26#[repr(i16)]
27enum Ei16 {
28 Ai16 = 0,
29 Bi16 = 1
30}
31
32#[repr(u16)]
33enum Eu16 {
34 Au16 = 0,
35 Bu16 = 1
36}
37
38#[repr(i32)]
39enum Ei32 {
40 Ai32 = 0,
41 Bi32 = 1
42}
43
44#[repr(u32)]
45enum Eu32 {
46 Au32 = 0,
47 Bu32 = 1
48}
49
50#[repr(i64)]
51enum Ei64 {
52 Ai64 = 0,
53 Bi64 = 1
54}
55
56#[repr(u64)]
57enum Eu64 {
58 Au64 = 0,
59 Bu64 = 1
60}
61
c34b1796 62#[repr(isize)]
1a4d82fc
JJ
63enum Eint {
64 Aint = 0,
65 Bint = 1
66}
67
c34b1796 68#[repr(usize)]
1a4d82fc
JJ
69enum Euint {
70 Auint = 0,
71 Buint = 1
72}
73
54a0048b
SL
74#[repr(u8)]
75enum Eu8NonCLike<T> {
76 _None,
77 _Some(T),
78}
79
80#[repr(i64)]
81enum Ei64NonCLike<T> {
82 _None,
83 _Some(T),
84}
85
86#[repr(u64)]
87enum Eu64NonCLike<T> {
88 _None,
89 _Some(T),
90}
91
1a4d82fc
JJ
92pub fn main() {
93 assert_eq!(size_of::<Ei8>(), 1);
94 assert_eq!(size_of::<Eu8>(), 1);
95 assert_eq!(size_of::<Ei16>(), 2);
96 assert_eq!(size_of::<Eu16>(), 2);
97 assert_eq!(size_of::<Ei32>(), 4);
98 assert_eq!(size_of::<Eu32>(), 4);
99 assert_eq!(size_of::<Ei64>(), 8);
100 assert_eq!(size_of::<Eu64>(), 8);
c34b1796
AL
101 assert_eq!(size_of::<Eint>(), size_of::<isize>());
102 assert_eq!(size_of::<Euint>(), size_of::<usize>());
54a0048b
SL
103 assert_eq!(size_of::<Eu8NonCLike<()>>(), 1);
104 assert_eq!(size_of::<Ei64NonCLike<()>>(), 8);
105 assert_eq!(size_of::<Eu64NonCLike<()>>(), 8);
106 let u8_expected_size = round_up(9, align_of::<Eu64NonCLike<u8>>());
107 assert_eq!(size_of::<Eu64NonCLike<u8>>(), u8_expected_size);
108 let array_expected_size = round_up(28, align_of::<Eu64NonCLike<[u32; 5]>>());
109 assert_eq!(size_of::<Eu64NonCLike<[u32; 5]>>(), array_expected_size);
110 assert_eq!(size_of::<Eu64NonCLike<[u32; 6]>>(), 32);
111}
112
113// Rounds x up to the next multiple of a
114fn round_up(x: usize, a: usize) -> usize {
115 ((x + (a - 1)) / a) * a
1a4d82fc 116}