]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/Expressions/11.6.1-3.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-3.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 
  60     Author:             christine@netscape.com 
  61     Date:               12 november 1997 
  63     var SECTION 
= "11.6.1-3"; 
  64     var VERSION 
= "ECMA_1"; 
  66     var testcases 
= getTestCases(); 
  68     writeHeaderToLog( SECTION 
+ " The Addition operator ( + )"); 
  72     for ( tc
=0; tc 
< testcases
.length
; tc
++ ) { 
  73         testcases
[tc
].passed 
= writeTestCaseResult( 
  76                             testcases
[tc
].description 
+" = "+ 
  77                             testcases
[tc
].actual 
); 
  79         testcases
[tc
].reason 
+= ( testcases
[tc
].passed 
) ? "" : "wrong value "; 
  84 function getTestCases() { 
  85     var array 
= new Array(); 
  88     // tests for boolean primitive, boolean object, Object object, a "MyObject" whose value is 
  89     // a boolean primitive and a boolean object, and "MyValuelessObject", where the value is 
  90     // set in the object's prototype, not the object itself. 
  92     var DATE1 
= new Date(); 
  94     array
[item
++] = new TestCase(   SECTION
, 
  95                                     "var DATE1 = new Date(); DATE1 + DATE1", 
  96                                     DATE1
.toString() + DATE1
.toString(), 
  99     array
[item
++] = new TestCase(   SECTION
, 
 100                                     "var DATE1 = new Date(); DATE1 + 0", 
 101                                     DATE1
.toString() + 0, 
 104     array
[item
++] = new TestCase(   SECTION
, 
 105                                     "var DATE1 = new Date(); DATE1 + new Number(0)", 
 106                                     DATE1
.toString() + 0, 
 107                                     DATE1 
+ new Number(0) ); 
 109     array
[item
++] = new TestCase(   SECTION
, 
 110                                     "var DATE1 = new Date(); DATE1 + true", 
 111                                     DATE1
.toString() + "true", 
 114     array
[item
++] = new TestCase(   SECTION
, 
 115                                     "var DATE1 = new Date(); DATE1 + new Boolean(true)", 
 116                                     DATE1
.toString() + "true", 
 117                                     DATE1 
+ new Boolean(true) ); 
 119     array
[item
++] = new TestCase(   SECTION
, 
 120                                     "var DATE1 = new Date(); DATE1 + new Boolean(true)", 
 121                                     DATE1
.toString() + "true", 
 122                                     DATE1 
+ new Boolean(true) ); 
 124     var MYOB1 
= new MyObject( DATE1 
); 
 125     var MYOB2 
= new MyValuelessObject( DATE1 
); 
 126     var MYOB3 
= new MyProtolessObject( DATE1 
); 
 127     var MYOB4 
= new MyProtoValuelessObject( DATE1 
); 
 129     array
[item
++] = new TestCase(   SECTION
, 
 130                                     "MYOB1 = new MyObject(DATE1); MYOB1 + new Number(1)", 
 132                                     MYOB1 
+ new Number(1) ); 
 134     array
[item
++] = new TestCase(   SECTION
, 
 135                                     "MYOB1 = new MyObject(DATE1); MYOB1 + 1", 
 139     array
[item
++] = new TestCase(   SECTION
, 
 140                                     "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + 'string'", 
 141                                     DATE1
.toString() + "string", 
 144     array
[item
++] = new TestCase(   SECTION
, 
 145                                     "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + new String('string')", 
 146                                     DATE1
.toString() + "string", 
 147                                     MYOB2 
+ new String('string') ); 
 149     array[item++] = new TestCase(   SECTION, 
 150                                     "MYOB3 = new MyProtolessObject(DATE1); MYOB3 + new Boolean(true)", 
 151                                     DATE1.toString() + "true", 
 152                                     MYOB3 + new Boolean(true) ); 
 154     array
[item
++] = new TestCase(   SECTION
, 
 155                                     "MYOB1 = new MyObject(DATE1); MYOB1 + true", 
 156                                     "[object Object]true", 
 161 function MyProtoValuelessObject() { 
 162     this.valueOf 
= new Function ( "" ); 
 163     this.__proto__ 
= null; 
 165 function MyProtolessObject( value 
) { 
 166     this.valueOf 
= new Function( "return this.value" ); 
 167     this.__proto__ 
= null; 
 170 function MyValuelessObject(value
) { 
 171     this.__proto__ 
= new MyPrototypeObject(value
); 
 173 function MyPrototypeObject(value
) { 
 174     this.valueOf 
= new Function( "return this.value;" ); 
 175     this.toString 
= new Function( "return (this.value + '');" ); 
 178 function MyObject( value 
) { 
 179     this.valueOf 
= new Function( "return this.value" );