Вход Регистрация
Файл: vkolhoze.com/vendor/maxmind-db/reader/tests/MaxMind/Db/Test/Reader/DecoderTest.php
Строк: 172
<?php

namespace MaxMindDbTestReader;

use 
MaxMindDbReaderDecoder;

class 
DecoderTest extends PHPUnit_Framework_TestCase
{
    private 
$arrays = array(
        array(
            
'expected' => array(),
            
'input' => array(0x00x4),
            
'name' => 'empty',
        ),
        array(
            
'expected' => array('Foo'),
            
'input' => array(0x10x4/* Foo */
                
0x430x460x6f0x6f),
            
'name' => 'one element',
        ),
        array(
            
'expected' => array('Foo''人'),
            
'input' => array(
                
0x20x4,
                
/* Foo */
                
0x430x460x6f0x6f,
                
/* 人 */
                
0x430xe40xba0xba
            
),
            
'name' => 'two elements',
        ),
    );

    private 
$booleans = array(
        
false => array(0x00x7),
        
true => array(0x10x7),
    );

    private 
$doubles = array(
        
'0.0' => array(0x680x00x00x00x00x00x00x00x0),
        
'0.5' => array(0x680x3F0xE00x00x00x00x00x00x0),
        
'3.14159265359' => array(0x680x400x90x210xFB0x540x44,
            
0x2E0xEA),
        
'123.0' => array(0x680x400x5E0xC00x00x00x00x00x0),
        
'1073741824.12457' => array(0x680x410xD00x00x00x00x7,
            
0xF80xF4),
        
'-0.5' => array(0x680xBF0xE00x00x00x00x00x00x0),
        
'-3.14159265359' => array(0x680xC00x90x210xFB0x540x44,
            
0x2E0xEA),
        
'-1073741824.12457' => array(0x680xC10xD00x00x00x00x7,
            
0xF80xF4),
    );

    private 
$floats = array(
        
'0.0' => array(0x40x80x00x00x00x0),
        
'1.0' => array(0x40x80x3F0x800x00x0),
        
'1.1' => array(0x40x80x3F0x8C0xCC0xCD),
        
'3.14' => array(0x40x80x400x480xF50xC3),
        
'9999.99' => array(0x40x80x460x1C0x3F0xF6),
        
'-1.0' => array(0x40x80xBF0x800x00x0),
        
'-1.1' => array(0x40x80xBF0x8C0xCC0xCD),
        
'-3.14' => array(0x40x80xC00x480xF50xC3),
        
'-9999.99' => array(0x40x80xC60x1C0x3F0xF6)
    );

    
// PHP can't have arrays/objects as keys. Maybe redo all of the tests
    // this way so that we can use one test runner
    
private $maps = array(
        array(
            
'expected' => array(),
            
'input' => array(0xe0),
            
'name' => 'empty',
        ),
        array(
            
'expected' => array('en' => 'Foo'),
            
'input' => array(0xe1/* en */
                
0x420x650x6e,
                
/* Foo */
                
0x430x460x6f0x6f),
            
'name' => 'one key',
        ),
        array(
            
'expected' => array('en' => 'Foo''zh' => '人'),
            
'input' => array(
                
0xe2,
                
/* en */
                
0x420x650x6e,
                
/* Foo */
                
0x430x460x6f0x6f,
                
/* zh */
                
0x420x7a0x68,
                
/* 人 */
                
0x430xe40xba0xba
            
),
            
'name' => 'two keys',
        ),
        array(
            
'expected' => array('name' => array('en' => 'Foo''zh' => '人')),
            
'input' => array(
                
0xe1,
                
/* name */
                
0x440x6e0x610x6d0x650xe2,
                
/* en */
                
0x420x650x6e,
                
/* Foo */
                
0x430x460x6f0x6f,
                
/* zh */
                
0x420x7a0x68,
                
/* 人 */
                
0x430xe40xba0xba
            
),
            
'name' => 'nested',
        ),
        array(
            
'expected' => array('languages' => array('en''zh')),
            
'input' => array(
                
0xe1,
                
/* languages */
                
0x490x6c0x610x6e0x670x750x61,
                
0x670x650x73,
                
/* array */
                
0x20x4,
                
/* en */
                
0x420x650x6e,
                
/* zh */
                
0x420x7a0x68
            
),
            
'name' => 'map with array in it'
        
),
    );

    private 
$pointers = array(
        
=> array(0x200x0),
        
=> array(0x200x5),
        
10 => array(0x200xa),
        
1023 => array(0x230xff,),
        
3017 => array(0x280x30xc9),
        
524283 => array(0x2f0xf70xfb),
        
526335 => array(0x2f0xff0xff),
        
134217726 => array(0x370xf70xf70xfe),
        
134744063 => array(0x370xff0xff0xff),
        
2147483647 => array(0x380x7f0xff0xff0xff),
        
4294967295 => array(0x380xff0xff0xff0xff),
    );

    private 
$uint16 = array(
        
=> array(0xa0),
        
255 => array(0xa10xff),
        
500 => array(0xa20x10xf4),
        
10872 => array(0xa20x2a0x78),
        
65535 => array(0xa20xff0xff),
    );


    private 
$int32 = array(
        
'0' => array(0x00x1),
        
'-1' => array(0x40x10xff0xff0xff0xff),
        
'255' => array(0x10x10xff),
        
'-255' => array(0x40x10xff0xff0xff0x1),
        
'500' => array(0x20x10x10xf4),
        
'-500' => array(0x40x10xff0xff0xfe0xc),
        
'65535' => array(0x20x10xff0xff),
        
'-65535' => array(0x40x10xff0xff0x00x1),
        
'16777215' => array(0x30x10xff0xff0xff),
        
'-16777215' => array(0x40x10xff0x00x00x1),
        
'2147483647' => array(0x40x10x7f0xff0xff0xff),
        
'-2147483647' => array(0x40x10x800x00x00x1),
    );

    private function 
strings()
    {
        
$strings = array(
            
'' => array(0x40),
            
=> array(0x410x31),
            
'人' => array(0x430xE40xBA0xBA),
            
'123' => array(0x430x310x320x33),
            
'123456789012345678901234567' => array(0x5b0x310x320x330x34,
                
0x350x360x370x380x390x300x310x320x330x340x35,
                
0x360x370x380x390x300x310x320x330x340x350x36,
                
0x37),
            
'1234567890123456789012345678' => array(0x5c0x310x320x330x34,
                
0x350x360x370x380x390x300x310x320x330x340x35,
                
0x360x370x380x390x300x310x320x330x340x350x36,
                
0x370x38),
            
'12345678901234567890123456789' => array(0x5d0x00x310x320x33,
                
0x340x350x360x370x380x390x300x310x320x330x34,
                
0x350x360x370x380x390x300x310x320x330x340x35,
                
0x360x370x380x39),
            
'123456789012345678901234567890' => array(0x5d0x10x310x320x33,
                
0x340x350x360x370x380x390x300x310x320x330x34,
                
0x350x360x370x380x390x300x310x320x330x340x35,
                
0x360x370x380x390x30),
        );

        
$strings[str_repeat('x'500)] =
            
array_pad(
                array(
0x5e0x00xd7),
                
503,
                
0x78
            
);

        
$strings[str_repeat('x'2000)] =
            
array_pad(
                array(
0x5e0x60xb3),
                
2003,
                
0x78
            
);

        
$strings[str_repeat('x'70000)] =
            
array_pad(
                array(
0x5f0x00x100x53),
                
70004,
                
0x78
            
);

        return 
$strings;
    }

    private 
$uint32 = array(
        
=> array(0xc0),
        
255 => array(0xc10xff),
        
500 => array(0xc20x10xf4),
        
10872 => array(0xc20x2a0x78),
        
65535 => array(0xc20xff0xff),
        
16777215 => array(0xc30xff0xff0xff),
        
4294967295 => array(0xc40xff0xff0xff0xff),
    );

    private function 
bytes()
    {
        
// ugly deep clone
        
$bytes unserialize(serialize($this->strings()));

        foreach (
$bytes as $key => $byte_array) {
            
$byte_array[0] ^= 0xc0;
            
$bytes[$key] = $byte_array;

        }
        return 
$bytes;
    }

    public function 
generateLargeUint($bits)
    {

        
$ctrlByte $bits == 64 0x2 0x3;

        
$uints = array(
            
=> array(0x0$ctrlByte),
            
500 => array(0x2$ctrlByte0x10xf4),
            
10872 => array(0x2$ctrlByte0x2a0x78),
        );

        for (
$power 1$power <= $bits 8$power++) {
            
$expected bcsub(bcpow(2$power), 1);
            
$input = array($power$ctrlByte);
            for (
$i 2$i $power$i++) {
                
$input[$i] = 0xff;
            }
            
$uints[$expected] = $input;
        }
        return 
$uints;
    }

    public function 
testArrays()
    {
        
$this->validateTypeDecodingList('array'$this->arrays);
    }

    public function 
testBooleans()
    {
        
$this->validateTypeDecoding('boolean'$this->booleans);
    }

    public function 
testBytes()
    {
        
$this->validateTypeDecoding('byte'$this->bytes());
    }

    public function 
testDoubles()
    {
        
$this->validateTypeDecoding('double'$this->doubles);
    }

    public function 
testFloats()
    {
        
$this->validateTypeDecoding('float'$this->floats);
    }

    public function 
testInt32()
    {
        
$this->validateTypeDecoding('int32'$this->int32);
    }

    public function 
testMaps()
    {
        
$this->validateTypeDecodingList('map'$this->maps);
    }

    public function 
testPointers()
    {
        
$this->validateTypeDecoding('pointers'$this->pointers);
    }

    public function 
testStrings()
    {
        
$this->validateTypeDecoding('utf8_string'$this->strings());
    }

    public function 
testUint16()
    {
        
$this->validateTypeDecoding('uint16'$this->uint16);
    }

    public function 
testUint32()
    {
        
$this->validateTypeDecoding('uint32'$this->uint32);
    }

    public function 
testUint64()
    {
        
$this->validateTypeDecoding('uint64'$this->generateLargeUint(64));
    }

    public function 
testUint128()
    {
        
$this->validateTypeDecoding('uint128'$this->generateLargeUint(128));
    }

    private function 
validateTypeDecoding($type$tests)
    {

        foreach (
$tests as $expected => $input) {
            
$this->checkDecoding($type$input$expected);
        }
    }

    private function 
validateTypeDecodingList($type$tests)
    {
        foreach (
$tests as $test) {
            
$this->checkDecoding(
                
$type,
                
$test['input'],
                
$test['expected'],
                
$test['name']
            );
        }
    }

    private function 
checkDecoding($type$input$expected$name null)
    {
        
$name $name || $expected;
        
$description "decoded $type - $name";
        
$handle fopen('php://memory''rw');

        foreach (
$input as $byte) {
            
fwrite($handlepack('C'$byte));
        }
        
fseek($handle0);
        
$decoder = new Decoder($handle0true);
        list(
$actual) = $decoder->decode(0);

        if (
$type == 'float') {
            
$actual round($actual2);
        }

        
$this->assertEquals($expected$actual$description);

    }
}
Онлайн: 1
Реклама