Re: Why does my Exercism task fail?
- Posted by jimcbrown (admin) Feb 13, 2023
- 562 views
axtens_bruce said...
Code
public function square(integer n if n < 1 or n > 64 then return "square must be between 1 and 64" end if return power(2, (n-1)) end function public function total() atom sum = 0 for i = 1 to 64 do sum += square(i) end for return sum end function
Tests
include std/unittest.e include grains.ex test_equal("grains on square 1",square(1),1) test_equal("grains on square 2",square(2),2) test_equal("grains on square 3",square(3),4) test_equal("grains on square 4",square(4),8) test_equal("grains on square 16",square(16),32768) test_equal("grains on square 32",square(32),2147483648) test_equal("grains on square 64",square(64),9223372036854775808) test_equal("square 0 raises an exception",square(0),"square must be between 1 and 64") test_equal("negative square raises an exception",square(-1),"square must be between 1 and 64") test_equal("square greater than 64 raises an exception",square(65),"square must be between 1 and 64") test_equal("returns the total number of grains on the board", total(), 18446744073709551615) test_report()
Run, failing on test test_equal("grains on square 64",square(64),9223372036854775808)
>eutest interpreting t_grains.e: failed: grains on square 64, expected: 9.22337203685478e+018 but got: -9.22337203685478e+018 11 tests run, 10 passed, 1 failed, 91% success FAILURE: t_grains.e program died with status 1 Test results summary: FAIL: t_grains.e Files (run: 1) (failed: 1) (0% success)
What does running "eui version" return for you?
This is really odd. You have the expected and outcome backwards (see https://openeuphoria.org/docs/std_unittest.html#_6306_unittestingframework ) so square(64) works correctly (more proof - if square(64) returned the wrong value, total() would also be wrong), but it seems using the hardcoded literal fails as it is somehow incorrectly interpreted to be a negative value.
Also, what happens if you specify 9.223372037e+18 as the result instead of 9223372036854775808 ?