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