RegressionTests__IntegerTest.st
branchjv
changeset 1539 5206114410f7
parent 1500 d406a10b2965
parent 1529 32bd39e0e14e
child 1542 a2d6838ad6f6
--- a/RegressionTests__IntegerTest.st	Thu Sep 08 13:09:13 2016 +0100
+++ b/RegressionTests__IntegerTest.st	Wed Oct 26 22:44:11 2016 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "{ Package: 'stx:goodies/regression' }"
 
 "{ NameSpace: RegressionTests }"
@@ -12,6 +14,23 @@
 
 !IntegerTest methodsFor:'misc'!
 
+do:a plus:b
+    ^  a + b
+!
+
+doAdd2_a:arg
+    ^ arg + 2
+
+    "
+     (self new doAdd2_a:0) ==   2    
+     (self new doAdd2_a:1) ==   3    
+     (self new doAdd2_a:SmallInteger maxVal) = (SmallInteger maxVal + 2)  
+     (self new doAdd2_a:SmallInteger minVal) = (SmallInteger minVal + 2)
+    "
+
+    "Modified: / 9.6.1999 / 16:57:20 / cg"
+!
+
 doMul0_a:arg
     ^ arg * 0
 
@@ -3119,7 +3138,7 @@
 !
 
 testLargeMultiplication
-    |t1 t2|
+    |t1 t2 p|
 
     "multiplication"
     t1 := 100.
@@ -3142,12 +3161,12 @@
 
     t1 := 100000.
     SmallInteger maxBytes == 4 ifTrue:[
-	self assert:(t1 * t1 = 10000000000).
-	self assert:((t1 perform:'*' asSymbol with:t1) = 10000000000).
+        self assert:(t1 * t1 = 10000000000).
+        self assert:((t1 perform:'*' asSymbol with:t1) = 10000000000).
     ].
     SmallInteger maxBytes == 8 ifTrue:[
-	self assert:(t1 * t1 == 10000000000).
-	self assert:((t1 perform:'*' asSymbol with:t1) == 10000000000).
+        self assert:(t1 * t1 == 10000000000).
+        self assert:((t1 perform:'*' asSymbol with:t1) == 10000000000).
     ].
 
     self assert:((t1 * t1) printString = '10000000000').
@@ -3178,11 +3197,19 @@
 
     t1 := 100000000.                            "/ small
     t2 := 100000000000000000.                   "/ large on 32bit; small on 64bt machines
-    self assert:(t1 * t2) = 10000000000000000000000000.
-    self assert:(t2 * t1) = 10000000000000000000000000.
+    p := t1 * t2.
+    self assert:(p = 10000000000000000000000000).
+    p := t2 * t1.
+    self assert:(p = 10000000000000000000000000).
+    self assert:((t1 * t2) = 10000000000000000000000000).
+    self assert:((t2 * t1) = 10000000000000000000000000).
 
     t1 := 100000000.                            "/ small
     t2 := 1000000000000000000.                  "/ large on 32bit; small on 64bt machines
+    p := t1 * t2.
+    self assert:(p = 100000000000000000000000000).
+    p := t2 * t1.
+    self assert:(p = 100000000000000000000000000).
     self assert:(t1 * t2) = 100000000000000000000000000.
     self assert:(t2 * t1) = 100000000000000000000000000.
 
@@ -3293,49 +3320,49 @@
     self assert:((t1 * t1) printString = '489576143188809998144298426641311496989165214458056165805143410866108690058261346129614030084813851082564698610174813898740525406243367146120734370570458429364167811695064904353093506532695086211301649001517008746471464304183710723162864634442619484210170532881759249266026059786349673031239277666195699357198366128286910123306594912484590029738722281929300359929462301099981920256369394887701755497894820998573896950238852994224811101315810851671448056042419257789317787959570728520197146733902575090480065597582292177085754523686580725860228636039424698638422538988050350726807943014483010988455057592156160000').
 
     #(
