1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ConvertDecimal; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; |
10
|
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments; |
11
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
12
|
|
|
|
13
|
|
|
class Dec2HexTest extends AllSetupTeardown |
14
|
|
|
{ |
15
|
|
|
#[DataProvider('providerDEC2HEX')] |
16
|
|
|
public function testDirectCallToDEC2HEX(mixed $expectedResult, bool|float|int|string $value, null|float|int|string $digits = null): void |
17
|
|
|
{ |
18
|
|
|
$result = ($digits === null) ? ConvertDecimal::toHex($value) : ConvertDecimal::toHex($value, $digits); |
19
|
|
|
self::assertSame($expectedResult, $result); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
#[DataProvider('providerDEC2HEX')] |
23
|
|
|
public function testDEC2HEXAsFormula(mixed $expectedResult, mixed ...$args): void |
24
|
|
|
{ |
25
|
|
|
$arguments = new FormulaArguments(...$args); |
26
|
|
|
|
27
|
|
|
$calculation = Calculation::getInstance(); |
28
|
|
|
$formula = "=DEC2HEX({$arguments})"; |
29
|
|
|
|
30
|
|
|
$result = $calculation->calculateFormula($formula); |
31
|
|
|
self::assertSame($expectedResult, $result); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
#[DataProvider('providerDEC2HEX')] |
35
|
|
|
public function testDEC2HEXInWorksheet(mixed $expectedResult, mixed ...$args): void |
36
|
|
|
{ |
37
|
|
|
$arguments = new FormulaArguments(...$args); |
38
|
|
|
|
39
|
|
|
$worksheet = $this->getSheet(); |
40
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
41
|
|
|
$formula = "=DEC2HEX({$argumentCells})"; |
42
|
|
|
|
43
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
44
|
|
|
->getCell('A1') |
45
|
|
|
->getCalculatedValue(); |
46
|
|
|
self::assertSame($expectedResult, $result); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function providerDEC2HEX(): array |
50
|
|
|
{ |
51
|
|
|
return require 'tests/data/Calculation/Engineering/DEC2HEX.php'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
#[DataProvider('providerUnhappyDEC2HEX')] |
55
|
|
|
public function testDEC2HEXUnhappyPath(string $expectedException, mixed ...$args): void |
56
|
|
|
{ |
57
|
|
|
$arguments = new FormulaArguments(...$args); |
58
|
|
|
|
59
|
|
|
$worksheet = $this->getSheet(); |
60
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
61
|
|
|
$formula = "=DEC2HEX({$argumentCells})"; |
62
|
|
|
|
63
|
|
|
$this->expectException(CalculationException::class); |
64
|
|
|
$this->expectExceptionMessage($expectedException); |
65
|
|
|
$worksheet->setCellValue('A1', $formula) |
66
|
|
|
->getCell('A1') |
67
|
|
|
->getCalculatedValue(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function providerUnhappyDEC2HEX(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
['Formula Error: Wrong number of arguments for DEC2HEX() function'], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
#[DataProvider('providerDEC2HEXOds')] |
78
|
|
|
public function testDEC2HEXOds(mixed $expectedResult, bool|float|int|string $value, null|float|int|string $digits = null): void |
79
|
|
|
{ |
80
|
|
|
$this->setOpenOffice(); |
81
|
|
|
|
82
|
|
|
$result = ($digits === null) ? ConvertDecimal::toHex($value) : ConvertDecimal::toHex($value, $digits); |
83
|
|
|
self::assertSame($expectedResult, $result); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function providerDEC2HEXOds(): array |
87
|
|
|
{ |
88
|
|
|
return require 'tests/data/Calculation/Engineering/DEC2HEXOpenOffice.php'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testDEC2HEXFrac(): void |
92
|
|
|
{ |
93
|
|
|
$calculation = Calculation::getInstance(); |
94
|
|
|
$formula = '=DEC2HEX(17.1)'; |
95
|
|
|
|
96
|
|
|
$this->setGnumeric(); |
97
|
|
|
$result = $calculation->calculateFormula($formula); |
98
|
|
|
self::assertSame('11', $result, 'Gnumeric'); |
99
|
|
|
|
100
|
|
|
$this->setOpenOffice(); |
101
|
|
|
$result = $calculation->calculateFormula($formula); |
102
|
|
|
self::assertSame('11', $result, 'OpenOffice'); |
103
|
|
|
|
104
|
|
|
$this->setExcel(); |
105
|
|
|
$result = $calculation->calculateFormula($formula); |
106
|
|
|
self::assertSame('11', $result, 'Excel'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function test32bitHex(): void |
110
|
|
|
{ |
111
|
|
|
self::assertEquals('A2DE246000', ConvertDecimal::hex32bit(-400000000000, 'DE246000', true)); |
112
|
|
|
self::assertEquals('7FFFFFFFFF', ConvertDecimal::hex32bit(549755813887, 'FFFFFFFF', true)); |
113
|
|
|
self::assertEquals('FFFFFFFFFF', ConvertDecimal::hex32bit(-1, 'FFFFFFFF', true)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** @param mixed[] $expectedResult */ |
117
|
|
|
#[DataProvider('providerDec2HexArray')] |
118
|
|
|
public function testDec2HexArray(array $expectedResult, string $value): void |
119
|
|
|
{ |
120
|
|
|
$calculation = Calculation::getInstance(); |
121
|
|
|
|
122
|
|
|
$formula = "=DEC2HEX({$value})"; |
123
|
|
|
$result = $calculation->calculateFormula($formula); |
124
|
|
|
self::assertEquals($expectedResult, $result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function providerDec2HexArray(): array |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
'row/column vector' => [ |
131
|
|
|
[['4', '7', '3F', '99', 'CC', '155']], |
132
|
|
|
'{4, 7, 63, 153, 204, 341}', |
133
|
|
|
], |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|