Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Expressions / 11.6.1-2.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 Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          11.6.1-2.js
42    ECMA Section:       11.6.1 The addition operator ( + )
43    Description:
44
45    The addition operator either performs string concatenation or numeric
46    addition.
47
48    The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression
49    is evaluated as follows:
50
51    1.  Evaluate AdditiveExpression.
52    2.  Call GetValue(Result(1)).
53    3.  Evaluate MultiplicativeExpression.
54    4.  Call GetValue(Result(3)).
55    5.  Call ToPrimitive(Result(2)).
56    6.  Call ToPrimitive(Result(4)).
57    7.  If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12.
58    (Note that this step differs from step 3 in the algorithm for comparison
59    for the relational operators in using or instead of and.)
60    8.  Call ToNumber(Result(5)).
61    9.  Call ToNumber(Result(6)).
62    10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3).
63    11. Return Result(10).
64    12. Call ToString(Result(5)).
65    13. Call ToString(Result(6)).
66    14. Concatenate Result(12) followed by Result(13).
67    15. Return Result(14).
68
69    Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6.
70    All native ECMAScript objects except Date objects handle the absence of a
71    hint as if the hint Number were given; Date objects handle the absence of a
72    hint as if the hint String were given. Host objects may handle the absence
73    of a hint in some other manner.
74
75    This test does only covers cases where the Additive or Mulplicative expression
76    ToPrimitive is a string.
77
78    Author:             christine@netscape.com
79    Date:               12 november 1997
80 */
81 var SECTION = "11.6.1-2";
82 var VERSION = "ECMA_1";
83 startTest();
84
85 writeHeaderToLog( SECTION + " The Addition operator ( + )");
86
87 // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is
88 // a boolean primitive and a boolean object.
89
90 new TestCase(   SECTION,
91                 "var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2",
92                 "stringfalse",
93                 eval("var EXP_1 = 'string'; var EXP_2 = false; EXP_1 + EXP_2") );
94
95 new TestCase(   SECTION,
96                 "var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2",
97                 "truestring",
98                 eval("var EXP_1 = true; var EXP_2 = 'string'; EXP_1 + EXP_2") );
99
100 new TestCase(   SECTION,
101                 "var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2",
102                 "truestring",
103                 eval("var EXP_1 = new Boolean(true); var EXP_2 = new String('string'); EXP_1 + EXP_2") );
104
105 new TestCase(   SECTION,
106                 "var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2",
107                 "truestring",
108                 eval("var EXP_1 = new Object(true); var EXP_2 = new Object('string'); EXP_1 + EXP_2") );
109
110 new TestCase(   SECTION,
111                 "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2",
112                 "stringfalse",
113                 eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Boolean(false)); EXP_1 + EXP_2") );
114
115 new TestCase(   SECTION,
116                 "var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2",
117                 "truestring",
118                 eval("var EXP_1 = new MyObject(true); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") );
119
120 new TestCase(   SECTION,
121                 "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2",
122                 "[object Object][object Object]",
123                 eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Boolean(false)); EXP_1 + EXP_2") );
124
125 // tests for number primitive, number object, Object object, a "MyObject" whose value is
126 // a number primitive and a number object.
127
128 new TestCase(   SECTION,
129                 "var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2",
130                 "100string",
131                 eval("var EXP_1 = 100; var EXP_2 = 'string'; EXP_1 + EXP_2") );
132
133 new TestCase(   SECTION,
134                 "var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2",
135                 "string-1",
136                 eval("var EXP_1 = new String('string'); var EXP_2 = new Number(-1); EXP_1 + EXP_2") );
137
138 new TestCase(   SECTION,
139                 "var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2",
140                 "100string",
141                 eval("var EXP_1 = new Object(100); var EXP_2 = new Object('string'); EXP_1 + EXP_2") );
142
143 new TestCase(   SECTION,
144                 "var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2",
145                 "string-1",
146                 eval("var EXP_1 = new Object(new String('string')); var EXP_2 = new Object(new Number(-1)); EXP_1 + EXP_2") );
147
148 new TestCase(   SECTION,
149                 "var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2",
150                 "100string",
151                 eval("var EXP_1 = new MyObject(100); var EXP_2 = new MyObject('string'); EXP_1 + EXP_2") );
152
153 new TestCase(   SECTION,
154                 "var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2",
155                 "[object Object][object Object]",
156                 eval("var EXP_1 = new MyObject(new String('string')); var EXP_2 = new MyObject(new Number(-1)); EXP_1 + EXP_2") );
157
158 test();
159
160 function MyObject( value ) {
161   this.valueOf = new Function( "return this.value" );
162   this.value = value;
163 }