]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tag-align-dyn-variants.rs
after syntax fixes, these tests appear to pass
[rust.git] / src / test / run-pass / tag-align-dyn-variants.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 enum a_tag<A,B> {
12     varA(A),
13     varB(B)
14 }
15
16 struct t_rec<A,B> {
17     chA: u8,
18     tA: a_tag<A,B>,
19     chB: u8,
20     tB: a_tag<A,B>
21 }
22
23 fn mk_rec<A:Copy,B:Copy>(a: A, b: B) -> t_rec<A,B> {
24     return t_rec{ chA:0u8, tA:varA(a), chB:1u8, tB:varB(b) };
25 }
26
27 fn is_aligned<A>(amnt: uint, u: &A) -> bool {
28     let p = ptr::to_unsafe_ptr(u) as uint;
29     return (p & (amnt-1u)) == 0u;
30 }
31
32 fn variant_data_is_aligned<A,B>(amnt: uint, u: &a_tag<A,B>) -> bool {
33     match u {
34       &varA(ref a) => is_aligned(amnt, a),
35       &varB(ref b) => is_aligned(amnt, b)
36     }
37 }
38
39 pub fn main() {
40     let x = mk_rec(22u64, 23u64);
41     assert!(is_aligned(8u, &x.tA));
42     assert!(variant_data_is_aligned(8u, &x.tA));
43     assert!(is_aligned(8u, &x.tB));
44     assert!(variant_data_is_aligned(8u, &x.tB));
45
46     let x = mk_rec(22u64, 23u32);
47     assert!(is_aligned(8u, &x.tA));
48     assert!(variant_data_is_aligned(8u, &x.tA));
49     assert!(is_aligned(8u, &x.tB));
50     assert!(variant_data_is_aligned(4u, &x.tB));
51
52     let x = mk_rec(22u32, 23u64);
53     assert!(is_aligned(8u, &x.tA));
54     assert!(variant_data_is_aligned(4u, &x.tA));
55     assert!(is_aligned(8u, &x.tB));
56     assert!(variant_data_is_aligned(8u, &x.tB));
57
58     let x = mk_rec(22u32, 23u32);
59     assert!(is_aligned(4u, &x.tA));
60     assert!(variant_data_is_aligned(4u, &x.tA));
61     assert!(is_aligned(4u, &x.tB));
62     assert!(variant_data_is_aligned(4u, &x.tB));
63
64     let x = mk_rec(22f64, 23f64);
65     assert!(is_aligned(8u, &x.tA));
66     assert!(variant_data_is_aligned(8u, &x.tA));
67     assert!(is_aligned(8u, &x.tB));
68     assert!(variant_data_is_aligned(8u, &x.tB));
69 }