Skip to content

Commit 6c1651e

Browse files
authored
Floating-point Equality in Two Tests (#3064)
* Floating-point Equality in Two Tests Merging a change today, Git reported failures that did not occur during "normal" unit testing. The merge still succeeded, but ... The problem was an error comparing float values for equal, and the inequality occurred beyond the 14th decimal digit. Change the tests in question, which incidentally were not part of the merged changed, to use assertEqualsWithDelta. * Egad - 112 More Precision-related Problems Spread across 9 test members.
1 parent 252474c commit 6c1651e

File tree

11 files changed

+36
-47
lines changed

11 files changed

+36
-47
lines changed

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/ConvertUoMTest.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
7-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
87
use PHPUnit\Framework\TestCase;
98

109
class ConvertUoMTest extends TestCase
1110
{
12-
protected function setUp(): void
13-
{
14-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
15-
}
11+
const UOM_PRECISION = 1E-12;
1612

1713
public function testGetConversionGroups(): void
1814
{
@@ -52,7 +48,7 @@ public function testGetBinaryConversionMultipliers(): void
5248
public function testCONVERTUOM($expectedResult, ...$args): void
5349
{
5450
$result = Engineering::CONVERTUOM(...$args);
55-
self::assertEquals($expectedResult, $result);
51+
self::assertEqualsWithDelta($expectedResult, $result, self::UOM_PRECISION);
5652
}
5753

5854
public function providerCONVERTUOM(): array
@@ -69,7 +65,7 @@ public function testConvertUoMArray(array $expectedResult, string $value, string
6965

7066
$formula = "=CONVERT({$value}, {$fromUoM}, {$toUoM})";
7167
$result = $calculation->_calculateFormulaValue($formula);
72-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
68+
self::assertEqualsWithDelta($expectedResult, $result, self::UOM_PRECISION);
7369
}
7470

7571
public function providerConvertUoMArray(): array

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/ErfCTest.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
7-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
87
use PHPUnit\Framework\TestCase;
98

109
class ErfCTest extends TestCase
1110
{
1211
const ERF_PRECISION = 1E-12;
1312

14-
protected function setUp(): void
15-
{
16-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
17-
}
18-
1913
/**
2014
* @dataProvider providerERFC
2115
*
@@ -25,7 +19,6 @@ protected function setUp(): void
2519
public function testERFC($expectedResult, $lower): void
2620
{
2721
$result = Engineering::ERFC($lower);
28-
self::assertEquals($expectedResult, $result);
2922
self::assertEqualsWithDelta($expectedResult, $result, self::ERF_PRECISION);
3023
}
3124

@@ -43,7 +36,7 @@ public function testErfCArray(array $expectedResult, string $lower): void
4336

4437
$formula = "=ERFC({$lower})";
4538
$result = $calculation->_calculateFormulaValue($formula);
46-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
39+
self::assertEqualsWithDelta($expectedResult, $result, self::ERF_PRECISION);
4740
}
4841

4942
public function providerErfCArray(): array

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/ErfPreciseTest.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
7-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
87
use PHPUnit\Framework\TestCase;
98

109
class ErfPreciseTest extends TestCase
1110
{
1211
const ERF_PRECISION = 1E-12;
1312

14-
protected function setUp(): void
15-
{
16-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
17-
}
18-
1913
/**
2014
* @dataProvider providerERFPRECISE
2115
*
@@ -25,7 +19,6 @@ protected function setUp(): void
2519
public function testERFPRECISE($expectedResult, $limit): void
2620
{
2721
$result = Engineering::ERFPRECISE($limit);
28-
self::assertEquals($expectedResult, $result);
2922
self::assertEqualsWithDelta($expectedResult, $result, self::ERF_PRECISION);
3023
}
3124

@@ -43,7 +36,7 @@ public function testErfPreciseArray(array $expectedResult, string $limit): void
4336

4437
$formula = "=ERF.PRECISE({$limit})";
4538
$result = $calculation->_calculateFormulaValue($formula);
46-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
39+
self::assertEqualsWithDelta($expectedResult, $result, self::ERF_PRECISION);
4740
}
4841

4942
public function providerErfPreciseArray(): array

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/ErfTest.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
7-
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
87
use PHPUnit\Framework\TestCase;
98

109
class ErfTest extends TestCase
1110
{
1211
const ERF_PRECISION = 1E-12;
1312

14-
protected function setUp(): void
15-
{
16-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
17-
}
18-
1913
/**
2014
* @dataProvider providerERF
2115
*
@@ -26,7 +20,6 @@ protected function setUp(): void
2620
public function testERF($expectedResult, $lower, $upper = null): void
2721
{
2822
$result = Engineering::ERF($lower, $upper);
29-
self::assertEquals($expectedResult, $result);
3023
self::assertEqualsWithDelta($expectedResult, $result, self::ERF_PRECISION);
3124
}
3225

@@ -44,7 +37,7 @@ public function testErfArray(array $expectedResult, string $lower, string $upper
4437

4538
$formula = "=ERF({$lower}, {$upper})";
4639
$result = $calculation->_calculateFormulaValue($formula);
47-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
40+
self::assertEqualsWithDelta($expectedResult, $result, self::ERF_PRECISION);
4841
}
4942

5043
public function providerErfArray(): array

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/FactTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class FactTest extends AllSetupTeardown
88
{
9+
const FACT_PRECISION = 1E-12;
10+
911
/**
1012
* @dataProvider providerFACT
1113
*
@@ -53,7 +55,7 @@ public function testFACTGnumeric($expectedResult, $arg1): void
5355
$sheet->getCell('B1')->setValue('=FACT(A1)');
5456
}
5557
$result = $sheet->getCell('B1')->getCalculatedValue();
56-
self::assertEquals($expectedResult, $result);
58+
self::assertEqualsWithDelta($expectedResult, $result, self::FACT_PRECISION);
5759
}
5860

5961
public function providerFACTGnumeric(): array
@@ -70,7 +72,7 @@ public function testFactArray(array $expectedResult, string $array): void
7072

7173
$formula = "=FACT({$array})";
7274
$result = $calculation->_calculateFormulaValue($formula);
73-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
75+
self::assertEqualsWithDelta($expectedResult, $result, self::FACT_PRECISION);
7476
}
7577

7678
public function providerFactArray(): array

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MUnitTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class MUnitTest extends AllSetupTeardown
88
{
9+
const MU_PRECISION = 1.0E-12;
10+
911
public function testMUNIT(): void
1012
{
1113
$identity = MatrixFunctions::identity(3);
@@ -15,7 +17,7 @@ public function testMUNIT(): void
1517
self::assertEquals($startArray, $resultArray);
1618
$inverseArray = MatrixFunctions::inverse($startArray);
1719
$resultArray = MatrixFunctions::multiply($startArray, $inverseArray);
18-
self::assertEquals($identity, $resultArray);
20+
self::assertEqualsWithDelta($identity, $resultArray, self::MU_PRECISION);
1921
self::assertEquals('#VALUE!', MatrixFunctions::identity(0));
2022
self::assertEquals('#VALUE!', MatrixFunctions::identity(-1));
2123
self::assertEquals('#VALUE!', MatrixFunctions::identity('X'));

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/NumberValueTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class NumberValueTest extends AllSetupTeardown
88
{
9+
const NV_PRECISION = 1.0E-8;
10+
911
/**
1012
* @dataProvider providerNUMBERVALUE
1113
*
@@ -34,7 +36,7 @@ public function testNUMBERVALUE($expectedResult, $number = 'omitted', $decimal =
3436
$sheet->getCell('B1')->setValue('=NUMBERVALUE(A1, A2, A3)');
3537
}
3638
$result = $sheet->getCell('B1')->getCalculatedValue();
37-
self::assertEquals($expectedResult, $result);
39+
self::assertEqualsWithDelta($expectedResult, $result, self::NV_PRECISION);
3840
}
3941

4042
public function providerNUMBERVALUE(): array
@@ -51,7 +53,7 @@ public function testNumberValueArray(array $expectedResult, string $argument1, s
5153

5254
$formula = "=NumberValue({$argument1}, {$argument2}, {$argument3})";
5355
$result = $calculation->_calculateFormulaValue($formula);
54-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
56+
self::assertEqualsWithDelta($expectedResult, $result, self::NV_PRECISION);
5557
}
5658

5759
public function providerNumberValueArray(): array

tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class AdvancedValueBinderTest extends TestCase
1313
{
14+
const AVB_PRECISION = 1.0E-8;
15+
1416
/**
1517
* @var string
1618
*/
@@ -161,7 +163,7 @@ public function testTimes($value, $valueBinded): void
161163
$spreadsheet = new Spreadsheet();
162164
$sheet = $spreadsheet->getActiveSheet();
163165
$sheet->getCell('A1')->setValue($value);
164-
self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue());
166+
self::assertEqualsWithDelta($valueBinded, $sheet->getCell('A1')->getValue(), self::AVB_PRECISION);
165167
$spreadsheet->disconnectWorksheets();
166168
}
167169

tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class XlsxTest extends TestCase
1717
{
18+
const XLSX_PRECISION = 1.0E-8;
19+
1820
public function testLoadXlsxRowColumnAttributes(): void
1921
{
2022
$filename = 'tests/data/Reader/XLSX/rowColumnAttributeTest.xlsx';
@@ -133,10 +135,10 @@ public function testLoadXlsxPageSetup(): void
133135

134136
$pageMargins = $worksheet->getPageMargins();
135137
// Convert from inches to cm for testing
136-
self::assertEquals(2.5, $pageMargins->getTop() * 2.54);
137-
self::assertEquals(3.3, $pageMargins->getLeft() * 2.54);
138-
self::assertEquals(3.3, $pageMargins->getRight() * 2.54);
139-
self::assertEquals(1.3, $pageMargins->getHeader() * 2.54);
138+
self::assertEqualsWithDelta(2.5, $pageMargins->getTop() * 2.54, self::XLSX_PRECISION);
139+
self::assertEqualsWithDelta(3.3, $pageMargins->getLeft() * 2.54, self::XLSX_PRECISION);
140+
self::assertEqualsWithDelta(3.3, $pageMargins->getRight() * 2.54, self::XLSX_PRECISION);
141+
self::assertEqualsWithDelta(1.3, $pageMargins->getHeader() * 2.54, self::XLSX_PRECISION);
140142

141143
self::assertEquals(PageSetup::PAPERSIZE_A4, $worksheet->getPageSetup()->getPaperSize());
142144
self::assertEquals(['A10', 'A20', 'A30', 'A40', 'A50'], array_keys($worksheet->getBreaks()));

tests/PhpSpreadsheetTests/Shared/FontTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class FontTest extends TestCase
1010
{
11+
const FONT_PRECISION = 1.0E-12;
12+
1113
public function testGetAutoSizeMethod(): void
1214
{
1315
$expectedResult = Font::AUTOSIZE_METHOD_APPROX;
@@ -63,7 +65,7 @@ public function providerFontSizeToPixels(): array
6365
public function testInchSizeToPixels($expectedResult, $size): void
6466
{
6567
$result = Font::inchSizeToPixels($size);
66-
self::assertEquals($expectedResult, $result);
68+
self::assertEqualsWithDelta($expectedResult, $result, self::FONT_PRECISION);
6769
}
6870

6971
public function providerInchSizeToPixels(): array
@@ -80,7 +82,7 @@ public function providerInchSizeToPixels(): array
8082
public function testCentimeterSizeToPixels($expectedResult, $size): void
8183
{
8284
$result = Font::centimeterSizeToPixels($size);
83-
self::assertEquals($expectedResult, $result);
85+
self::assertEqualsWithDelta($expectedResult, $result, self::FONT_PRECISION);
8486
}
8587

8688
public function providerCentimeterSizeToPixels(): array

tests/PhpSpreadsheetTests/Shared/Trend/LinearBestFitTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class LinearBestFitTest extends TestCase
99
{
10+
const LBF_PRECISION = 1.0E-8;
11+
1012
/**
1113
* @dataProvider providerLinearBestFit
1214
*
@@ -27,13 +29,13 @@ public function testLinearBestFit(
2729
): void {
2830
$bestFit = new LinearBestFit($yValues, $xValues);
2931
$slope = $bestFit->getSlope(1);
30-
self::assertEquals($expectedSlope[0], $slope);
32+
self::assertEqualsWithDelta($expectedSlope[0], $slope, self::LBF_PRECISION);
3133
$slope = $bestFit->getSlope();
32-
self::assertEquals($expectedSlope[1], $slope);
34+
self::assertEqualsWithDelta($expectedSlope[1], $slope, self::LBF_PRECISION);
3335
$intersect = $bestFit->getIntersect(1);
34-
self::assertEquals($expectedIntersect[0], $intersect);
36+
self::assertEqualsWithDelta($expectedIntersect[0], $intersect, self::LBF_PRECISION);
3537
$intersect = $bestFit->getIntersect();
36-
self::assertEquals($expectedIntersect[1], $intersect);
38+
self::assertEqualsWithDelta($expectedIntersect[1], $intersect, self::LBF_PRECISION);
3739

3840
$equation = $bestFit->getEquation(2);
3941
self::assertEquals($expectedEquation, $equation);

0 commit comments

Comments
 (0)