Debian Codegraph @

rhino/testsrc/tests/js1_2/Array/slice.js

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