Merge pull request #12 from PowerKiKi/unit-tests-base-conversion

Fix unit tests for all base conversion functions
This commit is contained in:
Mark Baker 2016-08-13 11:40:02 +01:00 committed by GitHub
commit 98ccf97e55
6 changed files with 40 additions and 25 deletions

View File

@ -1327,12 +1327,16 @@ class Engineering
if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) { if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
return Functions::VALUE(); return Functions::VALUE();
} }
$x = (string)floor($x); $x = (string)floor($x);
if ($x < -512 || $x > 511) {
return Functions::NAN();
}
$r = decbin($x); $r = decbin($x);
if (strlen($r) == 32) { // Two's Complement
// Two's Complement $r = substr($r, -10);
$r = substr($r, -10); if (strlen($r) >= 11) {
} elseif (strlen($r) >= 11) {
return Functions::NAN(); return Functions::NAN();
} }
@ -1491,11 +1495,8 @@ class Engineering
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) { if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
return Functions::NAN(); return Functions::NAN();
} }
if (hexdec($x) > 0x1FF) {
return Functions::NAN(); return self::DECTOBIN(self::HEXTODEC($x), $places);
}
$binVal = decbin(hexdec($x));
return substr(self::nbrConversionFormat($binVal, $places), -10);
} }
@ -1529,9 +1530,14 @@ class Engineering
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) { if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
return Functions::NAN(); return Functions::NAN();
} }
if (strlen($x)> 10) {
return Functions::NAN();
}
$binX = ''; $binX = '';
foreach (str_split($x) as $char) { foreach (str_split($x) as $char) {
$binX .= str_pad(base_convert($char, 16, 2), 3, '0', STR_PAD_LEFT); $binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
} }
if (strlen($binX) == 40 && $binX[0] == '1') { if (strlen($binX) == 40 && $binX[0] == '1') {
for ($i = 0; $i < 40; $i++) { for ($i = 0; $i < 40; $i++) {
@ -1587,12 +1593,15 @@ class Engineering
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) { if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
return Functions::NAN(); return Functions::NAN();
} }
$octVal = decoct(hexdec($x));
return self::nbrConversionFormat($octVal, $places); $decimal = self::HEXTODEC($x);
if ($decimal < -536870912 || $decimal > 536870911) {
return Functions::NAN();
}
return self::DECTOOCT($decimal, $places);
} }
/** /**
* OCTTOBIN * OCTTOBIN
* *
@ -1639,9 +1648,8 @@ class Engineering
if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) { if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
return Functions::NAN(); return Functions::NAN();
} }
$r = decbin(octdec($x));
return self::nbrConversionFormat($r, $places); return self::DECTOBIN(self::OCTTODEC($x), $places);
} }

View File

@ -472,7 +472,6 @@ class EngineeringTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider providerDEC2BIN * @dataProvider providerDEC2BIN
* @group fail19
*/ */
public function testDEC2BIN() public function testDEC2BIN()
{ {
@ -521,7 +520,6 @@ class EngineeringTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider providerHEX2BIN * @dataProvider providerHEX2BIN
* @group fail19
*/ */
public function testHEX2BIN() public function testHEX2BIN()
{ {
@ -538,7 +536,6 @@ class EngineeringTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider providerHEX2DEC * @dataProvider providerHEX2DEC
* @group fail19
*/ */
public function testHEX2DEC() public function testHEX2DEC()
{ {
@ -555,7 +552,6 @@ class EngineeringTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider providerHEX2OCT * @dataProvider providerHEX2OCT
* @group fail19
*/ */
public function testHEX2OCT() public function testHEX2OCT()
{ {
@ -572,7 +568,6 @@ class EngineeringTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider providerOCT2BIN * @dataProvider providerOCT2BIN
* @group fail19
*/ */
public function testOCT2BIN() public function testOCT2BIN()
{ {

View File

@ -1,5 +1,6 @@
357, "101100101" 357, "101100101"
1357, "#NUM!" // Too large 512, "#NUM!" // Too large
-513, "#NUM!" // Too small
9, 4, "1001" 9, 4, "1001"
9, 8, "00001001" 9, 8, "00001001"
9, 6.75, "001001" // Leading places as a float 9, 6.75, "001001" // Leading places as a float
@ -14,3 +15,4 @@
TRUE, "#VALUE!" // Non string TRUE, "#VALUE!" // Non string
-100, "1110011100" // 2's Complement -100, "1110011100" // 2's Complement
-107, "1110010101" // 2's Complement -107, "1110010101" // 2's Complement
-512, "1000000000" // 2's Complement

View File

@ -1,3 +1,8 @@
"FF", "11111111"
"1FF", "111111111"
"200", "#NUM!"
"FFFFFFFE00", "1000000000" // 2's Complement
"FFFFFFFDFF", "#NUM!" // 2's Complement
"01AB", "110101011" "01AB", "110101011"
"ABCD", "#NUM!" "ABCD", "#NUM!"
"F6", "11110110" "F6", "11110110"
@ -9,5 +14,4 @@
"0", "0" "0", "0"
"G3579A", "#NUM!" "G3579A", "#NUM!"
TRUE, "#VALUE!" TRUE, "#VALUE!"
"-107", "#NUM!" "-107", "#NUM!"
"FFFFFFFFFF", "1111111111" // 2's Complement

View File

@ -9,5 +9,7 @@
TRUE, "#VALUE!" TRUE, "#VALUE!"
"-107", "#NUM!" "-107", "#NUM!"
"A5", "165" "A5", "165"
"FFFFFFFF5B", "-165" "3DA408B9", "1034160313"
"3DA408B9", "1034160313" // 2's Complement "FFFFFFFF5B", "-165" // 2's Complement
"FFFFFFFFFF", "-1" // 2's Complement
"1FFFFFFFFFF", "#NUM!" // Too large

View File

@ -7,3 +7,7 @@
TRUE, "#VALUE!" TRUE, "#VALUE!"
"3579", "#NUM!" "3579", "#NUM!"
"7777777000", "1000000000" // 2's Complement "7777777000", "1000000000" // 2's Complement
"7777777777", "1111111111" // 2's Complement
"17777777777", "#NUM!" // Too small
"777", "111111111"
"1777", "#NUM!" // Too large