]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/Expressions/11.6.1-2.js
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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 File Name: 11.6.1-2.js
24 ECMA Section: 11.6.1 The addition operator ( + )
27 The addition operator either performs string concatenation or numeric
30 The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression
31 is evaluated as follows:
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).
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.
57 This test does only covers cases where the Additive or Mulplicative expression
58 ToPrimitive is a string.
60 Author: christine@netscape.com
61 Date: 12 november 1997
63 var SECTION
= "11.6.1-2";
64 var VERSION
= "ECMA_1";
67 var testcases
= getTestCases();
69 writeHeaderToLog( SECTION
+ " The Addition operator ( + )");
73 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
74 testcases
[tc
].passed
= writeTestCaseResult(
77 testcases
[tc
].description
+" = "+
78 testcases
[tc
].actual
);
80 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
85 function getTestCases() {
86 var array
= new Array();
89 // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is
90 // a boolean primitive and a boolean object, and "MyValuelessObject", where the value is
91 // set in the object's prototype, not the object itself.
93 array
[item
++] = new TestCase( SECTION
,
94 "var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2",
96 eval("var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2") );
98 array
[item
++] = new TestCase( SECTION
,
99 "var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2",
101 eval("var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2") );
103 array
[item
++] = new TestCase( SECTION
,
104 "var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2",
106 eval("var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2") );
108 array
[item
++] = new TestCase( SECTION
,
109 "var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2",
111 eval("var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2") );
113 array
[item
++] = new TestCase( SECTION
,
114 "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2",
116 eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2") );
118 array
[item
++] = new TestCase( SECTION
,
119 "var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2",
121 eval("var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") );
123 array
[item
++] = new TestCase( SECTION
,
124 "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2",
125 "[object Object][object Object]",
126 eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2") );
128 array
[item
++] = new TestCase( SECTION
,
129 "var EXP_1 = new MyValuelessObject('string'); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2",
131 eval("var EXP_1 = new MyValuelessObject('string'); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2") );
133 array
[item
++] = new TestCase( SECTION
,
134 "var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2",
136 eval("var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2") );
138 // tests for number primitive, number object, Object object, a "MyObject" whose value is
139 // a number primitive and a number object, and "MyValuelessObject", where the value is
140 // set in the object's prototype, not the object itself.
142 array
[item
++] = new TestCase( SECTION
,
143 "var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2",
145 eval("var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2") );
147 array
[item
++] = new TestCase( SECTION
,
148 "var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2",
150 eval("var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2") );
152 array
[item
++] = new TestCase( SECTION
,
153 "var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2",
155 eval("var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2") );
157 array
[item
++] = new TestCase( SECTION
,
158 "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2",
160 eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2") );
162 array
[item
++] = new TestCase( SECTION
,
163 "var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2",
165 eval("var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") );
167 array
[item
++] = new TestCase( SECTION
,
168 "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2",
169 "[object Object][object Object]",
170 eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2") );
172 array
[item
++] = new TestCase( SECTION
,
173 "var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject('string'); EXP_1 + EXP_2",
175 eval("var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject('string'); EXP_1 + EXP_2") );
177 array
[item
++] = new TestCase( SECTION
,
178 "var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2",
180 eval("var EXP_1 = new MyValuelessObject(new String('string')); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2") );
183 function MyProtoValuelessObject() {
184 this.valueOf
= new Function ( "" );
185 this.__proto__
= null;
187 function MyProtolessObject( value
) {
188 this.valueOf
= new Function( "return this.value" );
189 this.__proto__
= null;
192 function MyValuelessObject(value
) {
193 this.__proto__
= new MyPrototypeObject(value
);
195 function MyPrototypeObject(value
) {
196 this.valueOf
= new Function( "return this.value;" );
197 this.toString
= new Function( "return (this.value + '');" );
200 function MyObject( value
) {
201 this.valueOf
= new Function( "return this.value" );