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: splice1.js 24 Description: 'Tests Array.splice(x,y) w/no 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 1'; 34 var BUGNUMBER="123795"; 35 36 writeHeaderToLog('Executing script: splice1.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) && 59 (i < testArray.length); ++i) 60 { 61 removedArray.push(testArray[i]); 62 } 63 64 for (i = 0; i < elements.length; i++) splicedArray.push(elements[i]); 65 66 for (i = adjustedFirst + adjustedLen; i < testArray.length; i++) 67 splicedArray.push(testArray[i]); 68 69 return removedArray; 70 } 71 72 function exhaustiveSpliceTest(testname, testArray) 73 { 74 var errorMessage; 75 var passed = true; 76 var reason = ""; 77 78 for (var first = -(testArray.length+2); first <= 2 + testArray.length; first++) 79 { 80 var actualSpliced = []; 81 var expectedSpliced = []; 82 var actualRemoved = []; 83 var expectedRemoved = []; 84 85 for (var len = 0; len < testArray.length + 2; len++) 86 { 87 actualSpliced = []; 88 expectedSpliced = []; 89 90 for (var i = 0; i < testArray.length; ++i) 91 actualSpliced.push(testArray[i]); 92 93 actualRemoved = actualSpliced.splice(first,len); 94 expectedRemoved = mySplice(testArray,expectedSpliced,first,len,[]); 95 96 var adjustedFirst = first; 97 if (adjustedFirst < 0) adjustedFirst = testArray.length + first; 98 if (adjustedFirst < 0) adjustedFirst = 0; 99 100 if ( (String(actualSpliced) != String(expectedSpliced)) 101 ||(String(actualRemoved) != String(expectedRemoved))) 102 { 103 if ( (String(actualSpliced) == String(expectedSpliced)) 104 &&(String(actualRemoved) != String(expectedRemoved)) ) 105 { 106 if ( (expectedRemoved.length == 1) 107 &&(String(actualRemoved) == String(expectedRemoved[0]))) continue; 108 if ( expectedRemoved.length == 0 && actualRemoved == void 0) continue; 109 } 110 111 errorMessage = 112 "ERROR: 'TEST FAILED'\n" + 113 " test: " + "a.splice(" + first + "," + len + ",-97,new String('test arg'),[],9.8)\n" + 114 " a: " + String(testArray) + "\n" + 115 " actual spliced: " + String(actualSpliced) + "\n" + 116 " expected spliced: " + String(expectedSpliced) + "\n" + 117 " actual removed: " + String(actualRemoved) + "\n" + 118 " expected removed: " + String(expectedRemoved) + "\n"; 119 writeHeaderToLog(errorMessage); 120 reason = reason + errorMessage; 121 passed = false; 122 } 123 } 124 } 125 var testcase = new TestCase( SECTION, testname, true, passed); 126 if (!passed) 127 testcase.reason = reason; 128 return testcase; 129 } 130 131 var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']]; 132 var b = [1,2,3,4,5,6,7,8,9,0]; 133 134 testcases[count++] = exhaustiveSpliceTest("exhaustive splice w/no optional args 1",a); 135 testcases[count++] = exhaustiveSpliceTest("exhaustive splice w/no optional args 1",b); 136 137 function test() 138 { 139 for ( tc=0; tc < testcases.length; tc++ ) { 140 testcases[tc].passed = writeTestCaseResult( 141 testcases[tc].expect, 142 testcases[tc].actual, 143 testcases[tc].description +" = "+ 144 testcases[tc].actual ); 145 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 146 } 147 stopTest(); 148 return ( testcases ); 149 } 150 151 test(); 152 153