]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_2/String/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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
24 Description: 'This tests the String object method: slice'
27 Date: Fri Feb 13 09:58:28 PST 1998
30 var SECTION
= 'As described in Netscape doc "Whats new in JavaScript 1.2"';
31 var VERSION
= 'no version';
33 var TITLE
= 'String.slice';
35 writeHeaderToLog('Executing script: slice.js');
36 writeHeaderToLog( SECTION
+ " "+ TITLE
);
39 var testcases
= new Array();
42 function myStringSlice(a
, from, to
)
46 var returnString
= new String("");
49 if (from2
< 0) from2
= a
.length
+ from;
50 if (to2
< 0) to2
= a
.length
+ to
;
52 if ((to2
> from2
)&&(to2
> 0)&&(from2
< a
.length
))
54 if (from2
< 0) from2
= 0;
55 if (to2
> a
.length
) to2
= a
.length
;
57 for (i
= from2
; i
< to2
; ++i
) returnString
+= a
.charAt(i
);
62 // This function tests the slice command on a String
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 Strings are not similar false is returned.
69 function exhaustiveStringSliceTest(testname
, a
)
77 for (x
= -(2 + a
.length
); x
<= (2 + a
.length
); x
++)
78 for (y
= (2 + a
.length
); y
>= -(2 + a
.length
); y
--)
81 var c
= myStringSlice(a
,x
,y
);
83 if (String(b
) != String(c
))
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
;
96 var testCase
= new TestCase(SECTION
, testname
, true, passed
);
98 testCase
.reason
= reason
;
102 var a
= new String("abcdefghijklmnopqrstuvwxyz1234567890");
103 var b
= new String("this is a test string");
105 testcases
[count
++] = exhaustiveStringSliceTest("exhaustive String.slice test 1", a
);
106 testcases
[count
++] = exhaustiveStringSliceTest("exhaustive String.slice test 2", b
);
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 ";
119 return ( testcases
);