Debian Codegraph @

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