michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: Filename: slice.js michael@0: Description: 'This tests out some of the functionality on methods on the Array objects' michael@0: michael@0: Author: Nick Lerissa michael@0: Date: Fri Feb 13 09:58:28 PST 1998 michael@0: */ michael@0: michael@0: var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; michael@0: var VERSION = 'no version'; michael@0: startTest(); michael@0: var TITLE = 'String:slice'; michael@0: michael@0: writeHeaderToLog('Executing script: slice.js'); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']]; michael@0: var b = [1,2,3,4,5,6,7,8,9,0]; michael@0: michael@0: exhaustiveSliceTest("exhaustive slice test 1", a); michael@0: exhaustiveSliceTest("exhaustive slice test 2", b); michael@0: michael@0: test(); michael@0: michael@0: function mySlice(a, from, to) michael@0: { michael@0: var from2 = from; michael@0: var to2 = to; michael@0: var returnArray = []; michael@0: var i; michael@0: michael@0: if (from2 < 0) from2 = a.length + from; michael@0: if (to2 < 0) to2 = a.length + to; michael@0: michael@0: if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length)) michael@0: { michael@0: if (from2 < 0) from2 = 0; michael@0: if (to2 > a.length) to2 = a.length; michael@0: michael@0: for (i = from2; i < to2; ++i) returnArray.push(a[i]); michael@0: } michael@0: return returnArray; michael@0: } michael@0: michael@0: // This function tests the slice command on an Array michael@0: // passed in. The arguments passed into slice range in michael@0: // value from -5 to the length of the array + 4. Every michael@0: // combination of the two arguments is tested. The expected michael@0: // result of the slice(...) method is calculated and michael@0: // compared to the actual result from the slice(...) method. michael@0: // If the Arrays are not similar false is returned. michael@0: function exhaustiveSliceTest(testname, a) michael@0: { michael@0: var x = 0; michael@0: var y = 0; michael@0: var errorMessage; michael@0: var reason = ""; michael@0: var passed = true; michael@0: michael@0: for (x = -(2 + a.length); x <= (2 + a.length); x++) michael@0: for (y = (2 + a.length); y >= -(2 + a.length); y--) michael@0: { michael@0: var b = a.slice(x,y); michael@0: var c = mySlice(a,x,y); michael@0: michael@0: if (String(b) != String(c)) michael@0: { michael@0: errorMessage = michael@0: "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" + michael@0: " test: " + "a.slice(" + x + "," + y + ")\n" + michael@0: " a: " + String(a) + "\n" + michael@0: " actual result: " + String(b) + "\n" + michael@0: " expected result: " + String(c) + "\n"; michael@0: writeHeaderToLog(errorMessage); michael@0: reason = reason + errorMessage; michael@0: passed = false; michael@0: } michael@0: } michael@0: var testCase = new TestCase(SECTION, testname, true, passed); michael@0: if (passed == false) michael@0: testCase.reason = reason; michael@0: return testCase; michael@0: }