initial import
[vuplus_webkit] / Source / JavaScriptCore / tests / mozilla / js1_2 / Array / slice.js
1 /* The contents of this file are subject to the Netscape Public
2  * License Version 1.1 (the "License"); you may not use this file
3  * except in compliance with the License. You may obtain a copy of
4  * the License at http://www.mozilla.org/NPL/
5  *
6  * Software distributed under the License is distributed on an "AS
7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8  * implied. See the License for the specific language governing
9  * rights and limitations under the License.
10  *
11  * The Original Code is Mozilla Communicator client code, released March
12  * 31, 1998.
13  *
14  * The Initial Developer of the Original Code is Netscape Communications
15  * Corporation. Portions created by Netscape are
16  * Copyright (C) 1998 Netscape Communications Corporation. All
17  * Rights Reserved.
18  *
19  * Contributor(s): 
20  * 
21  */
22 /**
23         Filename:     slice.js
24         Description:  'This tests out some of the functionality on methods on the Array objects'
25
26         Author:       Nick Lerissa
27         Date:         Fri Feb 13 09:58:28 PST 1998
28 */
29
30         var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
31         var VERSION = 'no version';
32     startTest();
33         var TITLE = 'String:slice';
34
35         writeHeaderToLog('Executing script: slice.js');
36         writeHeaderToLog( SECTION + " "+ TITLE);
37
38         var count = 0;
39         var testcases = new Array();
40
41         
42         function mySlice(a, from, to)
43         {
44             var from2       = from;
45             var to2         = to;
46             var returnArray = [];
47             var i;
48         
49             if (from2 < 0) from2 = a.length + from;
50             if (to2 < 0)   to2   = a.length + to;
51         
52             if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length))
53             {
54                 if (from2 < 0)        from2 = 0;
55                 if (to2 > a.length) to2 = a.length;
56         
57                 for (i = from2; i < to2; ++i) returnArray.push(a[i]);
58             }
59             return returnArray;
60         }
61         
62         // This function tests the slice command on an Array
63         // passed in. The arguments passed into slice range in
64         // value from -5 to the length of the array + 4. Every
65         // combination of the two arguments is tested. The expected
66         // result of the slice(...) method is calculated and
67         // compared to the actual result from the slice(...) method.
68         // If the Arrays are not similar false is returned.
69         function exhaustiveSliceTest(testname, a)
70         {
71             var x = 0;
72             var y = 0;
73             var errorMessage;
74             var reason = "";
75             var passed = true;
76         
77             for (x = -(2 + a.length); x <= (2 + a.length); x++)
78                 for (y = (2 + a.length); y >= -(2 + a.length); y--)
79                 {
80                     var b  = a.slice(x,y);
81                     var c = mySlice(a,x,y);
82         
83                     if (String(b) != String(c))
84                     {
85                         errorMessage =
86                             "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" +
87                             "            test: " + "a.slice(" + x + "," + y + ")\n" +
88                             "               a: " + String(a) + "\n" +
89                             "   actual result: " + String(b) + "\n" +
90                             " expected result: " + String(c) + "\n";
91                         writeHeaderToLog(errorMessage);
92                         reason = reason + errorMessage;
93                         passed = false;
94                     }
95                 }
96             var testCase = new TestCase(SECTION, testname, true, passed);
97             if (passed == false)
98                 testCase.reason = reason;
99             return testCase;
100         }
101         
102         var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']];
103         var b = [1,2,3,4,5,6,7,8,9,0];
104         
105         testcases[count++] = exhaustiveSliceTest("exhaustive slice test 1", a);
106         testcases[count++] = exhaustiveSliceTest("exhaustive slice test 2", b);
107         
108         function test()
109         {
110            for ( tc=0; tc < testcases.length; tc++ ) {
111                 testcases[tc].passed = writeTestCaseResult(
112                 testcases[tc].expect,
113                 testcases[tc].actual,
114                 testcases[tc].description +" = "+
115                 testcases[tc].actual );
116                 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
117            }
118            stopTest();
119            return ( testcases );
120         }
121
122         test();
123