summaryrefslogtreecommitdiff
path: root/tests/test_union.rs
blob: 15759e7f3b056e5b2a1aece6bcf6f036c7fca7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::default::Default;

#[test]
fn with_anon_struct() {
    // XXX: Rustc thinks that the anonymous struct, bar, is unused.
    #[allow(dead_code)]
    mod ffi { bindgen!("headers/union_with_anon_struct.h"); }
    let mut x: ffi::Union_foo = Default::default();

    unsafe {
        (*x.bar()).a = 0x12345678;
        (*x.bar()).b = 0x87654321;

        assert_eq!((*x.bar()).a, 0x12345678);
        assert_eq!((*x.bar()).b, 0x87654321);
    }
}

#[test]
fn with_anon_union() {
    mod ffi { bindgen!("headers/union_with_anon_union.h"); }
    let mut x: ffi::Union_foo = Default::default();

    unsafe {
        *(*x.bar()).a() = 0x12345678;

        assert_eq!(*(*x.bar()).a(), 0x12345678);
        assert_eq!(*(*x.bar()).b(), 0x5678);
    }
}

#[test]
fn with_anon_unnamed_struct() {
    mod ffi { bindgen!("headers/union_with_anon_unnamed_struct.h"); }
    let mut x: ffi::Union_pixel = Default::default();

    unsafe {
        *x.r() = 0xca;
        *x.g() = 0xcb;
        *x.b() = 0xcc;
        *x.a() = 0xcd;

        assert_eq!(*x.rgba(), 0xcdcccbca);
        assert_eq!(*x.r(), 0xca);
        assert_eq!(*x.g(), 0xcb);
        assert_eq!(*x.b(), 0xcc);
        assert_eq!(*x.a(), 0xcd);
    }
}

#[test]
fn with_anon_unnamed_union() {
    mod ffi { bindgen!("headers/union_with_anon_unnamed_union.h"); }
    let mut x: ffi::Union_foo = Default::default();

    unsafe {
        *x.a() = 0x12345678;

        assert_eq!(*x.a(), 0x12345678);
        assert_eq!(*x.b(), 0x5678);
        assert_eq!(*x.c(), 0x78);
    }
}

#[test]
fn with_nesting() {
    mod ffi { bindgen!("headers/union_with_nesting.h"); }
    let mut x: ffi::Union_foo = Default::default();

    unsafe {
        *x.a() = 0x12345678;

        assert_eq!(*x.a(), 0x12345678);
        assert_eq!(*x.b1(), 0x5678);
        assert_eq!(*x.b2(), 0x5678);
        assert_eq!(*x.c1(), 0x1234);
        assert_eq!(*x.c2(), 0x1234);
    }
}

#[test]
fn default_impl() {
    mod ffi { bindgen!("headers/Union_with_nesting.h"); }

    let mut x: ffi::Union_foo = Default::default();

    unsafe {
        assert_eq!(*x.a(), 0);
        assert_eq!(*x.b1(), 0);
        assert_eq!(*x.b2(), 0);
        assert_eq!(*x.c1(), 0);
        assert_eq!(*x.c2(), 0);
    }
}