]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tag-align-dyn-variants.rs
rustdoc: Replace no-pretty-expanded with pretty-expanded
[rust.git] / src / test / run-pass / tag-align-dyn-variants.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 // pretty-expanded FIXME #23616
12
13 use std::mem;
14
15 enum Tag<A,B> {
16     VarA(A),
17     VarB(B),
18 }
19
20 struct Rec<A,B> {
21     chA: u8,
22     tA: Tag<A,B>,
23     chB: u8,
24     tB: Tag<A,B>,
25 }
26
27 fn mk_rec<A,B>(a: A, b: B) -> Rec<A,B> {
28     Rec { chA:0, tA:Tag::VarA(a), chB:1, tB:Tag::VarB(b) }
29 }
30
31 fn is_aligned<A>(amnt: uint, u: &A) -> bool {
32     let p: uint = unsafe { mem::transmute(u) };
33     return (p & (amnt-1)) == 0;
34 }
35
36 fn variant_data_is_aligned<A,B>(amnt: uint, u: &Tag<A,B>) -> bool {
37     match u {
38       &Tag::VarA(ref a) => is_aligned(amnt, a),
39       &Tag::VarB(ref b) => is_aligned(amnt, b)
40     }
41 }
42
43 pub fn main() {
44     let u64_align = std::mem::min_align_of::<u64>();
45     let x = mk_rec(22u64, 23u64);
46     assert!(is_aligned(u64_align, &x.tA));
47     assert!(variant_data_is_aligned(u64_align, &x.tA));
48     assert!(is_aligned(u64_align, &x.tB));
49     assert!(variant_data_is_aligned(u64_align, &x.tB));
50
51     let x = mk_rec(22u64, 23u32);
52     assert!(is_aligned(u64_align, &x.tA));
53     assert!(variant_data_is_aligned(u64_align, &x.tA));
54     assert!(is_aligned(u64_align, &x.tB));
55     assert!(variant_data_is_aligned(4, &x.tB));
56
57     let x = mk_rec(22u32, 23u64);
58     assert!(is_aligned(u64_align, &x.tA));
59     assert!(variant_data_is_aligned(4, &x.tA));
60     assert!(is_aligned(u64_align, &x.tB));
61     assert!(variant_data_is_aligned(u64_align, &x.tB));
62
63     let x = mk_rec(22u32, 23u32);
64     assert!(is_aligned(4, &x.tA));
65     assert!(variant_data_is_aligned(4, &x.tA));
66     assert!(is_aligned(4, &x.tB));
67     assert!(variant_data_is_aligned(4, &x.tB));
68
69     let x = mk_rec(22f64, 23f64);
70     assert!(is_aligned(u64_align, &x.tA));
71     assert!(variant_data_is_aligned(u64_align, &x.tA));
72     assert!(is_aligned(u64_align, &x.tB));
73     assert!(variant_data_is_aligned(u64_align, &x.tB));
74 }