* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.JScript / Test / Mozilla / js1_2 / Array / splice1.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    Filename:     splice1.js
41    Description:  'Tests Array.splice(x,y) w/no var args'
42
43    Author:       Nick Lerissa
44    Date:         Fri Feb 13 09:58:28 PST 1998
45 */
46
47 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
48 var VERSION = 'no version';
49 startTest();
50 var TITLE = 'String:splice 1';
51 var BUGNUMBER="123795";
52
53 writeHeaderToLog('Executing script: splice1.js');
54 writeHeaderToLog( SECTION + " "+ TITLE);
55
56 var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']];
57 var b = [1,2,3,4,5,6,7,8,9,0];
58
59 exhaustiveSpliceTest("exhaustive splice w/no optional args 1",a);
60 exhaustiveSpliceTest("exhaustive splice w/no optional args 1",b);
61
62 test();
63
64
65 function mySplice(testArray, splicedArray, first, len, elements)
66 {
67     var removedArray  = [];
68     var adjustedFirst = first;
69     var adjustedLen   = len;
70
71     if (adjustedFirst < 0) adjustedFirst = testArray.length + first;
72     if (adjustedFirst < 0) adjustedFirst = 0;
73
74     if (adjustedLen < 0) adjustedLen = 0;
75
76     for (i = 0; (i < adjustedFirst)&&(i < testArray.length); ++i)
77         splicedArray.push(testArray[i]);
78
79     if (adjustedFirst < testArray.length)
80         for (i = adjustedFirst; (i < adjustedFirst + adjustedLen) &&
81                  (i < testArray.length); ++i)
82         {
83             removedArray.push(testArray[i]);
84         }
85
86     for (i = 0; i < elements.length; i++) splicedArray.push(elements[i]);
87
88     for (i = adjustedFirst + adjustedLen; i < testArray.length; i++)
89         splicedArray.push(testArray[i]);
90
91     return removedArray;
92 }
93
94 function exhaustiveSpliceTest(testname, testArray)
95 {
96     var errorMessage;
97     var passed = true;
98     var reason = "";
99
100     for (var first = -(testArray.length+2); first <= 2 + testArray.length; first++)
101     {
102         var actualSpliced   = [];
103         var expectedSpliced = [];
104         var actualRemoved   = [];
105         var expectedRemoved = [];
106
107         for (var len = 0; len < testArray.length + 2; len++)
108         {
109             actualSpliced   = [];
110             expectedSpliced = [];
111
112             for (var i = 0; i < testArray.length; ++i)
113                 actualSpliced.push(testArray[i]);
114
115             actualRemoved   = actualSpliced.splice(first,len);
116             expectedRemoved = mySplice(testArray,expectedSpliced,first,len,[]);
117
118             var adjustedFirst = first;
119             if (adjustedFirst < 0) adjustedFirst = testArray.length + first;
120             if (adjustedFirst < 0) adjustedFirst = 0;
121
122             if (  (String(actualSpliced) != String(expectedSpliced))
123                   ||(String(actualRemoved) != String(expectedRemoved)))
124             {
125                 if (  (String(actualSpliced) == String(expectedSpliced))
126                       &&(String(actualRemoved) != String(expectedRemoved)) )
127                 {
128                     if ( (expectedRemoved.length == 1)
129                          &&(String(actualRemoved) == String(expectedRemoved[0]))) continue;
130                     if ( expectedRemoved.length == 0 && actualRemoved == void 0) continue;
131                 }
132
133                 errorMessage =
134                     "ERROR: 'TEST FAILED'\n" +
135                     "             test: " + "a.splice(" + first + "," + len + ",-97,new String('test arg'),[],9.8)\n" +
136                     "                a: " + String(testArray) + "\n" +
137                     "   actual spliced: " + String(actualSpliced) + "\n" +
138                     " expected spliced: " + String(expectedSpliced) + "\n" +
139                     "   actual removed: " + String(actualRemoved) + "\n" +
140                     " expected removed: " + String(expectedRemoved) + "\n";
141                 writeHeaderToLog(errorMessage);
142                 reason = reason + errorMessage;
143                 passed = false;
144             }
145         }
146     }
147     var testcase = new TestCase( SECTION, testname, true, passed);
148     if (!passed)
149         testcase.reason = reason;
150     return testcase;
151 }