Grains test doesn't handle bugs caused by `**`
sshine opened this issue · 1 comments
This should fail the test suite:
sub total_grains {
return 2 ** 64 - 42;
}
The test appears to pass for 2 ** 64
, 2 ** 64 - 1
and 2 ** 64 - 2
, which can't all be true.
This is because **
is made with C's pow
which operates internally on doubles. The current test converts the expected value 18446744073709551615
to a double and an imprecise comparison erroneously succeeds.
The test suite should detect this and report a problem to relieve mentors of having to point it out.
Perhaps the test suite can provide a negative test,
unlike total_grains(), qr/e\+/, "Using '**' without 'use bignum;' uses doubles that are too imprecise for this result.";
Additionally, use 'eq'
instead of '=='
to compare the result as strings instead of numbers, since then the error message shows up as:
# Failed test 'returns the total number of grains on the board'
# at ./grains.t line 16.
# got: '1.84467440737096e+19'
# expected: '18446744073709551615'
which should more clearly indicate the problem that the lower digits have vanished.
Perhaps apply this reasoning in the test for grains_on_square 64
, since otherwise **
erroneously succeeds in this case, too:
# Failed test 'square no. 64'
# at ./grains.t line 23.
# got: '9.22337203685478e+18'
# expected: '9223372036854775808'
This is still broken and students stumble upon it.