Fix status conversion

This commit is contained in:
Romain Neutron
2012-01-03 13:39:20 +01:00
parent 008b1d66f6
commit de91fc5c0e
2 changed files with 84 additions and 29 deletions

View File

@@ -480,6 +480,15 @@ class databox_status
$status = '0'; $status = '0';
if(substr($stat1, 0, 2) === '0x')
{
$stat1 = self::hex2bin(substr($stat1, 2));
}
if(substr($stat2, 0, 2) === '0x')
{
$stat2 = self::hex2bin(substr($stat2, 2));
}
$sql = 'select bin(0b' . trim($stat1) . ' & 0b' . trim($stat2) . ') as result'; $sql = 'select bin(0b' . trim($stat1) . ' & 0b' . trim($stat2) . ') as result';
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
@@ -501,6 +510,15 @@ class databox_status
$status = '0'; $status = '0';
if(substr($stat1, 0, 2) === '0x')
{
$stat1 = self::hex2bin(substr($stat1, 2));
}
if(substr($stat2, 0, 2) === '0x')
{
$stat2 = self::hex2bin(substr($stat2, 2));
}
$sql = 'select bin(0b' . trim($stat1) . ' & ~0b' . trim($stat2) . ') as result'; $sql = 'select bin(0b' . trim($stat1) . ' & ~0b' . trim($stat2) . ') as result';
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
@@ -522,6 +540,15 @@ class databox_status
$status = '0'; $status = '0';
if(substr($stat1, 0, 2) === '0x')
{
$stat1 = self::hex2bin(substr($stat1, 2));
}
if(substr($stat2, 0, 2) === '0x')
{
$stat2 = self::hex2bin(substr($stat2, 2));
}
$sql = 'select bin(0b' . trim($stat1) . ' | 0b' . trim($stat2) . ') as result'; $sql = 'select bin(0b' . trim($stat1) . ' | 0b' . trim($stat2) . ') as result';
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
@@ -539,17 +566,24 @@ class databox_status
public static function dec2bin($status) public static function dec2bin($status)
{ {
$status = (string) $status;
if(!ctype_digit($status))
{
throw new \Exception('Non-decimal value');
}
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection();
$status = '0'; $sql = 'select bin(' . $status . ') as result';
$sql = 'select bin(' . ((int) $status) . ') as result';
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
$stmt->execute(); $stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$status = '0';
if ($row) if ($row)
{ {
$status = $row['result']; $status = $row['result'];
@@ -560,17 +594,28 @@ class databox_status
public static function hex2bin($status) public static function hex2bin($status)
{ {
$status = (string) $status;
if(substr($status, 0, 2) === '0x')
{
$status = substr($status, 2);
}
if(!ctype_xdigit($status))
{
throw new \Exception('Non-hexadecimal value');
}
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection();
$status = '0'; $sql = 'select BIN( CAST( 0x'.trim($status).' AS UNSIGNED ) ) as result';
$sql = 'select bin(0x' . trim($status) . ') as result';
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
$stmt->execute(); $stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$status = '0';
if ($row) if ($row)
{ {
$status = $row['result']; $status = $row['result'];

View File

@@ -14,6 +14,7 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract
*/ */
protected $object; protected $object;
protected $databox; protected $databox;
protected static $need_records = 1;
public function setUp() public function setUp()
{ {
@@ -119,10 +120,10 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract
*/ */
public function testOperation_and() public function testOperation_and()
{ {
// Remove the following lines when you implement this test. $this->assertEquals('0', databox_status::operation_and('0x001','0x010'));
$this->markTestIncomplete( $this->assertEquals('1', databox_status::operation_and('01','11'));
'This test has not been implemented yet.' $this->assertEquals('0', databox_status::operation_and('01','10'));
); $this->assertEquals('10', databox_status::operation_and('11','10'));
} }
/** /**
@@ -130,10 +131,11 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract
*/ */
public function testOperation_and_not() public function testOperation_and_not()
{ {
// Remove the following lines when you implement this test. $this->assertEquals('0', databox_status::operation_and_not('0x001','0x011'));
$this->markTestIncomplete( $this->assertEquals('0', databox_status::operation_and_not('01','11'));
'This test has not been implemented yet.' $this->assertEquals('1', databox_status::operation_and_not('01','10'));
); $this->assertEquals('1', databox_status::operation_and_not('11','10'));
$this->assertEquals('10', databox_status::operation_and_not('10','01'));
} }
/** /**
@@ -141,10 +143,8 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract
*/ */
public function testOperation_or() public function testOperation_or()
{ {
// Remove the following lines when you implement this test. $this->assertEquals('10001', databox_status::operation_or('0x001','0x011'));
$this->markTestIncomplete( $this->assertEquals('11', databox_status::operation_or('01','11'));
'This test has not been implemented yet.'
);
} }
/** /**
@@ -152,21 +152,31 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract
*/ */
public function testDec2bin() public function testDec2bin()
{ {
// Remove the following lines when you implement this test. $this->assertEquals('1010', databox_status::dec2bin('10'));
$this->markTestIncomplete(
'This test has not been implemented yet.' try
); {
}
catch(Exception $e)
{
}
} }
/**
* @todo Implement testHex2bin().
*/
public function testHex2bin() public function testHex2bin()
{ {
// Remove the following lines when you implement this test. $this->assertEquals('10100001', databox_status::hex2bin('0x0A1'));
$this->markTestIncomplete( $this->assertEquals('10100001', databox_status::hex2bin('0A1'));
'This test has not been implemented yet.'
); try
{
databox_status::hex2bin('G1');
$this->fail('Should raise an exception');
}
catch(Exception $e)
{
}
} }
} }