• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
5 *
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
10 *
11 * The Original Code is Mozilla Communicator client code, released March
12 * 31, 1998.
13 *
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22/**
23    File Name:          11.6.1-1.js
24    ECMA Section:       11.6.1 The addition operator ( + )
25    Description:
26
27    The addition operator either performs string concatenation or numeric
28    addition.
29
30    The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression
31    is evaluated as follows:
32
33    1.  Evaluate AdditiveExpression.
34    2.  Call GetValue(Result(1)).
35    3.  Evaluate MultiplicativeExpression.
36    4.  Call GetValue(Result(3)).
37    5.  Call ToPrimitive(Result(2)).
38    6.  Call ToPrimitive(Result(4)).
39    7.  If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12.
40        (Note that this step differs from step 3 in the algorithm for comparison
41        for the relational operators in using or instead of and.)
42    8.  Call ToNumber(Result(5)).
43    9.  Call ToNumber(Result(6)).
44    10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3).
45    11. Return Result(10).
46    12. Call ToString(Result(5)).
47    13. Call ToString(Result(6)).
48    14. Concatenate Result(12) followed by Result(13).
49    15. Return Result(14).
50
51    Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6.
52    All native ECMAScript objects except Date objects handle the absence of a
53    hint as if the hint Number were given; Date objects handle the absence of a
54    hint as if the hint String were given. Host objects may handle the absence
55    of a hint in some other manner.
56
57    This test does not cover cases where the Additive or Mulplicative expression
58    ToPrimitive is string.
59
60    Author:             christine@netscape.com
61    Date:               12 november 1997
62*/
63    var SECTION = "11.6.1-1";
64    var VERSION = "ECMA_1";
65    startTest();
66
67    var testcases = getTestCases();
68
69    writeHeaderToLog( SECTION + " The Addition operator ( + )");
70    test();
71
72function test() {
73    for ( tc=0; tc < testcases.length; tc++ ) {
74        testcases[tc].passed = writeTestCaseResult(
75                            testcases[tc].expect,
76                            testcases[tc].actual,
77                            testcases[tc].description +" = "+
78                            testcases[tc].actual );
79
80        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
81    }
82    stopTest();
83    return ( testcases );
84}
85
86function getTestCases() {
87    var array = new Array();
88    var item = 0;
89
90    // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is
91    // a boolean primitive and a boolean object, and "MyValuelessObject", where the value is
92    // set in the object's prototype, not the object itself.
93
94    array[item++] = new TestCase(   SECTION,
95                                    "var EXP_1 = true; var EXP_2 = false; EXP_1 + EXP_2",
96                                    1,
97                                    eval("var EXP_1 = true; var EXP_2 = false; EXP_1 + EXP_2") );
98
99    array[item++] = new TestCase(   SECTION,
100                                    "var EXP_1 = new Boolean(true); var EXP_2 = new Boolean(false); EXP_1 + EXP_2",
101                                    1,
102                                    eval("var EXP_1 = new Boolean(true); var EXP_2 = new Boolean(false); EXP_1 + EXP_2") );
103
104    array[item++] = new TestCase(   SECTION,
105                                    "var EXP_1 = new Object(true); var EXP_2 = new Object(false); EXP_1 + EXP_2",
106                                    1,
107                                    eval("var EXP_1 = new Object(true); var EXP_2 = new Object(false); EXP_1 + EXP_2") );
108
109    array[item++] = new TestCase(   SECTION,
110                                    "var EXP_1 = new Object(new Boolean(true)); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2",
111                                    1,
112                                    eval("var EXP_1 = new Object(new Boolean(true)); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2") );
113
114    array[item++] = new TestCase(   SECTION,
115                                    "var EXP_1 = new MyObject(true); var EXP_2 = new MyObject(false); EXP_1 + EXP_2",
116                                    1,
117                                    eval("var EXP_1 = new MyObject(true); var EXP_2 = new MyObject(false); EXP_1 + EXP_2") );
118
119    array[item++] = new TestCase(   SECTION,
120                                    "var EXP_1 = new MyObject(new Boolean(true)); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2",
121                                    "[object Object][object Object]",
122                                    eval("var EXP_1 = new MyObject(new Boolean(true)); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2") );
123
124    array[item++] = new TestCase(   SECTION,
125                                    "var EXP_1 = new MyValuelessObject(true); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2",
126                                    1,
127                                    eval("var EXP_1 = new MyValuelessObject(true); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2") );
128
129    array[item++] = new TestCase(   SECTION,
130                                    "var EXP_1 = new MyValuelessObject(new Boolean(true)); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2",
131                                    "truefalse",
132                                    eval("var EXP_1 = new MyValuelessObject(new Boolean(true)); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2") );
133
134    // tests for number primitive, number object, Object object, a "MyObject" whose value is
135    // a number primitive and a number object, and "MyValuelessObject", where the value is
136    // set in the object's prototype, not the object itself.
137
138    array[item++] = new TestCase(   SECTION,
139                                    "var EXP_1 = 100; var EXP_2 = -1; EXP_1 + EXP_2",
140                                    99,
141                                    eval("var EXP_1 = 100; var EXP_2 = -1; EXP_1 + EXP_2") );
142
143    array[item++] = new TestCase(   SECTION,
144                                    "var EXP_1 = new Number(100); var EXP_2 = new Number(-1); EXP_1 + EXP_2",
145                                    99,
146                                    eval("var EXP_1 = new Number(100); var EXP_2 = new Number(-1); EXP_1 + EXP_2") );
147
148    array[item++] = new TestCase(   SECTION,
149                                    "var EXP_1 = new Object(100); var EXP_2 = new Object(-1); EXP_1 + EXP_2",
150                                    99,
151                                    eval("var EXP_1 = new Object(100); var EXP_2 = new Object(-1); EXP_1 + EXP_2") );
152
153    array[item++] = new TestCase(   SECTION,
154                                    "var EXP_1 = new Object(new Number(100)); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2",
155                                    99,
156                                    eval("var EXP_1 = new Object(new Number(100)); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2") );
157
158    array[item++] = new TestCase(   SECTION,
159                                    "var EXP_1 = new MyObject(100); var EXP_2 = new MyObject(-1); EXP_1 + EXP_2",
160                                    99,
161                                    eval("var EXP_1 = new MyObject(100); var EXP_2 = new MyObject(-1); EXP_1 + EXP_2") );
162
163    array[item++] = new TestCase(   SECTION,
164                                    "var EXP_1 = new MyObject(new Number(100)); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2",
165                                    "[object Object][object Object]",
166                                    eval("var EXP_1 = new MyObject(new Number(100)); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2") );
167
168
169    array[item++] = new TestCase(   SECTION,
170                                    "var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject(-1); EXP_1 + EXP_2",
171                                    99,
172                                    eval("var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject(-1); EXP_1 + EXP_2") );
173
174    array[item++] = new TestCase(   SECTION,
175                                    "var EXP_1 = new MyValuelessObject(new Number(100)); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2",
176                                    "100-1",
177                                    eval("var EXP_1 = new MyValuelessObject(new Number(100)); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2") );
178
179    array[item++] = new TestCase(   SECTION,
180                                    "var EXP_1 = new MyValuelessObject( new MyValuelessObject( new Boolean(true) ) ); EXP_1 + EXP_1",
181                                    "truetrue",
182                                    eval("var EXP_1 = new MyValuelessObject( new MyValuelessObject( new Boolean(true) ) ); EXP_1 + EXP_1") );
183
184    return ( array );
185}
186
187
188function MyProtoValuelessObject() {
189    this.valueOf = new Function ( "" );
190    this.__proto__ = null;
191}
192
193function MyProtolessObject( value ) {
194    this.valueOf = new Function( "return this.value" );
195    this.__proto__ = null;
196    this.value = value;
197}
198
199function MyValuelessObject(value) {
200    this.__proto__ = new MyPrototypeObject(value);
201}
202function MyPrototypeObject(value) {
203    this.valueOf = new Function( "return this.value;" );
204    this.toString = new Function( "return (this.value + '');" );
205    this.value = value;
206}
207
208function MyObject( value ) {
209    this.valueOf = new Function( "return this.value" );
210    this.value = value;
211}
212