Skip to content

Extend tests of bcmath extension #11563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 5, 2023
Merged

Conversation

jorgsowa
Copy link
Contributor

@jorgsowa jorgsowa commented Jun 29, 2023

I wanted to extend the BC library, but before that I thought about strengthening the test suite for the existing functionalities.

During those test I found some things which can be improved later.

I covered all of the possible types of numbers (accordingly to the functions):

  • different formats of zero (0, 0.0)
  • small integers and decimals
  • large integers and large decimals with large fractional part
  • negative numbers for all of the above ones

@iluuu1994
Copy link
Member

Thank you @jorgsowa! Maybe @Girgias would be interested to review this?

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the 3 nested foreach loops are repeated in every test, I would rather have an included file that defines a function that takes the required arguments + function to execute + symbol to display.

Lot of missing EOLs before EOF.

Those tests are generate a lot of output, that I only skimmed through the actual results.

Comment on lines 10 to 12
$firstSummands = ["0", "0.00", "-0", "-0.00"];
$secondSummands = array_merge($firstSummands, ["15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01", "15151324141414.412312232141241", "-132132245132134.1515123765412", "141241241241241248267654747412", "-149143276547656984948124912", "0.1322135476547459213732911312", "-0.123912932193769965476541321"]);
$scales = [0,10];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split that second array into multiple lines or have it as a constant at the beginning of the file.

As this line is just huge.

