Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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: slice.js |
michael@0 | 9 | Description: 'This tests the String object method: slice' |
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 | startTest(); |
michael@0 | 18 | var TITLE = 'String.slice'; |
michael@0 | 19 | |
michael@0 | 20 | writeHeaderToLog('Executing script: slice.js'); |
michael@0 | 21 | writeHeaderToLog( SECTION + " "+ TITLE); |
michael@0 | 22 | |
michael@0 | 23 | var a = new String("abcdefghijklmnopqrstuvwxyz1234567890"); |
michael@0 | 24 | var b = new String("this is a test string"); |
michael@0 | 25 | |
michael@0 | 26 | exhaustiveStringSliceTest("exhaustive String.slice test 1", a); |
michael@0 | 27 | exhaustiveStringSliceTest("exhaustive String.slice test 2", b); |
michael@0 | 28 | |
michael@0 | 29 | test(); |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | function myStringSlice(a, from, to) |
michael@0 | 33 | { |
michael@0 | 34 | var from2 = from; |
michael@0 | 35 | var to2 = to; |
michael@0 | 36 | var returnString = new String(""); |
michael@0 | 37 | var i; |
michael@0 | 38 | |
michael@0 | 39 | if (from2 < 0) from2 = a.length + from; |
michael@0 | 40 | if (to2 < 0) to2 = a.length + to; |
michael@0 | 41 | |
michael@0 | 42 | if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length)) |
michael@0 | 43 | { |
michael@0 | 44 | if (from2 < 0) from2 = 0; |
michael@0 | 45 | if (to2 > a.length) to2 = a.length; |
michael@0 | 46 | |
michael@0 | 47 | for (i = from2; i < to2; ++i) returnString += a.charAt(i); |
michael@0 | 48 | } |
michael@0 | 49 | return returnString; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | // This function tests the slice command on a String |
michael@0 | 53 | // passed in. The arguments passed into slice range in |
michael@0 | 54 | // value from -5 to the length of the array + 4. Every |
michael@0 | 55 | // combination of the two arguments is tested. The expected |
michael@0 | 56 | // result of the slice(...) method is calculated and |
michael@0 | 57 | // compared to the actual result from the slice(...) method. |
michael@0 | 58 | // If the Strings are not similar false is returned. |
michael@0 | 59 | function exhaustiveStringSliceTest(testname, a) |
michael@0 | 60 | { |
michael@0 | 61 | var x = 0; |
michael@0 | 62 | var y = 0; |
michael@0 | 63 | var errorMessage; |
michael@0 | 64 | var reason = ""; |
michael@0 | 65 | var passed = true; |
michael@0 | 66 | |
michael@0 | 67 | for (x = -(2 + a.length); x <= (2 + a.length); x++) |
michael@0 | 68 | for (y = (2 + a.length); y >= -(2 + a.length); y--) |
michael@0 | 69 | { |
michael@0 | 70 | var b = a.slice(x,y); |
michael@0 | 71 | var c = myStringSlice(a,x,y); |
michael@0 | 72 | |
michael@0 | 73 | if (String(b) != String(c)) |
michael@0 | 74 | { |
michael@0 | 75 | errorMessage = |
michael@0 | 76 | "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" + |
michael@0 | 77 | " test: " + "a.slice(" + x + "," + y + ")\n" + |
michael@0 | 78 | " a: " + String(a) + "\n" + |
michael@0 | 79 | " actual result: " + String(b) + "\n" + |
michael@0 | 80 | " expected result: " + String(c) + "\n"; |
michael@0 | 81 | writeHeaderToLog(errorMessage); |
michael@0 | 82 | reason = reason + errorMessage; |
michael@0 | 83 | passed = false; |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | var testCase = new TestCase(SECTION, testname, true, passed); |
michael@0 | 87 | if (passed == false) |
michael@0 | 88 | testCase.reason = reason; |
michael@0 | 89 | return testCase; |
michael@0 | 90 | } |