• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// check properties
16
17function length_configurable()
18{
19  function is_es51() {
20    return (typeof g === "function");
21    { function g() {} }
22  }
23  return is_es51() ? false : true;
24}
25
26assert(Object.getOwnPropertyDescriptor(String.prototype.lastIndexOf, 'length').configurable === length_configurable());
27
28assert(Object.getOwnPropertyDescriptor(String.prototype.lastIndexOf, 'length').enumerable === false);
29
30assert(Object.getOwnPropertyDescriptor(String.prototype.lastIndexOf, 'length').writable === false);
31
32assert(String.prototype.lastIndexOf.length === 1);
33
34// simple checks
35assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome") === 15);
36
37assert("Hello world, welcome to the universe.".lastIndexOf("Hello world, welcome to the universe.") === 0);
38
39assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome", 5) === -1);
40
41assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome", -100) == -1);
42
43assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome", 15) === 15);
44
45assert("Hello welcome, welcome to the universe o.".lastIndexOf("o", 10) === 10);
46
47assert("Hello welcome, welcome to the universe o.".lastIndexOf("o", 25) === 24);
48
49assert("Helloooo woooorld".lastIndexOf("oooo", 6) === 4);
50
51// check utf8 strings
52assert("\uFFA2".lastIndexOf("\uFFA2") === 0);
53
54assert("\uFFA2".lastIndexOf("A") === -1);
55
56assert("w2\uFFA2 A".lastIndexOf("A") === 4);
57
58assert("w2\u1D306A".indexOf("A") === 4);
59
60assert("\u0070A".lastIndexOf("A") === 1);
61
62assert("\u8000A".lastIndexOf("A") === 1);
63
64assert("\u0080\u0080\u0980\u1080A".lastIndexOf("A") === 4);
65
66assert("\u0080\u0980\u1080A\u0080\u0080\u0980\u1080".lastIndexOf("A", 4) === 3);
67
68assert("\u0080\u0080\u0980\u1080A\u0980AA\u0980A".lastIndexOf("A\u0980A") === 7);
69
70assert("\u0080\u0080\u0980\u1080A\u0980AA\u0980A".lastIndexOf("A\u0980A", 4) === 4);
71
72assert("\uD834\uDF06".lastIndexOf("\uDF06") === 1);
73
74assert("\uD834\uDF06w2\u1D306D\uDF06w2\u1D306D".lastIndexOf("D") === 12);
75
76assert("\ud800\dc00\ud800\dc00".lastIndexOf("\dc00") === 6);
77
78// check empty string
79assert(String.prototype.lastIndexOf.call(new String()) === -1);
80
81assert(String.prototype.lastIndexOf.call("Hello world, welcome to the universe.","") === 37);
82
83assert(String.prototype.lastIndexOf.call("","") === 0);
84
85// check NaN
86assert("Hello world, welcome to the universe.".lastIndexOf(NaN) === -1);
87
88assert("Hello world, welcome to the universe.".lastIndexOf("o", NaN) === 22);
89
90// check Object
91assert(String.prototype.lastIndexOf.call({}) === -1);
92
93// check +-Inf
94assert("hello world!".lastIndexOf("world", -Infinity) === -1);
95
96assert("hello world!".lastIndexOf("world", Infinity) === 6);
97
98// check numbers
99assert("hello world!".lastIndexOf(-1) === -1);
100
101assert("hello 0 world!".lastIndexOf(-0) === 6);
102
103// check undefined
104assert("hello world!".lastIndexOf(undefined) === -1);
105
106var undefined_var;
107assert("Hello world, welcome to the universe.".lastIndexOf("welcome", undefined_var) === 13);
108
109// check booleans
110assert("true".lastIndexOf(true, false) === 0);
111
112// check coercible - undefined
113try {
114  String.prototype.lastIndexOf.call(undefined);
115  assert(false);
116} catch(e) {
117  assert(e instanceof TypeError);
118}
119
120// check coercible - null
121try {
122  String.prototype.lastIndexOf.call(null);
123  assert(false);
124} catch (e) {
125  assert(e instanceof TypeError);
126}
127
128// check coercible - Boolean
129assert(String.prototype.lastIndexOf.call(true, "e") === 3);
130assert(String.prototype.lastIndexOf.call(false, "e") === 4);
131
132// check coercible - Object
133var test_object = {firstName:"John", lastName:"Doe"};
134assert(String.prototype.lastIndexOf.call(test_object, "Obj") === 8);
135
136// check coercible - Number
137assert(String.prototype.lastIndexOf.call(123, "2") === 1);
138