blob: 0963ef775555f9b55f7bd2279dec285e53df3ba0 [file] [log] [blame]
The Android Open Source Project9364f222008-10-21 07:00:00 -07001/* ***** 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//-----------------------------------------------------------------------------
47var UBound = 0;
48var bug = 196290;
49var summary = 'Testing left-associativity of the + operator';
50var status = '';
51var statusitems = [];
52var actual = '';
53var actualvalues = [];
54var expect= '';
55var expectedvalues = [];
56
57
58status = inSection(1);
59actual = 1 + 1 + 'px';
60expect = '2px';
61addThis();
62
63status = inSection(2);
64actual = 'px' + 1 + 1;
65expect = 'px11';
66addThis();
67
68status = inSection(3);
69actual = 1 + 1 + 1 + 'px';
70expect = '3px';
71addThis();
72
73status = inSection(4);
74actual = 1 + 1 + 'a' + 1 + 1 + 'b';
75expect = '2a11b';
76addThis();
77
78/*
79 * The next sections test the + operator via eval()
80 */
81status = inSection(5);
82actual = sumThese(1, 1, 'a', 1, 1, 'b');
83expect = '2a11b';
84addThis();
85
86status = inSection(6);
87actual = sumThese(new Number(1), new Number(1), 'a');
88expect = '2a';
89addThis();
90
91status = inSection(7);
92actual = sumThese('a', new Number(1), new Number(1));
93expect = 'a11';
94addThis();
95
96
97
98//-----------------------------------------------------------------------------
99test();
100//-----------------------------------------------------------------------------
101
102
103
104function 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 */
130function 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
153function quoteThis(x)
154{
155 return '"' + x + '"';
156}
157
158
159function 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}