public function testZigZagEncodeDecode()
 {
     $this->assertSame(0, GPBWire::zigZagEncode32(0));
     $this->assertSame(1, GPBWire::zigZagEncode32(-1));
     $this->assertSame(2, GPBWire::zigZagEncode32(1));
     $this->assertSame(3, GPBWire::zigZagEncode32(-2));
     $this->assertSame(0x7ffffffe, GPBWire::zigZagEncode32(0x3fffffff));
     $this->assertSame(0x7fffffff, GPBWire::zigZagEncode32(0xc0000000));
     $this->assertSame(-2, GPBWire::zigZagEncode32(0x7fffffff));
     $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
     $this->assertSame(0, GPBWire::zigZagDecode32(0));
     $this->assertSame(-1, GPBWire::zigZagDecode32(1));
     $this->assertSame(1, GPBWire::zigZagDecode32(2));
     $this->assertSame(-2, GPBWire::zigZagDecode32(3));
     $this->assertSame(0x3fffffff, GPBWire::zigZagDecode32(0x7ffffffe));
     $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7fffffff));
     $this->assertSame(0x7fffffff, GPBWire::zigZagDecode32(0xfffffffe));
     $this->assertSame(-2147483648, GPBWire::zigZagDecode32(0xffffffff));
     $this->assertEquals(GPBUtil::Uint64(0), GPBWire::zigZagEncode64(GPBUtil::Int64(0)));
     $this->assertEquals(GPBUtil::Uint64(1), GPBWire::zigZagEncode64(GPBUtil::Int64(-1)));
     $this->assertEquals(GPBUtil::Uint64(2), GPBWire::zigZagEncode64(GPBUtil::Int64(1)));
     $this->assertEquals(GPBUtil::Uint64(3), GPBWire::zigZagEncode64(GPBUtil::Int64(-2)));
     $this->assertEquals(GPBUtil::Uint64(0x7ffffffe), GPBWire::zigZagEncode64(GPBUtil::Int64(0x3fffffff)));
     $this->assertEquals(GPBUtil::Uint64(0x7fffffff), GPBWire::zigZagEncode64(GPBUtil::Int64(1.844674407263581E+19)));
     $this->assertEquals(GPBUtil::Uint64(0xfffffffe), GPBWire::zigZagEncode64(GPBUtil::Int64(0x7fffffff)));
     $this->assertEquals(GPBUtil::Uint64(0xffffffff), GPBWire::zigZagEncode64(GPBUtil::Int64(1.8446744071562068E+19)));
     $this->assertEquals(Uint64::newValue(4294967295, 4294967294), GPBWire::zigZagEncode64(GPBUtil::Int64(0x7fffffffffffffff)));
     $this->assertEquals(Uint64::newValue(4294967295, 4294967295), GPBWire::zigZagEncode64(GPBUtil::Int64(9.223372036854776E+18)));
     $this->assertEquals(GPBUtil::Int64(0), GPBWire::zigZagDecode64(GPBUtil::Uint64(0)));
     $this->assertEquals(GPBUtil::Int64(-1), GPBWire::zigZagDecode64(GPBUtil::Uint64(1)));
     $this->assertEquals(GPBUtil::Int64(1), GPBWire::zigZagDecode64(GPBUtil::Uint64(2)));
     $this->assertEquals(GPBUtil::Int64(-2), GPBWire::zigZagDecode64(GPBUtil::Uint64(3)));
     // Round trip
     $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
     $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
     $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
     $this->assertSame(14927, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
     $this->assertSame(-3612, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
 }
Пример #2
0
 public static function writeSint64(&$output, $value)
 {
     $value = GPBWire::zigZagEncode64(GPBUtil::Int64($value));
     return $output->writeVarint64($value->toInteger());
 }
Пример #3
0
 public function testZigZagEncodeDecode()
 {
     $this->assertSame(0, GPBWire::zigZagEncode32(0));
     $this->assertSame(1, GPBWire::zigZagEncode32(-1));
     $this->assertSame(2, GPBWire::zigZagEncode32(1));
     $this->assertSame(3, GPBWire::zigZagEncode32(-2));
     $this->assertSame(0x7ffffffe, GPBWire::zigZagEncode32(0x3fffffff));
     $this->assertSame(0x7fffffff, GPBWire::zigZagEncode32(0xc0000000));
     $this->assertSame(-2, GPBWire::zigZagEncode32(0x7fffffff));
     $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
     $this->assertSame(0, GPBWire::zigZagDecode32(0));
     $this->assertSame(-1, GPBWire::zigZagDecode32(1));
     $this->assertSame(1, GPBWire::zigZagDecode32(2));
     $this->assertSame(-2, GPBWire::zigZagDecode32(3));
     $this->assertSame(0x3fffffff, GPBWire::zigZagDecode32(0x7ffffffe));
     $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7fffffff));
     $this->assertSame(0x7fffffff, GPBWire::zigZagDecode32(0xfffffffe));
     $this->assertSame((int) -2147483648, GPBWire::zigZagDecode32(0xffffffff));
     if (PHP_INT_SIZE == 4) {
         $this->assertSame('0', GPBWire::zigZagEncode64(0));
         $this->assertSame('1', GPBWire::zigZagEncode64(-1));
         $this->assertSame('2', GPBWire::zigZagEncode64(1));
         $this->assertSame('3', GPBWire::zigZagEncode64(-2));
         $this->assertSame('2147483646', GPBWire::zigZagEncode64(0x3fffffff));
         $this->assertSame('2147483647', GPBWire::zigZagEncode64(-1073741824));
         // 0xFFFFFFFFC0000000
         $this->assertSame('4294967294', GPBWire::zigZagEncode64(2147483647));
         // 0x7FFFFFFF
         $this->assertSame('4294967295', GPBWire::zigZagEncode64(-2147483648));
         // 0xFFFFFFFF80000000
         $this->assertSame('18446744073709551614', GPBWire::zigZagEncode64("9223372036854775807"));
         $this->assertSame('18446744073709551615', GPBWire::zigZagEncode64("-9223372036854775808"));
         $this->assertSame('0', GPBWire::zigZagDecode64(0));
         $this->assertSame('-1', GPBWire::zigZagDecode64(1));
         $this->assertSame('1', GPBWire::zigZagDecode64(2));
         $this->assertSame('-2', GPBWire::zigZagDecode64(3));
     } else {
         $this->assertSame(0, GPBWire::zigZagEncode64(0));
         $this->assertSame(1, GPBWire::zigZagEncode64(-1));
         $this->assertSame(2, GPBWire::zigZagEncode64(1));
         $this->assertSame(3, GPBWire::zigZagEncode64(-2));
         $this->assertSame(0x7ffffffe, GPBWire::zigZagEncode64(0x3fffffff));
         $this->assertSame(0x7fffffff, GPBWire::zigZagEncode64(1.844674407263581E+19));
         $this->assertSame(0xfffffffe, GPBWire::zigZagEncode64(0x7fffffff));
         $this->assertSame(0xffffffff, GPBWire::zigZagEncode64(1.8446744071562068E+19));
         $this->assertSame(-2, GPBWire::zigZagEncode64(0x7fffffffffffffff));
         $this->assertSame(-1, GPBWire::zigZagEncode64(9.223372036854776E+18));
         $this->assertSame(0, GPBWire::zigZagDecode64(0));
         $this->assertSame(-1, GPBWire::zigZagDecode64(1));
         $this->assertSame(1, GPBWire::zigZagDecode64(2));
         $this->assertSame(-2, GPBWire::zigZagDecode64(3));
     }
     // Round trip
     $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
     $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
     $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
     $this->assertSame(14927, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
     $this->assertSame(-3612, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
 }