|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 Filename: slice.js |
|
9 Description: 'This tests out some of the functionality on methods on the Array objects' |
|
10 |
|
11 Author: Nick Lerissa |
|
12 Date: Fri Feb 13 09:58:28 PST 1998 |
|
13 */ |
|
14 |
|
15 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; |
|
16 var VERSION = 'no version'; |
|
17 startTest(); |
|
18 var TITLE = 'String:slice'; |
|
19 |
|
20 writeHeaderToLog('Executing script: slice.js'); |
|
21 writeHeaderToLog( SECTION + " "+ TITLE); |
|
22 |
|
23 var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']]; |
|
24 var b = [1,2,3,4,5,6,7,8,9,0]; |
|
25 |
|
26 exhaustiveSliceTest("exhaustive slice test 1", a); |
|
27 exhaustiveSliceTest("exhaustive slice test 2", b); |
|
28 |
|
29 test(); |
|
30 |
|
31 function mySlice(a, from, to) |
|
32 { |
|
33 var from2 = from; |
|
34 var to2 = to; |
|
35 var returnArray = []; |
|
36 var i; |
|
37 |
|
38 if (from2 < 0) from2 = a.length + from; |
|
39 if (to2 < 0) to2 = a.length + to; |
|
40 |
|
41 if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length)) |
|
42 { |
|
43 if (from2 < 0) from2 = 0; |
|
44 if (to2 > a.length) to2 = a.length; |
|
45 |
|
46 for (i = from2; i < to2; ++i) returnArray.push(a[i]); |
|
47 } |
|
48 return returnArray; |
|
49 } |
|
50 |
|
51 // This function tests the slice command on an Array |
|
52 // passed in. The arguments passed into slice range in |
|
53 // value from -5 to the length of the array + 4. Every |
|
54 // combination of the two arguments is tested. The expected |
|
55 // result of the slice(...) method is calculated and |
|
56 // compared to the actual result from the slice(...) method. |
|
57 // If the Arrays are not similar false is returned. |
|
58 function exhaustiveSliceTest(testname, a) |
|
59 { |
|
60 var x = 0; |
|
61 var y = 0; |
|
62 var errorMessage; |
|
63 var reason = ""; |
|
64 var passed = true; |
|
65 |
|
66 for (x = -(2 + a.length); x <= (2 + a.length); x++) |
|
67 for (y = (2 + a.length); y >= -(2 + a.length); y--) |
|
68 { |
|
69 var b = a.slice(x,y); |
|
70 var c = mySlice(a,x,y); |
|
71 |
|
72 if (String(b) != String(c)) |
|
73 { |
|
74 errorMessage = |
|
75 "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" + |
|
76 " test: " + "a.slice(" + x + "," + y + ")\n" + |
|
77 " a: " + String(a) + "\n" + |
|
78 " actual result: " + String(b) + "\n" + |
|
79 " expected result: " + String(c) + "\n"; |
|
80 writeHeaderToLog(errorMessage); |
|
81 reason = reason + errorMessage; |
|
82 passed = false; |
|
83 } |
|
84 } |
|
85 var testCase = new TestCase(SECTION, testname, true, passed); |
|
86 if (passed == false) |
|
87 testCase.reason = reason; |
|
88 return testCase; |
|
89 } |