foreach($firstSummands as $firstSummand) {
echo "Number \"$firstSummand\" (scale $scale)\n";
foreach($secondSummands as $secondSummand) {
echo str_pad($firstSummand, 6, ' ', STR_PAD_LEFT), " + ", str_pad($secondSummand, 30), ' = ', bcadd($firstSummand, $secondSummand, $scale),"\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use constants for the padded length.

?>
--EXPECT--
-1
-1
0
1
-1
1
0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL before EOF

?>
--EXPECT--
0
1
-1
1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL before EOF

@@ -20,3 +21,4 @@ echo bccomp("-2.29", "-2.3", "1");
-1
1
1
-1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL before EOF

Comment on lines 10 to 22
$firstFactors = ["15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01"];
$secondFactors = array_merge($firstFactors, ["0", "0.00", "-0", "-0.00", "15151324141414.412312232141241", "-132132245132134.1515123765412", "141241241241241248267654747412", "-149143276547656984948124912", "0.1322135476547459213732911312", "-0.123912932193769965476541321"]);
$scales = [0,10];

foreach($scales as $scale) {
foreach($firstFactors as $firstFactor) {
echo "Number \"$firstFactor\" (scale $scale)\n";
foreach($secondFactors as $secondFactor) {
echo str_pad($firstFactor, 6, ' ', STR_PAD_LEFT), " × " , str_pad($secondFactor, 30), ' = ', bcmul($firstFactor, $secondFactor, $scale),"\n";
}
echo "\n";
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format

-0.01 × 141241241241241248267654747412 = -1412412412412412482676547474.1200000000
-0.01 × -149143276547656984948124912 = 1491432765476569849481249.1200000000
-0.01 × 0.1322135476547459213732911312 = -0.0013221354
-0.01 × -0.123912932193769965476541321 = 0.0012391293
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL

Comment on lines 10 to 23
$firstSummands = ["15151324141414.412312232141241", "-132132245132134.1515123765412", "141241241241241248267654747412", "-149143276547656984948124912", "0.1322135476547459213732911312", "-0.123912932193769965476541321"];
$secondSummands = array_merge($firstSummands, ["0", "0.00", "-0", "-0.00", "15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01"]);
$scales = [0,10];

foreach($scales as $scale) {
foreach($firstSummands as $firstSummand) {
echo "Number \"$firstSummand\" (scale $scale)\n";
foreach($secondSummands as $secondSummand) {
echo $firstSummand, " + ", str_pad($secondSummand, 30), ' = ', bcadd($firstSummand, $secondSummand, $scale),"\n";
}
echo "\n";
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format

Comment on lines 10 to 22
$firstSummands = ["15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01"];
$secondSummands = array_merge($firstSummands, ["0", "0.00", "-0", "-0.00", "15151324141414.412312232141241", "-132132245132134.1515123765412", "141241241241241248267654747412", "-149143276547656984948124912", "0.1322135476547459213732911312", "-0.123912932193769965476541321"]);
$scales = [0,10];

foreach($scales as $scale) {
foreach($firstSummands as $firstSummand) {
echo "Number \"$firstSummand\" (scale $scale)\n";
foreach($secondSummands as $secondSummand) {
echo str_pad($firstSummand, 6, ' ', STR_PAD_LEFT), " + ", str_pad($secondSummand, 30), ' = ', bcadd($firstSummand, $secondSummand, $scale),"\n";
}
echo "\n";
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format

-0.01 + 141241241241241248267654747412 = 141241241241241248267654747411.9900000000
-0.01 + -149143276547656984948124912 = -149143276547656984948124912.0100000000
-0.01 + 0.1322135476547459213732911312 = 0.1222135476
-0.01 + -0.123912932193769965476541321 = -0.1339129321
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL

@jorgsowa
Copy link
Contributor Author

jorgsowa commented Jul 1, 2023

@Girgias, thank you for the comprehensive review. I applied the changes and I will focus more on a proper formatting next time. I hope that extended tests will help with refactoring of this library within your existing PR: #10774

}
}

?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent output happening

Suggested change
?>

echo bcpow("2", "64"),"\n";
echo bcpow("-2.555", "5", 2),"\n";

$exponents = ["15", "-15", "1", "-9", "0", "-0", "252", "-112"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the large exponents where moved to another test but not dropped here?

Comment on lines +20 to +23
"0",
"0.00",
"-0",
"-0.00",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As multiplying with 0 is already done in another test those should be removed IMHO

Comment on lines +13 to +16
"0",
"0.00",
"-0",
"-0.00",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

-0.00 / 141241241241241248267654747412 = 0.0000000000
-0.00 / -149143276547656984948124912 = 0.0000000000
-0.00 / 0.1322135476547459213732911312 = 0.0000000000
-0.00 / -0.123912932193769965476541321 = 0.0000000000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL before EOF

Comment on lines 14 to 15
echo bccomp("-2.29", "-2.3", "1")."\n";
echo bccomp("-2.29", "0", "1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo bccomp("-2.29", "-2.3", "1")."\n";
echo bccomp("-2.29", "0", "1");
echo bccomp("-2.29", "-2.3", "1")."\n";
echo bccomp("-2.29", "0", "1")."\n";

Just to be consistent.

Comment on lines 11 to 12
echo bccomp("2.29", "2.3", "2")."\n";
echo bccomp("2.29", "0", "2");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo bccomp("2.29", "2.3", "2")."\n";
echo bccomp("2.29", "0", "2");
echo bccomp("2.29", "2.3", "2")."\n";
echo bccomp("2.29", "0", "2")."\n";

Just to be consistent

Comment on lines 14 to 15
echo bccomp("1", "0"), "\n";
echo bccomp("0.000", "0", 3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo bccomp("1", "0"), "\n";
echo bccomp("0.000", "0", 3);
echo bccomp("1", "0"), "\n";
echo bccomp("0.000", "0", 3), "\n";

Consistency

Comment on lines +20 to +23
"0",
"0.00",
"-0",
"-0.00",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already testing 0 in another test

Copy link
Contributor Author

@jorgsowa jorgsowa Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We test it, but as a first arguments of the function. In this test we pass zeroes only as second argument of the function. The same with other tests. Do you think it's too much coverage and should be removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, should be fine then.

@jorgsowa jorgsowa requested a review from Girgias July 3, 2023 06:50
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forget to push the changes while asking for a rereview?

@jorgsowa
Copy link
Contributor Author

jorgsowa commented Jul 4, 2023

@Girgias, thank you for the review. I pushed another small set of improvements. Found some duplicated values in tests. I'm not sure about removing zeroes from other tests, but if you think it's not needed then I can remove it.

@jorgsowa jorgsowa requested a review from Girgias July 4, 2023 21:42
@Girgias Girgias merged commit ee22612 into php:master Jul 5, 2023
@Girgias
Copy link
Member

Girgias commented Jul 5, 2023

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants