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.5.1-1.js
24    ECMA Section:       [[ Put]] (P, V)
25    Description:
26    Array objects use a variation of the [[Put]] method used for other native
27    ECMAScript objects (section 8.6.2.2).
28
29    Assume A is an Array object and P is a string.
30
31    When the [[Put]] method of A is called with property P and value V, the
32    following steps are taken:
33
34    1.  Call the [[CanPut]] method of A with name P.
35    2.  If Result(1) is false, return.
36    3.  If A doesn't have a property with name P, go to step 7.
37    4.  If P is "length", go to step 12.
38    5.  Set the value of property P of A to V.
39    6.  Go to step 8.
40    7.  Create a property with name P, set its value to V and give it empty
41        attributes.
42    8.  If P is not an array index, return.
43    9.  If A itself has a property (not an inherited property) named "length",
44        andToUint32(P) is less than the value of the length property of A, then
45        return.
46    10. Change (or set) the value of the length property of A to ToUint32(P)+1.
47    11. Return.
48    12. Compute ToUint32(V).
49    13. For every integer k that is less than the value of the length property
50        of A but not less than Result(12), if A itself has a property (not an
51        inherited property) named ToString(k), then delete that property.
52    14. Set the value of property P of A to Result(12).
53    15. Return.
54    Author:             christine@netscape.com
55    Date:               12 november 1997
56*/
57
58    var SECTION = "15.4.5.1-1";
59    var VERSION = "ECMA_1";
60    startTest();
61    var TITLE   = "Array [[Put]] (P, V)";
62
63    writeHeaderToLog( SECTION + " "+ TITLE);
64
65    var testcases = getTestCases();
66    test();
67
68function getTestCases() {
69    var array = new Array();
70
71    var item = 0;
72
73    // P is "length"
74
75    array[item++] = new TestCase(   SECTION,
76                                    "var A = new Array(); A.length = 1000; A.length",
77                                    1000,
78                                    eval("var A = new Array(); A.length = 1000; A.length") );
79
80    // A has Property P, and P is not length or an array index
81    array[item++] = new TestCase(   SECTION,
82                                    "var A = new Array(1000); A.name = 'name of this array'; A.name",
83                                    'name of this array',
84                                    eval("var A = new Array(1000); A.name = 'name of this array'; A.name") );
85
86    array[item++] = new TestCase(   SECTION,
87                                    "var A = new Array(1000); A.name = 'name of this array'; A.length",
88                                    1000,
89                                    eval("var A = new Array(1000); A.name = 'name of this array'; A.length") );
90
91
92    // A has Property P, P is not length, P is an array index, and ToUint32(p) is less than the
93    // value of length
94
95    array[item++] = new TestCase(   SECTION,
96                                    "var A = new Array(1000); A[123] = 'hola'; A[123]",
97                                    'hola',
98                                    eval("var A = new Array(1000); A[123] = 'hola'; A[123]") );
99
100    array[item++] = new TestCase(   SECTION,
101                                    "var A = new Array(1000); A[123] = 'hola'; A.length",
102                                    1000,
103                                    eval("var A = new Array(1000); A[123] = 'hola'; A.length") );
104
105
106    for ( var i = 0X0020, TEST_STRING = "var A = new Array( " ; i < 0x00ff; i++ ) {
107        if ( i === 0x58 || i === 0x78 ) // x or X - skip testing invalid hex escapes.
108            continue;
109        TEST_STRING += "\'\\"+ String.fromCharCode( i ) +"\'";
110        if ( i < 0x00FF - 1   ) {
111            TEST_STRING += ",";
112        } else {
113            TEST_STRING += ");"
114        }
115    }
116
117    var LENGTH = 0x00ff - 0x0020 - 2; // x & X
118
119    array[item++] = new TestCase(   SECTION,
120                                    TEST_STRING +" A[150] = 'hello'; A[150]",
121                                    'hello',
122                                    eval( TEST_STRING + " A[150] = 'hello'; A[150]" ) );
123
124    array[item++] = new TestCase(   SECTION,
125                                    TEST_STRING +" A[150] = 'hello'; A[150]",
126                                    LENGTH,
127                                    eval( TEST_STRING + " A[150] = 'hello'; A.length" ) );
128
129    // A has Property P, P is not length, P is an array index, and ToUint32(p) is not less than the
130    // value of length
131
132    array[item++] = new TestCase(   SECTION,
133                                    "var A = new Array(); A[123] = true; A.length",
134                                    124,
135                                    eval("var A = new Array(); A[123] = true; A.length") );
136
137    array[item++] = new TestCase(   SECTION,
138                                    "var A = new Array(0,1,2,3,4,5,6,7,8,9,10); A[15] ='15'; A.length",
139                                    16,
140                                    eval("var A = new Array(0,1,2,3,4,5,6,7,8,9,10); A[15] ='15'; A.length") );
141
142    for ( var i = 0; i < A.length; i++, item++ ) {
143        array[item] = new TestCase( SECTION,
144                                    "var A = new Array(0,1,2,3,4,5,6,7,8,9,10); A[15] ='15'; A[" +i +"]",
145                                    (i <= 10) ? i : ( i == 15 ? '15' : void 0 ),
146                                    A[i] );
147    }
148    // P is not an array index, and P is not "length"
149
150    array[item++] = new TestCase(   SECTION,
151                                    "var A = new Array(); A.join.length = 4; A.join.length",
152                                    1,
153                                    eval("var A = new Array(); A.join.length = 4; A.join.length") );
154
155    array[item++] = new TestCase(   SECTION,
156                                    "var A = new Array(); A.join.length = 4; A.length",
157                                    0,
158                                    eval("var A = new Array(); A.join.length = 4; A.length") );
159
160    return array;
161}
162function test() {
163    for ( tc=0; tc < testcases.length; tc++ ) {
164        testcases[tc].passed = writeTestCaseResult(
165                            testcases[tc].expect,
166                            testcases[tc].actual,
167                            testcases[tc].description +" = "+
168                            testcases[tc].actual );
169
170        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
171    }
172    stopTest();
173    return ( testcases );
174}
175