RegressionTests__ScaledDecimalTest.st
changeset 312 d39568c0560e
child 313 347d94236f10
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RegressionTests__ScaledDecimalTest.st	Fri Feb 17 15:47:31 2006 +0100
@@ -0,0 +1,228 @@
+"{ Package: 'exept:regression' }"
+
+"{ NameSpace: RegressionTests }"
+
+TestCase subclass:#ScaledDecimalTest
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'tests-Regression'
+!
+
+
+!ScaledDecimalTest methodsFor:'tests'!
+
+testAsNumber
+        "Ensure no loss of precision"
+
+        | sd |
+        sd := '1.40s2' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 2.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '1.40' = sd printString.
+            self assert: '1.40s2' = sd storeString.
+        ] ifFalse:[
+            self assert: '1.40s2' = sd printString.
+        ]
+!
+
+testAsNumberNegatedWithoutDecimalPoint
+
+        | sd |
+        sd := '-123s0' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 0.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '-123' = sd printString.
+            self assert: '-123s0' = sd storeString.
+        ] ifFalse:[
+            self assert: '-123s0' = sd printString.
+        ]
+!
+
+testAsNumberNegatedWithoutDecimalPoint2
+
+        | sd |
+        sd := '-123s2' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 2.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '-123.00' = sd printString.
+            self assert: '-123.00s2' = sd storeString.
+        ] ifFalse:[
+            self assert: '-123.00s2' = sd printString.
+        ]
+!
+
+testAsNumberWithExtendedScale
+
+        | sd |
+        sd := '123s2' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 2.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '123.00' = sd printString.
+            self assert: '123.00s2' = sd storeString.
+        ] ifFalse:[
+            self assert: '123.00s2' = sd printString.
+        ]
+!
+
+testAsNumberWithRadix
+
+        | sd |
+        sd := '10r-22.2s5' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 5.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '-22.20000' = sd printString.
+            self assert: '-22.20000s5' = sd storeString.
+        ] ifFalse:[
+            self assert: '-22.20000s5' = sd printString.
+        ]
+!
+
+testAsNumberWithSuperfluousDecimalPoint
+
+        | sd |
+        sd := '123.s2' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 2.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '123.00' = sd printString.
+            self assert: '123.00s2' = sd storeString.
+        ] ifFalse:[
+            self assert: '123.00s2' = sd printString.
+        ]
+!
+
+testAsNumberWithoutDecimalPoint
+
+        | sd |
+        sd := '123s0' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 0.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '123' = sd printString.
+            self assert: '123s0' = sd storeString.
+        ] ifFalse:[
+            self assert: '123s0' = sd printString.
+        ]
+!
+
+testAsNumberWithoutDecimalPoint2
+
+        | sd |
+        sd := '123s2' asNumber.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 2.
+
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '123.00' = sd printString.
+            self assert: '123.00s2' = sd storeString.
+        ] ifFalse:[
+            self assert: '123.00s2' = sd printString.
+        ]
+!
+
+testConvertFromFloat
+
+        | aFloat sd f2 diff |
+        aFloat := 11/13 asFloat.
+        sd := aFloat asScaledDecimal: 2.
+        self assert: 2 == sd scale.
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '0.85' = sd printString.
+        ] ifFalse:[
+            self assert: '0.84s2' = sd printString.
+        ].
+        f2 := sd asFloat.
+        diff := f2 - aFloat.
+        self assert: diff < 1.0e-9. "actually, f = f2, but this is not a requirement"
+!
+
+testConvertFromFraction
+
+        | sd |
+        sd := (13 / 11) asScaledDecimal: 6.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: ('1.181818' = sd printString).
+        ] ifFalse:[
+            self assert: ('1.181818s6' = sd printString).
+        ].
+        self assert: 6 == sd scale
+!
+
+testConvertFromInteger
+        "Converting an Integer to a ScaledDecimal yields a ScaledDecimal with
+        scale 0, regardless of the scale specified in the #asScaledDecimal: message."
+
+        | sd |
+
+        Smalltalk isSmalltalkX ifTrue:[
+            "/ I think this behavior is wrong.
+            ^ self.
+        ].
+
+        sd := 13 asScaledDecimal: 6.
+        self assert: 0 = sd scale.
+        self assert: ('13s0' = sd printString).
+        sd := -13 asScaledDecimal: 6.
+        self assert: 0 = sd scale.
+        self assert: ('-13s0' = sd printString).
+        sd := 130000000013 asScaledDecimal: 6.
+        self assert: 0 = sd scale.
+        self assert: ('130000000013s0' = sd printString).
+        sd := -130000000013 asScaledDecimal: 6.
+        self assert: 0 = sd scale.
+        self assert: ('-130000000013s0' = sd printString)
+!
+
+testLiteral
+        | sd |
+
+        sd := 1.40s2.
+        self assert: (ScaledDecimal ? FixedPoint) == sd class.
+        self assert: sd scale == 2.
+        Smalltalk isSmalltalkX ifTrue:[
+            self assert: '1.40' = sd printString.
+            self assert: '1.40s2' = sd storeString.
+        ] ifFalse:[
+            self assert: '1.40s2' = sd printString.
+        ].
+!
+
+testStoreAndRead
+    |check|
+
+    check := [:originalNum |
+        |s readNum|
+
+        s := originalNum storeString.
+        readNum := Number readFrom:s.
+        self assert:readNum = originalNum.
+    ].
+
+    check value:((FixedPoint fromString:'0.66666666')                   ).
+    check value:((FixedPoint fromString:'0.66666666') withScale:2       ).
+
+    check value:((FixedPoint fromString:'1.5')                          ).
+    check value:((FixedPoint fromString:'1.5') withScale:2              ).
+    check value:((FixedPoint fromString:'1.5') withScale:1              ).
+    check value:((FixedPoint fromString:'1.5') withScale:0              ).
+! !
+
+!ScaledDecimalTest class methodsFor:'documentation'!
+
+version
+    ^ '$Header$'
+! !