• 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 array = [4, 3, 2, 1, 0]
16
17array.reverse();
18
19for (i = 0; i < array.length; i++) {
20  assert(array[i] === i);
21}
22
23// Checking behavior when unable to get length
24var obj = { reverse : Array.prototype.reverse };
25Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
26
27try {
28  obj.reverse();
29  assert(false);
30} catch (e) {
31  assert(e.message === "foo");
32  assert(e instanceof ReferenceError);
33}
34
35// Checking behavior when unable to get element
36var obj = { reverse : Array.prototype.reverse, length : 3 };
37Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
38
39try {
40  obj.reverse();
41  assert(false);
42} catch (e) {
43  assert(e.message === "foo");
44  assert(e instanceof ReferenceError);
45}
46
47/* ES v5.1 15.4.4.8.6.e.
48   Checking behavior when unable to get the last element */
49var obj = { reverse : Array.prototype.reverse, length : 4 };
50Object.defineProperty(obj, '3', { 'get' : function () {throw new ReferenceError ("foo"); } });
51
52try {
53  obj.reverse();
54  assert(false);
55} catch (e) {
56  assert(e.message === "foo");
57  assert(e instanceof ReferenceError);
58}
59
60/* ES v5.1 15.4.4.8.6.h.i.
61   Checking behavior when first 3 elements are not writable */
62try {
63  var arr = [,,, 3, 4, 5, 6,,,,,,,,,0, 1, 2, 3, 4, 5, 6];
64  Object.defineProperty(arr, '0', {});
65  Object.defineProperty(arr, '1', {});
66  Object.defineProperty(arr, '2', {});
67  Array.prototype.reverse.call(arr);
68  assert(false);
69} catch (e) {
70  assert(e instanceof TypeError);
71}
72
73/* ES v5.1 15.4.4.8.6.h.ii.
74   Checking behavior when last 3 elements are not writable */
75try {
76  var arr = [0, 1, 2, 3, 4, 5, 6,,,,,,,,,0, 1, 2, 3,,,];
77  Object.defineProperty(arr, '19', {});
78  Object.defineProperty(arr, '20', {});
79  Object.defineProperty(arr, '21', {});
80  Array.prototype.reverse.call(arr);
81  assert(false);
82} catch (e) {
83  assert(e instanceof TypeError);
84}
85
86/* ES v5.1 15.4.4.8.6.i.i.
87   Checking behavior when first elements do not exist and the array is freezed */
88try {
89  var arr = [,,,,,,,,,,,,,,,,0, 1, 2, 3, 4, 5, 6];
90  arr = Object.freeze(arr);
91  Array.prototype.reverse.call(arr);
92  assert(false);
93} catch (e) {
94  assert(e instanceof TypeError);
95}
96
97/* ES v5.1 15.4.4.8.6.i.ii.
98   Checking behavior when unable to get the first 2 elements */
99var obj = { reverse : Array.prototype.reverse, length : 4 };
100Object.defineProperty(obj, '2', { value : 0 });
101Object.defineProperty(obj, '3', { value : 0 });
102try {
103  obj.reverse();
104  assert(false);
105} catch (e) {
106  assert(e instanceof TypeError);
107}
108
109/* ES v5.1 15.4.4.8.6.j.i.
110   Checking behavior when unable to get the last 2 elements */
111var obj = { reverse : Array.prototype.reverse, length : 4 };
112Object.defineProperty(obj, '0', { value : 0 });
113Object.defineProperty(obj, '1', { value : 0 });
114try {
115  obj.reverse();
116  assert(false);
117} catch (e) {
118  assert(e instanceof TypeError);
119}
120