312
|
1 |
"{ Package: 'exept:regression' }"
|
|
2 |
|
|
3 |
"{ NameSpace: RegressionTests }"
|
|
4 |
|
|
5 |
TestCase subclass:#ScaledDecimalTest
|
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'tests-Regression'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
620
|
13 |
!ScaledDecimalTest class methodsFor:'others'!
|
|
14 |
|
|
15 |
version_CVS
|
|
16 |
^ '$Header$'
|
|
17 |
! !
|
|
18 |
|
312
|
19 |
!ScaledDecimalTest methodsFor:'tests'!
|
|
20 |
|
|
21 |
testAsNumber
|
|
22 |
"Ensure no loss of precision"
|
|
23 |
|
|
24 |
| sd |
|
|
25 |
sd := '1.40s2' asNumber.
|
|
26 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
27 |
self assert: sd scale == 2.
|
|
28 |
|
|
29 |
Smalltalk isSmalltalkX ifTrue:[
|
|
30 |
self assert: '1.40' = sd printString.
|
|
31 |
self assert: '1.40s2' = sd storeString.
|
|
32 |
] ifFalse:[
|
|
33 |
self assert: '1.40s2' = sd printString.
|
|
34 |
]
|
|
35 |
!
|
|
36 |
|
|
37 |
testAsNumberNegatedWithoutDecimalPoint
|
|
38 |
|
|
39 |
| sd |
|
|
40 |
sd := '-123s0' asNumber.
|
|
41 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
42 |
self assert: sd scale == 0.
|
|
43 |
|
|
44 |
Smalltalk isSmalltalkX ifTrue:[
|
|
45 |
self assert: '-123' = sd printString.
|
|
46 |
self assert: '-123s0' = sd storeString.
|
|
47 |
] ifFalse:[
|
|
48 |
self assert: '-123s0' = sd printString.
|
|
49 |
]
|
|
50 |
!
|
|
51 |
|
|
52 |
testAsNumberNegatedWithoutDecimalPoint2
|
|
53 |
|
|
54 |
| sd |
|
|
55 |
sd := '-123s2' asNumber.
|
|
56 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
57 |
self assert: sd scale == 2.
|
|
58 |
|
|
59 |
Smalltalk isSmalltalkX ifTrue:[
|
|
60 |
self assert: '-123.00' = sd printString.
|
|
61 |
self assert: '-123.00s2' = sd storeString.
|
|
62 |
] ifFalse:[
|
|
63 |
self assert: '-123.00s2' = sd printString.
|
|
64 |
]
|
|
65 |
!
|
|
66 |
|
|
67 |
testAsNumberWithExtendedScale
|
|
68 |
|
|
69 |
| sd |
|
|
70 |
sd := '123s2' asNumber.
|
|
71 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
72 |
self assert: sd scale == 2.
|
|
73 |
|
|
74 |
Smalltalk isSmalltalkX ifTrue:[
|
|
75 |
self assert: '123.00' = sd printString.
|
|
76 |
self assert: '123.00s2' = sd storeString.
|
|
77 |
] ifFalse:[
|
|
78 |
self assert: '123.00s2' = sd printString.
|
|
79 |
]
|
|
80 |
!
|
|
81 |
|
|
82 |
testAsNumberWithRadix
|
620
|
83 |
| sd oldSetting |
|
317
|
84 |
|
|
85 |
Smalltalk isSmalltalkX ifTrue:[
|
620
|
86 |
oldSetting := ParserFlags allowFixedPointLiterals.
|
|
87 |
ParserFlags allowFixedPointLiterals:true.
|
|
88 |
[
|
|
89 |
sd := Number readSmalltalkSyntaxFrom:'10r-22.2s5'.
|
|
90 |
] ensure:[
|
|
91 |
ParserFlags allowFixedPointLiterals:oldSetting.
|
|
92 |
]
|
317
|
93 |
] ifFalse:[
|
|
94 |
sd := '10r-22.2s5' asNumber.
|
|
95 |
].
|
|
96 |
|
312
|
97 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
98 |
self assert: sd scale == 5.
|
|
99 |
|
|
100 |
Smalltalk isSmalltalkX ifTrue:[
|
|
101 |
self assert: '-22.20000' = sd printString.
|
|
102 |
self assert: '-22.20000s5' = sd storeString.
|
|
103 |
] ifFalse:[
|
|
104 |
self assert: '-22.20000s5' = sd printString.
|
|
105 |
]
|
620
|
106 |
|
|
107 |
"Modified: / 09-08-2011 / 20:57:54 / cg"
|
312
|
108 |
!
|
|
109 |
|
|
110 |
testAsNumberWithSuperfluousDecimalPoint
|
|
111 |
|
|
112 |
| sd |
|
|
113 |
sd := '123.s2' asNumber.
|
|
114 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
115 |
self assert: sd scale == 2.
|
|
116 |
|
|
117 |
Smalltalk isSmalltalkX ifTrue:[
|
|
118 |
self assert: '123.00' = sd printString.
|
|
119 |
self assert: '123.00s2' = sd storeString.
|
|
120 |
] ifFalse:[
|
|
121 |
self assert: '123.00s2' = sd printString.
|
|
122 |
]
|
|
123 |
!
|
|
124 |
|
|
125 |
testAsNumberWithoutDecimalPoint
|
|
126 |
|
|
127 |
| sd |
|
|
128 |
sd := '123s0' asNumber.
|
|
129 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
130 |
self assert: sd scale == 0.
|
|
131 |
|
|
132 |
Smalltalk isSmalltalkX ifTrue:[
|
|
133 |
self assert: '123' = sd printString.
|
|
134 |
self assert: '123s0' = sd storeString.
|
|
135 |
] ifFalse:[
|
|
136 |
self assert: '123s0' = sd printString.
|
|
137 |
]
|
|
138 |
!
|
|
139 |
|
|
140 |
testAsNumberWithoutDecimalPoint2
|
|
141 |
|
|
142 |
| sd |
|
|
143 |
sd := '123s2' asNumber.
|
|
144 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
145 |
self assert: sd scale == 2.
|
|
146 |
|
|
147 |
Smalltalk isSmalltalkX ifTrue:[
|
|
148 |
self assert: '123.00' = sd printString.
|
|
149 |
self assert: '123.00s2' = sd storeString.
|
|
150 |
] ifFalse:[
|
|
151 |
self assert: '123.00s2' = sd printString.
|
|
152 |
]
|
|
153 |
!
|
|
154 |
|
|
155 |
testConvertFromFloat
|
|
156 |
|
|
157 |
| aFloat sd f2 diff |
|
|
158 |
aFloat := 11/13 asFloat.
|
|
159 |
sd := aFloat asScaledDecimal: 2.
|
|
160 |
self assert: 2 == sd scale.
|
|
161 |
Smalltalk isSmalltalkX ifTrue:[
|
|
162 |
self assert: '0.85' = sd printString.
|
|
163 |
] ifFalse:[
|
|
164 |
self assert: '0.84s2' = sd printString.
|
|
165 |
].
|
|
166 |
f2 := sd asFloat.
|
|
167 |
diff := f2 - aFloat.
|
|
168 |
self assert: diff < 1.0e-9. "actually, f = f2, but this is not a requirement"
|
|
169 |
!
|
|
170 |
|
|
171 |
testConvertFromFraction
|
|
172 |
|
|
173 |
| sd |
|
|
174 |
sd := (13 / 11) asScaledDecimal: 6.
|
|
175 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
176 |
Smalltalk isSmalltalkX ifTrue:[
|
|
177 |
self assert: ('1.181818' = sd printString).
|
|
178 |
] ifFalse:[
|
|
179 |
self assert: ('1.181818s6' = sd printString).
|
|
180 |
].
|
|
181 |
self assert: 6 == sd scale
|
|
182 |
!
|
|
183 |
|
|
184 |
testConvertFromInteger
|
|
185 |
"Converting an Integer to a ScaledDecimal yields a ScaledDecimal with
|
|
186 |
scale 0, regardless of the scale specified in the #asScaledDecimal: message."
|
|
187 |
|
|
188 |
| sd |
|
|
189 |
|
|
190 |
Smalltalk isSmalltalkX ifTrue:[
|
|
191 |
"/ I think this behavior is wrong.
|
|
192 |
^ self.
|
|
193 |
].
|
|
194 |
|
|
195 |
sd := 13 asScaledDecimal: 6.
|
|
196 |
self assert: 0 = sd scale.
|
|
197 |
self assert: ('13s0' = sd printString).
|
|
198 |
sd := -13 asScaledDecimal: 6.
|
|
199 |
self assert: 0 = sd scale.
|
|
200 |
self assert: ('-13s0' = sd printString).
|
|
201 |
sd := 130000000013 asScaledDecimal: 6.
|
|
202 |
self assert: 0 = sd scale.
|
|
203 |
self assert: ('130000000013s0' = sd printString).
|
|
204 |
sd := -130000000013 asScaledDecimal: 6.
|
|
205 |
self assert: 0 = sd scale.
|
|
206 |
self assert: ('-130000000013s0' = sd printString)
|
|
207 |
!
|
|
208 |
|
|
209 |
testLiteral
|
|
210 |
| sd |
|
|
211 |
|
|
212 |
sd := 1.40s2.
|
|
213 |
self assert: (ScaledDecimal ? FixedPoint) == sd class.
|
|
214 |
self assert: sd scale == 2.
|
|
215 |
Smalltalk isSmalltalkX ifTrue:[
|
|
216 |
self assert: '1.40' = sd printString.
|
|
217 |
self assert: '1.40s2' = sd storeString.
|
|
218 |
] ifFalse:[
|
|
219 |
self assert: '1.40s2' = sd printString.
|
|
220 |
].
|
|
221 |
!
|
|
222 |
|
313
|
223 |
testPrintString
|
|
224 |
"The printed representation of a ScaledDecimal is truncated, not rounded.
|
|
225 |
Not sure if this is right, so this test describes the current Squeak implementation.
|
|
226 |
If someone knows a reason that rounding would be preferable, then update
|
|
227 |
this test."
|
|
228 |
|
|
229 |
| sd |
|
|
230 |
|
|
231 |
sd := (13 / 11) asScaledDecimal: 6.
|
|
232 |
Smalltalk isSmalltalkX ifTrue:[
|
|
233 |
self assert: ('1.181818' = sd printString).
|
|
234 |
] ifFalse:[
|
|
235 |
self assert: ('1.181818s6' = sd printString).
|
|
236 |
].
|
|
237 |
|
|
238 |
sd := (13 / 11) asScaledDecimal: 5.
|
|
239 |
Smalltalk isSmalltalkX ifTrue:[
|
|
240 |
self assert: ('1.18182' = sd printString).
|
|
241 |
] ifFalse:[
|
|
242 |
self deny: ('1.18182s5' = sd printString).
|
|
243 |
].
|
|
244 |
|
|
245 |
sd := (13 / 11) asScaledDecimal: 5.
|
|
246 |
Smalltalk isSmalltalkX ifTrue:[
|
|
247 |
self deny: ('1.18181' = sd printString).
|
|
248 |
] ifFalse:[
|
|
249 |
self assert: ('1.18181s5' = sd printString).
|
|
250 |
].
|
|
251 |
!
|
|
252 |
|
312
|
253 |
testStoreAndRead
|
|
254 |
|check|
|
|
255 |
|
|
256 |
check := [:originalNum |
|
|
257 |
|s readNum|
|
|
258 |
|
|
259 |
s := originalNum storeString.
|
|
260 |
readNum := Number readFrom:s.
|
|
261 |
self assert:readNum = originalNum.
|
|
262 |
].
|
|
263 |
|
|
264 |
check value:((FixedPoint fromString:'0.66666666') ).
|
|
265 |
check value:((FixedPoint fromString:'0.66666666') withScale:2 ).
|
|
266 |
|
|
267 |
check value:((FixedPoint fromString:'1.5') ).
|
|
268 |
check value:((FixedPoint fromString:'1.5') withScale:2 ).
|
|
269 |
check value:((FixedPoint fromString:'1.5') withScale:1 ).
|
|
270 |
check value:((FixedPoint fromString:'1.5') withScale:0 ).
|
|
271 |
! !
|
|
272 |
|
|
273 |
!ScaledDecimalTest class methodsFor:'documentation'!
|
|
274 |
|
|
275 |
version
|
|
276 |
^ '$Header$'
|
|
277 |
! !
|