Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_7 / block / order-of-operation.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Jeff Walden <jwalden+code@mit.edu>.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 //-----------------------------------------------------------------------------
39 var BUGNUMBER     = "(none)";
40 var summary = "Test let and order of operation issues";
41 var actual, expect;
42
43 printBugNumber(BUGNUMBER);
44 printStatus(summary);
45
46 /**************
47  * BEGIN TEST *
48  **************/
49
50 var failed = false;
51
52 function f1(x)
53 {
54   let (foo) {
55     // scope of lhs x includes rhs, so x is NaN here -- bug 344952
56     let x = ++x;
57     return x;
58   }
59 }
60
61 function f2(x)
62 {
63   let (foo)
64   {
65     // scope of lhs x includes rhs, so x is NaN here -- bug 344952
66     let x = x++;
67     return x;
68   }
69 }
70
71 function f3(x)
72 {
73   let (foo)
74   {
75     var q = x;
76     let (x = x++)
77     {
78       if (x != q)
79         throw "f3():\n" +
80           "  expected: x == q\n" +
81           "  actual:   x != q " +
82           "(where x == " + x + ", q == " + q + ")\n";
83     }
84     return x;
85   }
86 }
87
88 function f4(x)
89 {
90   var y = 7;
91   let (y = x, x = 3)
92   {
93     var q = 7 + x;
94   }
95   return x + y + q;
96 }
97
98 function f5(x)
99 {
100   var q = x++;
101   let (y = x, r = 17, m = 32) {
102     return function(code)
103     {
104       return eval(code);
105     };
106   }
107 }
108
109 function f6() {
110   let (foo)
111   {
112     var i=3;
113     for (let i=i;;) { if (i != 3) throw "f6(): fail 1"; i = 7; break; }
114     if (i != 3) throw "f6(): fail 2";
115   }
116 }
117
118 try
119 {
120   var rv = f1(5);
121   if (!isNaN(rv))
122     throw "f1(5):\n" +
123       "  expected:  NaN\n" +
124       "  actual:    " + rv;
125
126   rv = f2(5);
127   if (!isNaN(rv))
128     throw "f2(5):\n" +
129       "  expected:  NaN\n" +
130       "  actual:    " + rv;
131 /*
132   rv = f3(8);
133   if (rv != 9)
134   throw "f3(8):\n" +
135   "  expected:  9\n" +
136   "  actual:    " + rv;
137 */
138
139   rv = f4(13);
140   if (rv != 30)
141     throw "f4(13):\n" +
142       "  expected:  30\n" +
143       "  actual:    " + rv;
144
145   var fun = f5(2);
146
147   rv = fun("q");
148   if (rv != 2)
149     throw "fun('q'):\n" +
150       "  expected:  2\n" +
151       "  actual:    " + rv;
152
153   rv = fun("x");
154   if (rv != 3)
155     throw "fun('x'):\n" +
156       "  expected:  3\n" +
157       "  actual:    " + rv;
158
159   rv = fun("y");
160   if (rv != 3)
161     throw "fun('y'):\n" +
162       "  expected:  3\n" +
163       "  actual:    " + rv;
164
165   rv = fun("let (y = y) { y += 32; }; y");
166   if (rv != 3)
167     throw "fun('let (y = y) { y += 32; }; y'):\n" +
168       "  expected:  3\n" +
169       "  actual:    " + rv;
170
171 /*
172   f6();
173 */
174 }
175 catch (e)
176 {
177   print(e.toSource());
178   failed = e;
179 }
180
181 expect = false;
182 actual = failed;
183
184 reportCompare(expect, actual, summary);