022.phpt 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. FFI 022: structure/union alignment
  3. --EXTENSIONS--
  4. ffi
  5. --INI--
  6. ffi.enable=1
  7. --FILE--
  8. <?php
  9. function test_size($size, $type) {
  10. if (FFI::sizeof(FFI::new($type)) !== $size) {
  11. echo "FAIL: sizeof($type) != $size\n";
  12. }
  13. }
  14. function test_align($align, $type) {
  15. if (FFI::alignof(FFI::new($type)) !== $align) {
  16. echo "FAIL: alignof($type) != $align\n";
  17. }
  18. }
  19. test_size(8, "struct {uint32_t a; uint32_t b;}");
  20. test_size(3, "struct {char a; uint8_t b; uint8_t c;}");
  21. test_size(8, "struct {char a; uint32_t b;}");
  22. test_size(8, "struct {uint32_t a; char b;}");
  23. test_size(5, "struct __attribute__((packed)) {char a; uint32_t b;}");
  24. test_size(5, "struct __attribute__((packed)) {uint32_t a; char b;}");
  25. test_size(16, "struct {uint32_t a; uint32_t b;}[2]");
  26. test_size(6, "struct {char a; uint8_t b; uint8_t c;}[2]");
  27. test_size(16, "struct {char a; uint32_t b;}[2]");
  28. test_size(16, "struct {uint32_t a; char b;}[2]");
  29. test_size(10, "struct __attribute__((packed)) {char a; uint32_t b;}[2]");
  30. test_size(10, "struct __attribute__((packed)) {uint32_t a; char b;}[2]");
  31. test_align(4, "union {uint32_t a; uint32_t b;}");
  32. test_align(1, "union {char a; uint8_t b; uint8_t c;}");
  33. test_align(4, "union {char a; uint32_t b;}");
  34. test_align(4, "union {uint32_t a; char b;}");
  35. test_align(1, "union __attribute__((packed)) {char a; uint32_t b;}");
  36. test_align(1, "union __attribute__((packed)) {uint32_t a; char b;}");
  37. test_size(8, "struct {char a __attribute__((packed)); uint32_t b;}");
  38. test_size(5, "struct {char a; uint32_t b __attribute__((packed));}");
  39. test_size(5, "struct {uint32_t a __attribute__((packed)); char b;}");
  40. test_size(8, "struct {uint32_t a; char b __attribute__((packed));}");
  41. test_align(4, "struct {char a __attribute__((packed)); uint32_t b;}");
  42. test_align(1, "struct {char a; uint32_t b __attribute__((packed));}");
  43. test_align(1, "struct {uint32_t a __attribute__((packed)); char b;}");
  44. test_align(4, "struct {uint32_t a; char b __attribute__((packed));}");
  45. test_size(16, "struct __attribute__((aligned(16))) {char a; uint32_t b;}");
  46. test_align(16, "struct __attribute__((aligned(16))) {char a; uint32_t b;}");
  47. test_size(16, "struct {char a; uint32_t b;} __attribute__((aligned(16)))");
  48. test_align(16, "struct {char a; uint32_t b;} __attribute__((aligned(16)))");
  49. test_size(8, "struct {char a; uint32_t b __attribute__((aligned(1)));}");
  50. test_align(4, "struct {char a; uint32_t b __attribute__((aligned(1)));}");
  51. test_size(32, "struct {char a; uint32_t b __attribute__((aligned(16)));}");
  52. test_align(16, "struct {char a; uint32_t b __attribute__((aligned(16)));}");
  53. if (substr(PHP_OS, 0, 3) != 'WIN') {
  54. test_size(FFI::__BIGGEST_ALIGNMENT__ * 2, "struct {char a; uint32_t b __attribute__((aligned));}");
  55. test_align(FFI::__BIGGEST_ALIGNMENT__, "struct {char a; uint32_t b __attribute__((aligned));}");
  56. }
  57. test_size(16, "struct __declspec(align(16)) {char a; uint32_t b;}");
  58. test_align(16, "struct __declspec(align(16)) {char a; uint32_t b;}");
  59. ?>
  60. ok
  61. --EXPECT--
  62. ok