| 1 |
/* Area: ffi_call, closure_call |
| 2 |
Purpose: Test doubles passed in variable argument lists. |
| 3 |
Limitations: none. |
| 4 |
PR: none. |
| 5 |
Originator: Blake Chaffin 6/6/2007 */ |
| 6 |
|
| 7 |
/* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */ |
| 8 |
/* { dg-output "" { xfail avr32*-*-* } } */ |
| 9 |
/* { dg-output "" { xfail mips-sgi-irix6* } } PR libffi/46660 */ |
| 10 |
|
| 11 |
#include "ffitest.h" |
| 12 |
|
| 13 |
#define BUF_SIZE 50 |
| 14 |
static char buffer[BUF_SIZE]; |
| 15 |
|
| 16 |
static void |
| 17 |
cls_double_va_fn(ffi_cif* cif __UNUSED__, void* resp, |
| 18 |
void** args, void* userdata __UNUSED__) |
| 19 |
{ |
| 20 |
char* format = *(char**)args[0]; |
| 21 |
double doubleValue = *(double*)args[1]; |
| 22 |
|
| 23 |
*(ffi_arg*)resp = printf(format, doubleValue); |
| 24 |
CHECK(*(ffi_arg*)resp == 4); |
| 25 |
snprintf(buffer, BUF_SIZE, format, doubleValue); |
| 26 |
CHECK(strncmp(buffer, "7.0\n", 4) == 0); |
| 27 |
} |
| 28 |
|
| 29 |
int main (void) |
| 30 |
{ |
| 31 |
ffi_cif cif; |
| 32 |
void *code; |
| 33 |
ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code); |
| 34 |
void* args[3]; |
| 35 |
ffi_type* arg_types[3]; |
| 36 |
|
| 37 |
char* format = "%.1f\n"; |
| 38 |
double doubleArg = 7; |
| 39 |
ffi_arg res = 0; |
| 40 |
|
| 41 |
arg_types[0] = &ffi_type_pointer; |
| 42 |
arg_types[1] = &ffi_type_double; |
| 43 |
arg_types[2] = NULL; |
| 44 |
|
| 45 |
/* This printf call is variadic */ |
| 46 |
CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 2, &ffi_type_sint, |
| 47 |
arg_types) == FFI_OK); |
| 48 |
|
| 49 |
args[0] = &format; |
| 50 |
args[1] = &doubleArg; |
| 51 |
args[2] = NULL; |
| 52 |
|
| 53 |
ffi_call(&cif, FFI_FN(printf), &res, args); |
| 54 |
/* { dg-output "7.0" } */ |
| 55 |
printf("res: %d\n", (int) res); |
| 56 |
/* { dg-output "\nres: 4" } */ |
| 57 |
CHECK(res == 4); |
| 58 |
|
| 59 |
CHECK(ffi_prep_closure_loc(pcl, &cif, cls_double_va_fn, NULL, |
| 60 |
code) == FFI_OK); |
| 61 |
|
| 62 |
res = ((int(*)(char*, ...))(code))(format, doubleArg); |
| 63 |
/* { dg-output "\n7.0" } */ |
| 64 |
printf("res: %d\n", (int) res); |
| 65 |
/* { dg-output "\nres: 4" } */ |
| 66 |
CHECK(res == 4); |
| 67 |
|
| 68 |
exit(0); |
| 69 |
} |