-	16rFF
-	16rFFFF
-	16rFFFFFF
-	16rFFFFFFFF
-	16rFFFFFFFFFF
-	16rFFFFFFFFFFFF
-	16rFFFFFFFFFFFFFF
-	16rFFFFFFFFFFFFFFFF
-	16rFFFFFFFFFFFFFFFFFF
-	16rFFFFFFFFFFFFFFFFFFFF
-	16rFFFFFFFFFFFFFFFFFFFFFF
-	16rFFFFFFFFFFFFFFFFFFFFFFFF
+        16rFF
+        16rFFFF
+        16rFFFFFF
+        16rFFFFFFFF
+        16rFFFFFFFFFF
+        16rFFFFFFFFFFFF
+        16rFFFFFFFFFFFFFF
+        16rFFFFFFFFFFFFFFFF
+        16rFFFFFFFFFFFFFFFFFF
+        16rFFFFFFFFFFFFFFFFFFFF
+        16rFFFFFFFFFFFFFFFFFFFFFF
+        16rFFFFFFFFFFFFFFFFFFFFFFFF
     ) do:[:eachFactor1 |
-	#(
-	    16rFF
-	    16rFFFF
-	    16rFFFFFF
-	    16rFFFFFFFF
-	    16rFFFFFFFFFF
-	    16rFFFFFFFFFFFF
-	    16rFFFFFFFFFFFFFF
-	    16rFFFFFFFFFFFFFFFF
-	    16rFFFFFFFFFFFFFFFFFF
-	    16rFFFFFFFFFFFFFFFFFFFF
-	    16rFFFFFFFFFFFFFFFFFFFFFF
-	    16rFFFFFFFFFFFFFFFFFFFFFFFF
-	) do:[:eachFactor2 |
-	    |t3|
-
-	    t1 := (eachFactor1 * eachFactor2).
-	    t2 := (eachFactor2 * eachFactor1).
-
-	    self assert:(t1 = t2).
+        #(
+            16rFF
+            16rFFFF
+            16rFFFFFF
+            16rFFFFFFFF
+            16rFFFFFFFFFF
+            16rFFFFFFFFFFFF
+            16rFFFFFFFFFFFFFF
+            16rFFFFFFFFFFFFFFFF
+            16rFFFFFFFFFFFFFFFFFF
+            16rFFFFFFFFFFFFFFFFFFFF
+            16rFFFFFFFFFFFFFFFFFFFFFF
+            16rFFFFFFFFFFFFFFFFFFFFFFFF
+        ) do:[:eachFactor2 |
+            |t3|
+
+            t1 := (eachFactor1 * eachFactor2).
+            t2 := (eachFactor2 * eachFactor1).
+
+            self assert:(t1 = t2).
 t1 = 20203181441155852828228393631745 ifTrue:[
  eachFactor1 = 1099511627775 ifTrue:[
 self halt
 ]].
 
-	    self assert:(t1 / eachFactor1) = eachFactor2.
-	    self assert:(t1 / eachFactor2) = eachFactor1.
-	    t3 := (eachFactor1 asLargeInteger * eachFactor2 asLargeInteger).
-	    self assert: t1 = t3.
-	].
+            self assert:(t1 / eachFactor1) = eachFactor2.
+            self assert:(t1 / eachFactor2) = eachFactor1.
+            t3 := (eachFactor1 asLargeInteger * eachFactor2 asLargeInteger).
+            self assert: t1 = t3.
+        ].
     ].
 
     "
@@ -4048,6 +4075,54 @@
     self assert:((Integer readFrom:'4294967297') printString = '4294967297').
     self assert:((Integer readFrom:'4294967297') = 4294967297).
 
