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