JS solution to Stack Machine fails tests
tclamb opened this issue · comments
$ npm test
...
3 failing
...
1) stackMachine should return the top number from the stack:
AssertionError: -1 == 0
Input: 0
2) stackMachine should multiply two numbers from the top of the stack:
AssertionError: -1 == 0
Input: 01*
3) stackMachine should return -1 when 12-bit stack-machine overflows:
AssertionError: 4096 == -1
Input: 88*1+97**1+
...
npm ERR! weird error 3
...
For the last error, it's helpful to note that 212 = 4096.
The input it fails on is equivalent to 4095 + 1 = 4096.
@tclamb I fixed the first two tests, but the final one is odd. The function says it accepts an "arbitrary number of 12-bit unsigned integers". I'm not sure whether that would imply that the return is also 12-bit?
Yes, I believe it would imply the return must be 12-bit. Since the hypothetical stack machine returns the last unsigned integer on top of its stack, and its stack can only hold 12-bit integers, I think it follows that it must return a 12-bit unsigned integer. At least, that was my rationale for the test.
@blakeembrey It would probably be a good idea to also add a test where an intermediate step overflows, but the overall result would fit within 12-bits. For example, "88_88_1-" (= 64 * 64 - 1 = 4096 - 1 = 4095).
@tclamb Cool, I can add that tomorrow. Just to be clear, it should fail if any intermediate steps overflow?
Yup! So that example should return -1 due to overflow. :)