+    "/ max-smallint on 32bit machines
+    self assert:((Integer readFrom:'1073741823') printString = '1073741823').
+    self assert:((Integer readFrom:'1073741823') class == SmallInteger).
+
+    "/ min-smallint on 32bit machines
+    self assert:((Integer readFrom:'-1073741824') printString = '-1073741824').
+    self assert:((Integer readFrom:'-1073741824') class == SmallInteger).
+
+    "/ max-smallint on 64bit machines
+    self assert:((Integer readFrom:'4611686018427387903') printString = '4611686018427387903').
+    SmallInteger maxBytes == 8 ifTrue:[
+        self assert:((Integer readFrom:'4611686018427387903') class == SmallInteger)
+    ].
+    "/ max-smallint+1 on 64bit machines
+    self assert:((Integer readFrom:'4611686018427387900') printString = '4611686018427387900').
+    SmallInteger maxBytes == 8 ifTrue:[
+        |t t3 t3b t4a t4b t4c|
+
+        self assert:((Integer readFrom:'4611686018427387900') class == SmallInteger).
+        t := (Integer readFrom:'4611686018427387900').
+        t3 := t + 3.
+        self assert:(t3 class == SmallInteger).
+        self assert:(t3 printString = '4611686018427387903').
+        t3b := (self do:t plus:3).
+        self assert:(t3b class == SmallInteger).
+        self assert:(t3b printString = '4611686018427387903').
+        t4a := t3 + 1.
+        self assert:(t4a class == LargeInteger).
+        self assert:(t4a printString = '4611686018427387904').
+        t4b := t + 4.
+        self assert:(t4b class == LargeInteger).
+        self assert:(t4b printString = '4611686018427387904').
+        t4c := (self do:t plus:4).
+        self assert:(t4c class == LargeInteger).
+        self assert:(t4c printString = '4611686018427387904').
+    ].
+    self assert:((Integer readFrom:'4611686018427387904') printString = '4611686018427387904').
+    self assert:((Integer readFrom:'4611686018427387904') class == LargeInteger).
+
+    "/ min-smallint on 64bit machines
+    self assert:((Integer readFrom:'-4611686018427387904') printString = '-4611686018427387904').
+    SmallInteger maxBytes == 8 ifTrue:[
+        self assert:((Integer readFrom:'-4611686018427387904') class == SmallInteger)
+    ].
+    self assert:((Integer readFrom:'-4611686018427387904') printString = '-4611686018427387904').
+    SmallInteger maxBytes == 8 ifTrue:[
+        self assert:((Integer readFrom:'-4611686018427387904') class == SmallInteger)
+    ].
     self assert:((Integer readFrom:'FFFFFFFF' radix:16) hexPrintString = 'FFFFFFFF').
     self assert:((Integer readFrom:'FFFFFFFFF' radix:16) hexPrintString = 'FFFFFFFFF').
     self assert:((Integer readFrom:'FFFFFFFFFF' radix:16) hexPrintString = 'FFFFFFFFFF').
@@ -4055,6 +4130,24 @@
     self assert:((Integer readFrom:'FFFFFFFFFFFF' radix:16) hexPrintString = 'FFFFFFFFFFFF').
     self assert:((Integer readFrom:'FFFFFFFFFFFFF' radix:16) hexPrintString = 'FFFFFFFFFFFFF').
     self assert:((Integer readFrom:'FFFFFFFFFFFFFF' radix:16) hexPrintString = 'FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'FFFFFFFFFFFFFFF' radix:16) hexPrintString = 'FFFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'3FFFFFFFFFFFFFF' radix:16) hexPrintString = '3FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'7FFFFFFFFFFFFFF' radix:16) hexPrintString = '7FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'400000000000000' radix:16) hexPrintString = '400000000000000').
+    self assert:((Integer readFrom:'800000000000000' radix:16) hexPrintString = '800000000000000').
+
+    self assert:((Integer readFrom:'16r-FFFFFFFF') printString = '-4294967295').
+    self assert:((Integer readFrom:'-16rFFFFFFFFF') hexPrintString = '-FFFFFFFFF').
+    self assert:((Integer readFrom:'-16rFFFFFFFFFF') hexPrintString = '-FFFFFFFFFF').
+    self assert:((Integer readFrom:'-16rFFFFFFFFFFF') hexPrintString = '-FFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16rFFFFFFFFFFFF') hexPrintString = '-FFFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16rFFFFFFFFFFFFF') hexPrintString = '-FFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16rFFFFFFFFFFFFFF') hexPrintString = '-FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16rFFFFFFFFFFFFFFF') hexPrintString = '-FFFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16r3FFFFFFFFFFFFFF') hexPrintString = '-3FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16r7FFFFFFFFFFFFFF') hexPrintString = '-7FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'-16r400000000000000') hexPrintString = '-400000000000000').
+    self assert:((Integer readFrom:'-16r800000000000000') hexPrintString = '-800000000000000').
 
     self assert:((Integer readFrom:'10000000' radix:16) hexPrintString = '10000000').
     self assert:((Integer readFrom:'100000000' radix:16) hexPrintString = '100000000').
