summaryrefslogtreecommitdiff
path: root/ext/ffi/tests/022.phpt
blob: 44ddaeebd29e3cf8ca1f2b8d070c67092b227ca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--TEST--
FFI 022: structure/union alignment
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--INI--
ffi.enable=1
--FILE--
<?php
function test_size($size, $type) {
    if (FFI::sizeof(FFI::new($type)) !== $size) {
        echo "FAIL: sizeof($type) != $size\n";
    }
}

function test_align($align, $type) {
    if (FFI::alignof(FFI::new($type)) !== $align) {
        echo "FAIL: alignof($type) != $align\n";
    }
}

test_size(8, "struct {uint32_t a; uint32_t b;}");
test_size(3, "struct {char a; uint8_t b; uint8_t c;}");
test_size(8, "struct {char a; uint32_t b;}");
test_size(8, "struct {uint32_t a; char b;}");
test_size(5, "struct __attribute__((packed)) {char a; uint32_t b;}");
test_size(5, "struct __attribute__((packed)) {uint32_t a; char b;}");

test_size(16, "struct {uint32_t a; uint32_t b;}[2]");
test_size(6,  "struct {char a; uint8_t b; uint8_t c;}[2]");
test_size(16, "struct {char a; uint32_t b;}[2]");
test_size(16, "struct {uint32_t a; char b;}[2]");
test_size(10, "struct __attribute__((packed)) {char a; uint32_t b;}[2]");
test_size(10, "struct __attribute__((packed)) {uint32_t a; char b;}[2]");

test_align(4, "union {uint32_t a; uint32_t b;}");
test_align(1, "union {char a; uint8_t b; uint8_t c;}");
test_align(4, "union {char a; uint32_t b;}");
test_align(4, "union {uint32_t a; char b;}");
test_align(1, "union __attribute__((packed)) {char a; uint32_t b;}");
test_align(1, "union __attribute__((packed)) {uint32_t a; char b;}");

test_size(8, "struct {char a __attribute__((packed)); uint32_t b;}");
test_size(5, "struct {char a; uint32_t b  __attribute__((packed));}");
test_size(5, "struct {uint32_t a __attribute__((packed)); char b;}");
test_size(8, "struct {uint32_t a; char b __attribute__((packed));}");

test_align(4, "struct {char a __attribute__((packed)); uint32_t b;}");
test_align(1, "struct {char a; uint32_t b  __attribute__((packed));}");
test_align(1, "struct {uint32_t a __attribute__((packed)); char b;}");
test_align(4, "struct {uint32_t a; char b __attribute__((packed));}");

test_size(16, "struct  __attribute__((aligned(16))) {char a; uint32_t b;}");
test_align(16, "struct  __attribute__((aligned(16))) {char a; uint32_t b;}");

test_size(16, "struct {char a; uint32_t b;} __attribute__((aligned(16)))");
test_align(16, "struct {char a; uint32_t b;} __attribute__((aligned(16)))");

test_size(8, "struct  {char a; uint32_t b __attribute__((aligned(1)));}");
test_align(4, "struct  {char a; uint32_t b __attribute__((aligned(1)));}");
test_size(32, "struct {char a; uint32_t b __attribute__((aligned(16)));}");
test_align(16, "struct {char a; uint32_t b __attribute__((aligned(16)));}");

if (substr(PHP_OS, 0, 3) != 'WIN') {
    test_size(FFI::__BIGGEST_ALIGNMENT__ * 2, "struct  {char a; uint32_t b __attribute__((aligned));}");
    test_align(FFI::__BIGGEST_ALIGNMENT__, "struct  {char a; uint32_t b __attribute__((aligned));}");
}

test_size(16, "struct  __declspec(align(16)) {char a; uint32_t b;}");
test_align(16, "struct  __declspec(align(16)) {char a; uint32_t b;}");
?>
ok
--EXPECT--
ok