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