RegressionTests__ScaledDecimalTest.st
changeset 1447 2351db93aa5b
parent 1073 9b0ba74b1f48
child 1500 d406a10b2965
child 1711 d564663fca34
equal deleted inserted replaced
1446:c3b09cef9f87 1447:2351db93aa5b
     1 "{ Package: 'exept:regression' }"
     1 "{ Package: 'stx:goodies/regression' }"
     2 
     2 
     3 "{ NameSpace: RegressionTests }"
     3 "{ NameSpace: RegressionTests }"
     4 
     4 
     5 TestCase subclass:#ScaledDecimalTest
     5 TestCase subclass:#ScaledDecimalTest
     6 	instanceVariableNames:''
     6 	instanceVariableNames:''
    34 
    34 
    35     "Created: / 09-08-2011 / 21:23:56 / cg"
    35     "Created: / 09-08-2011 / 21:23:56 / cg"
    36 !
    36 !
    37 
    37 
    38 test02_AsNumber
    38 test02_AsNumber
    39         "Ensure no loss of precision"
    39 	"Ensure no loss of precision"
    40 
    40 
    41         | sd |
    41 	| sd |
    42         sd := '1.40s2' asNumber.
    42 	sd := '1.40s2' asNumber.
    43         self assert: (ScaledDecimal ? FixedPoint) == sd class.
    43 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
    44         self assert: sd scale == 2.
    44 	self assert: sd scale == 2.
    45 
    45 
    46         Smalltalk isSmalltalkX ifTrue:[
    46 	Smalltalk isSmalltalkX ifTrue:[
    47             self assert: '1.40' = sd printString.
    47 	    self assert: '1.40' = sd printString.
    48             self assert: '1.40s2' = sd storeString.
    48 	    self assert: '1.40s2' = sd storeString.
    49         ] ifFalse:[
    49 	] ifFalse:[
    50             self assert: '1.40s2' = sd printString.
    50 	    self assert: '1.40s2' = sd printString.
    51         ]
    51 	]
    52 
    52 
    53     "Created: / 09-08-2011 / 21:25:31 / cg"
    53     "Created: / 09-08-2011 / 21:25:31 / cg"
    54 !
    54 !
    55 
    55 
    56 test03_AsNumberNegatedWithoutDecimalPoint
    56 test03_AsNumberNegatedWithoutDecimalPoint
    57 
    57 
    58         | sd |
    58 	| sd |
    59         sd := '-123s0' asNumber.
    59 	sd := '-123s0' asNumber.
    60         self assert: (ScaledDecimal ? FixedPoint) == sd class.
    60 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
    61         self assert: sd scale == 0.
    61 	self assert: sd scale == 0.
    62 
    62 
    63         Smalltalk isSmalltalkX ifTrue:[
    63 	Smalltalk isSmalltalkX ifTrue:[
    64             self assert: '-123' = sd printString.
    64 	    self assert: '-123' = sd printString.
    65             self assert: '-123s0' = sd storeString.
    65 	    self assert: '-123s0' = sd storeString.
    66         ] ifFalse:[
    66 	] ifFalse:[
    67             self assert: '-123s0' = sd printString.
    67 	    self assert: '-123s0' = sd printString.
    68         ]
    68 	]
    69 
    69 
    70     "Created: / 09-08-2011 / 21:25:43 / cg"
    70     "Created: / 09-08-2011 / 21:25:43 / cg"
    71 !
    71 !
    72 
    72 
    73 test04_AsNumberNegatedWithoutDecimalPoint2
    73 test04_AsNumberNegatedWithoutDecimalPoint2
    74 
    74 
    75         | sd |
    75 	| sd |
    76         sd := '-123s2' asNumber.
    76 	sd := '-123s2' asNumber.
    77         self assert: (ScaledDecimal ? FixedPoint) == sd class.
    77 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
    78         self assert: sd scale == 2.
    78 	self assert: sd scale == 2.
    79 
    79 
    80         Smalltalk isSmalltalkX ifTrue:[
    80 	Smalltalk isSmalltalkX ifTrue:[
    81             self assert: '-123.00' = sd printString.
    81 	    self assert: '-123.00' = sd printString.
    82             self assert: '-123.00s2' = sd storeString.
    82 	    self assert: '-123.00s2' = sd storeString.
    83         ] ifFalse:[
    83 	] ifFalse:[
    84             self assert: '-123.00s2' = sd printString.
    84 	    self assert: '-123.00s2' = sd printString.
    85         ]
    85 	]
    86 
    86 
    87     "Created: / 09-08-2011 / 21:25:54 / cg"
    87     "Created: / 09-08-2011 / 21:25:54 / cg"
    88 !
    88 !
    89 
    89 
    90 test05_AsNumberWithExtendedScale
    90 test05_AsNumberWithExtendedScale
    91 
    91 
    92         | sd |
    92 	| sd |
    93         sd := '123s2' asNumber.
    93 	sd := '123s2' asNumber.
    94         self assert: (ScaledDecimal ? FixedPoint) == sd class.
    94 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
    95         self assert: sd scale == 2.
    95 	self assert: sd scale == 2.
    96 
    96 
    97         Smalltalk isSmalltalkX ifTrue:[
    97 	Smalltalk isSmalltalkX ifTrue:[
    98             self assert: '123.00' = sd printString.
    98 	    self assert: '123.00' = sd printString.
    99             self assert: '123.00s2' = sd storeString.
    99 	    self assert: '123.00s2' = sd storeString.
   100         ] ifFalse:[
   100 	] ifFalse:[
   101             self assert: '123.00s2' = sd printString.
   101 	    self assert: '123.00s2' = sd printString.
   102         ]
   102 	]
   103 
   103 
   104     "Created: / 09-08-2011 / 21:25:59 / cg"
   104     "Created: / 09-08-2011 / 21:25:59 / cg"
   105 !
   105 !
   106 
   106 
   107 test06_AsNumberWithRadix
   107 test06_AsNumberWithRadix
   108         | sd oldSetting |
   108 	| sd oldSetting |
   109 
   109 
   110         Smalltalk isSmalltalkX ifTrue:[
   110 	Smalltalk isSmalltalkX ifTrue:[
   111             oldSetting := ParserFlags allowFixedPointLiterals.
   111 	    oldSetting := ParserFlags allowFixedPointLiterals.
   112             ParserFlags allowFixedPointLiterals:true.
   112 	    ParserFlags allowFixedPointLiterals:true.
   113             [
   113 	    [
   114                 sd := Number readSmalltalkSyntaxFrom:'10r-22.2s5'.
   114 		sd := Number readSmalltalkSyntaxFrom:'10r-22.2s5'.
   115             ] ensure:[
   115 	    ] ensure:[
   116                 ParserFlags allowFixedPointLiterals:oldSetting.
   116 		ParserFlags allowFixedPointLiterals:oldSetting.
   117             ]
   117 	    ]
   118         ] ifFalse:[
   118 	] ifFalse:[
   119             sd := '10r-22.2s5' asNumber.
   119 	    sd := '10r-22.2s5' asNumber.
   120         ].
   120 	].
   121 
   121 
   122         self assert: (ScaledDecimal ? FixedPoint) == sd class.
   122 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
   123         self assert: sd scale == 5.
   123 	self assert: sd scale == 5.
   124 
   124 
   125         Smalltalk isSmalltalkX ifTrue:[
   125 	Smalltalk isSmalltalkX ifTrue:[
   126             self assert: '-22.20000' = sd printString.
   126 	    self assert: '-22.20000' = sd printString.
   127             self assert: '-22.20000s5' = sd storeString.
   127 	    self assert: '-22.20000s5' = sd storeString.
   128         ] ifFalse:[
   128 	] ifFalse:[
   129             self assert: '-22.20000s5' = sd printString.
   129 	    self assert: '-22.20000s5' = sd printString.
   130         ]
   130 	]
   131 
   131 
   132     "Created: / 09-08-2011 / 21:26:05 / cg"
   132     "Created: / 09-08-2011 / 21:26:05 / cg"
   133 !
   133 !
   134 
   134 
   135 test07_AsNumberWithSuperfluousDecimalPoint
   135 test07_AsNumberWithSuperfluousDecimalPoint
   136 
   136 
   137         | sd |
   137 	| sd |
   138         sd := '123.s2' asNumber.
   138 	sd := '123.s2' asNumber.
   139         self assert: (ScaledDecimal ? FixedPoint) == sd class.
   139 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
   140         self assert: sd scale == 2.
   140 	self assert: sd scale == 2.
   141 
   141 
   142         Smalltalk isSmalltalkX ifTrue:[
   142 	Smalltalk isSmalltalkX ifTrue:[
   143             self assert: '123.00' = sd printString.
   143 	    self assert: '123.00' = sd printString.
   144             self assert: '123.00s2' = sd storeString.
   144 	    self assert: '123.00s2' = sd storeString.
   145         ] ifFalse:[
   145 	] ifFalse:[
   146             self assert: '123.00s2' = sd printString.
   146 	    self assert: '123.00s2' = sd printString.
   147         ]
   147 	]
   148 
   148 
   149     "Created: / 09-08-2011 / 21:26:20 / cg"
   149     "Created: / 09-08-2011 / 21:26:20 / cg"
   150 !
   150 !
   151 
   151 
   152 test08_AsNumberWithoutDecimalPoint
   152 test08_AsNumberWithoutDecimalPoint
   153 
   153 
   154         | sd |
   154 	| sd |
   155         sd := '123s0' asNumber.
   155 	sd := '123s0' asNumber.
   156         self assert: (ScaledDecimal ? FixedPoint) == sd class.
   156 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
   157         self assert: sd scale == 0.
   157 	self assert: sd scale == 0.
   158 
   158 
   159         Smalltalk isSmalltalkX ifTrue:[
   159 	Smalltalk isSmalltalkX ifTrue:[
   160             self assert: '123' = sd printString.
   160 	    self assert: '123' = sd printString.
   161             self assert: '123s0' = sd storeString.
   161 	    self assert: '123s0' = sd storeString.
   162         ] ifFalse:[
   162 	] ifFalse:[
   163             self assert: '123s0' = sd printString.
   163 	    self assert: '123s0' = sd printString.
   164         ]
   164 	]
   165 
   165 
   166     "Created: / 09-08-2011 / 21:26:25 / cg"
   166     "Created: / 09-08-2011 / 21:26:25 / cg"
   167 !
   167 !
   168 
   168 
   169 test09_AsNumberWithoutDecimalPoint2
   169 test09_AsNumberWithoutDecimalPoint2
   170 
   170 
   171         | sd |
   171 	| sd |
   172         sd := '123s2' asNumber.
   172 	sd := '123s2' asNumber.
   173         self assert: (ScaledDecimal ? FixedPoint) == sd class.
   173 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
   174         self assert: sd scale == 2.
   174 	self assert: sd scale == 2.
   175 
   175 
   176         Smalltalk isSmalltalkX ifTrue:[
   176 	Smalltalk isSmalltalkX ifTrue:[
   177             self assert: '123.00' = sd printString.
   177 	    self assert: '123.00' = sd printString.
   178             self assert: '123.00s2' = sd storeString.
   178 	    self assert: '123.00s2' = sd storeString.
   179         ] ifFalse:[
   179 	] ifFalse:[
   180             self assert: '123.00s2' = sd printString.
   180 	    self assert: '123.00s2' = sd printString.
   181         ]
   181 	]
   182 
   182 
   183     "Created: / 09-08-2011 / 21:26:30 / cg"
   183     "Created: / 09-08-2011 / 21:26:30 / cg"
   184 !
   184 !
   185 
   185 
   186 test10_ConvertFromFloat
   186 test10_ConvertFromFloat
   187 
   187 
   188         | aFloat sd f2 diff |
   188 	| aFloat sd f2 diff |
   189         aFloat := 11/13 asFloat.
   189 	aFloat := 11/13 asFloat.
   190         sd := aFloat asScaledDecimal: 2.
   190 	sd := aFloat asScaledDecimal: 2.
   191         self assert: 2 == sd scale.
   191 	self assert: 2 == sd scale.
   192         Smalltalk isSmalltalkX ifTrue:[
   192 	Smalltalk isSmalltalkX ifTrue:[
   193             self assert: '0.85' = sd printString.
   193 	    self assert: '0.85' = sd printString.
   194         ] ifFalse:[
   194 	] ifFalse:[
   195             self assert: '0.84s2' = sd printString.
   195 	    self assert: '0.84s2' = sd printString.
   196         ].
   196 	].
   197         f2 := sd asFloat.
   197 	f2 := sd asFloat.
   198         diff := f2 - aFloat.
   198 	diff := f2 - aFloat.
   199         self assert: diff < 1.0e-9. "actually, f = f2, but this is not a requirement"
   199 	self assert: diff < 1.0e-9. "actually, f = f2, but this is not a requirement"
   200 
   200 
   201     "Created: / 09-08-2011 / 21:26:38 / cg"
   201     "Created: / 09-08-2011 / 21:26:38 / cg"
   202 !
   202 !
   203 
   203 
   204 test11_ConvertFromFraction
   204 test11_ConvertFromFraction
   205 
   205 
   206         | sd |
   206 	| sd |
   207         sd := (13 / 11) asScaledDecimal: 6.
   207 	sd := (13 / 11) asScaledDecimal: 6.
   208         self assert: (ScaledDecimal ? FixedPoint) == sd class.
   208 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
   209         Smalltalk isSmalltalkX ifTrue:[
   209 	Smalltalk isSmalltalkX ifTrue:[
   210             self assert: ('1.181818' = sd printString).
   210 	    self assert: ('1.181818' = sd printString).
   211         ] ifFalse:[
   211 	] ifFalse:[
   212             self assert: ('1.181818s6' = sd printString).
   212 	    self assert: ('1.181818s6' = sd printString).
   213         ].
   213 	].
   214         self assert: 6 == sd scale
   214 	self assert: 6 == sd scale
   215 
   215 
   216     "Created: / 09-08-2011 / 21:26:43 / cg"
   216     "Created: / 09-08-2011 / 21:26:43 / cg"
   217 !
   217 !
   218 
   218 
   219 test12_ConvertFromInteger
   219 test12_ConvertFromInteger
   220         "Converting an Integer to a ScaledDecimal yields a ScaledDecimal with
   220 	"Converting an Integer to a ScaledDecimal yields a ScaledDecimal with
   221         scale 0, regardless of the scale specified in the #asScaledDecimal: message."
   221 	scale 0, regardless of the scale specified in the #asScaledDecimal: message."
   222 
   222 
   223         | sd |
   223 	| sd |
   224 
   224 
   225         Smalltalk isSmalltalkX ifTrue:[
   225 	Smalltalk isSmalltalkX ifTrue:[
   226             "/ I think this behavior is wrong.
   226 	    "/ I think this behavior is wrong.
   227             ^ self.
   227 	    ^ self.
   228         ].
   228 	].
   229 
   229 
   230         sd := 13 asScaledDecimal: 6.
   230 	sd := 13 asScaledDecimal: 6.
   231         self assert: 0 = sd scale.
   231 	self assert: 0 = sd scale.
   232         self assert: ('13s0' = sd printString).
   232 	self assert: ('13s0' = sd printString).
   233         sd := -13 asScaledDecimal: 6.
   233 	sd := -13 asScaledDecimal: 6.
   234         self assert: 0 = sd scale.
   234 	self assert: 0 = sd scale.
   235         self assert: ('-13s0' = sd printString).
   235 	self assert: ('-13s0' = sd printString).
   236         sd := 130000000013 asScaledDecimal: 6.
   236 	sd := 130000000013 asScaledDecimal: 6.
   237         self assert: 0 = sd scale.
   237 	self assert: 0 = sd scale.
   238         self assert: ('130000000013s0' = sd printString).
   238 	self assert: ('130000000013s0' = sd printString).
   239         sd := -130000000013 asScaledDecimal: 6.
   239 	sd := -130000000013 asScaledDecimal: 6.
   240         self assert: 0 = sd scale.
   240 	self assert: 0 = sd scale.
   241         self assert: ('-130000000013s0' = sd printString)
   241 	self assert: ('-130000000013s0' = sd printString)
   242 
   242 
   243     "Created: / 09-08-2011 / 21:26:46 / cg"
   243     "Created: / 09-08-2011 / 21:26:46 / cg"
   244 !
   244 !
   245 
   245 
   246 test13_Literal1
   246 test13_Literal1
   247     "s2 is not a message to be sent to a float"
   247     "s2 is not a message to be sent to a float"
   248 
   248 
   249     self 
   249     self
   250         shouldnt:[ self literal_helper1 ] 
   250 	shouldnt:[ self literal_helper1 ]
   251         raise:MessageNotUnderstood
   251 	raise:MessageNotUnderstood
   252 
   252 
   253     "Created: / 09-08-2011 / 21:26:55 / cg"
   253     "Created: / 09-08-2011 / 21:26:55 / cg"
   254 !
   254 !
   255 
   255 
   256 test14_Literal2
   256 test14_Literal2
   257         | sd |
   257 	| sd |
   258 
   258 
   259         sd := 1.40s2.
   259 	sd := 1.40s2.
   260         self assert: (ScaledDecimal ? FixedPoint) == sd class.
   260 	self assert: (ScaledDecimal ? FixedPoint) == sd class.
   261         self assert: sd scale == 2.
   261 	self assert: sd scale == 2.
   262         Smalltalk isSmalltalkX ifTrue:[
   262 	Smalltalk isSmalltalkX ifTrue:[
   263             self assert: '1.40' = sd printString.
   263 	    self assert: '1.40' = sd printString.
   264             self assert: '1.40s2' = sd storeString.
   264 	    self assert: '1.40s2' = sd storeString.
   265         ] ifFalse:[
   265 	] ifFalse:[
   266             self assert: '1.40s2' = sd printString.
   266 	    self assert: '1.40s2' = sd printString.
   267         ].
   267 	].
   268 
   268 
   269     "Created: / 09-08-2011 / 21:27:01 / cg"
   269     "Created: / 09-08-2011 / 21:27:01 / cg"
   270 !
   270 !
   271 
   271 
   272 test15_LiteralInSTC
   272 test15_LiteralInSTC
   273     |value|
   273     |value|
   274 
   274 
   275     Class withoutUpdatingChangesDo:[
   275     Class withoutUpdatingChangesDo:[
   276         self class
   276 	self class
   277             compile:
   277 	    compile:
   278 'literal_helper2
   278 'literal_helper2
   279     ^ 1.40s2.
   279     ^ 1.40s2.
   280 '
   280 '
   281             classified:'temporary'.
   281 	    classified:'temporary'.
   282 
   282 
   283         Compiler stcCompileMethod:(self class compiledMethodAt:#literal_helper2).
   283 	Compiler stcCompileMethod:(self class compiledMethodAt:#literal_helper2).
   284     ].
   284     ].
   285 
   285 
   286     self 
   286     self
   287         shouldnt:[ self literal_helper2 ] 
   287 	shouldnt:[ self literal_helper2 ]
   288         raise:MessageNotUnderstood.
   288 	raise:MessageNotUnderstood.
   289 
   289 
   290     value := self literal_helper2.
   290     value := self literal_helper2.
   291     self assert:( value isFixedPoint ).
   291     self assert:( value isFixedPoint ).
   292     self assert:( (value * 10) = 14 ).
   292     self assert:( (value * 10) = 14 ).
   293     self assert:( value asFloat = 1.4 ).
   293     self assert:( value asFloat = 1.4 ).
   294 
   294 
   295     "Created: / 09-08-2011 / 21:27:07 / cg"
   295     "Created: / 09-08-2011 / 21:27:07 / cg"
   296 !
   296 !
   297 
   297 
   298 test16_PrintString
   298 test16_PrintString
   299         "The printed representation of a ScaledDecimal is truncated, not rounded.
   299 	"The printed representation of a ScaledDecimal is truncated, not rounded.
   300         Not sure if this is right, so this test describes the current Squeak implementation.
   300 	Not sure if this is right, so this test describes the current Squeak implementation.
   301         If someone knows a reason that rounding would be preferable, then update
   301 	If someone knows a reason that rounding would be preferable, then update
   302         this test."
   302 	this test."
   303 
   303 
   304         | sd |
   304 	| sd |
   305 
   305 
   306         sd := (13 / 11) asScaledDecimal: 6.
   306 	sd := (13 / 11) asScaledDecimal: 6.
   307         Smalltalk isSmalltalkX ifTrue:[
   307 	Smalltalk isSmalltalkX ifTrue:[
   308             self assert: ('1.181818' = sd printString).
   308 	    self assert: ('1.181818' = sd printString).
   309         ] ifFalse:[
   309 	] ifFalse:[
   310             self assert: ('1.181818s6' = sd printString).
   310 	    self assert: ('1.181818s6' = sd printString).
   311         ].
   311 	].
   312 
   312 
   313         sd := (13 / 11) asScaledDecimal: 5.
   313 	sd := (13 / 11) asScaledDecimal: 5.
   314         Smalltalk isSmalltalkX ifTrue:[
   314 	Smalltalk isSmalltalkX ifTrue:[
   315             self assert: ('1.18182' = sd printString).
   315 	    self assert: ('1.18182' = sd printString).
   316         ] ifFalse:[
   316 	] ifFalse:[
   317             self deny: ('1.18182s5' = sd printString).
   317 	    self deny: ('1.18182s5' = sd printString).
   318         ].
   318 	].
   319 
   319 
   320         sd := (13 / 11) asScaledDecimal: 5.
   320 	sd := (13 / 11) asScaledDecimal: 5.
   321         Smalltalk isSmalltalkX ifTrue:[
   321 	Smalltalk isSmalltalkX ifTrue:[
   322             self deny: ('1.18181' = sd printString).
   322 	    self deny: ('1.18181' = sd printString).
   323         ] ifFalse:[
   323 	] ifFalse:[
   324             self assert: ('1.18181s5' = sd printString).
   324 	    self assert: ('1.18181s5' = sd printString).
   325         ].
   325 	].
   326 
   326 
   327     "Created: / 09-08-2011 / 21:27:49 / cg"
   327     "Created: / 09-08-2011 / 21:27:49 / cg"
   328 !
   328 !
   329 
   329 
   330 test17_StoreAndRead
   330 test17_StoreAndRead
   331     |check|
   331     |check|
   332 
   332 
   333     check := [:originalNum |
   333     check := [:originalNum |
   334         |s readNum|
   334 	|s readNum|
   335 
   335 
   336         s := originalNum storeString.
   336 	s := originalNum storeString.
   337         readNum := Number readFrom:s.
   337 	readNum := Number readFrom:s.
   338         self assert:readNum = originalNum.
   338 	self assert:readNum = originalNum.
   339     ].
   339     ].
   340 
   340 
   341     check value:((FixedPoint fromString:'0.66666666')                   ).
   341     check value:((FixedPoint fromString:'0.66666666')                   ).
   342     check value:((FixedPoint fromString:'0.66666666') withScale:2       ).
   342     check value:((FixedPoint fromString:'0.66666666') withScale:2       ).
   343 
   343