Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / String / 15.5.4.9-1.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          15.5.4.9-1.js
42    ECMA Section:       15.5.4.9 String.prototype.substring( start )
43    Description:
44
45    15.5.4.9 String.prototype.substring(start)
46
47    Returns a substring of the result of converting this object to a string,
48    starting from character position start and running to the end of the
49    string. The result is a string value, not a String object.
50
51    If the argument is NaN or negative, it is replaced with zero; if the
52    argument is larger than the length of the string, it is replaced with the
53    length of the string.
54
55    When the substring method is called with one argument start, the following
56    steps are taken:
57
58    1.Call ToString, giving it the this value as its argument.
59    2.Call ToInteger(start).
60    3.Compute the number of characters in Result(1).
61    4.Compute min(max(Result(2), 0), Result(3)).
62    5.Return a string whose length is the difference between Result(3) and Result(4),
63    containing characters from Result(1), namely the characters with indices Result(4)
64    through Result(3)1, in ascending order.
65
66    Author:             christine@netscape.com
67    Date:               12 november 1997
68 */
69
70 var SECTION = "15.5.4.9-1";
71 var VERSION = "ECMA_1";
72 startTest();
73 var TITLE   = "String.prototype.substring( start )";
74
75 writeHeaderToLog( SECTION + " "+ TITLE);
76
77 new TestCase( SECTION,  "String.prototype.substring.length",        2,          String.prototype.substring.length );
78 new TestCase( SECTION,  "delete String.prototype.substring.length", false,      delete String.prototype.substring.length );
79 new TestCase( SECTION,  "delete String.prototype.substring.length; String.prototype.substring.length", 2,      eval("delete String.prototype.substring.length; String.prototype.substring.length") );
80
81 // test cases for when substring is called with no arguments.
82
83 // this is a string object
84
85 new TestCase(   SECTION,
86                 "var s = new String('this is a string object'); typeof s.substring()",
87                 "string",
88                 eval("var s = new String('this is a string object'); typeof s.substring()") );
89
90 new TestCase(   SECTION,
91                 "var s = new String(''); s.substring()",
92                 "",
93                 eval("var s = new String(''); s.substring()") );
94
95
96 new TestCase(   SECTION,
97                 "var s = new String('this is a string object'); s.substring()",
98                 "this is a string object",
99                 eval("var s = new String('this is a string object'); s.substring()") );
100
101 new TestCase(   SECTION,
102                 "var s = new String('this is a string object'); s.substring(NaN)",
103                 "this is a string object",
104                 eval("var s = new String('this is a string object'); s.substring(NaN)") );
105
106
107 new TestCase(   SECTION,
108                 "var s = new String('this is a string object'); s.substring(-0.01)",
109                 "this is a string object",
110                 eval("var s = new String('this is a string object'); s.substring(-0.01)") );
111
112
113 new TestCase(   SECTION,
114                 "var s = new String('this is a string object'); s.substring(s.length)",
115                 "",
116                 eval("var s = new String('this is a string object'); s.substring(s.length)") );
117
118 new TestCase(   SECTION,
119                 "var s = new String('this is a string object'); s.substring(s.length+1)",
120                 "",
121                 eval("var s = new String('this is a string object'); s.substring(s.length+1)") );
122
123
124 new TestCase(   SECTION,
125                 "var s = new String('this is a string object'); s.substring(Infinity)",
126                 "",
127                 eval("var s = new String('this is a string object'); s.substring(Infinity)") );
128
129 new TestCase(   SECTION,
130                 "var s = new String('this is a string object'); s.substring(-Infinity)",
131                 "this is a string object",
132                 eval("var s = new String('this is a string object'); s.substring(-Infinity)") );
133
134 // this is not a String object, start is not an integer
135
136
137 new TestCase(   SECTION,
138                 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()",
139                 "1,2,3,4,5",
140                 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring()") );
141
142 new TestCase(   SECTION,
143                 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)",
144                 ",2,3,4,5",
145                 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true)") );
146
147 new TestCase(   SECTION,
148                 "var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')",
149                 "3,4,5",
150                 eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4')") );
151
152 new TestCase(   SECTION,
153                 "var s = new Array(); s.substring = String.prototype.substring; s.substring('4')",
154                 "",
155                 eval("var s = new Array(); s.substring = String.prototype.substring; s.substring('4')") );
156
157 // this is an object object
158 new TestCase(   SECTION,
159                 "var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)",
160                 "Object]",
161                 eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8)") );
162
163 // this is a function object
164 new TestCase(   SECTION,
165                 "var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)",
166                 "Function]",
167                 eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8)") );
168 // this is a number object
169 new TestCase(   SECTION,
170                 "var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)",
171                 "NaN",
172                 eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(false)") );
173
174 // this is the Math object
175 new TestCase(   SECTION,
176                 "var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)",
177                 "ject Math]",
178                 eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI)") );
179
180 // this is a Boolean object
181
182 new TestCase(   SECTION,
183                 "var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())",
184                 "false",
185                 eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array())") );
186
187 // this is a user defined object
188
189 new TestCase( SECTION,
190               "var obj = new MyObject( null ); obj.substring(0)",
191               "null",
192               eval( "var obj = new MyObject( null ); obj.substring(0)") );
193
194
195 test();
196
197 function MyObject( value ) {
198   this.value = value;
199   this.substring = String.prototype.substring;
200   this.toString = new Function ( "return this.value+''" );
201 }