|
| 1 | +<?php |
| 2 | +error_reporting(-1); |
| 3 | +ini_set('implicit_flush', 0); |
| 4 | +require(__DIR__.'/../vendor/autoload.php'); |
| 5 | +use wrossmann\json_stream\JsonStream; |
| 6 | + |
| 7 | +function test($obj) { |
| 8 | + $handle = fopen('php://memory', 'rwb'); |
| 9 | + $js = new JsonStream($handle); |
| 10 | + |
| 11 | + $js->encode($obj); |
| 12 | + rewind($handle); |
| 13 | + $js_out = stream_get_contents($handle); |
| 14 | + |
| 15 | + $stock_out = json_encode($obj); |
| 16 | + |
| 17 | + if( $js_out !== $stock_out ) { |
| 18 | + printf("FAIL:\n\t%s\n\t%s\n", $js_out, $stock_out); |
| 19 | + } |
| 20 | + |
| 21 | + return $js_out === $stock_out; |
| 22 | +} |
| 23 | + |
| 24 | +$tests = [ |
| 25 | + 0, 1, 2, true, false, null, |
| 26 | + "", "\0", "a", "abc", "klâwen", |
| 27 | + [], [0], [1], [0,1,2,3], |
| 28 | + [0,1,2,3,4=>4], [0,1,2,3,5=>5] |
| 29 | +]; |
| 30 | +$testobject = new stdClass(); |
| 31 | +$testobject->foo = $tests; |
| 32 | + |
| 33 | +$tests[] = $testobject; |
| 34 | + |
| 35 | + |
| 36 | +$res = true; |
| 37 | +foreach($tests as $test) { |
| 38 | + $res = test($test) && $res; |
| 39 | +} |
| 40 | + |
| 41 | +echo ($res ? "PASS" : "FAIL") . PHP_EOL; |
| 42 | + |
| 43 | +exit( $res ? 0 : 1 ); |
0 commit comments