• 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// This test will not pass on FLOAT32 due to precision issues
16
17var obj = {};
18var array = ["foo", 19, "bar", obj, "foo", 29, "baz"];
19
20var index = array.lastIndexOf("foo");
21assert(index === 4);
22assert(array[index] === "foo");
23
24assert(array.lastIndexOf("foo", 3) === 0);
25assert(array.lastIndexOf("foo", -8) === -1);
26
27var index = array.lastIndexOf("baz");
28assert(index === 6);
29assert(array[index] === "baz");
30
31assert(array.lastIndexOf("baz", -2) === -1);
32
33var index = array.lastIndexOf(obj);
34assert(index === 3);
35assert(array[index] === obj);
36
37assert(array.lastIndexOf("foo", NaN) === 0);
38assert(array.lastIndexOf("foo", Infinity) === 4);
39assert(array.lastIndexOf("foo", -Infinity) === -1);
40
41var arr = [];
42arr[4294967294] = "foo";
43assert(arr.lastIndexOf("foo", -1) === 4294967294)
44
45var arr = [1,2];
46assert(arr.lastIndexOf(2, undefined) === -1);
47assert(arr.lastIndexOf(2) === 1);
48
49// Checking behavior when unable to get length
50var obj = { lastIndexOf : Array.prototype.lastIndexOf}
51Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
52
53try {
54  obj.lastIndexOf("bar");
55  assert(false);
56} catch (e) {
57  assert(e.message === "foo");
58  assert(e instanceof ReferenceError);
59}
60
61// Checking behavior when unable to get element
62var obj = { lastIndexOf : Array.prototype.lastIndexOf, length : 1}
63Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
64
65try {
66  obj.lastIndexOf("bar");
67  assert(false);
68} catch (e) {
69  assert(e.message === "foo");
70  assert(e instanceof ReferenceError);
71}
72
73// Remove the buffer
74var array = [1, 2, 3, 4, 5];
75var value = array.lastIndexOf(4, {
76    valueOf: function() {
77        array.length = 0;
78    }
79})
80
81assert(value === -1);
82
83// Extend the buffer
84var array = [1, 2, 3];
85var value = array.lastIndexOf(1, {
86    valueOf: function() {
87        array.length = 5;
88    }
89})
90
91assert(value === 0);
92
93// Reduce the buffer
94var array = [1, 2, 3, 4, 5, 6, 7];
95var value = array.indexOf(5, {
96    valueOf: function() {
97        array.length = 2;
98    }
99})
100
101assert(value === -1);
102