• 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:          15.4.2.2-1.js
24    ECMA Section:       15.4.2.2 new Array(len)
25
26    Description:        This description only applies of the constructor is
27                        given two or more arguments.
28
29                        The [[Prototype]] property of the newly constructed
30                        object is set to the original Array prototype object,
31                        the one that is the initial value of Array.prototype(0)
32                        (15.4.3.1).
33
34                        The [[Class]] property of the newly constructed object
35                        is set to "Array".
36
37                        If the argument len is a number, then the length
38                        property  of the newly constructed object is set to
39                        ToUint32(len).
40
41                        If the argument len is not a number, then the length
42                        property of the newly constructed object is set to 1
43                        and the 0 property of the newly constructed object is
44                        set to len.
45
46                        This file tests cases where len is a number.
47
48                        The cases in this test need to be updated since the
49                        ToUint32 description has changed.
50
51    Author:             christine@netscape.com
52    Date:               7 october 1997
53*/
54    var SECTION = "15.4.2.2-1";
55    var VERSION = "ECMA_1";
56    startTest();
57    var TITLE   = "The Array Constructor:  new Array( len )";
58
59    writeHeaderToLog( SECTION + " "+ TITLE);
60
61    var testcases = getTestCases();
62    test();
63
64function getTestCases() {
65    var array = new Array();
66    var item = 0;
67
68    array[item++] = new TestCase( SECTION,	"new Array(0)",             "",                 (new Array(0)).toString() );
69    array[item++] = new TestCase( SECTION,	"typeof new Array(0)",      "object",           (typeof new Array(0)) );
70    array[item++] = new TestCase( SECTION,	"(new Array(0)).length",    0,                  (new Array(0)).length );
71    array[item++] = new TestCase( SECTION,	"(new Array(0)).toString", Array.prototype.toString,    (new Array(0)).toString );
72
73    array[item++] = new TestCase( SECTION,   "new Array(1)",            "",                 (new Array(1)).toString() );
74    array[item++] = new TestCase( SECTION,   "new Array(1).length",     1,                  (new Array(1)).length );
75    array[item++] = new TestCase( SECTION,   "(new Array(1)).toString", Array.prototype.toString,   (new Array(1)).toString );
76
77    array[item++] = new TestCase( SECTION,	"(new Array(-0)).length",                       0,  (new Array(-0)).length );
78    array[item++] = new TestCase( SECTION,	"(new Array(0)).length",                        0,  (new Array(0)).length );
79
80    array[item++] = new TestCase( SECTION,	"(new Array(10)).length",           10,         (new Array(10)).length );
81    array[item++] = new TestCase( SECTION,	"(new Array('1')).length",          1,          (new Array('1')).length );
82    array[item++] = new TestCase( SECTION,	"(new Array(1000)).length",         1000,       (new Array(1000)).length );
83    array[item++] = new TestCase( SECTION,	"(new Array('1000')).length",       1,          (new Array('1000')).length );
84
85    array[item++] = new TestCase( SECTION,	"(new Array(4294967295)).length",   ToUint32(4294967295),   (new Array(4294967295)).length );
86
87    array[item++] = new TestCase( SECTION,	"(new Array('8589934592')).length", 1,                      (new Array("8589934592")).length );
88    array[item++] = new TestCase( SECTION,	"(new Array('4294967296')).length", 1,                      (new Array("4294967296")).length );
89    array[item++] = new TestCase( SECTION,	"(new Array(1073741824)).length",   ToUint32(1073741824),	(new Array(1073741824)).length );
90
91    return ( array );
92}
93function test() {
94    for ( tc=0; tc < testcases.length; tc++ ) {
95        testcases[tc].passed = writeTestCaseResult(
96                            testcases[tc].expect,
97                            testcases[tc].actual,
98                            testcases[tc].description +" = "+
99                            testcases[tc].actual );
100
101        testcases[tc].reason += ( testcases[tc].passed )
102                             ? ""
103                             : "wrong value ";
104    }
105    stopTest();
106    return ( testcases );
107}
108function ToUint32( n ) {
109    n = Number( n );
110    var sign = ( n < 0 ) ? -1 : 1;
111
112    if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
113        return 0;
114    }
115    n = sign * Math.floor( Math.abs(n) )
116
117    n = n % Math.pow(2,32);
118
119    if ( n < 0 ){
120        n += Math.pow(2,32);
121    }
122
123    return ( n );
124}