source: Dev/branches/rest-dojo-ui/client/dojo/tests/number.js @ 263

Last change on this file since 263 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 44.3 KB
Line 
1define(["../main", "doh", "../number", "../i18n"], function(dojo, doh){
2var tests= {number:{}};
3/**
4 * Refer to ICU4J's NumberFormatTest.expect(...)
5 */
6tests.number.check=function(t,options,sourceInput,expectResult){
7        tests.number.checkFormatParseCycle(t, t,options,sourceInput,expectResult,false);
8        tests.number.checkParse(t, t,options,expectResult,sourceInput);
9};
10
11/**
12 * Perform a single formatting check or a backward check
13 * backward check:number1 -(formatted)-> string1 -(parsed)-> number2 -(formated)-> string2
14 * then number should == number2; string1 should == string2
15 */
16tests.number.checkFormatParseCycle=function(t,options,sourceInput,expectResult,
17                 backwardCheck/*boolean,indicates whether need a backward chain check,like formate->parse->format*/){
18        if(null != options){
19                var pattern = options.pattern;
20                var locale = options.locale;
21                //TODO: add more fields
22        }
23
24        //print("\n");
25        var str = null==pattern?"default":pattern;
26        //print("pattern:" + str + "| locale:" + locale);
27        //print("input:" + sourceInput);
28        var result = dojo.number.format(sourceInput,options);
29        //print("result:" + result);
30        if(null != expectResult){
31            t.is(expectResult,result);
32        }
33        if(backwardCheck){
34                var resultParsed = dojo.number.parse(result,options);
35                //print("resultParsed:" + resultParsed);
36                if(!tests.number._decimalNumberDiff(sourceInput,resultParsed)){
37                    t.is(sourceInput,resultParsed);
38                }
39                var resultParsedReformatted = dojo.number.format(resultParsed,options);
40                //print("resultParsedReformatted:" + resultParsedReformatted);
41            if(!tests.number._decimalNumberDiff(result,resultParsedReformatted)){
42                        t.is(result,resultParsedReformatted);
43                }
44        }
45};
46
47/**
48 * Perform a single parsing check
49 */
50tests.number.checkParse=function(t,options,sourceInput,expectResult){
51        var str = "default";
52        if(null != options && null != options.pattern){
53                str = options.pattern;
54        }
55        //print("input:" + sourceInput);
56        var result = dojo.number.parse(sourceInput,options);
57        //print("result :" + result);
58        if(null != expectResult){
59            t.is(expectResult,result);
60        }
61};
62
63/**
64 * //TODO:Round a given number
65 */
66tests.number.rounding = function(t,number,maxFractionDigits,expected){
67        var pattern="#0.";
68        for(var i=0; i<maxFractionDigits; i++){pattern += "#";}
69        var result = dojo.number.format(number,{locale:tests.number.locale, pattern:pattern});
70        t.is(expected,result);
71};
72
73/**
74 * Run a batch parsing
75 */
76function runBatchParse(options,dataArray/*array*/,pass/*boolean*/){
77        var exception = null;
78        var result;
79        var i=0;
80        var str = (null==options.pattern)?"default":options.pattern;
81
82        //print("\n");
83        for(; i<dataArray.length; i++){
84                try{
85                        //print("["+i+"]"+"input:"+dataArray[i]);
86                        result = dojo.number.parse(dataArray[i],options);
87                        if(isNaN(result)){
88                                throw "\"" + dataArray[i] + "\" is parsed to NaN with pattern " + str;
89                        }
90                        //print("["+i+"]"+"output:"+result);
91                }catch(e){
92                        exception = e;
93                        break;
94                }
95        }
96
97        if(!pass && (exception == null)) {
98                throw "runBatchParse() - stric parse failed, no exception when parsing illegal data";
99        }else if(exception != null){
100                if(!pass && i == 0){
101                        //strict parsing should fail for all the dataArray elements as expected
102                        //pass condition for strict parsing
103                        return;
104                }
105                throw "runBatchParse() failed: " + exception;
106        }
107}
108
109/**
110 * Check whether the given two numbers differ under the decimal bound
111 *
112 */
113tests.number._decimalNumberDiff = function(num1,num2){
114        //TODO: should be more accurate when dojo.number finish rounding in the future
115        var diffBound = 1e-3;
116        var diff = num1 - num2;
117        //print("Math.abs(diff) " + Math.abs(diff));
118        if(Math.abs(diff) < diffBound ){
119                return true;
120        }else if(isNaN(Math.abs(diff))){
121                var s = num1.toString().split(num2);
122                s[1] = s[1].replace(",","0");
123                s[1] = s[1].replace('\u066b','0');
124                return (new Number(s[1])< diffBound);
125        }
126        return false;
127};
128
129doh.register("tests.number",
130        [
131                {
132                        // Test formatting and parsing of currencies in various locales pre-built in dojo.cldr
133                        // NOTE: we can't set djConfig.extraLocale before bootstrapping unit tests, so directly
134                        // load resources here for specific locales:
135
136                        name: "number",
137                        runTest: function(t){
138                                var partLocaleList = ["en-us", "fr-fr", "de-de"];
139                                tests.number.locale = "en-us";
140                                if(require.async){
141                                        var
142                                                def = new doh.Deferred(),
143                                                deps = [];
144                                        dojo.forEach(partLocaleList, function(locale){
145                                                deps.push(dojo.getL10nName("dojo/cldr", "number", locale));
146                                        });
147                                        require(deps, function(){
148                                                def.callback(true);
149                                        });
150                                        return def;
151                                }else{ // tests for the v1.x loader/i18n machinery
152                                        for(var i = 0 ; i < partLocaleList.length; i ++){
153                                                dojo.i18n.getLocalization("dojo.cldr","number",partLocaleList[i]);
154                                        }
155                                }
156                        }
157                },
158                {
159                        name: "invalid",
160                        runTest: function(t){
161                                t.t(null === dojo.number.format(NaN));
162                                t.t(null === dojo.number.format(Number.NaN));
163                                t.t(null === dojo.number.format(Infinity));
164                                t.t(null === dojo.number.format(-Infinity));
165                        }
166                },
167                {
168                        name: "round",
169                        runTest: function(t){
170                                t.is(0, dojo.number.round(0));
171                                t.is(1, dojo.number.round(0.5));
172                                t.is(-1, dojo.number.round(-0.5));
173                                t.is(0.1, dojo.number.round(0.05, 1));
174                                t.is(-0.1, dojo.number.round(-0.05, 1));
175                                t.is(1.1, dojo.number.round(1.05, 1));
176                                t.is(-1.1, dojo.number.round(-1.05, 1));
177//                              t.is(-162.29, dojo.number.round(-162.295, 2)); // see ticket #7930, dojox.math.round
178//                              t.is(162.29, dojo.number.round(162.295, 2)); // ibid
179                        }
180                },
181                {
182                        name: "round_multiple",
183                        runTest: function(t){
184                                t.is("123.455", dojo.number.round(123.4525, 2, 5));
185                                t.is("123.45", dojo.number.round(123.452, 2, 5));
186                                t.is("123.455", dojo.number.round(123.454, 2, 5));
187                                t.is("123.455", dojo.number.round(123.456, 2, 5));
188                                t.is("-123.45", dojo.number.round(-123.452, 2, 5));
189                                t.is("-123.455", dojo.number.round(-123.4525, 2, 5));
190                                t.is("-123.455", dojo.number.round(-123.454, 2, 5));
191                                t.is("-123.455", dojo.number.round(-123.456, 2, 5));
192                        }
193                },
194                {
195                        name: "round_speleotrove",
196                        runTest: function(t){
197                                // submitted Mike Cowlishaw (IBM, CCLA), see http://speleotrove.com/decimal/#testcases
198                                t.is(12345, dojo.number.round(12345 + -0.1), "radx200");
199                                t.is(12345, dojo.number.round(12345 + -0.01), "radx201");
200                                t.is(12345, dojo.number.round(12345 + -0.001), "radx202");
201                                t.is(12345, dojo.number.round(12345 + -0.00001), "radx203");
202                                t.is(12345, dojo.number.round(12345 + -0.000001), "radx204");
203                                t.is(12345, dojo.number.round(12345 + -0.0000001), "radx205");
204                                t.is(12345, dojo.number.round(12345 +  0), "radx206");
205                                t.is(12345, dojo.number.round(12345 +  0.0000001), "radx207");
206                                t.is(12345, dojo.number.round(12345 +  0.000001), "radx208");
207                                t.is(12345, dojo.number.round(12345 +  0.00001), "radx209");
208                                t.is(12345, dojo.number.round(12345 +  0.0001), "radx210");
209                                t.is(12345, dojo.number.round(12345 +  0.001), "radx211");
210                                t.is(12345, dojo.number.round(12345 +  0.01), "radx212");
211                                t.is(12345, dojo.number.round(12345 +  0.1), "radx213");
212
213                                t.is(12346, dojo.number.round(12346 +  0.49999), "radx215");
214                                t.is(12347, dojo.number.round(12346 +  0.5), "radx216");
215                                t.is(12347, dojo.number.round(12346 +  0.50001), "radx217");
216
217                                t.is(12345, dojo.number.round(12345 +  0.4), "radx220");
218                                t.is(12345, dojo.number.round(12345 +  0.49), "radx221");
219                                t.is(12345, dojo.number.round(12345 +  0.499), "radx222");
220                                t.is(12345, dojo.number.round(12345 +  0.49999), "radx223");
221                                t.is(12346, dojo.number.round(12345 +  0.5), "radx224");
222                                t.is(12346, dojo.number.round(12345 +  0.50001), "radx225");
223                                t.is(12346, dojo.number.round(12345 +  0.5001), "radx226");
224                                t.is(12346, dojo.number.round(12345 +  0.501), "radx227");
225                                t.is(12346, dojo.number.round(12345 +  0.51), "radx228");
226                                t.is(12346, dojo.number.round(12345 +  0.6), "radx229");
227
228                                //negatives
229                                t.is(-12345, dojo.number.round(-12345 + -0.1), "rsux200");
230                                t.is(-12345, dojo.number.round(-12345 + -0.01), "rsux201");
231                                t.is(-12345, dojo.number.round(-12345 + -0.001), "rsux202");
232                                t.is(-12345, dojo.number.round(-12345 + -0.00001), "rsux203");
233                                t.is(-12345, dojo.number.round(-12345 + -0.000001), "rsux204");
234                                t.is(-12345, dojo.number.round(-12345 + -0.0000001), "rsux205");
235                                t.is(-12345, dojo.number.round(-12345 +  0), "rsux206");
236                                t.is(-12345, dojo.number.round(-12345 +  0.0000001), "rsux207");
237                                t.is(-12345, dojo.number.round(-12345 +  0.000001), "rsux208");
238                                t.is(-12345, dojo.number.round(-12345 +  0.00001), "rsux209");
239                                t.is(-12345, dojo.number.round(-12345 +  0.0001), "rsux210");
240                                t.is(-12345, dojo.number.round(-12345 +  0.001), "rsux211");
241                                t.is(-12345, dojo.number.round(-12345 +  0.01), "rsux212");
242                                t.is(-12345, dojo.number.round(-12345 +  0.1), "rsux213");
243
244                                t.is(-12346, dojo.number.round(-12346 +  0.49999), "rsux215");
245                                t.is(-12346, dojo.number.round(-12346 +  0.5), "rsux216");
246                                t.is(-12345, dojo.number.round(-12346 +  0.50001   ), "rsux217");
247
248                                t.is(-12345, dojo.number.round(-12345 +  0.4), "rsux220");
249                                t.is(-12345, dojo.number.round(-12345 +  0.49), "rsux221");
250                                t.is(-12345, dojo.number.round(-12345 +  0.499), "rsux222");
251                                t.is(-12345, dojo.number.round(-12345 +  0.49999), "rsux223");
252                                t.is(-12345, dojo.number.round(-12345 +  0.5), "rsux224");
253                                t.is(-12344, dojo.number.round(-12345 +  0.50001), "rsux225");
254                                t.is(-12344, dojo.number.round(-12345 +  0.5001), "rsux226");
255                                t.is(-12344, dojo.number.round(-12345 +  0.501), "rsux227");
256                                t.is(-12344, dojo.number.round(-12345 +  0.51), "rsux228");
257                                t.is(-12344, dojo.number.round(-12345 +  0.6), "rsux229");
258
259                                t.is(12345, dojo.number.round(  12345 /  1), "rdvx401");
260                                t.is(12344, dojo.number.round(  12345 /  1.0001), "rdvx402");
261                                t.is(12333, dojo.number.round(  12345 /  1.001), "rdvx403");
262                                t.is(12223, dojo.number.round(  12345 /  1.01), "rdvx404");
263                                t.is(11223, dojo.number.round(  12345 /  1.1), "rdvx405");
264
265                                t.is(3088.8, dojo.number.round( 12355 /  4, 1), "rdvx406");
266                                t.is(3086.3, dojo.number.round( 12345 /  4, 1), "rdvx407");
267                                t.is(3088.7, dojo.number.round( 12355 /  4.0001, 1), "rdvx408");
268                                t.is(3086.2, dojo.number.round( 12345 /  4.0001, 1), "rdvx409");
269                                t.is(2519.4, dojo.number.round( 12345 /  4.9, 1), "rdvx410");
270                                t.is(2473.9, dojo.number.round( 12345 /  4.99, 1), "rdvx411");
271                                t.is(2469.5, dojo.number.round( 12345 /  4.999, 1), "rdvx412");
272                                t.is(2469.0, dojo.number.round( 12345 /  4.9999, 1), "rdvx413");
273                                t.is(2469, dojo.number.round( 12345 /  5, 1), "rdvx414");
274                                t.is(2469.0, dojo.number.round( 12345 /  5.0001, 1), "rdvx415");
275                                t.is(2468.5, dojo.number.round( 12345 /  5.001, 1), "rdvx416");
276                                t.is(2464.1, dojo.number.round( 12345 /  5.01, 1), "rdvx417");
277                                t.is(2420.6, dojo.number.round( 12345 /  5.1, 1), "rdvx418");
278
279                                t.is(12345, dojo.number.round(  12345 *  1), "rmux401");
280                                t.is(12346, dojo.number.round(  12345 *  1.0001), "rmux402");
281                                t.is(12357, dojo.number.round(  12345 *  1.001), "rmux403");
282                                t.is(12468, dojo.number.round(  12345 *  1.01), "rmux404");
283                                t.is(13580, dojo.number.round(  12345 *  1.1), "rmux405");
284                                t.is(49380, dojo.number.round(  12345 *  4), "rmux406");
285                                t.is(49381, dojo.number.round(  12345 *  4.0001), "rmux407");
286                                t.is(60491, dojo.number.round(  12345 *  4.9), "rmux408");
287                                t.is(61602, dojo.number.round(  12345 *  4.99), "rmux409");
288                                t.is(61713, dojo.number.round(  12345 *  4.999), "rmux410");
289                                t.is(61724, dojo.number.round(  12345 *  4.9999), "rmux411");
290                                t.is(61725, dojo.number.round(  12345 *  5), "rmux412");
291                                t.is(61726, dojo.number.round(  12345 *  5.0001), "rmux413");
292                                t.is(61737, dojo.number.round(  12345 *  5.001), "rmux414");
293                                t.is(61848, dojo.number.round(  12345 *  5.01), "rmux415");
294/*
295                                t.is(1.4814E+5, dojo.number.round(  12345 *  12), "rmux416");
296                                t.is(1.6049E+5, dojo.number.round(  12345 *  13), "rmux417");
297                                t.is(1.4826E+5, dojo.number.round(  12355 *  12), "rmux418");
298                                t.is(1.6062E+5, dojo.number.round(  12355 *  13), "rmux419");
299*/
300                        }
301                },
302                {
303                        name: "format", // old tests
304                        runTest: function(t){
305
306        t.is("0123", dojo.number.format(123, {pattern: "0000"}));
307        t.is("-12,34,567.890", dojo.number.format(-1234567.89, {pattern: "#,##,##0.000##", locale: 'en-us'}));
308        t.is("-12,34,567.89012", dojo.number.format(-1234567.890123, {pattern: "#,##,##0.000##", locale: 'en-us'}));
309        t.is("(1,234,567.89012)", dojo.number.format(-1234567.890123, {pattern: "#,##0.000##;(#,##0.000##)", locale: 'en-us'}));
310        t.is("(1,234,567.89012)", dojo.number.format(-1234567.890123, {pattern: "#,##0.000##;(#)", locale: 'en-us'}));
311        t.is("50.1%", dojo.number.format(0.501, {pattern: "#0.#%", locale: 'en-us'}));
312        t.is("98", dojo.number.format(1998, {pattern: "00"}));
313        t.is("01998", dojo.number.format(1998, {pattern: "00000"}));
314        t.is("0.13", dojo.number.format(0.125, {pattern: "0.##", locale: 'en-us'})); //NOTE: expects round_half_up, not round_half_even
315        t.is("0.1250", dojo.number.format(0.125, {pattern: "0.0000", locale: 'en-us'}));
316        t.is("0.1", dojo.number.format(0.100004, {pattern: "0.####", locale: 'en-us'}));
317
318        t.is("-12", dojo.number.format(-12.3, {places:0, locale: "en-us"}));
319        t.is("-1,234,567.89", dojo.number.format(-1234567.89, {locale: "en-us"}));
320//      t.is("-12,34,567.89", dojo.number.format(-1234567.89, {locale: "en-in"}));
321        t.is("-1,234,568", dojo.number.format(-1234567.89, {places:0, locale: "en-us"}));
322//      t.is("-12,34,568", dojo.number.format(-1234567.89, {places:0, locale: "en-in"}));
323        t.is("-1\xa0000,10", dojo.number.format(-1000.1, {places:2, locale: "fr-fr"}));
324        t.is("-1,000.10", dojo.number.format(-1000.1, {places:2, locale: "en-us"}));
325        t.is("-1\xa0000,10", dojo.number.format(-1000.1, {places:2, locale: "fr-fr"}));
326        t.is("-1.234,56", dojo.number.format(-1234.56, {places:2, locale: "de-de"}));
327        t.is("-1,000.10", dojo.number.format(-1000.1, {places:2, locale: "en-us"}));
328        t.is("123.46%", dojo.number.format(1.23456, {places:2, locale: "en-us", type: "percent"}));
329        t.is("123.4", dojo.number.format(123.4, {places:'1,3', locale: 'en-us'}));
330        t.is("123.45", dojo.number.format(123.45, {places:'1,3', locale: 'en-us'}));
331        t.is("123.456", dojo.number.format(123.456, {places:'1,3', locale: 'en-us'}));
332
333        //rounding
334        t.is("-1,234,568", dojo.number.format(-1234567.89, {places:0, locale: "en-us"}));
335//      t.is("-12,34,568", dojo.number.format(-1234567.89, {places:0, locale: "en-in"}));
336        t.is("-1,000.11", dojo.number.format(-1000.114, {places:2, locale: "en-us"}));
337        t.is("-1,000.12", dojo.number.format(-1000.115, {places:2, locale: "en-us"}));
338        t.is("-1,000.12", dojo.number.format(-1000.116, {places:2, locale: "en-us"}));
339        t.is("-0.00", dojo.number.format(-0.0001, {places:2, locale: "en-us"}));
340        t.is("0.00", dojo.number.format(0, {places:2, locale: "en-us"}));
341
342        //change decimal places
343        t.is("-1\xa0000,100", dojo.number.format(-1000.1, {places:3, locale: "fr-fr"}));
344        t.is("-1,000.100", dojo.number.format(-1000.1, {places:3, locale: "en-us"}));
345                        }
346                },
347                {
348                        name: "parse", // old tests
349                        runTest: function(t){
350        t.is(1000, dojo.number.parse("1000", {locale: "en-us"}));
351        t.is(1000.123, dojo.number.parse("1000.123", {locale: "en-us"}));
352        t.is(1000, dojo.number.parse("1,000", {locale: "en-us"}));
353        t.is(-1000, dojo.number.parse("-1000", {locale: "en-us"}));
354        t.is(-1000.123, dojo.number.parse("-1000.123", {locale: "en-us"}));
355        t.is(-1234567.89, dojo.number.parse("-1,234,567.89", {locale: "en-us"}));
356        t.is(-1234567.89, dojo.number.parse("-1 234 567,89", {locale: "fr-fr"}));
357        t.t(isNaN(dojo.number.parse("-1 234 567,89", {locale: "en-us"})));
358
359        t.is(123, dojo.number.parse("0123", {pattern: "0000"}));
360
361        t.t(isNaN(dojo.number.parse("10,00", {locale: "en-us"})));
362        t.t(isNaN(dojo.number.parse("1000.1", {locale: "fr-fr"})));
363
364        t.t(isNaN(dojo.number.parse("")));
365        t.t(isNaN(dojo.number.parse("abcd")));
366
367        // should allow unlimited precision, by default
368        t.is(1.23456789, dojo.number.parse("1.23456789", {locale: "en-us"}));
369
370        //test whitespace
371//      t.is(-1234567, dojo.number.parse("  -1,234,567  ", {locale: "en-us"}));
372
373//      t.t(dojo.number.parse("9.1093826E-31"));
374        t.is(1.23, dojo.number.parse("123%", {locale: "en-us", type: "percent"}));
375        t.is(1.23, dojo.number.parse("123%", {places:0, locale: "en-us", type: "percent"}));
376        t.t(isNaN(dojo.number.parse("123.46%", {places:0, locale: "en-us", type: "percent"})));
377        t.is(1.2346, dojo.number.parse("123.46%", {places:2, locale: "en-us", type: "percent"}));
378        t.is(0.501, dojo.number.parse("50.1%", {pattern: "#0.#%", locale: 'en-us'}));
379
380        t.is(123.4, dojo.number.parse("123.4", {pattern: "#0.#", locale: 'en-us'}));
381        t.is(-123.4, dojo.number.parse("-123.4", {pattern: "#0.#", locale: 'en-us'}));
382        t.is(123.4, dojo.number.parse("123.4", {pattern: "#0.#;(#0.#)", locale: 'en-us'}));
383        t.is(-123.4, dojo.number.parse("(123.4)", {pattern: "#0.#;(#0.#)", locale: 'en-us'}));
384
385        t.is(null, dojo.number.format("abcd", {pattern: "0000"}));
386
387        t.is(123, dojo.number.parse("123", {places:0}));
388        t.is(123, dojo.number.parse("123", {places:'0'}));
389        t.is(123.4, dojo.number.parse("123.4", {places:1, locale: 'en-us'}));
390        t.is(123.45, dojo.number.parse("123.45", {places:'1,3', locale: 'en-us'}));
391        t.is(123.45, dojo.number.parse("123.45", {places:'0,2', locale: 'en-us'}));
392                        }
393                },
394                {
395                        name: "format_icu4j3_6",
396                        runTest: function(t){
397
398/*************************************************************************************************
399 * Evan:The following test cases are referred from ICU4J 3.6 (NumberFormatTest etc.)
400 * see http://icu.sourceforge.net/download/3.6.html#ICU4J
401 *************************************************************************************************/
402
403
404/**
405 * In ICU4J, testing logic for NumberFormat.format() is seperated into
406 * differernt single tese cases. So part of these logic are
407 * collected together in this single method.
408 *
409 * !!Failed cases are as follows:
410 * 1.1234567890987654321234567890987654321 should be formatted as
411 *   1,234,567,890,987,654,321,234,567,890,987,654,321 with all the default parameters,
412 *   but got 1.234 instead, may due to the unimplemeted exponent.
413 * 2.\u00a4 and ' are not replaced
414 *       with pattern "'*&'' '\u00a4' ''&*' #,##0.00"
415 *   1.0 should be formatted to "*&' Re. '&* 1.00",but got "'*&'' '\u00a4' ''&*' 1.00" instead
416 *   etc.
417 *
418 */
419        //print("test_number_format_icu4j3_6() start..............");
420        /* !!Failed case, 1.234 returned instead
421        //refer to ICU4J's NumberFormatTest.TestCoverage()
422        var bigNum = 1234567890987654321234567890987654321;
423        var expectResult = "1,234,567,890,987,654,321,234,567,890,987,654,321";
424        tests.number.checkFormatParseCycle(t, null,bigNum,expectResult,false);
425        */
426
427        //in icu4j should throw out an exception when formatting a string,
428        //but it seems dojo.number.format can deal with strings
429        //return 123,456,789
430        dojo.number.format("123456789");
431
432        //!!Failed case, \u00a4 and ' are not replaced
433        /*
434        var options = {pattern:"'*&'' '\u00a4' ''&*' #,##0.00",locale:"en-us"};
435        tests.number.check(t, options,1.0, "*&' Re. '&* 1.00");
436        tests.number.check(t, options,-2.0, "-*&' Rs. '&* 2.00");
437
438        options = {pattern:"#,##0.00 '*&'' '\u00a4' ''&*'",locale:"en-us"};
439        tests.number.check(t, options,1.0,"1.00 *&' Re. '&*");
440        tests.number.check(t, options,-2.0,"-2.00 *&' Rs. '&*");
441        */
442        //print("test_number_format_icu4j3_6() end..............\n");
443                        }
444                },
445                {
446                        name: "format_patterns",
447                        runTest: function(t){
448
449/**
450 * Refer to ICU4J's NumberFormatTest.TestPatterns() which now only coveres us locale
451 */
452        //print("test_number_format_Patterns() start..............");
453        var patterns = (["#0.#", "#0.", "#.0", "#"]);
454        var patternsLength = patterns.length;
455        var num = (["0","0", "0.0", "0"]);
456        var options;
457        //icu4j result seems doesn't work as:
458        //var num = (["0","0.", ".0", "0"]);
459        for (var i=0; i<patternsLength; ++i)
460        {
461                options = {pattern:patterns[i], locale: 'en-us'};
462                tests.number.checkFormatParseCycle(t, options,0,num[i],false);
463        }
464
465        //!!Failed case
466        //In ICU4J:
467        //        unquoted special characters in the suffix are illegal
468        //        so "000.000|###" is illegal; "000.000'|###'" is legal
469        //dojo.number.format:
470        //        when formatting 1.2 with illegal pattern "000.000|###"
471        //                no exception was thrown but got "001.200|###" instead.
472
473        /*
474        patterns = (["000.000|###","000.000'|###'"]);
475        var exception = false;
476        var result;
477        for(var i = 0; i < patterns.length; i ++){
478                try{
479                        //"001.200'|###'" is return for "000.000'|###'"
480                        //"001.200|###" is return for "000.000|###"
481                        result = dojo.number.format(1.2,{pattern:patterns[i]});
482                        print("["+i+"] 1.2 is formatted to " + result + " with pattern " + patterns[i]);
483                }catch(e){
484                        exception = true;
485                }
486                if(exception && i==1){
487                        throw "["+i+"]Failed when formatting 1.2 using legal pattern " + patterns[i];
488                }else if(!exception && i==0){
489                        throw "["+i+"]Failed when formatting 1.2 using illegal pattern  " + patterns[i];
490                }
491        }*/
492        //print("test_number_format_Patterns() end..............\n");
493                        }
494                },
495                {
496                        name: "exponential",
497                        runTest: function(t){
498/**
499 * TODO: For dojo.number future version
500 * Refer to ICU4J's NumberFormatTest.TestExponential()
501 */
502                        }
503                },
504                {
505                        name: "format_quotes",
506                        runTest: function(t){
507/**
508 * TODO: Failed case
509 * Refer to ICU4J's NumberFormatTest.TestQuotes()
510 */
511        //print("test_number_format_Quotes() start..............");
512        //TODO: add more locales
513
514        //TODO:!!Failed case
515        //Pattern "s'aa''s'c#" should format 6666 to "saa'sc6666", but got s'aa''s'c6666 instead
516        // is this case necessary?
517        /*
518        var pattern = "s'aa''s'c#";
519        var result = dojo.number.format(6666,{pattern:pattern,locale:"en-us"});
520        var expectResult = "saa'sc6666";
521        t.is(expectResult,result);
522        */
523        //print("test_number_format_Quotes() end..............");
524                        }
525                },
526                {
527                        name: "format_rounding",
528                        runTest: function(t){
529/**
530 * Refer to ICU4J's NumberFormatTest.TestRounding487() and NumberFormatTest.TestRounding()
531 */
532        //print("test_number_format_rounding() start..............");
533        tests.number.rounding(t,0.000179999, 5, "0.00018");
534        tests.number.rounding(t,0.00099, 4, "0.001");
535        tests.number.rounding(t,17.6995, 3, "17.7");
536        tests.number.rounding(t,15.3999, 0, "15");
537        tests.number.rounding(t,-29.6, 0, "-30");
538
539        //TODO refer to NumberFormatTest.TestRounding()
540
541        //print("test_number_format_rounding() end..............");
542                        }
543                },
544                {
545                        name: "format_scientific",
546                        runTest: function(t){
547/**
548 * TODO: For dojo.number future version
549 * Refer to ICU4J's NumberFormatTest.TestScientific()- Exponential testing
550 * Refer to ICU4J's NumberFormatTest.TestScientific2()
551 * Refer to ICU4J's NumberFormatTest.TestScientificGrouping()
552 */
553                        }
554                },
555                {
556                        name: "format_perMill",
557                        runTest: function(t){
558/**
559 * TODO: Failed case
560 * Refer to ICU4J's NumberFormatTest.TestPerMill()
561 */
562        //print("test_number_format_PerMill() start..............");
563        var pattern;
564        var result;
565        var expectResult;
566
567    //TODO: !!Failed case - ###.###\u2030(\u2030 is ‰)
568        //Pattern ###.###\u2030 should format 0.4857 as 485.7\u2030,but got 485.700\u2030 instead
569        pattern = "###.###\u2030";
570        expectResult = "485.7\u2030";
571        result = dojo.number.format(0.4857,{pattern:pattern, locale: 'en-us'});
572        t.is(expectResult,result);
573
574    //TODO: !!Failed mile percent case - ###.###m
575        //Pattern "###.###m" should format 0.4857 to 485.7m, but got 0.485m instead
576        /*
577        pattern = "###.###m";
578        expectResult = "485.7m";
579        result = dojo.number.format(0.4857,{pattern:pattern,locale:"en"});
580        t.is(expectResult,result);
581        */
582        //print("test_number_format_PerMill() end..............\n");
583                        }
584                },
585                {
586                        name: "format_grouping",
587                        runTest: function(t){
588/**
589 * Only test en-us and en-in
590 * Refer to ICU4J's NumberFormatTest.TestSecondaryGrouping()
591 */
592        //print("test_number_format_Grouping() start..............");
593        //primary grouping
594        var sourceInput = 123456789;
595        var expectResult = "12,34,56,789";
596        var options = {pattern:"#,##,###",locale:"en-us"};
597
598        //step1: 123456789 formated=> 12,34,56,789
599        //step2:12,34,56,789 parsed=> 123456789 => formated => 12,34,56,789
600        tests.number.checkFormatParseCycle(t, options,sourceInput,expectResult,true);
601
602        //TODO: sencondary grouping not implemented yet ?
603        //Pattern "#,###" and secondaryGroupingSize=4 should format 123456789 to "12,3456,789"
604
605        //Special case for "en-in" locale
606        //1876543210 should be formated as 1,87,65,43,210 in "en-in" (India)
607/*
608        sourceInput = 1876543210;
609        expectResult = "1,87,65,43,210";
610        var result = dojo.number.format(sourceInput,{locale:"en-in"});
611        t.is(expectResult,result);
612*/
613        //print("test_number_format_Grouping() end..............\n");
614                        }
615                },
616                {
617                        name: "format_pad",
618                        runTest: function(t){
619/**
620 * TODO:!!Failed cases:
621 * According to ICU4J test criteria:
622 * 1.with pattern "*^##.##":
623 *       0 should be formatted to "^^^^0",but got "*^0" instead,
624 *   -1.3 should be formatted to "^-1.3",but got "-*^1.3" instead.
625 *
626 * 2.with pattern "##0.0####*_ 'g-m/s^2'" :
627 *   0 should be formatted to "0.0______ g-m/s^2",but got ":0.0*_ 'g-m/s^2'" instead
628 *   1.0/3 should be formatted to "0.33333__ g-m/s^2",but got "0.33333*_ 'g-m/s^2'" instead
629 *
630 * 3.with pattern "*x#,###,###,##0.0#;*x(###,###,##0.0#)":
631 *       -10 should be formatted to "xxxxxxxxxx(10.0)",but got "*x(10.0)" instead.
632 *   10 should be formatted to "xxxxxxxxxxxx10.0",but got "*x10.0" instead.
633 *   ......
634 *   -1120456.37 should be formatted to "xx(1,120,456.37)",but got "*x(1,120,456.37)" instead.
635 *   1120456.37 should be formatted to "xxxx1,120,456.37",but got "*x1,120,456.37" instead.
636 *   -1252045600.37 should be formatted to "(1,252,045,600.37)",but got "*x(1,252,045,600.37)" instead.
637 *   1252045600.37 should be formatted to "10,252,045,600.37",but got "*x10,252,045,600.37" instead.
638 *
639 * 4.with pattern "#,###,###,##0.0#*x;(###,###,##0.0#*x)"
640 *       -10 should be formatted to (10.0xxxxxxxxxx),but got "(10.0*x)" instead.
641 *   10 should be formatted to "10.0xxxxxxxxxxxx",but got "10.0*x" instead.
642 *   ......
643 *   -1120456.37 should be formatted to "(1,120,456.37xx)",but got "(1,120,456.37*x)" instead.
644 *   1120456.37 should be formatted to "xxxx1,120,456.37",but got "1,120,456.37*x" instead.
645 *   -1252045600.37 should be formatted to "(1,252,045,600.37)",but got "(1,252,045,600.37*x)" instead.
646 *   1252045600.37 should be formatted to ""10,252,045,600.37"",but got "10,252,045,600.37*x" instead.*
647 *
648 * Refer to ICU4J's NumberFormatTest.TestPad()
649 */
650/*
651function test_number_format_pad(){
652        var locale = "en-us";
653        print("test_number_format_Pad() start..............");
654        var options = {pattern:"*^##.##",locale:locale};
655
656        tests.number.check(t, options,0,"^^^^0");
657        tests.number.check(t, options,-1.3,"^-1.3");
658
659
660        options = {pattern:"##0.0####*_ 'g-m/s^2'",locale:locale};
661        tests.number.check(t, options,0,"0.0______ g-m/s^2");
662        tests.number.checkFormatParseCycle(t, options,1.0/3,"0.33333__ g-m/s^2",true);
663
664        //exponent not implemented
665        //options = {pattern:"##0.0####E0*_ 'g-m/s^2'",locale:locale};
666        //tests.number.check(t, options,0,"0.0E0______ g-m/s^2");
667        //tests.number.checkFormatParseCycle(t, options,1.0/3,"333.333E-3_ g-m/s^2",true);
668
669        // Test padding before a sign
670        options = {pattern:"*x#,###,###,##0.0#;*x(###,###,##0.0#)",locale:locale};
671
672        tests.number.check(t, options,-10,"xxxxxxxxxx(10.0)");
673        tests.number.check(t, options,-1000, "xxxxxxx(1,000.0)");
674        tests.number.check(t, options,-1000000, "xxx(1,000,000.0)");
675        tests.number.check(t, options,-100.37, "xxxxxxxx(100.37)");
676        tests.number.check(t, options,-10456.37, "xxxxx(10,456.37)");
677        tests.number.check(t, options,-1120456.37, "xx(1,120,456.37)");
678        tests.number.check(t, options,-112045600.37, "(112,045,600.37)");
679        tests.number.check(t, options,-1252045600.37, "(1,252,045,600.37)");
680
681
682        tests.number.check(t, options,10, "xxxxxxxxxxxx10.0");
683        tests.number.check(t, options,1000, "xxxxxxxxx1,000.0");
684        tests.number.check(t, options,1000000, "xxxxx1,000,000.0");
685        tests.number.check(t, options,100.37, "xxxxxxxxxx100.37");
686        tests.number.check(t, options,10456.37, "xxxxxxx10,456.37");
687        tests.number.check(t, options,1120456.37, "xxxx1,120,456.37");
688        tests.number.check(t, options,112045600.37, "xx112,045,600.37");
689        tests.number.check(t, options,10252045600.37, "10,252,045,600.37");
690
691        // Test padding between a sign and a number
692        options = {pattern:"#,###,###,##0.0#*x;(###,###,##0.0#*x)",locale:locale};
693        tests.number.check(t, options, -10, "(10.0xxxxxxxxxx)");
694        tests.number.check(t, options, -1000, "(1,000.0xxxxxxx)");
695        tests.number.check(t, options, -1000000, "(1,000,000.0xxx)");
696        tests.number.check(t, options, -100.37, "(100.37xxxxxxxx)");
697        tests.number.check(t, options, -10456.37, "(10,456.37xxxxx)");
698        tests.number.check(t, options, -1120456.37, "(1,120,456.37xx)");
699        tests.number.check(t, options, -112045600.37, "(112,045,600.37)");
700        tests.number.check(t, options, -1252045600.37, "(1,252,045,600.37)");
701
702        tests.number.check(t, options, 10, "10.0xxxxxxxxxxxx");
703        tests.number.check(t, options, 1000, "1,000.0xxxxxxxxx");
704        tests.number.check(t, options, 1000000, "1,000,000.0xxxxx");
705        tests.number.check(t, options, 100.37, "100.37xxxxxxxxxx");
706        tests.number.check(t, options, 10456.37, "10,456.37xxxxxxx");
707        tests.number.check(t, options, 1120456.37, "1,120,456.37xxxx");
708        tests.number.check(t, options, 112045600.37, "112,045,600.37xx");
709        tests.number.check(t, options, 10252045600.37, "10,252,045,600.37");
710
711        //Not implemented yet,refer to NumberFormatTest.TestPatterns2()
712        //For future use - maily test pad patterns
713        print("test_number_format_Pad() end..............");
714}
715*/
716                        }
717                },
718                {
719                        name: "parse_icu4j3_6",
720                        runTest: function(t){
721/**
722 * In ICU4J, testing logic for NumberFormat.parse() is seperated into
723 * differernt single tese cases. So part of these logic are
724 * collected together in this test case. *
725 */
726        //print("test_number_parse_icu4j3_6() start..............");
727        //Refer to ICU4J's NumberFormatTest.TestParse() which is only a rudimentary version
728        var pattern = "00";
729        var str = "0.0";
730        var result = dojo.number.parse(str,{pattern:pattern, locale: 'en-us'});
731        //TODO: add more locales
732//FIXME: is this a valid test?
733//      t.is(0,result);
734
735        /**************************************** tolerant parse *****************************************
736         * refers to ICU4J's NumberFormatTest.TestStrictParse()??
737         * TODO: Seems dojo.number parses string in a tolerant way.
738         */
739         var options = {locale:"en-us"};
740        /*
741         * TODO: !!Failed case,Should all pass,
742         * but the following elements failed (all parsed to NaN):
743         * [1]-"0 ",[2]-"0.",[3]-"0,",[5]-"0. ",[6]-"0.100,5",
744         * [7]-".00",[9]-"12345, ",[10]-"1,234, ",[12]-"0E"
745         */
746        var passData = ([
747                "0",           //[0] single zero before end of text is not leading
748        //"0 ",        //[1] single zero at end of number is not leading
749        //"0.",        //[2] single zero before period (or decimal, it's ambiguous) is not leading
750        //"0,",          //[3] single zero before comma (not group separator) is not leading
751        "0.0",         //[4] single zero before decimal followed by digit is not leading
752        //"0. ",       //[5] same as above before period (or decimal) is not leading
753        //"0.100,5",   //[6] comma stops parse of decimal (no grouping)
754        //".00",       //[7] leading decimal is ok, even with zeros
755        "1234567",     //[8] group separators are not required
756        //"12345, ",   //[9] comma not followed by digit is not a group separator, but end of number
757        //"1,234, ",   //[10] if group separator is present, group sizes must be appropriate
758        "1,234,567"   //[11] ...secondary too
759        //,"0E"         //[12]not implemented yet,an exponnent not followed by zero or digits is not an exponent
760         ]);
761        runBatchParse(options,passData,true/*tolerant parse*/);
762
763        /*
764         * TODO:!!Failed case,should all pass,
765         * but the following failed,
766         * [10]-"1,45 that" implies that we partially parse input
767         */
768        var failData = ([
769// leading zeros without separators are tolerated #6933
770//              "00",          //[0] leading zero before zero
771//              "012",         //[1] leading zero before digit
772        "0,456",       //[2] leading zero before group separator
773        "1,2",         //[3] wrong number of digits after group separator
774        ",0",          //[4] leading group separator before zero
775        ",1",          //[5] leading group separator before digit
776        ",.02",        //[6] leading group separator before decimal
777        "1,.02",       //[7] group separator before decimal
778        "1,,200",      //[8] multiple group separators
779        "1,45",        //[9] wrong number of digits in primary group
780        //"1,45 that",   //[10] wrong number of digits in primary group
781        "1,45.34",     //[11] wrong number of digits in primary group
782        "1234,567",    //[12] wrong number of digits in secondary group
783        "12,34,567",   //[13] wrong number of digits in secondary group
784        "1,23,456,7890" //[14] wrong number of digits in primary and secondary groups
785                ]);
786        runBatchParse(options,failData,false);
787
788         options = {pattern:"#,##,##0.#",locale:"en-us"};
789        /*
790         * TODO:!!Failed case,shoudl all pass.
791
792         * but [1] [2] and [3] failed
793         * should be parsed to 1234567,but NaN instead
794         */
795        var mixedPassData = ([
796                "12,34,567"     //[0]
797        //,"12,34,567,"         //[1]
798        //"12,34,567, that",//[2]
799        //"12,34,567 that"      //[3]
800                ]);
801        runBatchParse(options,mixedPassData,true/*tolerant parse*/);
802
803        /*
804         * TODO:!!Failed case,should all pass,
805         * but actually mixedFailData[2] and mixedFailData[3] passed.
806         * "12,34,56, that " and [3]-"12,34,56 that" should be parsed to 123456,but NaN instead
807         */
808        var mixedFailData = ([
809        "12,34,56",                     //[0]
810        "12,34,56,"             //[1]
811        //,"12,34,56, that ",//[2]
812        //"12,34,56 that",      //[3]
813                ]);
814        runBatchParse(options,mixedFailData,false);
815
816
817        /**************************************** strict parse ******************************************
818         * TODO:May need to test strict parsing in the future?
819         * e.g. A strict parsing like (with pattern "#,##0.#")
820         * 1.Leading zeros
821         *              '00', '0123' fail the parse, but '0' and '0.001' pass
822         * 2.Leading or doubled grouping separators
823         *              ',123' and '1,,234" fail
824         * 3.Groups of incorrect length when grouping is used
825         *              '1,23' and '1234,567' fail, but '1234' passes
826         * 4.Grouping separators used in numbers followed by exponents
827         *              '1,234E5' fails, but '1234E5' and '1,234E' pass
828         */
829        //options={locale:"en",strict:true};
830        //runBatchParse(options,passData,false/*strict parse*/);
831        //runBatchParse(options,failData,false/*strict parse*/);
832
833        //options = {pattern:"#,##,##0.#",locale:"en-us",strict:true};
834        //runBatchParse(options,mixedPassData,false/*strict parse*/);
835        //runBatchParse(options,mixedFailData,false/*strict parse*/);
836
837        //print("test_number_parse_icu4j3_6() end..............\n");
838                        }
839                },
840                {
841                        name: "parse_whitespace",
842                        runTest: function(t){
843/**
844 * TODO:!!Failed case
845 * With pattern "a  b#0c  ",both "a b3456c " and and "a   b1234c   " should be parsed to 3456,but got NaN instead.
846 *
847 * Refer to ICU4J's NumberFormatTest.TestWhiteSpaceParsing
848 */
849    /*
850        print("test_number_parse_WhiteSpace() start..............");
851        var pattern = "a  b#0c  ";
852        var expectResult = 3456;
853        result =  dojo.number.parse("a b3456c ",{pattern:pattern,locale:"en-us"});
854        t.is(expectResult,result);
855        result =  dojo.number.parse("a   b3456c   ",{pattern:pattern,locale:"en-us"});
856        t.is(expectResult,result);
857        print("test_number_parse_WhiteSpace() end..............\n");
858        */
859                        }
860                },
861/*************************************************************************************************
862 *                            Regression test cases
863 * These test cases are referred to ICU4J's NumberFormatRegressionTest and NumberFormatRegression.
864 * The regression cases in ICU4J are used as unit test cases for bug fixing,
865 * They are inluced here so that dojo.number may avoid those similar bugs.
866 *************************************************************************************************/
867                {
868                        name: "number_regression_1",
869                        runTest: function(t){
870/**
871 * Refer to ICU4J's NumberFormatRegressionTest.Test4161100()
872 */
873        tests.number.checkFormatParseCycle(t, {pattern:"#0.#", locale: 'en-us'},-0.09,"-0.1",false);
874                        }
875                },
876                {
877                        name: "number_regression_2",
878                        runTest: function(t){
879/**
880 * !!Failed case,rounding hasn't been implemented yet.
881 * Refer to ICU4J's NumberFormatRegressionTest.Test4408066()
882 */
883        /*
884        var data =   ([-3.75, -2.5, -1.5,
885                   -1.25, 0,    1.0,
886                   1.25,  1.5,  2.5,
887                   3.75,  10.0, 255.5]);
888        var expected = (["-4", "-2", "-2",
889                    "-1", "0",  "1",
890                        "1",  "2",  "2",
891                        "4",  "10", "256"]);
892        var options = {locale:"zh-cn",round:true};
893        for(var i =0; i < data.length; i++){
894                tests.number.checkFormatParseCycle(t, options,data[i],expected[i],false);
895        }
896
897        data = ([       "-3.75", "-2.5", "-1.5",
898                "-1.25", "0",    "1.0",
899                "1.25",  "1.5",  "2.5",
900                "3.75",  "10.0", "255.5"]);
901        expected =([ -3, -2, -1,
902                 -1, 0,  1,
903                 1,  1,  2,
904                 3,  10, 255]);
905
906        for(var i =0; i < data.length; i++){
907                tests.number.checkParse(t, options,data[i],expected[i]);
908        }
909        */
910                        }
911                },
912                {
913                        name: "number_regression_3",
914                        runTest: function(t){
915/**
916 * Refer to ICU4J's NumberRegression.Test4087535() and Test4243108()
917 */
918        tests.number.checkFormatParseCycle(t, {places:0},0,"0",false);
919        //TODO:in icu4j,0.1 should be formatted to ".1" when minimumIntegerDigits=0
920        tests.number.checkFormatParseCycle(t, {places:0},0.1,"0",false);
921        tests.number.checkParse(t, {pattern:"#0.#####", locale: 'en-us'},123.55456,123.55456);
922//!! fails because default pattern only has 3 decimal places
923//      tests.number.checkParse(t, null,123.55456,123.55456);
924
925        //See whether it fails first format 0.0 ,parse "99.99",and then reformat 0.0
926        tests.number.checkFormatParseCycle(t, {pattern:"#.#"},0.0,"0",false);
927        tests.number.checkParse(t, {locale: 'en-us'},"99.99",99.99);
928        tests.number.checkFormatParseCycle(t, {pattern:"#.#"},0.0,"0",false);
929                        }
930                },
931                {
932                        name: "number_regression_4",
933                        runTest: function(t){
934/**
935 * TODO:
936 * In ICU -0.0 and -0.0001 should be formatted to "-0" with FieldPosition(0)
937 * dojo.i18n.number format -0.0 to "-0"; -0.0001 to "-0.000100"
938 *
939 * Refer to ICU4J's NumberRegression.Test4088503() and Test4106658()
940 */
941        tests.number.checkFormatParseCycle(t, {places:0},123,"123",false);
942
943        //TODO: differernt from ICU where -0.0 is formatted to "-0"
944        tests.number.checkFormatParseCycle(t, {locale:"en-us"},-0.0,"0",false);
945
946        //TODO: differernt from ICU where -0.0001 is formatted to "-0"
947        tests.number.checkFormatParseCycle(t, {locale:"en-us",places:6},-0.0001,"-0.000100",false);
948                        }
949                },
950                {
951                        name: "number_regression_5",
952                        runTest: function(t){
953/**
954 * !!Failed case,rounding has not implemented yet.
955 * 0.00159999 should be formatted as 0.0016 but got 0.0015 instead.
956 * Refer to ICU4J's NumberRegression.Test4071492()
957 */
958        //tests.number.checkFormatParseCycle(t, {places:4,round:true},0.00159999,"0.0016",false);
959                        }
960                },
961                {
962                        name: "number_regression_6",
963                        runTest: function(t){
964/**
965 * Refer to ICU4J's NumberRegression.Test4086575()
966 */
967        var pattern = "###.00;(###.00)";
968        var locale = "fr";
969        var options = {pattern:pattern,locale:locale};
970
971        //no group separator
972        tests.number.checkFormatParseCycle(t, options,1234,"1234,00",false);
973        tests.number.checkFormatParseCycle(t, options,-1234,"(1234,00)",false);
974
975        //space as group separator
976        pattern = "#,###.00;(#,###.00)";
977        options = {pattern:pattern,locale:locale};
978        tests.number.checkFormatParseCycle(t, options,1234,"1\u00a0234,00",false);// Expect 1 234,00
979        tests.number.checkFormatParseCycle(t, options,-1234,"(1\u00a0234,00)",false);  // Expect (1 234,00)
980                        }
981                },
982                {
983                        name: "number_regression_7",
984                        runTest: function(t){
985/**
986 * !!Failed case - expontent has not implemented yet
987 * shuold format 1.000000000000001E7 to 10000000.00000001, but got 10,000,000.000 instead
988 * Refer to ICU4J's NumberRegression.Test4090489() - loses precision
989 */
990        //tests.number.checkFormatParseCycle(t, null,1.000000000000001E7,"10000000.00000001",false);
991                        }
992                },
993                {
994                        name: "number_regression_8",
995                        runTest: function(t){
996/**
997 * !!Failed case
998 * 1.with pattern "#,#00.00 p''ieces;-#,#00.00 p''ieces"
999 *   3456.78 should be formated to "3,456.78 p'ieces",
1000 *   but got "3,456.78 p''ieces","''" should be replaced with "'"
1001 * 2.with illegal pattern "000.0#0"
1002 *       no error for the illegal pattern, and 3456.78 is formatted to 456.780
1003 * 3.with illegal pattern "0#0.000"
1004 *       no error for the illegal pattern, and 3456.78 is formatted to 3456.780
1005 *
1006 * Refer to ICU4J's NumberRegression.Test4092480(),Test4074454()
1007 */
1008        var patterns = (["#0000","#000","#00","#0","#"]);
1009        var expect = (["0042","042","42","42","42"]);
1010
1011        for(var i =0; i < patterns.length; i ++){
1012                tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},42,expect[i],false);
1013                tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},-42,"-"+expect[i],false);
1014        }
1015
1016        tests.number.checkFormatParseCycle(t, {pattern:"#,#00.00;-#.#", locale: 'en-us'},3456.78,"3,456.78",false);
1017        //!!Failed case
1018        //tests.number.checkFormatParseCycle(t, {pattern:"#,#00.00 p''ieces;-#,#00.00 p''ieces"},3456.78,"3,456.78 p'ieces",false);
1019        //tests.number.checkFormatParseCycle(t, {pattern:"000.0#0"},3456.78,null,false);
1020        //tests.number.checkFormatParseCycle(t, {pattern:"0#0.000"},3456.78,null,false);
1021                        }
1022                },
1023                {
1024                        name: "number_regression_9",
1025                        runTest: function(t){
1026/**
1027 * TODO
1028 * Refer to ICU4J's NumberRegression.Test4052223()
1029 */
1030        //TODO:only got NaN,need an illegal pattern exception?
1031        tests.number.checkParse(t, {pattern:"#,#00.00"},"abc3");
1032
1033        //TODO: got NaN instead of 1.222, is it ok?
1034        //tests.number.checkParse(t, {pattern:"#,##0.###",locale:"en-us"},"1.222,111",1.222);
1035        //tests.number.checkParse(t, {pattern:"#,##0.###",locale:"en-us"},"1.222x111",1.222);
1036
1037        //got NaN for illeal input,ok
1038        tests.number.checkParse(t, null,"hello: ,.#$@^&**10x");
1039                        }
1040                },
1041                {
1042                        name: "number_regression_10",
1043                        runTest: function(t){
1044/**
1045 * Refer to ICU4J's NumberRegression.Test4125885()
1046 */
1047        tests.number.checkFormatParseCycle(t, {pattern:"000.00", locale: 'en-us'},12.34,"012.34",false);
1048        tests.number.checkFormatParseCycle(t, {pattern:"+000.00%;-000.00%", locale: 'en-us'},0.1234,"+012.34%",false);
1049        tests.number.checkFormatParseCycle(t, {pattern:"##,###,###.00", locale: 'en-us'},9.02,"9.02",false);
1050
1051        var patterns =(["#.00", "0.00", "00.00", "#0.0#", "#0.00"]);
1052        var expect =  (["1.20", "1.20", "01.20", "1.2",   "1.20" ]);
1053        for(var i =0 ; i < patterns.length; i ++){
1054                tests.number.checkFormatParseCycle(t, {pattern:patterns[i], locale: 'en-us'},1.2,expect[i],false);
1055        }
1056                        }
1057                },
1058                {
1059                        name: "number_regression_11",
1060                        runTest: function(t){
1061/**
1062 * TODO:!!Failed case
1063 * Make sure that all special characters, when quoted in a suffix or prefix, lose their special meaning.
1064 * The detail error info :
1065 * for input 123
1066 * pattern:'0'#0'0'; expect:"01230"; but got "'3'#0'0'" instead
1067 * pattern:','#0','; expect:",123,"; but got "','123','" instead
1068 * pattern:'.'#0'.'; expect:".123."; but got "'.'123'.'" instead
1069 * pattern:'‰'#0'‰'; expect:"‰123‰"; but got "'‰'123000'‰'" instead
1070 * pattern:'%'#0'%'; expect:"%123%"; but got "'%'12300'%'" instead
1071 * pattern:'#'#0'#'; expect:"#123#"; but got "'123'#0'#'" instead
1072 * pattern:';'#0';'; expect:";123;"; but got "[dojo-test] FATAL exception raised:
1073 *                                                                                        unable to find a number expression in pattern: '"
1074 * pattern:'E'#0'E'; expect:"E123E"; not implemeted yet
1075 * pattern:'*'#0'*'; expect:"*123*"; but got "'*'123'*'" instead
1076 * pattern:'+'#0'+'; expect:"+123+"; but got "'+'123'+'" instead
1077 * pattern:'-'#0'-'; expect:"-123-"; but got "'-'123'-'" instead
1078 *
1079 * TODO: is it ok to remain "'" in the formatted result as above??
1080 *
1081 * Refer to ICU4J's NumberRegression.Test4212072()
1082 */
1083/*
1084        var specials = ([ '0', ',', '.', '\u2030', '%', '#',';', 'E', '*', '+', '-']);
1085        var pattern;
1086        var expect;
1087
1088        for(var i=0; i < specials.length; i ++){
1089                pattern = "'" + specials[i] + "'#0'" + specials[i] + "'";
1090                expect = "" +  specials[i] + "123" +  specials[i];
1091                tests.number.checkFormatParseCycle(t, {pattern:pattern,locale:"en-us"},123,expect,false);
1092        }
1093*/
1094                        }
1095                },
1096                {
1097                        name: "number_regression_12",
1098                        runTest: function(t){
1099/**
1100 * TODO: add more rounding test cases, refer to ICU4J's NumberRegression.Test4071005(),Test4071014() etc..
1101 */
1102
1103/**
1104 * TODO:Decimal format doesnt round a double properly when the number is less than 1
1105 *
1106 * Refer to ICU4J's NumberRegression.test4241880()
1107 */
1108/*
1109        var input = ([ .019, .009, .015, .016, .014,
1110                        .004, .005, .006, .007, .008,
1111                        .5, 1.5, .05, .15, .005,
1112                        .015, .0005, .0015]);
1113        var patterns = (["##0%", "##0%", "##0%", "##0%", "##0%",
1114                         "##0%", "##0%", "##0%", "##0%", "##0%",
1115                         "#,##0", "#,##0", "#,##0.0", "#,##0.0", "#,##0.00",
1116                         "#,##0.00", "#,##0.000", "#,##0.000"]);
1117        var expect =([   "2%", "1%", "2%", "2%", "1%",
1118                         "0%", "0%", "1%", "1%", "1%",
1119                         "0", "2", "0.0", "0.2", "0.00",
1120                         "0.02", "0.000", "0.002"]);
1121        for(var i = 0; i <input.length; i ++){
1122                tests.number.checkFormatParseCycle(t, {pattern:patterns[i],round:true},input[i],expect[i],false);
1123        }
1124*/
1125                        }
1126                }
1127        ]
1128);
1129});
Note: See TracBrowser for help on using the repository browser.