@@ -4064,6 +4157,12 @@
     self assert:((Integer readFrom:'1000000000000' radix:16) hexPrintString = '1000000000000').
     self assert:((Integer readFrom:'10000000000000' radix:16) hexPrintString = '10000000000000').
 
+    self assert:((Integer readFrom:'1152921504606846975' radix:10) hexPrintString = 'FFFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'288230376151711743' radix:10) hexPrintString = '3FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'576460752303423487' radix:10) hexPrintString = '7FFFFFFFFFFFFFF').
+    self assert:((Integer readFrom:'288230376151711744' radix:10) hexPrintString = '400000000000000').
+    self assert:((Integer readFrom:'576460752303423488' radix:10) hexPrintString = '800000000000000').
+
     t := Integer readFrom:'FFFFFFFF' radix:16.
     self assert:((t + 1) hexPrintString = '100000000').
     t := Integer readFrom:'FFFFFFFFF' radix:16.
@@ -4589,6 +4688,74 @@
 
     "Modified: / 9.6.1999 / 17:47:56 / cg"
     "Created: / 9.6.1999 / 17:49:45 / cg"
+!
+
+testSmallIntegerNegation
+    "tests boundary conditions at small/large phase change."
+
+    |n1 n2 n3|
+
+    n1 := 16r3fffffff.
+    n2 := n1 negated.
+    n3 := n2 negated.
+
+    self assert:(n1 class == SmallInteger).
+    self assert:(n2 class == SmallInteger).
+    self assert:(n3 == n1).
+
+    self assert:(n1 = 16r3FFFFFFF).
+    self assert:(n2 = -16r3FFFFFFF).
+    self assert:(n1 == 16r3FFFFFFF).
+    self assert:(n2 == -16r3FFFFFFF).
+    self assert:(n1 printString = '1073741823').
+    self assert:(n2 printString = '-1073741823').
+
+    "/ ---------------------
+
+    n1 := 16rffffffff.
+    n2 := n1 negated.
+    n3 := n2 negated.
+
+    self assert:(n1 class == SmallInteger).
+    self assert:(n2 class == SmallInteger).
+    self assert:(n3 == n1).
+
+    self assert:(n1 = 16rfFFFFFFF).
+    self assert:(n2 = -16rfFFFFFFF).
+    self assert:(n1 == 16rfFFFFFFF).
+    self assert:(n2 == -16rfFFFFFFF).
+    self assert:(n1 printString = '4294967295').
+    self assert:(n2 printString = '-4294967295').
+
+    "/ ---------------------
+
+    n1 := SmallInteger maxVal.
+    n2 := n1 negated.
+    n3 := n2 negated.
+
+    self assert:(n1 class == SmallInteger).
+    self assert:(n2 class == SmallInteger).
+    self assert:(n3 == n1).
+
+    SmallInteger maxBytes == 4 ifTrue:[
+        self assert:(n1 = 16r3FFFFFFF).
+        self assert:(n2 = -16r3FFFFFFF).
+        self assert:(n1 == 16r3FFFFFFF).
+        self assert:(n2 == -16r3FFFFFFF).
+        self assert:(n1 printString = '1073741823').
+        self assert:(n2 printString = '-1073741823').
+    ] ifFalse:[
+        self assert:(n1 printString = '4611686018427387903').
+        self assert:(n2 printString = '-4611686018427387903').
+        self assert:(n1 =   16r3FFFFFFFFFFFFFFF).
+        self assert:(n2 =  -16r3FFFFFFFFFFFFFFF).
+        self assert:(n1 ==  16r3FFFFFFFFFFFFFFF).
+        self assert:(n2 == -16r3FFFFFFFFFFFFFFF).
+    ].
+
+    "
+     self basicNew testSmallIntegerNegation
+    "
 ! !
 
 !IntegerTest methodsFor:'tests-class protocol'!