public function testEncodeDecodeValid()
 {
     $validData = array(array(20, 30, 40, 50), array(0, IntegerQuartet::MAX_16BIT_INTEGER_VALUE, 0, IntegerQuartet::MAX_16BIT_INTEGER_VALUE), array(IntegerQuartet::MAX_16BIT_INTEGER_VALUE, 0, IntegerQuartet::MAX_16BIT_INTEGER_VALUE, 0), array(IntegerQuartet::MAX_16BIT_INTEGER_VALUE, IntegerQuartet::MAX_16BIT_INTEGER_VALUE, IntegerQuartet::MAX_16BIT_INTEGER_VALUE, IntegerQuartet::MAX_16BIT_INTEGER_VALUE));
     foreach ($validData as $i => $data) {
         // Test encode
         $integerQuartet = new IntegerQuartet($data[0], $data[1], $data[2], $data[3]);
         $encodedInteger = $integerQuartet->get64bitInteger();
         $this->assertEquals($data[0], $integerQuartet->getIntegerA());
         $this->assertEquals($data[1], $integerQuartet->getIntegerB());
         $this->assertEquals($data[2], $integerQuartet->getIntegerC());
         $this->assertEquals($data[3], $integerQuartet->getIntegerD());
         $this->assertLessThanOrEqual(PHP_INT_MAX, $encodedInteger);
         $this->assertGreaterThanOrEqual(-(PHP_INT_MAX + 1), $encodedInteger);
         // Test decode
         $decodedQuartet = IntegerQuartet::unpack($encodedInteger);
         $this->assertEquals($data[0], $decodedQuartet->getIntegerA());
         $this->assertEquals($data[1], $decodedQuartet->getIntegerB());
         $this->assertEquals($data[2], $decodedQuartet->getIntegerC());
         $this->assertEquals($data[3], $decodedQuartet->getIntegerD());
     }
 }
Beispiel #2
0
<?php

require "./vendor/autoload.php";
Header("Content-type: text/plain");
// INTEGER PAIR ------------------------------------------------
// Create an IntegerPair object with 2 integers (within the 32bit range)
$integerPair = new \IntegerPacking\IntegerPair(20, 30);
echo "Encoded Pair | " . $integerPair->get64bitInteger() . "\n";
// Prints the 64 bit integer that combines the 2 32bit integers
// Creates a new IntegerPair object from the packed 64bit integer
$otherIntegerPair = \IntegerPacking\IntegerPair::unpack($integerPair->get64bitInteger());
echo "Decoded Pair | A: " . $otherIntegerPair->getIntegerA() . " - B: " . $otherIntegerPair->getIntegerB();
// INTEGER QUARTET ------------------------------------------------
// Create an IntegerQuartet object with 4 integers (within the 16bit range)
$integerQuartet = new \IntegerPacking\IntegerQuartet(20, 30, 40, 50);
echo "Encoded Quartet | " . $integerPair->get64bitInteger() . "\n";
// Prints the 64 bit integer that combines the 4 16bit integers
// Creates a new IntegerQuartet object from the packed 64bit integer
$otherIntegerQuartet = \IntegerPacking\IntegerQuartet::unpack($integerQuartet->get64bitInteger());
echo "Decoded Quartet | A: " . $otherIntegerQuartet->getIntegerA() . " - B: " . $otherIntegerQuartet->getIntegerB() . " - C: " . $otherIntegerQuartet->getIntegerC() . " - D: " . $otherIntegerQuartet->getIntegerD();