2013-11-04 15:34:07 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-05-26 19:05:52 -05:00
|
|
|
#[link(name = "rust_test_helpers")]
|
2013-11-04 15:34:07 -06:00
|
|
|
extern {
|
2016-06-02 09:18:11 -05:00
|
|
|
fn rust_interesting_average(_: u64, ...) -> f64;
|
2013-11-04 15:34:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2016-05-26 19:05:52 -05:00
|
|
|
// Call without variadic arguments
|
2013-11-04 15:34:07 -06:00
|
|
|
unsafe {
|
2016-05-26 19:05:52 -05:00
|
|
|
assert!(rust_interesting_average(0).is_nan());
|
|
|
|
}
|
2013-11-04 15:34:07 -06:00
|
|
|
|
2016-05-26 19:05:52 -05:00
|
|
|
// Call with direct arguments
|
|
|
|
unsafe {
|
2016-06-02 09:18:11 -05:00
|
|
|
assert_eq!(rust_interesting_average(1, 10i64, 10.0f64) as i64, 20);
|
2016-05-26 19:05:52 -05:00
|
|
|
}
|
2013-11-04 15:34:07 -06:00
|
|
|
|
2016-05-26 19:05:52 -05:00
|
|
|
// Call with named arguments, variable number of them
|
2016-06-02 09:18:11 -05:00
|
|
|
let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
|
2016-05-26 19:05:52 -05:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(rust_interesting_average(2, x1, x2, x3, x4) as i64, 30);
|
|
|
|
}
|
2013-11-04 15:34:07 -06:00
|
|
|
|
2016-05-26 19:05:52 -05:00
|
|
|
// A function that takes a function pointer
|
2016-06-02 09:18:11 -05:00
|
|
|
unsafe fn call(fp: unsafe extern fn(u64, ...) -> f64) {
|
|
|
|
let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
|
2016-05-26 19:05:52 -05:00
|
|
|
assert_eq!(fp(2, x1, x2, x3, x4) as i64, 30);
|
|
|
|
}
|
2013-11-04 15:34:07 -06:00
|
|
|
|
2016-05-26 19:05:52 -05:00
|
|
|
unsafe {
|
|
|
|
call(rust_interesting_average);
|
2013-11-04 15:34:07 -06:00
|
|
|
|
2016-05-26 19:05:52 -05:00
|
|
|
// Make a function pointer, pass indirectly
|
2016-06-02 09:18:11 -05:00
|
|
|
let x: unsafe extern fn(u64, ...) -> f64 = rust_interesting_average;
|
2013-11-04 15:34:07 -06:00
|
|
|
call(x);
|
|
|
|
}
|
|
|
|
}
|