xref: /PHP-8.0/ext/ffi/tests/040.phpt (revision 8c5faf7a)
1--TEST--
2FFI 040: Support for scalar types
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6if (pack('S', 0xABCD) !== pack('v', 0xABCD)) {
7    die('skip for little-endian architectures only');
8}
9?>
10--INI--
11ffi.enable=1
12--FILE--
13<?php
14$x = FFI::new("int");
15$x->cdata = 5;
16var_dump($x);
17var_dump(FFI::typeof($x));
18var_dump(FFI::cast("int8_t[4]", $x));
19$p = FFI::addr($x);
20var_dump($p);
21$p[0] += 2;
22var_dump($x);
23var_dump(FFI::sizeof($x));
24var_dump(FFI::alignof($x));
25FFI::memset($x, ord("a"), 4);
26var_dump(FFI::string($x, 4));
27
28echo "\n";
29
30$y = FFI::new("int[2]");
31$y[0] = 6;
32var_dump($y[0]);
33var_dump(FFI::typeof($y[0]));
34var_dump(FFI::cast("int8_t[4]", $y[0]));
35$p = FFI::addr($y[0]);
36var_dump($p);
37$p[0] += 2;
38var_dump($y[0]);
39var_dump(FFI::sizeof($y[0]));
40var_dump(FFI::alignof($y[0]));
41FFI::memset($y[0], ord("b"), 4);
42var_dump(FFI::string($y[0], 4));
43
44echo "\n";
45
46var_dump(FFI::memcmp($x, $y[0], 4));
47FFI::memcpy($x, $y[0], 4);
48var_dump(FFI::memcmp($x, $y[0], 4));
49?>
50--EXPECTF--
51object(FFI\CData:int32_t)#%d (1) {
52  ["cdata"]=>
53  int(5)
54}
55object(FFI\CType:int32_t)#%d (0) {
56}
57object(FFI\CData:int8_t[4])#%d (4) {
58  [0]=>
59  int(5)
60  [1]=>
61  int(0)
62  [2]=>
63  int(0)
64  [3]=>
65  int(0)
66}
67object(FFI\CData:int32_t*)#%d (1) {
68  [0]=>
69  int(5)
70}
71object(FFI\CData:int32_t)#%d (1) {
72  ["cdata"]=>
73  int(7)
74}
75int(4)
76int(4)
77string(4) "aaaa"
78
79int(6)
80object(FFI\CType:int32_t)#%d (0) {
81}
82object(FFI\CData:int8_t[4])#%d (4) {
83  [0]=>
84  int(6)
85  [1]=>
86  int(0)
87  [2]=>
88  int(0)
89  [3]=>
90  int(0)
91}
92object(FFI\CData:int32_t*)#%d (1) {
93  [0]=>
94  int(6)
95}
96int(8)
97int(4)
98int(4)
99string(4) "bbbb"
100
101int(-1)
102int(0)
103