]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/struct-return.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / run-pass / struct-return.rs
1 // Copyright 2012-2015 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 // ignore-wasm32-bare no libc to test ffi with
12
13 #[repr(C)]
14 #[derive(Copy, Clone)]
15 pub struct Quad { a: u64, b: u64, c: u64, d: u64 }
16
17 #[repr(C)]
18 #[derive(Copy, Clone)]
19 pub struct Floats { a: f64, b: u8, c: f64 }
20
21 mod rustrt {
22 use super::{Floats, Quad};
23
24 #[link(name = "rust_test_helpers", kind = "static")]
25 extern {
26 pub fn rust_dbg_abi_1(q: Quad) -> Quad;
27 pub fn rust_dbg_abi_2(f: Floats) -> Floats;
28 }
29 }
30
31 fn test1() {
32 unsafe {
33 let q = Quad { a: 0xaaaa_aaaa_aaaa_aaaa,
34 b: 0xbbbb_bbbb_bbbb_bbbb,
35 c: 0xcccc_cccc_cccc_cccc,
36 d: 0xdddd_dddd_dddd_dddd };
37 let qq = rustrt::rust_dbg_abi_1(q);
38 println!("a: {:x}", qq.a as usize);
39 println!("b: {:x}", qq.b as usize);
40 println!("c: {:x}", qq.c as usize);
41 println!("d: {:x}", qq.d as usize);
42 assert_eq!(qq.a, q.c + 1);
43 assert_eq!(qq.b, q.d - 1);
44 assert_eq!(qq.c, q.a + 1);
45 assert_eq!(qq.d, q.b - 1);
46 }
47 }
48
49 #[cfg(target_pointer_width = "64")]
50 fn test2() {
51 unsafe {
52 let f = Floats { a: 1.234567890e-15_f64,
53 b: 0b_1010_1010,
54 c: 1.0987654321e-15_f64 };
55 let ff = rustrt::rust_dbg_abi_2(f);
56 println!("a: {}", ff.a as f64);
57 println!("b: {}", ff.b as usize);
58 println!("c: {}", ff.c as f64);
59 assert_eq!(ff.a, f.c + 1.0f64);
60 assert_eq!(ff.b, 0xff);
61 assert_eq!(ff.c, f.a - 1.0f64);
62 }
63 }
64
65 #[cfg(target_pointer_width = "32")]
66 fn test2() {
67 }
68
69 pub fn main() {
70 test1();
71 test2();
72 }