js/src/tests/js1_2/Array/splice1.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial