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