Skip to content

Commit 98ccf97

Browse files
author
Mark Baker
authored
Merge pull request #12 from PowerKiKi/unit-tests-base-conversion
Fix unit tests for all base conversion functions
2 parents d8abf07 + 8fc29bd commit 98ccf97

File tree

6 files changed

+40
-25
lines changed

6 files changed

+40
-25
lines changed

src/PhpSpreadsheet/Calculation/Engineering.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,12 +1327,16 @@ public static function DECTOBIN($x, $places = null)
13271327
if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
13281328
return Functions::VALUE();
13291329
}
1330+
13301331
$x = (string)floor($x);
1332+
if ($x < -512 || $x > 511) {
1333+
return Functions::NAN();
1334+
}
1335+
13311336
$r = decbin($x);
1332-
if (strlen($r) == 32) {
1333-
// Two's Complement
1334-
$r = substr($r, -10);
1335-
} elseif (strlen($r) >= 11) {
1337+
// Two's Complement
1338+
$r = substr($r, -10);
1339+
if (strlen($r) >= 11) {
13361340
return Functions::NAN();
13371341
}
13381342

@@ -1491,11 +1495,8 @@ public static function HEXTOBIN($x, $places = null)
14911495
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
14921496
return Functions::NAN();
14931497
}
1494-
if (hexdec($x) > 0x1FF) {
1495-
return Functions::NAN();
1496-
}
1497-
$binVal = decbin(hexdec($x));
1498-
return substr(self::nbrConversionFormat($binVal, $places), -10);
1498+
1499+
return self::DECTOBIN(self::HEXTODEC($x), $places);
14991500
}
15001501

15011502

@@ -1529,9 +1530,14 @@ public static function HEXTODEC($x)
15291530
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
15301531
return Functions::NAN();
15311532
}
1533+
1534+
if (strlen($x)> 10) {
1535+
return Functions::NAN();
1536+
}
1537+
15321538
$binX = '';
15331539
foreach (str_split($x) as $char) {
1534-
$binX .= str_pad(base_convert($char, 16, 2), 3, '0', STR_PAD_LEFT);
1540+
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
15351541
}
15361542
if (strlen($binX) == 40 && $binX[0] == '1') {
15371543
for ($i = 0; $i < 40; $i++) {
@@ -1587,11 +1593,14 @@ public static function HEXTOOCT($x, $places = null)
15871593
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
15881594
return Functions::NAN();
15891595
}
1590-
$octVal = decoct(hexdec($x));
15911596

1592-
return self::nbrConversionFormat($octVal, $places);
1593-
}
1597+
$decimal = self::HEXTODEC($x);
1598+
if ($decimal < -536870912 || $decimal > 536870911) {
1599+
return Functions::NAN();
1600+
}
15941601

1602+
return self::DECTOOCT($decimal, $places);
1603+
}
15951604

15961605
/**
15971606
* OCTTOBIN
@@ -1639,9 +1648,8 @@ public static function OCTTOBIN($x, $places = null)
16391648
if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
16401649
return Functions::NAN();
16411650
}
1642-
$r = decbin(octdec($x));
16431651

1644-
return self::nbrConversionFormat($r, $places);
1652+
return self::DECTOBIN(self::OCTTODEC($x), $places);
16451653
}
16461654

16471655

unitTests/Classes/src/Calculation/EngineeringTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ public function providerBIN2OCT()
472472

473473
/**
474474
* @dataProvider providerDEC2BIN
475-
* @group fail19
476475
*/
477476
public function testDEC2BIN()
478477
{
@@ -521,7 +520,6 @@ public function providerDEC2OCT()
521520

522521
/**
523522
* @dataProvider providerHEX2BIN
524-
* @group fail19
525523
*/
526524
public function testHEX2BIN()
527525
{
@@ -538,7 +536,6 @@ public function providerHEX2BIN()
538536

539537
/**
540538
* @dataProvider providerHEX2DEC
541-
* @group fail19
542539
*/
543540
public function testHEX2DEC()
544541
{
@@ -555,7 +552,6 @@ public function providerHEX2DEC()
555552

556553
/**
557554
* @dataProvider providerHEX2OCT
558-
* @group fail19
559555
*/
560556
public function testHEX2OCT()
561557
{
@@ -572,7 +568,6 @@ public function providerHEX2OCT()
572568

573569
/**
574570
* @dataProvider providerOCT2BIN
575-
* @group fail19
576571
*/
577572
public function testOCT2BIN()
578573
{

unitTests/rawTestData/Calculation/Engineering/DEC2BIN.data

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
357, "101100101"
2-
1357, "#NUM!" // Too large
2+
512, "#NUM!" // Too large
3+
-513, "#NUM!" // Too small
34
9, 4, "1001"
45
9, 8, "00001001"
56
9, 6.75, "001001" // Leading places as a float
@@ -14,3 +15,4 @@
1415
TRUE, "#VALUE!" // Non string
1516
-100, "1110011100" // 2's Complement
1617
-107, "1110010101" // 2's Complement
18+
-512, "1000000000" // 2's Complement

unitTests/rawTestData/Calculation/Engineering/HEX2BIN.data

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"FF", "11111111"
2+
"1FF", "111111111"
3+
"200", "#NUM!"
4+
"FFFFFFFE00", "1000000000" // 2's Complement
5+
"FFFFFFFDFF", "#NUM!" // 2's Complement
16
"01AB", "110101011"
27
"ABCD", "#NUM!"
38
"F6", "11110110"
@@ -9,5 +14,4 @@
914
"0", "0"
1015
"G3579A", "#NUM!"
1116
TRUE, "#VALUE!"
12-
"-107", "#NUM!"
13-
"FFFFFFFFFF", "1111111111" // 2's Complement
17+
"-107", "#NUM!"

unitTests/rawTestData/Calculation/Engineering/HEX2DEC.data

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
TRUE, "#VALUE!"
1010
"-107", "#NUM!"
1111
"A5", "165"
12-
"FFFFFFFF5B", "-165"
13-
"3DA408B9", "1034160313" // 2's Complement
12+
"3DA408B9", "1034160313"
13+
"FFFFFFFF5B", "-165" // 2's Complement
14+
"FFFFFFFFFF", "-1" // 2's Complement
15+
"1FFFFFFFFFF", "#NUM!" // Too large

unitTests/rawTestData/Calculation/Engineering/OCT2BIN.data

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
TRUE, "#VALUE!"
88
"3579", "#NUM!"
99
"7777777000", "1000000000" // 2's Complement
10+
"7777777777", "1111111111" // 2's Complement
11+
"17777777777", "#NUM!" // Too small
12+
"777", "111111111"
13+
"1777", "#NUM!" // Too large

0 commit comments

Comments
 (0)