'From Squeak3.10beta of 22 July 2007 [latest update: #7159] on 26 January 2008 at 2:37:44 am'!
"Change Set: TwoComplementDigitLogic-Test-M6874-nice
Date: 26 January 2008
Author: nice
This is test for http://bugs.squeak.org/view.php?id=6874
bitAnd: use digitLogic:op:len: which is bugged in some special cases:
When bit logic is performed on LargeNegativeInteger (two complement)
And the highbit just fall on a digit (8bit) boundary
And a carry is not taken into account
Example of tricky two complement:
11111111 10000000 00000000 00000000 00000000 is (2 raisedTo: 31) negated
11111111 01111111 11111111 11111111 11111111 is (2 raisedTo: 31) negated-1
result of bitAnd:
11111111 00000000 00000000 00000000 00000000 is (2 raisedTo: 32) negated
Implementation is tricky, because the absolute value is stored, not the two complements. Thus there are sort of -1-self operations leading to some carry for processing two complement of two complements....
Well, except the carry rz in algorithm was not taken into account,
the result was stored on 4 digits,
then reduced to zero...
A symptom of this bug was http://bugs.squeak.org/view.php?id=6873"
!
!IntegerTest methodsFor: 'tests - bitLogic' stamp: 'nice 1/26/2008 02:22'!
testTwoComplementBitLogicWithCarry
"This is non regression test for http://bugs.squeak.org/view.php?id=6874"
"By property of two complement, following operation is:
...111110000 this is -16
...111101111 this is -16-1
...111100000 this is -32, the result of bitAnd: on two complement
This test used to fail with n=31 39 47.... because of bug 6874"
self assert: ((2 to: 80) allSatisfy: [:n | ((2 raisedTo: n) negated bitAnd: (2 raisedTo: n) negated - 1) = (2 raisedTo: n + 1) negated]).! !