struct10.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Area: ffi_call
  2. Purpose: Check structures.
  3. Limitations: none.
  4. PR: none.
  5. Originator: Sergei Trofimovich <slyfox@gentoo.org>
  6. The test originally discovered in ruby's bindings
  7. for ffi in https://bugs.gentoo.org/634190 */
  8. /* { dg-do run } */
  9. #include "ffitest.h"
  10. struct s {
  11. int s32;
  12. float f32;
  13. signed char s8;
  14. };
  15. struct s make_s(void) {
  16. struct s r;
  17. r.s32 = 0x1234;
  18. r.f32 = 7.0;
  19. r.s8 = 0x78;
  20. return r;
  21. }
  22. int main() {
  23. ffi_cif cif;
  24. struct s r;
  25. ffi_type rtype;
  26. ffi_type* s_fields[] = {
  27. &ffi_type_sint,
  28. &ffi_type_float,
  29. &ffi_type_schar,
  30. NULL,
  31. };
  32. rtype.size = 0;
  33. rtype.alignment = 0,
  34. rtype.type = FFI_TYPE_STRUCT,
  35. rtype.elements = s_fields,
  36. r.s32 = 0xbad;
  37. r.f32 = 999.999;
  38. r.s8 = 0x51;
  39. // Here we emulate the following call:
  40. //r = make_s();
  41. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0, &rtype, NULL) == FFI_OK);
  42. ffi_call(&cif, FFI_FN(make_s), &r, NULL);
  43. CHECK(r.s32 == 0x1234);
  44. CHECK(r.f32 == 7.0);
  45. CHECK(r.s8 == 0x78);
  46. exit(0);
  47. }