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