ec145836bd8d4af00b98a97a506f11d489e20d80
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / MWMessagePackTest.php
1 <?php
2 /**
3 * PHP Unit tests for MWMessagePack
4 * @covers MWMessagePack
5 */
6 class MWMessagePackTest extends PHPUnit_Framework_TestCase {
7
8 /**
9 * Provides test cases for MWMessagePackTest::testMessagePack
10 *
11 * Returns an array of test cases. Each case is an array of (type, value,
12 * expected encoding as hex string). The expected values were generated
13 * using <https://github.com/msgpack/msgpack-php>, which includes a
14 * serialization function.
15 */
16 public static function providePacks() {
17 $tests = array(
18 array( 'nil', null, 'c0' ),
19 array( 'bool', true, 'c3' ),
20 array( 'bool', false, 'c2' ),
21 array( 'positive fixnum', 0, '00' ),
22 array( 'positive fixnum', 1, '01' ),
23 array( 'positive fixnum', 5, '05' ),
24 array( 'positive fixnum', 35, '23' ),
25 array( 'uint 8', 128, 'cc80' ),
26 array( 'uint 16', 1000, 'cd03e8' ),
27 array( 'uint 32', 100000, 'ce000186a0' ),
28 array( 'negative fixnum', -1, 'ff' ),
29 array( 'negative fixnum', -2, 'fe' ),
30 array( 'int 8', -128, 'd080' ),
31 array( 'int 8', -35, 'd0dd' ),
32 array( 'int 16', -1000, 'd1fc18' ),
33 array( 'int 32', -100000, 'd2fffe7960' ),
34 array( 'double', 0.1, 'cb3fb999999999999a' ),
35 array( 'double', 1.1, 'cb3ff199999999999a' ),
36 array( 'double', 123.456, 'cb405edd2f1a9fbe77' ),
37 array( 'fix raw', '', 'a0' ),
38 array( 'fix raw', 'foobar', 'a6666f6f626172' ),
39 array(
40 'raw 16',
41 'Lorem ipsum dolor sit amet amet.',
42 'da00204c6f72656d20697073756d20646f6c6f722073697420616d657420616d65742e'
43 ),
44 array(
45 'fix array',
46 array( 'abc', 'def', 'ghi' ),
47 '93a3616263a3646566a3676869'
48 ),
49 array(
50 'fix map',
51 array( 'one' => 1, 'two' => 2 ),
52 '82a36f6e6501a374776f02'
53 ),
54 );
55
56 if ( PHP_INT_SIZE > 4 ) {
57 $tests[] = array( 'uint 64', 10000000000, 'cf00000002540be400' );
58 $tests[] = array( 'int 64', -10000000000, 'd3fffffffdabf41c00' );
59 $tests[] = array( 'int 64', -223372036854775807, 'd3fce66c50e2840001' );
60 $tests[] = array( 'int 64', -9223372036854775807, 'd38000000000000001' );
61 }
62
63 return $tests;
64 }
65
66 /**
67 * Verify that values are serialized correctly.
68 * @covers MWMessagePack::pack
69 * @dataProvider providePacks
70 */
71 public function testPack( $type, $value, $expected ) {
72 $actual = bin2hex( MWMessagePack::pack( $value ) );
73 $this->assertEquals( $expected, $actual, $type );
74 }
75 }