source: Dev/trunk/src/client/dojox/calc/tests/test_Executor.html

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 9.8 KB
Line 
1<!DOCTYPE html>
2<html>
3<head>
4        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
5        <title>Calculator Executor Test</title>
6
7        <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug:true, parseOnLoad: true"></script>
8
9        <script type="text/javascript">
10        var executor;
11        var waitForLoad;
12        function executorLoaded(){
13                executor = dijit.byId('executor');
14                waitForLoad.callback(true);
15        }
16        require(["dojox/calc/_Executor", "doh/runner", "dojo/number", "dojo/ready", "dojo/parser", "dojox/math/_base"],
17        function(calc, runner, number, ready, parser, math){
18
19                waitForLoad = new doh.Deferred();
20
21                function test(text, ans){
22                        if (ans!=true&&ans!=false)
23                                ans = calc.approx(ans);
24                        console.log("text is "+text);
25                        doh.is(ans, calc.approx(executor.eval(text)), text);
26                }
27                var status = "Error(s) on page";
28                dojo.ready(function(){
29                        function allTests(){
30                                dojo.byId("status").innerHTML = "Testing...";
31                                func = executor.Function("fcn1", "i,j","k=i+j+1;return k;");
32
33                                executor.Function("prompt", "", "       return (arguments[2]=='j') ? 6 : NaN    ");
34                                test("fcn1(5)", 12);
35                                test(func(4), 11);
36                                executor.Function("fcn2", "i","return j=fcn1(k,i);");
37                                test("fcn2(i=fcn1(6,7))", 29);
38                                test("j+k", 58);
39
40                                // number conversion tests
41                                test("toRadix(5,2)", "0b101");
42                                test("toRadix(toRadix(5,2), 10)", 5);
43                                test("toRadix(toRadix(5,3), 10)", 5);
44                                test("toRadix(0xf, 16)", "0xf");
45
46                                test("toRadix(0xf, 15)", "10#15");
47                                test("toRadix(0b1111, 15)", "10#15");
48                                test("toRadix(0o17, 15)", "10#15");
49                                test("toRadix(10#15, 16)", "0xf");
50                                test("toRadix(10#15, 2)", "0b1111");
51                                test("toRadix(10#15, 8)", "0o17");
52
53                                test("toRadix(0xf, 10)", "15");
54                                test("toRadix(0xf, 2)", "0b1111");
55                                test("toRadix(0b1111, 16)", "0xf");
56                                test("toRadix(0o111, 16)", "0x49");
57                               
58                                // unary tests
59                                test("0--2", 2);
60                                test("0 --2", 2);
61                                test("0 - -2", 2);
62                                test("0 - - 2", 2);
63                                test("0- -2", 2);
64                                test("0- - 2", 2);
65                                test("0 -  -  2", 2);
66                                test("0  --2", 2);
67                                test("0  - - 2", 2);
68
69                                // pi tests
70                                // some browsers get 8105800789910702, some get 8105800789910700
71                                test("Math.abs(pi^32-8105800789910701) <= 1", true);
72                                test("32\u221A8105800789910700", Math.PI);
73                                test("64\u221A65704006445717084572022626334541", Math.PI);
74
75                                // new pow tests
76                                test(" (-27)^(1/3) ", -3);
77                                test(" (-36)^(1/2) ", NaN);
78
79                                test("y=true", true);
80                                test("y=!y", false);
81                                test("y=(true||!true)", true);
82                                test("x=5", 5);
83                                test("x", 5);
84                                test("x++", 5);
85                                test("x", 6);
86                                               
87                                test("x--", 6);
88                                test("x", 5);
89                                test("x==5", true);
90                                test("5+x++", 10);
91
92                                test("x", 6);
93
94                                test("x=5", 5);
95                                test("y=(x!=6)", true);// x != 6
96                                test("x=3", 3);
97                                test("y=(x!!=6)", false);// factorial(x) != 6
98                                test("5", 5);
99                                test("3!", 6);
100                                test("\u221A(3!)", Math.sqrt(math.factorial(3))); // 2.4494897427831780981972840747059);
101                                // factorial goes first, 2^3! = 2^6
102                                test("\u221A3!", Math.sqrt(math.factorial(3))); // 2.4494897427831780981972840747059);
103                                test("\u221A(3!^2)", 6);
104                                test("3\u221A(3!^3)", 6);
105                                test("4\u221A(4^4)", 4);
106                                test("(4^(1/2))\u221A(4^2)", 4);
107                                test("(4^(1/2))\u221A4^2", 4);
108                                test("4^(1/2)\u221A(4^2)", 4);
109                                test("4^(1/2)\u221A4^2", 4);
110
111                                test("2^Math.sin(\u03C0)", 1);
112                                test("\u221A\u221A"+"16", 2);
113                                test("Math.sqrt(\u221A16, 2)", 2);
114                                test("\u221AMath.sqrt(16, 2)", 2);
115
116                                // New Radical tests
117                                test("\u221A2\u221A4\u221A16", 2);
118                                test("2\u221A4\u221A16", 4);
119                                test("2^2\u221A16", 2);
120                                test("2^-3^-4", Math.pow(2,(-Math.pow(3,(-4)))));
121                                test("2^-2\u221A4", .25);
122
123
124                                test("2^Math.sin((\u03C0))", 1);
125                                test("2^Math.sin(3!!)", calc.pow(2, Math.sin(math.factorial(math.factorial(3)))));
126                                test("2^Math.sin((3!)!)", calc.pow(2, Math.sin(math.factorial(math.factorial(3)))));
127                                test("2^Math.sin((3!+3!)-1)", calc.pow(2, Math.sin((math.factorial(3)+math.factorial(3))-1)));
128                                test("2^\u221A-Math.sin(3!+3!-1)", calc.pow(2, Math.sqrt(-Math.sin(math.factorial(3)+math.factorial(3)-1))));
129                                test("2^-\u221A+4", 1/4);
130                                test("2^-2\u221A+4", 1/4);
131                                test("2^(-2)\u221A+4", 256);
132                                test("2^2\u221A+4", Math.sqrt(2));
133                                test("2^\u221A+4", 4);
134                                test("2^-(-2)\u221A+4", calc.pow(2, (-1/2)));
135                                test("2^--2\u221A+4", 4);
136                                test("-\u221A4", -2);
137                                test("2^-2", 1/4);
138                                test("--2", 2);
139                                test("pow(2^2, 2^2)", 256)
140                                // extreme negative sign scenarios
141                                test("2^--\u221A--4", 4);
142                                test("---\u221A--4", -2);
143                                test("---\u221A4", -2);
144                                test("---2*(3-5)+1", 5);
145                                test("---(2*(3-5))+1", 5);
146                                // floating poMath.floor tests
147                                test("--4.56443*2", 4.56443*2);
148                                test("\u221A--\u221A4.56443*2", Math.sqrt(Math.sqrt(4.56443))*2);//2.923321855);
149                                test("\u221A--\u221A4.56443!*2", Math.sqrt(Math.sqrt(math.factorial(4.56443)))*2);
150                                test("--4.56443*2-4.56443", 4.56443);
151                                // more arithmetic
152                                test("4*2-(5+2*3-2)*6/-2", 35);
153                                test("Math.floor(2^3^4)+Math.floor(2^Math.floor(2))", 4100);
154                                // scientific notation
155                                test("10e10", 10e10);
156                                test("10e-10", 10e-10);
157                                test("10e+10", 10e10);
158                                test("10e+10*2", 10e10*2);
159                                test("2*10e+10*2", 2*10e10*2);
160                                test("2*10e10*2", 2*10e10*2);
161                                // with \u221A (radicals)
162                                test("\u221A10e10*2", Math.sqrt(10e10)*2);
163                                test("\u221A10e-10*2", Math.sqrt(10e-10)*2);
164                                test("5\u221A10e-10*2", calc.pow(10e-10, 1/5)*2);
165                                test("5\u221A10e-10*2+5\u221A10e-10*2", calc.pow(10e-10, 1/5)*2*2);
166
167                                test("2^3!\u221A3", calc.pow(3, 1/calc.pow(2, 6)));
168                                test("2^-3!\u221A3", calc.pow(2, -calc.pow(3, 1/math.factorial(3))));
169                                test("2^(-3!)\u221A3", calc.pow(3, 1/calc.pow(2, -math.factorial(3))));
170
171                                // full space tests
172                                test(" x = 5 ", 5);
173                                test(" x ", 5);
174                                test(" x ++ ", 5);
175                                test(" x ", 6);
176                                test(" x -- ", 6);
177                                test(" x ", 5);
178                                test(" x == 5 ", true);
179                                test(" 5 ", 5);
180                                test(" 3 !", 6);
181                                test(" \u221A ( 3 ! )", Math.sqrt(math.factorial(3))); //2.4494897427831780981972840747059);
182                                // factorial goes first, 2^3! = 2^6
183                                test(" \u221A 3 !", Math.sqrt(math.factorial(3))); //2.4494897427831780981972840747059);
184                                test(" \u221A ( 3 ! ^ 2 ) ", 6);
185                                test(" 3 \u221A ( 3 ! ^ 3 ) ", 6);
186                                test(" 4 \u221A ( 4 ^ 4 ) ", 4);
187                                test(" ( 4 ^ ( 1 / 2 ) ) \u221A ( 4 ^ 2 ) ", 4);
188                                test(" ( 4 ^ ( 1 / 2 ) ) \u221A 4 ^ 2 ", 4);
189                                test(" 4 ^ ( 1 / 2 ) \u221A ( 4 ^ 2 ) ", 4);
190                                test(" 4 ^ ( 1 / 2 ) \u221A 4 ^ 2 ", 4);
191
192                                test(" 2 ^ Math.sin ( \u03C0 ) ", 1);
193                                test(" \u221A \u221A 16 ", 2);
194                                test(" Math.sqrt ( \u221A 16 , 2 )", 2);
195                                test(" \u221A Math.sqrt ( 16 , 2 ) ", 2);
196
197                                test(" 2 ^ Math.sin ( ( \u03C0 ) ) ", 1);
198                                test(" 2 ^ Math.sin ( 3 ! ! ) ", calc.pow(2, Math.sin(math.factorial(math.factorial(3)))));
199                                test(" 2 ^ Math.sin( ( 3 ! ) ! ) ", calc.pow(2, Math.sin(math.factorial(math.factorial(3)))));
200                                test(" 2 ^ Math.sin ( ( 3 ! + 3 ! ) - 1 ) ", calc.pow(2, Math.sin((math.factorial(3)+math.factorial(3))-1)));
201                                test(" 2 ^ \u221A - Math.sin ( 3 ! + 3 ! - 1 ) ", calc.pow(2, Math.sqrt(-Math.sin(math.factorial(3)+math.factorial(3)-1))));
202                                test(" 2 ^ - \u221A + 4 ", 1/4);
203                                test(" 2 ^ - 2 \u221A + 4 ", 1/4);
204                                test(" 2 ^ ( - 2 ) \u221A + 4 ", 256);
205                                test(" 2 ^ 2 \u221A + 4 ", Math.sqrt(2));
206                                test(" 2 ^ \u221A + 4 ", 4);
207                                test(" 2 ^ - ( - 2 ) \u221A + 4 ", calc.pow(2, (-1/2)));
208                                test(" 2 ^ - - 2 \u221A + 4 ", 4);
209                                test(" - \u221A 4 " , -2);
210                                test(" 2 ^ - 2 ", 1/4);
211                                test(" - - 2 ", 2);
212                                test("pow( 2 ^ 2 ,  2 ^ 2)", 256)
213
214                                // extreme negative sign scenarios
215                                test(" 2 ^ - - \u221A - - 4 ", 4);
216                                test(" - - - \u221A - - 4 ", -2);
217                                test(" - - - \u221A 4 ", -2);
218                                test(" - - - 2 * ( 3 - 5 ) + 1 ", 5);
219                                test(" - - - ( 2 * ( 3 - 5 ) ) + 1 ", 5);
220                                // floating point tests
221                                test(" - - 4.56443 * 2 ", 4.56443*2);
222                                test(" \u221A - - \u221A 4.56443 * 2 ", Math.sqrt(Math.sqrt(4.56443))*2);
223                                test(" \u221A - - \u221A 4.56443 ! * 2 ", Math.sqrt(Math.sqrt(math.factorial(4.56443)))*2);
224                                test(" - - 4.56443 * 2 - 4.56443 ", 4.56443);
225                                // more arithmetic
226                                test(" 4 * 2 - ( 5 + 2 * 3 - 2 ) * 6 / - 2 ", 35);
227                                test("Math.floor( 2 ^ 3 ^ 4 )+Math.floor( 2 ^ Math.floor ( 2 ) ) ", 4100);
228                                // scientific notation
229                                test(" 2 * 10e+10 * 2 ", 2*10e10*2);
230                                test(" 2 * 10e10 * 2 ", 2*10e10*2);
231                                // with \u221A (radicals)
232                                test(" \u221A 10e10 * 2 ", Math.sqrt(10e10)*2);
233                                test(" \u221A 10e-10 * 2 ", Math.sqrt(10e-10)*2);
234                                test(" 5 \u221A 10e-10 * 2 ", calc.pow(10e-10, 1/5)*2);
235                                test(" 5 \u221A 10e-10 * 2 + 5 \u221A 10e-10 * 2 ", calc.pow(10e-10, 1/5)*2*2);
236
237                                test(" 2 ^ 3 ! \u221A 3 ", calc.pow(3, 1/calc.pow(2, 6)));
238                                test(" 2 ^ - 3 ! \u221A 3 ", calc.pow(2, -calc.pow(3, 1/math.factorial(3))));
239                                test(" 2 ^ ( - 3 ! ) \u221A 3 ", calc.pow(3, 1/calc.pow(2, -math.factorial(3))));
240
241                                test("3!=6", true);
242                                test("3!==6", true);
243
244                                // try to break it on purpose; once upon a time, some might have caused infinite loops
245                                test("2*2.4.5", NaN);
246
247                                test("2^^3", NaN);
248                                test("2^^^^^^3", NaN);
249                                test("2^^^^^^^3", NaN);
250                                test("^2^3", NaN);
251                                test("!2^3", false);
252                                test("!2!+3!", 6);
253                                test("2+3\u221A", NaN);
254                                test("2+3\u221A^", NaN);
255                                test("!", NaN);
256                                test("((23+4)", NaN);                                   
257                                test("4+", NaN);
258                                test("4++", NaN);
259
260                                status = "Completed without errors";
261                        }
262                        doh.register("calc._Executor", [
263                                {
264                                        name: "wait for Executor to load",
265                                        timeout: 5000,
266                                        runTest: function(){ if(!executor){ return waitForLoad; }}
267                                },
268                                {
269                                        name: "Executor tests",
270                                        timeout: 9000,
271                                        runTest: function(){ allTests() }
272
273                                },
274                                function done(){
275                                        dojo.byId("status").innerHTML = status;
276                                }
277                        ]);
278                        doh.run();
279                });
280        });
281        </script>
282</head>
283<body>
284        <b>dojox.calc._Executor tests</b>
285        <div id="status" style="direction:ltr">Loading...</div>
286        <span id="executor" data-dojo-type="dojox.calc._Executor" data-dojo-props='onLoad:function(){ executorLoaded() }'></span>
287</body>
288</html>
Note: See TracBrowser for help on using the repository browser.