• 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
15var obj = {};
16var array = ["foo", 19, "bar", obj, "foo", 29, "baz"];
17
18var index = array.indexOf("foo");
19assert(index === 0);
20assert(array[index] === "foo");
21
22assert(array.indexOf("foo", 1) === 4);
23assert(array.indexOf("foo", 5) === -1);
24
25var index = array.indexOf("baz");
26assert(index === 6);
27assert(array[index] === "baz");
28
29assert(array.indexOf("baz", 7) === -1);
30
31var index = array.indexOf(obj);
32assert(index === 3);
33assert(array[index] === obj);
34
35assert(array.indexOf("foo", NaN) === 0);
36assert(array.indexOf("foo", Infinity) === -1);
37assert(array.indexOf("foo", -Infinity) === 0);
38
39assert([true].indexOf(true, -0) === 0);
40
41// Checking behavior when length is zero
42var obj = { indexOf : Array.prototype.indexOf, length : 0 };
43assert(obj.indexOf("foo") === -1);
44
45// Checking behavior when start index >= length
46var arr = [11, 22, 33, 44];
47assert(arr.indexOf(44, 4) === -1);
48
49var fromIndex = {
50  toString: function () {
51    return {};
52  },
53
54  valueOf: function () {
55    return {};
56  }
57};
58
59try {
60  [0, 1].indexOf(1, fromIndex);
61  assert(false);
62} catch (e) {
63  assert(e instanceof TypeError);
64}
65
66// Checking behavior when unable to get length
67var obj = { indexOf : Array.prototype.indexOf}
68Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
69
70try {
71  obj.indexOf("bar");
72  assert(false);
73} catch (e) {
74  assert(e.message === "foo");
75  assert(e instanceof ReferenceError);
76}
77
78// Checking behavior when unable to get element
79var obj = { indexOf : Array.prototype.indexOf, length : 1}
80Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
81
82try {
83  obj.indexOf("bar");
84  assert(false);
85} catch (e) {
86  assert(e.message === "foo");
87  assert(e instanceof ReferenceError);
88}
89
90// Remove the buffer
91var array = [1, 2, 3, 4, 5];
92var value = array.indexOf(4, {
93    valueOf: function() {
94        array.length = 0;
95    }
96})
97
98assert(value === -1);
99
100// Extend the buffer
101var array = [1, 2, 3];
102var value = array.indexOf(2, {
103    valueOf: function() {
104        array.length = 5;
105    }
106})
107
108assert(value === 1);
109
110// Reduce the buffer
111var array = [1, 2, 3, 4, 5, 6, 7];
112var value = array.indexOf(6, {
113    valueOf: function() {
114        array.length = 5;
115    }
116})
117
118assert(value === -1);
119