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 the String object method: slice' 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 = new String("abcdefghijklmnopqrstuvwxyz1234567890"); michael@0: var b = new String("this is a test string"); michael@0: michael@0: exhaustiveStringSliceTest("exhaustive String.slice test 1", a); michael@0: exhaustiveStringSliceTest("exhaustive String.slice test 2", b); michael@0: michael@0: test(); michael@0: michael@0: michael@0: function myStringSlice(a, from, to) michael@0: { michael@0: var from2 = from; michael@0: var to2 = to; michael@0: var returnString = new String(""); 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) returnString += a.charAt(i); michael@0: } michael@0: return returnString; michael@0: } michael@0: michael@0: // This function tests the slice command on a String 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 Strings are not similar false is returned. michael@0: function exhaustiveStringSliceTest(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 = myStringSlice(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: }