1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_2/String/slice.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + Filename: slice.js 1.12 + Description: 'This tests the String object method: slice' 1.13 + 1.14 + Author: Nick Lerissa 1.15 + Date: Fri Feb 13 09:58:28 PST 1998 1.16 +*/ 1.17 + 1.18 +var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; 1.19 +var VERSION = 'no version'; 1.20 +startTest(); 1.21 +var TITLE = 'String.slice'; 1.22 + 1.23 +writeHeaderToLog('Executing script: slice.js'); 1.24 +writeHeaderToLog( SECTION + " "+ TITLE); 1.25 + 1.26 +var a = new String("abcdefghijklmnopqrstuvwxyz1234567890"); 1.27 +var b = new String("this is a test string"); 1.28 + 1.29 +exhaustiveStringSliceTest("exhaustive String.slice test 1", a); 1.30 +exhaustiveStringSliceTest("exhaustive String.slice test 2", b); 1.31 + 1.32 +test(); 1.33 + 1.34 + 1.35 +function myStringSlice(a, from, to) 1.36 +{ 1.37 + var from2 = from; 1.38 + var to2 = to; 1.39 + var returnString = new String(""); 1.40 + var i; 1.41 + 1.42 + if (from2 < 0) from2 = a.length + from; 1.43 + if (to2 < 0) to2 = a.length + to; 1.44 + 1.45 + if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length)) 1.46 + { 1.47 + if (from2 < 0) from2 = 0; 1.48 + if (to2 > a.length) to2 = a.length; 1.49 + 1.50 + for (i = from2; i < to2; ++i) returnString += a.charAt(i); 1.51 + } 1.52 + return returnString; 1.53 +} 1.54 + 1.55 +// This function tests the slice command on a String 1.56 +// passed in. The arguments passed into slice range in 1.57 +// value from -5 to the length of the array + 4. Every 1.58 +// combination of the two arguments is tested. The expected 1.59 +// result of the slice(...) method is calculated and 1.60 +// compared to the actual result from the slice(...) method. 1.61 +// If the Strings are not similar false is returned. 1.62 +function exhaustiveStringSliceTest(testname, a) 1.63 +{ 1.64 + var x = 0; 1.65 + var y = 0; 1.66 + var errorMessage; 1.67 + var reason = ""; 1.68 + var passed = true; 1.69 + 1.70 + for (x = -(2 + a.length); x <= (2 + a.length); x++) 1.71 + for (y = (2 + a.length); y >= -(2 + a.length); y--) 1.72 + { 1.73 + var b = a.slice(x,y); 1.74 + var c = myStringSlice(a,x,y); 1.75 + 1.76 + if (String(b) != String(c)) 1.77 + { 1.78 + errorMessage = 1.79 + "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" + 1.80 + " test: " + "a.slice(" + x + "," + y + ")\n" + 1.81 + " a: " + String(a) + "\n" + 1.82 + " actual result: " + String(b) + "\n" + 1.83 + " expected result: " + String(c) + "\n"; 1.84 + writeHeaderToLog(errorMessage); 1.85 + reason = reason + errorMessage; 1.86 + passed = false; 1.87 + } 1.88 + } 1.89 + var testCase = new TestCase(SECTION, testname, true, passed); 1.90 + if (passed == false) 1.91 + testCase.reason = reason; 1.92 + return testCase; 1.93 +}