Skip to content

Commit 915b003

Browse files
committed
Auto merge of #33894 - nagisa:windows-lack-endurance-for-sprint, r=alexcrichton
Rewrite variadic-ffi pass to use test helper The sprintf used in this test previously isn’t available on some versions of MSVC. Fixes #32305 r? @alexcrichton
2 parents 12d1653 + ed4688c commit 915b003

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

src/test/run-pass/variadic-ffi.rs

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,39 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-msvc -- sprintf isn't a symbol in msvcrt? maybe a #define?
12-
13-
#![feature(libc, std_misc)]
14-
15-
extern crate libc;
16-
17-
use std::ffi::{CStr, CString};
18-
use libc::{c_char, c_int};
19-
20-
11+
#[link(name = "rust_test_helpers")]
2112
extern {
22-
fn sprintf(s: *mut c_char, format: *const c_char, ...) -> c_int;
23-
}
24-
25-
unsafe fn check<T, F>(expected: &str, f: F) where F: FnOnce(*mut c_char) -> T {
26-
let mut x = [0 as c_char; 50];
27-
f(&mut x[0] as *mut c_char);
28-
assert_eq!(expected.as_bytes(), CStr::from_ptr(x.as_ptr()).to_bytes());
13+
fn rust_interesting_average(_: u64, ...) -> f64;
2914
}
3015

3116
pub fn main() {
32-
17+
// Call without variadic arguments
3318
unsafe {
34-
// Call with just the named parameter
35-
let c = CString::new(&b"Hello World\n"[..]).unwrap();
36-
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
37-
38-
// Call with variable number of arguments
39-
let c = CString::new(&b"%d %f %c %s\n"[..]).unwrap();
40-
check("42 42.500000 a %d %f %c %s\n\n", |s| {
41-
sprintf(s, c.as_ptr(), 42, 42.5f64, 'a' as c_int, c.as_ptr());
42-
});
19+
assert!(rust_interesting_average(0).is_nan());
20+
}
4321

44-
// Make a function pointer
45-
let x: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int = sprintf;
22+
// Call with direct arguments
23+
unsafe {
24+
assert_eq!(rust_interesting_average(1, 10i64, 10.0f64) as i64, 20);
25+
}
4626

47-
// A function that takes a function pointer
48-
unsafe fn call(fp: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int) {
49-
// Call with just the named parameter
50-
let c = CString::new(&b"Hello World\n"[..]).unwrap();
51-
check("Hello World\n", |s| fp(s, c.as_ptr()));
27+
// Call with named arguments, variable number of them
28+
let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
29+
unsafe {
30+
assert_eq!(rust_interesting_average(2, x1, x2, x3, x4) as i64, 30);
31+
}
5232

53-
// Call with variable number of arguments
54-
let c = CString::new(&b"%d %f %c %s\n"[..]).unwrap();
55-
check("42 42.500000 a %d %f %c %s\n\n", |s| {
56-
fp(s, c.as_ptr(), 42, 42.5f64, 'a' as c_int, c.as_ptr());
57-
});
58-
}
33+
// A function that takes a function pointer
34+
unsafe fn call(fp: unsafe extern fn(u64, ...) -> f64) {
35+
let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
36+
assert_eq!(fp(2, x1, x2, x3, x4) as i64, 30);
37+
}
5938

60-
// Pass sprintf directly
61-
call(sprintf);
39+
unsafe {
40+
call(rust_interesting_average);
6241

63-
// Pass sprintf indirectly
42+
// Make a function pointer, pass indirectly
43+
let x: unsafe extern fn(u64, ...) -> f64 = rust_interesting_average;
6444
call(x);
6545
}
66-
6746
}

0 commit comments

Comments
 (0)