1--TEST--
2disabled php only for class methods unpacker (ini_set)
3--SKIPIF--
4--FILE--
5<?php
6if(!extension_loaded('msgpack')) {
7    dl('msgpack.' . PHP_SHLIB_SUFFIX);
8}
9
10ini_set('msgpack.php_only', 0);
11
12function test($type, $variable, $test = null)
13{
14    $msgpack = new MessagePack();
15
16    $serialized = $msgpack->pack($variable);
17    $unpacker = $msgpack->unpacker();
18
19    $length = strlen($serialized);
20
21    if (rand(0, 1))
22    {
23        for ($i = 0; $i < $length;)
24        {
25            $len = rand(1, 10);
26            $str = substr($serialized, $i, $len);
27
28            $unpacker->feed($str);
29            if ($unpacker->execute())
30            {
31                $unserialized = $unpacker->data();
32                var_dump($unserialized);
33                $unpacker->reset();
34            }
35
36            $i += $len;
37        }
38    }
39    else
40    {
41        $str = "";
42        $offset = 0;
43
44        for ($i = 0; $i < $length;)
45        {
46            $len = rand(1, 10);
47            $str .= substr($serialized, $i, $len);
48
49            if ($unpacker->execute($str, $offset))
50            {
51                $unserialized = $unpacker->data();
52                var_dump($unserialized);
53
54                $unpacker->reset();
55                $str = "";
56                $offset = 0;
57            }
58
59            $i += $len;
60        }
61    }
62
63    if (!is_bool($test))
64    {
65        echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL;
66    }
67    else
68    {
69        echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL;
70    }
71}
72
73test('null', null);
74
75test('bool: true', true);
76test('bool: false', false);
77
78test('zero: 0', 0);
79test('small: 1', 1);
80test('small: -1', -1);
81test('medium: 1000', 1000);
82test('medium: -1000', -1000);
83test('large: 100000', 100000);
84test('large: -100000', -100000);
85
86test('double: 123.456', 123.456);
87
88test('empty: ""', "");
89test('string: "foobar"', "foobar");
90
91test('array: empty', array(), false);
92test('array(1, 2, 3)', array(1, 2, 3), false);
93test('array(array(1, 2, 3), arr...', array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9)), false);
94
95test('array("foo", "foo", "foo")', array("foo", "foo", "foo"), false);
96test('array("one" => 1, "two" => 2))', array("one" => 1, "two" => 2), false);
97test('array("kek" => "lol", "lol" => "kek")', array("kek" => "lol", "lol" => "kek"), false);
98test('array("" => "empty")', array("" => "empty"), false);
99
100$a = array('foo');
101test('array($a, $a)', array($a, $a), false);
102test('array(&$a, &$a)', array(&$a, &$a), false);
103
104$a = array(null);
105$b = array(&$a);
106$a[0] = &$b;
107
108test('cyclic', $a, true);
109
110$a = array(
111    'a' => array(
112        'b' => 'c',
113        'd' => 'e'
114        ),
115    'f' => array(
116        'g' => 'h'
117        )
118    );
119
120test('array', $a, false);
121
122class Obj {
123    public $a;
124    protected $b;
125    private $c;
126
127    function __construct($a, $b, $c) {
128        $this->a = $a;
129        $this->b = $b;
130        $this->c = $c;
131    }
132}
133
134test('object', new Obj(1, 2, 3), true);
135
136test('object', array(new Obj(1, 2, 3), new Obj(4, 5, 6)), true);
137
138$o = new Obj(1, 2, 3);
139
140test('object', array(&$o, &$o), true);
141--EXPECTF--
142NULL
143OK
144bool(true)
145OK
146bool(false)
147OK
148int(0)
149OK
150int(1)
151OK
152int(-1)
153OK
154int(1000)
155OK
156int(-1000)
157OK
158int(100000)
159OK
160int(-100000)
161OK
162float(123.456)
163OK
164string(0) ""
165OK
166string(6) "foobar"
167OK
168array(0) {
169}
170OK
171array(3) {
172  [0]=>
173  int(1)
174  [1]=>
175  int(2)
176  [2]=>
177  int(3)
178}
179OK
180array(3) {
181  [0]=>
182  array(3) {
183    [0]=>
184    int(1)
185    [1]=>
186    int(2)
187    [2]=>
188    int(3)
189  }
190  [1]=>
191  array(3) {
192    [0]=>
193    int(4)
194    [1]=>
195    int(5)
196    [2]=>
197    int(6)
198  }
199  [2]=>
200  array(3) {
201    [0]=>
202    int(7)
203    [1]=>
204    int(8)
205    [2]=>
206    int(9)
207  }
208}
209OK
210array(3) {
211  [0]=>
212  string(3) "foo"
213  [1]=>
214  string(3) "foo"
215  [2]=>
216  string(3) "foo"
217}
218OK
219array(2) {
220  ["one"]=>
221  int(1)
222  ["two"]=>
223  int(2)
224}
225OK
226array(2) {
227  ["kek"]=>
228  string(3) "lol"
229  ["lol"]=>
230  string(3) "kek"
231}
232OK
233array(1) {
234  [""]=>
235  string(5) "empty"
236}
237OK
238array(2) {
239  [0]=>
240  array(1) {
241    [0]=>
242    string(3) "foo"
243  }
244  [1]=>
245  array(1) {
246    [0]=>
247    string(3) "foo"
248  }
249}
250OK
251array(2) {
252  [0]=>
253  array(1) {
254    [0]=>
255    string(3) "foo"
256  }
257  [1]=>
258  array(1) {
259    [0]=>
260    string(3) "foo"
261  }
262}
263OK
264array(1) {
265  [0]=>
266  array(1) {
267    [0]=>
268    array(1) {
269      [0]=>
270      NULL
271    }
272  }
273}
274OK
275array(2) {
276  ["a"]=>
277  array(2) {
278    ["b"]=>
279    string(1) "c"
280    ["d"]=>
281    string(1) "e"
282  }
283  ["f"]=>
284  array(1) {
285    ["g"]=>
286    string(1) "h"
287  }
288}
289OK
290array(3) {
291  [0]=>
292  int(1)
293  [1]=>
294  int(2)
295  [2]=>
296  int(3)
297}
298OK
299array(2) {
300  [0]=>
301  array(3) {
302    [0]=>
303    int(1)
304    [1]=>
305    int(2)
306    [2]=>
307    int(3)
308  }
309  [1]=>
310  array(3) {
311    [0]=>
312    int(4)
313    [1]=>
314    int(5)
315    [2]=>
316    int(6)
317  }
318}
319OK
320array(2) {
321  [0]=>
322  array(3) {
323    [0]=>
324    int(1)
325    [1]=>
326    int(2)
327    [2]=>
328    int(3)
329  }
330  [1]=>
331  array(3) {
332    [0]=>
333    int(1)
334    [1]=>
335    int(2)
336    [2]=>
337    int(3)
338  }
339}
340OK
341