]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/struct-return.rs
109287a83b16a88e93ced69fd83047aac5ad4188
[rust.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
12 #[derive(Copy, Clone)]
13 pub struct Quad { a: u64, b: u64, c: u64, d: u64 }
14
15 #[derive(Copy, Clone)]
16 pub struct Floats { a: f64, b: u8, c: f64 }
17
18 mod rustrt {
19     use super::{Floats, Quad};
20
21     #[link(name = "rust_test_helpers")]
22     extern {
23         pub fn rust_dbg_abi_1(q: Quad) -> Quad;
24         pub fn rust_dbg_abi_2(f: Floats) -> Floats;
25     }
26 }
27
28 fn test1() {
29     unsafe {
30         let q = Quad { a: 0xaaaa_aaaa_aaaa_aaaa,
31                  b: 0xbbbb_bbbb_bbbb_bbbb,
32                  c: 0xcccc_cccc_cccc_cccc,
33                  d: 0xdddd_dddd_dddd_dddd };
34         let qq = rustrt::rust_dbg_abi_1(q);
35         println!("a: {:x}", qq.a as usize);
36         println!("b: {:x}", qq.b as usize);
37         println!("c: {:x}", qq.c as usize);
38         println!("d: {:x}", qq.d as usize);
39         assert_eq!(qq.a, q.c + 1);
40         assert_eq!(qq.b, q.d - 1);
41         assert_eq!(qq.c, q.a + 1);
42         assert_eq!(qq.d, q.b - 1);
43     }
44 }
45
46 #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
47 fn test2() {
48     unsafe {
49         let f = Floats { a: 1.234567890e-15_f64,
50                  b: 0b_1010_1010,
51                  c: 1.0987654321e-15_f64 };
52         let ff = rustrt::rust_dbg_abi_2(f);
53         println!("a: {}", ff.a as f64);
54         println!("b: {}", ff.b as usize);
55         println!("c: {}", ff.c as f64);
56         assert_eq!(ff.a, f.c + 1.0f64);
57         assert_eq!(ff.b, 0xff);
58         assert_eq!(ff.c, f.a - 1.0f64);
59     }
60 }
61
62 #[cfg(any(target_arch = "x86", target_arch = "arm"))]
63 fn test2() {
64 }
65
66 pub fn main() {
67     test1();
68     test2();
69 }