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