OLD | NEW |
| (Empty) |
1 // for details. All rights reserved. Use of this source code is governed by a | |
2 // BSD-style license that can be found in the LICENSE file. | |
3 | |
4 /** | |
5 * Tests based on the closure number formatting tests. | |
6 */ | |
7 library number_closure_test; | |
8 | |
9 import "package:intl/intl.dart"; | |
10 import "package:unittest/unittest.dart"; | |
11 | |
12 main() { | |
13 test("testVeryBigNumber", testVeryBigNumber); | |
14 test("testStandardFormat", testStandardFormat); | |
15 test("testNegativePercentage", testNegativePercentage); | |
16 test("testCustomPercentage", testCustomPercentage); | |
17 test("testBasicFormat", testBasicFormat); | |
18 test("testGrouping", testGrouping); | |
19 test("testPerMill", testPerMill); | |
20 test("testQuotes", testQuotes); | |
21 test("testZeros", testZeros); | |
22 test("testExponential", testExponential); | |
23 test("testPlusSignInExponentPart", testPlusSignInExponentPart); | |
24 test("testApis", testApis); | |
25 test("testLocaleSwitch", testLocaleSwitch); | |
26 } | |
27 | |
28 /** | |
29 * Test two large numbers for equality, assuming that there may be some | |
30 * loss of precision in the less significant digits. | |
31 */ | |
32 veryBigNumberCompare(str1, str2) { | |
33 return str1.length == str2.length && | |
34 str1.substring(0, 8) == str2.substring(0, 8); | |
35 } | |
36 | |
37 testVeryBigNumber() { | |
38 var str; | |
39 var fmt; | |
40 | |
41 fmt = new NumberFormat.decimalPattern(); | |
42 str = fmt.format(1.3456E20); | |
43 expect(veryBigNumberCompare('134,560,000,000,000,000,000', str), isTrue); | |
44 | |
45 fmt = new NumberFormat.percentPattern(); | |
46 str = fmt.format(1.3456E20); | |
47 expect(veryBigNumberCompare('13,456,000,000,000,000,000,000%', str), isTrue); | |
48 | |
49 // TODO(alanknight): Note that this disagrees with what ICU would print | |
50 // for this. We need significant digit support to do this properly. | |
51 fmt = new NumberFormat.scientificPattern(); | |
52 str = fmt.format(1.3456E20); | |
53 expect('1E20', str); | |
54 | |
55 fmt = new NumberFormat.decimalPattern(); | |
56 str = fmt.format(-1.234567890123456e306); | |
57 expect(1 + 1 + 306 + 306 / 3, str.length); | |
58 expect('-1,234,567,890,123,45', str.substring(0, 21)); | |
59 | |
60 str = fmt.format(1 / 0); | |
61 expect('∞', str); | |
62 str = fmt.format(-1 / 0); | |
63 expect('-∞', str); | |
64 } | |
65 | |
66 void testStandardFormat() { | |
67 var str; | |
68 var fmt; | |
69 fmt = new NumberFormat.decimalPattern(); | |
70 str = fmt.format(1234.579); | |
71 expect('1,234.579', str); | |
72 fmt = new NumberFormat.percentPattern(); | |
73 str = fmt.format(1234.579); | |
74 expect('123,458%', str); | |
75 fmt = new NumberFormat.scientificPattern(); | |
76 str = fmt.format(1234.579); | |
77 expect('1E3', str); | |
78 } | |
79 | |
80 void testNegativePercentage() { | |
81 var str; | |
82 var fmt = new NumberFormat('#,##0.00%'); | |
83 str = fmt.format(-1234.56); | |
84 expect('-123,456.00%', str); | |
85 | |
86 fmt = new NumberFormat.percentPattern(); | |
87 str = fmt.format(-1234.579); | |
88 expect('-123,458%', str); | |
89 } | |
90 | |
91 void testCustomPercentage() { | |
92 var fmt = new NumberFormat.percentPattern(); | |
93 fmt.maximumFractionDigits = 1; | |
94 fmt.minimumFractionDigits = 1; | |
95 var str = fmt.format(0.1291); | |
96 expect('12.9%', str); | |
97 fmt.maximumFractionDigits = 2; | |
98 fmt.minimumFractionDigits = 1; | |
99 str = fmt.format(0.129); | |
100 expect('12.9%', str); | |
101 fmt.maximumFractionDigits = 2; | |
102 fmt.minimumFractionDigits = 1; | |
103 str = fmt.format(0.12); | |
104 expect('12.0%', str); | |
105 fmt.maximumFractionDigits = 2; | |
106 fmt.minimumFractionDigits = 1; | |
107 str = fmt.format(0.12911); | |
108 expect('12.91%', str); | |
109 } | |
110 | |
111 void testBasicFormat() { | |
112 var fmt = new NumberFormat('0.0000'); | |
113 var str = fmt.format(123.45789179565757); | |
114 expect('123.4579', str); | |
115 } | |
116 | |
117 void testGrouping() { | |
118 var str; | |
119 | |
120 var fmt = new NumberFormat('#,###'); | |
121 str = fmt.format(1234567890); | |
122 expect('1,234,567,890', str); | |
123 fmt = new NumberFormat('#,####'); | |
124 str = fmt.format(1234567890); | |
125 expect('12,3456,7890', str); | |
126 | |
127 fmt = new NumberFormat('#'); | |
128 str = fmt.format(1234567890); | |
129 expect('1234567890', str); | |
130 } | |
131 | |
132 void testPerMill() { | |
133 var str; | |
134 | |
135 var fmt = new NumberFormat('###.###\u2030'); | |
136 str = fmt.format(0.4857); | |
137 expect('485.7\u2030', str); | |
138 } | |
139 | |
140 void testQuotes() { | |
141 var str; | |
142 | |
143 var fmt = new NumberFormat('a\'fo\'\'o\'b#'); | |
144 str = fmt.format(123); | |
145 expect('afo\'ob123', str); | |
146 | |
147 fmt = new NumberFormat('a\'\'b#'); | |
148 str = fmt.format(123); | |
149 expect('a\'b123', str); | |
150 | |
151 fmt = new NumberFormat('a\'fo\'\'o\'b#'); | |
152 str = fmt.format(-123); | |
153 expect('-afo\'ob123', str); | |
154 | |
155 fmt = new NumberFormat('a\'\'b#'); | |
156 str = fmt.format(-123); | |
157 expect('-a\'b123', str); | |
158 } | |
159 | |
160 void testZeros() { | |
161 var str; | |
162 var fmt; | |
163 | |
164 fmt = new NumberFormat('#.#'); | |
165 str = fmt.format(0); | |
166 expect('0', str); | |
167 fmt = new NumberFormat('#.'); | |
168 str = fmt.format(0); | |
169 expect('0.', str); | |
170 fmt = new NumberFormat('.#'); | |
171 str = fmt.format(0); | |
172 expect('.0', str); | |
173 fmt = new NumberFormat('#'); | |
174 str = fmt.format(0); | |
175 expect('0', str); | |
176 | |
177 fmt = new NumberFormat('#0.#'); | |
178 str = fmt.format(0); | |
179 expect('0', str); | |
180 fmt = new NumberFormat('#0.'); | |
181 str = fmt.format(0); | |
182 expect('0.', str); | |
183 fmt = new NumberFormat('#.0'); | |
184 str = fmt.format(0); | |
185 expect('.0', str); | |
186 fmt = new NumberFormat('#'); | |
187 str = fmt.format(0); | |
188 expect('0', str); | |
189 fmt = new NumberFormat('000'); | |
190 str = fmt.format(0); | |
191 expect('000', str); | |
192 } | |
193 | |
194 void testExponential() { | |
195 var str; | |
196 var fmt; | |
197 | |
198 fmt = new NumberFormat('0.####E0'); | |
199 str = fmt.format(0.01234); | |
200 expect('1.234E-2', str); | |
201 fmt = new NumberFormat('00.000E00'); | |
202 str = fmt.format(0.01234); | |
203 expect('12.340E-03', str); | |
204 fmt = new NumberFormat('##0.######E000'); | |
205 str = fmt.format(0.01234); | |
206 expect('12.34E-003', str); | |
207 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
208 str = fmt.format(0.01234); | |
209 expect('1.234E-2', str); | |
210 | |
211 fmt = new NumberFormat('0.####E0'); | |
212 str = fmt.format(123456789); | |
213 expect('1.2346E8', str); | |
214 fmt = new NumberFormat('00.000E00'); | |
215 str = fmt.format(123456789); | |
216 expect('12.346E07', str); | |
217 fmt = new NumberFormat('##0.######E000'); | |
218 str = fmt.format(123456789); | |
219 expect('123.456789E006', str); | |
220 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
221 str = fmt.format(123456789); | |
222 expect('1.235E8', str); | |
223 | |
224 fmt = new NumberFormat('0.####E0'); | |
225 str = fmt.format(1.23e300); | |
226 expect('1.23E300', str); | |
227 fmt = new NumberFormat('00.000E00'); | |
228 str = fmt.format(1.23e300); | |
229 expect('12.300E299', str); | |
230 fmt = new NumberFormat('##0.######E000'); | |
231 str = fmt.format(1.23e300); | |
232 expect('1.23E300', str); | |
233 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
234 str = fmt.format(1.23e300); | |
235 expect('1.23E300', str); | |
236 | |
237 fmt = new NumberFormat('0.####E0'); | |
238 str = fmt.format(-3.141592653e-271); | |
239 expect('-3.1416E-271', str); | |
240 fmt = new NumberFormat('00.000E00'); | |
241 str = fmt.format(-3.141592653e-271); | |
242 expect('-31.416E-272', str); | |
243 fmt = new NumberFormat('##0.######E000'); | |
244 str = fmt.format(-3.141592653e-271); | |
245 expect('-314.159265E-273', str); | |
246 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
247 str = fmt.format(-3.141592653e-271); | |
248 expect('[3.142E-271]', str); | |
249 | |
250 fmt = new NumberFormat('0.####E0'); | |
251 str = fmt.format(0); | |
252 expect('0E0', str); | |
253 fmt = new NumberFormat('00.000E00'); | |
254 str = fmt.format(0); | |
255 expect('00.000E00', str); | |
256 fmt = new NumberFormat('##0.######E000'); | |
257 str = fmt.format(0); | |
258 expect('0E000', str); | |
259 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
260 str = fmt.format(0); | |
261 expect('0E0', str); | |
262 | |
263 fmt = new NumberFormat('0.####E0'); | |
264 str = fmt.format(-1); | |
265 expect('-1E0', str); | |
266 fmt = new NumberFormat('00.000E00'); | |
267 str = fmt.format(-1); | |
268 expect('-10.000E-01', str); | |
269 fmt = new NumberFormat('##0.######E000'); | |
270 str = fmt.format(-1); | |
271 expect('-1E000', str); | |
272 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
273 str = fmt.format(-1); | |
274 expect('[1E0]', str); | |
275 | |
276 fmt = new NumberFormat('0.####E0'); | |
277 str = fmt.format(1); | |
278 expect('1E0', str); | |
279 fmt = new NumberFormat('00.000E00'); | |
280 str = fmt.format(1); | |
281 expect('10.000E-01', str); | |
282 fmt = new NumberFormat('##0.######E000'); | |
283 str = fmt.format(1); | |
284 expect('1E000', str); | |
285 fmt = new NumberFormat('0.###E0;[0.###E0]'); | |
286 str = fmt.format(1); | |
287 expect('1E0', str); | |
288 | |
289 fmt = new NumberFormat('#E0'); | |
290 str = fmt.format(12345.0); | |
291 expect('1E4', str); | |
292 fmt = new NumberFormat('0E0'); | |
293 str = fmt.format(12345.0); | |
294 expect('1E4', str); | |
295 fmt = new NumberFormat('##0.###E0'); | |
296 str = fmt.format(12345.0); | |
297 expect('12.345E3', str); | |
298 fmt = new NumberFormat('##0.###E0'); | |
299 str = fmt.format(12345.00001); | |
300 expect('12.345E3', str); | |
301 fmt = new NumberFormat('##0.###E0'); | |
302 str = fmt.format(12345); | |
303 expect('12.345E3', str); | |
304 | |
305 fmt = new NumberFormat('##0.####E0'); | |
306 str = fmt.format(789.12345e-9); | |
307 fmt = new NumberFormat('##0.####E0'); | |
308 str = fmt.format(780e-9); | |
309 expect('780E-9', str); | |
310 fmt = new NumberFormat('.###E0'); | |
311 str = fmt.format(45678.0); | |
312 expect('.457E5', str); | |
313 fmt = new NumberFormat('.###E0'); | |
314 str = fmt.format(0); | |
315 expect('.0E0', str); | |
316 | |
317 fmt = new NumberFormat('#E0'); | |
318 str = fmt.format(45678000); | |
319 expect('5E7', str); | |
320 fmt = new NumberFormat('##E0'); | |
321 str = fmt.format(45678000); | |
322 expect('46E6', str); | |
323 fmt = new NumberFormat('####E0'); | |
324 str = fmt.format(45678000); | |
325 expect('4568E4', str); | |
326 fmt = new NumberFormat('0E0'); | |
327 str = fmt.format(45678000); | |
328 expect('5E7', str); | |
329 fmt = new NumberFormat('00E0'); | |
330 str = fmt.format(45678000); | |
331 expect('46E6', str); | |
332 fmt = new NumberFormat('000E0'); | |
333 str = fmt.format(45678000); | |
334 expect('457E5', str); | |
335 fmt = new NumberFormat('###E0'); | |
336 str = fmt.format(0.0000123); | |
337 expect('12E-6', str); | |
338 fmt = new NumberFormat('###E0'); | |
339 str = fmt.format(0.000123); | |
340 expect('123E-6', str); | |
341 fmt = new NumberFormat('###E0'); | |
342 str = fmt.format(0.00123); | |
343 expect('1E-3', str); | |
344 fmt = new NumberFormat('###E0'); | |
345 str = fmt.format(0.0123); | |
346 expect('12E-3', str); | |
347 fmt = new NumberFormat('###E0'); | |
348 str = fmt.format(0.123); | |
349 expect('123E-3', str); | |
350 fmt = new NumberFormat('###E0'); | |
351 str = fmt.format(1.23); | |
352 expect('1E0', str); | |
353 fmt = new NumberFormat('###E0'); | |
354 str = fmt.format(12.3); | |
355 expect('12E0', str); | |
356 fmt = new NumberFormat('###E0'); | |
357 str = fmt.format(123.0); | |
358 expect('123E0', str); | |
359 fmt = new NumberFormat('###E0'); | |
360 str = fmt.format(1230.0); | |
361 expect('1E3', str); | |
362 } | |
363 | |
364 void testPlusSignInExponentPart() { | |
365 var fmt; | |
366 fmt = new NumberFormat('0E+0'); | |
367 var str = fmt.format(45678000); | |
368 expect('5E+7', str); | |
369 } | |
370 | |
371 void testApis() { | |
372 var fmt; | |
373 var str; | |
374 | |
375 fmt = new NumberFormat('#,###'); | |
376 str = fmt.format(1234567890); | |
377 expect('1,234,567,890', str); | |
378 } | |
379 | |
380 testLocaleSwitch() { | |
381 Intl.withLocale("fr", verifyFrenchLocale); | |
382 } | |
383 | |
384 void verifyFrenchLocale() { | |
385 var fmt = new NumberFormat('#,###'); | |
386 var str = fmt.format(1234567890); | |
387 expect('1\u00a0234\u00a0567\u00a0890', str); | |
388 } | |
OLD | NEW |