Debian Codegraph @

rhino/testsrc/tests/js1_2/Array/splice2.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'splice2.js';
7
8 /**
9 Filename: splice2.js
10 Description: 'Tests Array.splice(x,y) w/4 var args'
11
12 Author: Nick Lerissa
13 Date: Fri Feb 13 09:58:28 PST 1998
14 */
15
16 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
17 var VERSION = 'no version';
18 var TITLE = 'String:splice 2';
19 var BUGNUMBER="123795";
20
21 startTest();
22 writeHeaderToLog('Executing script: splice2.js');
23 writeHeaderToLog( SECTION + " "+ TITLE);
24
25 var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']];
26 var b = [1,2,3,4,5,6,7,8,9,0];
27
28 exhaustiveSpliceTestWithArgs("exhaustive splice w/2 optional args 1",a);
29 exhaustiveSpliceTestWithArgs("exhaustive splice w/2 optional args 2",b);
30
31 test();
32
33
34 function mySplice(testArray, splicedArray, first, len, elements)
35 {
36 var removedArray = [];
37 var adjustedFirst = first;
38 var adjustedLen = len;
39
40 if (adjustedFirst < 0) adjustedFirst = testArray.length + first;
41 if (adjustedFirst < 0) adjustedFirst = 0;
42
43 if (adjustedLen < 0) adjustedLen = 0;
44
45 for (i = 0; (i < adjustedFirst)&&(i < testArray.length); ++i)
46 splicedArray.push(testArray[i]);
47
48 if (adjustedFirst < testArray.length)
49 for (i = adjustedFirst; (i < adjustedFirst + adjustedLen) && (i < testArray.length); ++i)
50 removedArray.push(testArray[i]);
51
52 for (i = 0; i < elements.length; i++) splicedArray.push(elements[i]);
53
54 for (i = adjustedFirst + adjustedLen; i < testArray.length; i++)
55 splicedArray.push(testArray[i]);
56
57 return removedArray;
58 }
59
60 function exhaustiveSpliceTestWithArgs(testname, testArray)
61 {
62 var passed = true;
63 var errorMessage;
64 var reason = "";
65 for (var first = -(testArray.length+2); first <= 2 + testArray.length; first++)
66 {
67 var actualSpliced = [];
68 var expectedSpliced = [];
69 var actualRemoved = [];
70 var expectedRemoved = [];
71
72 for (var len = 0; len < testArray.length + 2; len++)
73 {
74 actualSpliced = [];
75 expectedSpliced = [];
76
77 for (var i = 0; i < testArray.length; ++i)
78 actualSpliced.push(testArray[i]);
79
80 actualRemoved = actualSpliced.splice(first,len,-97,new String("test arg"),[],9.8);
81 expectedRemoved = mySplice(testArray,expectedSpliced,first,len,[-97,new String("test arg"),[],9.8]);
82
83 var adjustedFirst = first;
84 if (adjustedFirst < 0) adjustedFirst = testArray.length + first;
85 if (adjustedFirst < 0) adjustedFirst = 0;
86
87
88 if ( (String(actualSpliced) != String(expectedSpliced))
89 ||(String(actualRemoved) != String(expectedRemoved)))
90 {
91 if ( (String(actualSpliced) == String(expectedSpliced))
92 &&(String(actualRemoved) != String(expectedRemoved)) )
93 {
94
95 if ( (expectedRemoved.length == 1)
96 &&(String(actualRemoved) == String(expectedRemoved[0]))) continue;
97 if ( expectedRemoved.length == 0 && actualRemoved == void 0 ) continue;
98 }
99
100 errorMessage =
101 "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" +
102 " test: " + "a.splice(" + first + "," + len + ",-97,new String('test arg'),[],9.8)\n" +
103 " a: " + String(testArray) + "\n" +
104 " actual spliced: " + String(actualSpliced) + "\n" +
105 " expected spliced: " + String(expectedSpliced) + "\n" +
106 " actual removed: " + String(actualRemoved) + "\n" +
107 " expected removed: " + String(expectedRemoved);
108 reason = reason + errorMessage;
109 writeHeaderToLog(errorMessage);
110 passed = false;
111 }
112 }
113 }
114 var testcase = new TestCase(SECTION, testname, true, passed);
115 if (!passed) testcase.reason = reason;
116 return testcase;
117 }