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