]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* ***** BEGIN LICENSE BLOCK ***** |
2 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 | |
3 | * | |
4 | * The contents of this file are subject to the Netscape Public License | |
5 | * Version 1.1 (the "License"); you may not use this file except in | |
6 | * compliance with the License. You may obtain a copy of the License at | |
7 | * http://www.mozilla.org/NPL/ | |
8 | * | |
9 | * Software distributed under the License is distributed on an "AS IS" basis, | |
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
11 | * for the specific language governing rights and limitations under the | |
12 | * License. | |
13 | * | |
14 | * The Original Code is JavaScript Engine testing utilities. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Netscape Communications Corp. | |
17 | * Portions created by the Initial Developer are Copyright (C) 2003 | |
18 | * the Initial Developer. All Rights Reserved. | |
19 | * | |
20 | * Contributor(s): bzbarsky@mit.edu, pschwartau@netscape.com | |
21 | * | |
22 | * Alternatively, the contents of this file may be used under the terms of | |
23 | * either the GNU General Public License Version 2 or later (the "GPL"), or | |
24 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
25 | * in which case the provisions of the GPL or the LGPL are applicable instead | |
26 | * of those above. If you wish to allow use of your version of this file only | |
27 | * under the terms of either the GPL or the LGPL, and not to allow others to | |
28 | * use your version of this file under the terms of the NPL, indicate your | |
29 | * decision by deleting the provisions above and replace them with the notice | |
30 | * and other provisions required by the GPL or the LGPL. If you do not delete | |
31 | * the provisions above, a recipient may use your version of this file under | |
32 | * the terms of any one of the NPL, the GPL or the LGPL. | |
33 | * | |
34 | * ***** END LICENSE BLOCK ***** | |
35 | * | |
36 | * | |
37 | * Date: 14 Mar 2003 | |
38 | * SUMMARY: Testing left-associativity of the + operator | |
39 | * | |
40 | * See ECMA-262 Ed.3, Section 11.6.1, "The Addition operator" | |
41 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=196290 | |
42 | * | |
43 | * The upshot: |a + b + c| should always equal |(a + b) + c| | |
44 | * | |
45 | */ | |
46 | //----------------------------------------------------------------------------- | |
47 | var UBound = 0; | |
48 | var bug = 196290; | |
49 | var summary = 'Testing left-associativity of the + operator'; | |
50 | var status = ''; | |
51 | var statusitems = []; | |
52 | var actual = ''; | |
53 | var actualvalues = []; | |
54 | var expect= ''; | |
55 | var expectedvalues = []; | |
56 | ||
57 | ||
58 | status = inSection(1); | |
59 | actual = 1 + 1 + 'px'; | |
60 | expect = '2px'; | |
61 | addThis(); | |
62 | ||
63 | status = inSection(2); | |
64 | actual = 'px' + 1 + 1; | |
65 | expect = 'px11'; | |
66 | addThis(); | |
67 | ||
68 | status = inSection(3); | |
69 | actual = 1 + 1 + 1 + 'px'; | |
70 | expect = '3px'; | |
71 | addThis(); | |
72 | ||
73 | status = inSection(4); | |
74 | actual = 1 + 1 + 'a' + 1 + 1 + 'b'; | |
75 | expect = '2a11b'; | |
76 | addThis(); | |
77 | ||
78 | /* | |
79 | * The next sections test the + operator via eval() | |
80 | */ | |
81 | status = inSection(5); | |
82 | actual = sumThese(1, 1, 'a', 1, 1, 'b'); | |
83 | expect = '2a11b'; | |
84 | addThis(); | |
85 | ||
86 | status = inSection(6); | |
87 | actual = sumThese(new Number(1), new Number(1), 'a'); | |
88 | expect = '2a'; | |
89 | addThis(); | |
90 | ||
91 | status = inSection(7); | |
92 | actual = sumThese('a', new Number(1), new Number(1)); | |
93 | expect = 'a11'; | |
94 | addThis(); | |
95 | ||
96 | ||
97 | ||
98 | //----------------------------------------------------------------------------- | |
99 | test(); | |
100 | //----------------------------------------------------------------------------- | |
101 | ||
102 | ||
103 | ||
104 | function addThis() | |
105 | { | |
106 | statusitems[UBound] = status; | |
107 | actualvalues[UBound] = actual; | |
108 | expectedvalues[UBound] = expect; | |
109 | UBound++; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Applies the + operator to the provided arguments via eval(). | |
114 | * | |
115 | * Form an eval string of the form 'arg1 + arg2 + arg3', but | |
116 | * remember to add double-quotes inside the eval string around | |
117 | * any argument that is of string type. For example, suppose the | |
118 | * arguments were 11, 'a', 22. Then the eval string should be | |
119 | * | |
120 | * arg1 + quoteThis(arg2) + arg3 | |
121 | * | |
122 | * If we didn't put double-quotes around the string argument, | |
123 | * we'd get this for an eval string: | |
124 | * | |
125 | * '11 + a + 22' | |
126 | * | |
127 | * If we eval() this, we get 'ReferenceError: a is not defined'. | |
128 | * With proper quoting, we get eval('11 + "a" + 22') as desired. | |
129 | */ | |
130 | function sumThese() | |
131 | { | |
132 | var sEval = ''; | |
133 | var arg; | |
134 | var i; | |
135 | ||
136 | var L = arguments.length; | |
137 | for (i=0; i<L; i++) | |
138 | { | |
139 | arg = arguments[i]; | |
140 | if (typeof arg === 'string') | |
141 | arg = quoteThis(arg); | |
142 | ||
143 | if (i < L-1) | |
144 | sEval += arg + ' + '; | |
145 | else | |
146 | sEval += arg; | |
147 | } | |
148 | ||
149 | return eval(sEval); | |
150 | } | |
151 | ||
152 | ||
153 | function quoteThis(x) | |
154 | { | |
155 | return '"' + x + '"'; | |
156 | } | |
157 | ||
158 | ||
159 | function test() | |
160 | { | |
161 | enterFunc('test'); | |
162 | printBugNumber(bug); | |
163 | printStatus(summary); | |
164 | ||
165 | for (var i=0; i<UBound; i++) | |
166 | { | |
167 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); | |
168 | } | |
169 | ||
170 | exitFunc ('test'); | |
171 | } |