Home | History | Annotate | Download | only in Array
      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:    15.4.4.3-1.js
     24     ECMA Section: 15.4.4.3-1 Array.prototype.join()
     25     Description:  The elements of this object are converted to strings and
     26                   these strings are then concatenated, separated by comma
     27                   characters. The result is the same as if the built-in join
     28                   method were invoiked for this object with no argument.
     29     Author:       christine (at) netscape.com, pschwartau (at) netscape.com
     30     Date:         07 October 1997
     31     Modified:     14 July 2002
     32     Reason:       See http://bugzilla.mozilla.org/show_bug.cgi?id=155285
     33                   ECMA-262 Ed.3  Section 15.4.4.5 Array.prototype.join()
     34                   Step 3: If |separator| is |undefined|, let |separator|
     35                           be the single-character string ","
     36 *
     37 */
     38 
     39     var SECTION = "15.4.4.3-1";
     40     var VERSION = "ECMA_1";
     41     startTest();
     42 
     43     writeHeaderToLog( SECTION + " Array.prototype.join()");
     44 
     45     var testcases = getTestCases();
     46     test();
     47 
     48 function getTestCases() {
     49     var array = new Array();
     50     var item = 0;
     51 
     52     var ARR_PROTOTYPE = Array.prototype;
     53 
     54     array[item++] = new TestCase( SECTION, "Array.prototype.join.length",           1,      Array.prototype.join.length );
     55     array[item++] = new TestCase( SECTION, "delete Array.prototype.join.length",    false,  delete Array.prototype.join.length );
     56     array[item++] = new TestCase( SECTION, "delete Array.prototype.join.length; Array.prototype.join.length",    1, eval("delete Array.prototype.join.length; Array.prototype.join.length") );
     57 
     58     // case where array length is 0
     59 
     60     array[item++] = new TestCase(   SECTION,
     61                                     "var TEST_ARRAY = new Array(); TEST_ARRAY.join()",
     62                                     "",
     63                                     eval("var TEST_ARRAY = new Array(); TEST_ARRAY.join()") );
     64 
     65     // array length is 0, but spearator is specified
     66 
     67     array[item++] = new TestCase(   SECTION,
     68                                     "var TEST_ARRAY = new Array(); TEST_ARRAY.join(' ')",
     69                                     "",
     70                                     eval("var TEST_ARRAY = new Array(); TEST_ARRAY.join(' ')") );
     71 
     72     // length is greater than 0, separator is supplied
     73     array[item++] = new TestCase(   SECTION,
     74                                     "var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join('&')",
     75                                     "&&true&false&123&[object Object]&true",
     76                                     eval("var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join('&')") );
     77 
     78     // length is greater than 0, separator is empty string
     79     array[item++] = new TestCase(   SECTION,
     80                                     "var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join('')",
     81                                     "truefalse123[object Object]true",
     82                                     eval("var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join('')") );
     83     // length is greater than 0, separator is undefined
     84     array[item++] = new TestCase(   SECTION,
     85                                     "var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join(void 0)",
     86                                     ",,true,false,123,[object Object],true",
     87                                     eval("var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join(void 0)") );
     88 
     89     // length is greater than 0, separator is not supplied
     90     array[item++] = new TestCase(   SECTION,
     91                                     "var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join()",
     92                                     ",,true,false,123,[object Object],true",
     93                                     eval("var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join()") );
     94 
     95     // separator is a control character
     96     array[item++] = new TestCase(   SECTION,
     97                                     "var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join('\v')",
     98                                     unescape("%u000B%u000Btrue%u000Bfalse%u000B123%u000B[object Object]%u000Btrue"),
     99                                     eval("var TEST_ARRAY = new Array(null, void 0, true, false, 123, new Object(), new Boolean(true) ); TEST_ARRAY.join('\v')") );
    100 
    101     // length of array is 1
    102     array[item++] = new TestCase(   SECTION,
    103                                     "var TEST_ARRAY = new Array(true) ); TEST_ARRAY.join('\v')",
    104                                     "true",
    105                                     eval("var TEST_ARRAY = new Array(true); TEST_ARRAY.join('\v')") );
    106 
    107 
    108     SEPARATOR = "\t"
    109     TEST_LENGTH = 100;
    110     TEST_STRING = "";
    111     ARGUMENTS = "";
    112     TEST_RESULT = "";
    113 
    114     for ( var index = 0; index < TEST_LENGTH; index++ ) {
    115         ARGUMENTS   += index;
    116         ARGUMENTS   += ( index == TEST_LENGTH -1 ) ? "" : ",";
    117 
    118         TEST_RESULT += index;
    119         TEST_RESULT += ( index == TEST_LENGTH -1 ) ? "" : SEPARATOR;
    120     }
    121 
    122     TEST_ARRAY = eval( "new Array( "+ARGUMENTS +")" );
    123 
    124     array[item++] = new TestCase( SECTION, "TEST_ARRAY.join("+SEPARATOR+")",   TEST_RESULT,    TEST_ARRAY.join( SEPARATOR ) );
    125 
    126     array[item++] = new TestCase( SECTION, "(new Array( Boolean(true), Boolean(false), null,  void 0, Number(1e+21), Number(1e-7))).join()",
    127                                        "true,false,,,1e+21,1e-7",
    128                                        (new Array( Boolean(true), Boolean(false), null,  void 0, Number(1e+21), Number(1e-7))).join() );
    129 
    130     // this is not an Array object
    131     array[item++] = new TestCase(   SECTION,
    132                                     "var OB = new Object_1('true,false,111,0.5,1.23e6,NaN,void 0,null'); OB.join(':')",
    133                                     "true:false:111:0.5:1230000:NaN::",
    134                                     eval("var OB = new Object_1('true,false,111,0.5,1.23e6,NaN,void 0,null'); OB.join(':')") );
    135 
    136 
    137 
    138     return ( array );
    139 }
    140 function test() {
    141     for ( tc=0; tc < testcases.length; tc++ ) {
    142         testcases[tc].passed = writeTestCaseResult(
    143                             testcases[tc].expect,
    144                             testcases[tc].actual,
    145                             testcases[tc].description +" = "+ testcases[tc].actual );
    146         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
    147     }
    148     stopTest();
    149     return ( testcases );
    150 }
    151 function Object_1( value ) {
    152     this.array = value.split(",");
    153     this.length = this.array.length;
    154     for ( var i = 0; i < this.length; i++ ) {
    155         this[i] = eval(this.array[i]);
    156     }
    157     this.join = Array.prototype.join;
    158     this.getClass = Object.prototype.toString;
    159 }
    160