Debian Codegraph @

js/src/tests/test262/built-ins/Array/S15.4_A1.1_T9.js

raw
1 // Copyright 2009 the Sputnik authors. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3
4 /*---
5 info: |
6 A property name P (in the form of a string value) is an array index
7 if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32 - 1
8 es5id: 15.4_A1.1_T9
9 description: If Type(value) is Object, evaluate ToPrimitive(value, String)
10 ---*/
11
12 var x = [];
13 var object = {
14 valueOf: function() {
15 return 1
16 }
17 };
18 x[object] = 0;
19 assert.sameValue(x["[object Object]"], 0, 'The value of x["[object Object]"] is expected to be 0');
20
21 x = [];
22 var object = {
23 valueOf: function() {
24 return 1
25 },
26 toString: function() {
27 return 0
28 }
29 };
30 x[object] = 0;
31 assert.sameValue(x[0], 0, 'The value of x[0] is expected to be 0');
32
33 x = [];
34 var object = {
35 valueOf: function() {
36 return 1
37 },
38 toString: function() {
39 return {}
40 }
41 };
42 x[object] = 0;
43 assert.sameValue(x[1], 0, 'The value of x[1] is expected to be 0');
44
45 try {
46 x = [];
47 var object = {
48 valueOf: function() {
49 throw "error"
50 },
51 toString: function() {
52 return 1
53 }
54 };
55 x[object] = 0;
56 assert.sameValue(x[1], 0, 'The value of x[1] is expected to be 0');
57 }
58 catch (e) {
59 assert.notSameValue(e, "error", 'The value of e is not "error"');
60 }
61
62 x = [];
63 var object = {
64 toString: function() {
65 return 1
66 }
67 };
68 x[object] = 0;
69 assert.sameValue(x[1], 0, 'The value of x[1] is expected to be 0');
70
71 x = [];
72 var object = {
73 valueOf: function() {
74 return {}
75 },
76 toString: function() {
77 return 1
78 }
79 }
80 x[object] = 0;
81 assert.sameValue(x[1], 0, 'The value of x[1] is expected to be 0');
82
83 try {
84 x = [];
85 var object = {
86 valueOf: function() {
87 return 1
88 },
89 toString: function() {
90 throw "error"
91 }
92 };
93 x[object];
94 throw new Test262Error('#7.1: x = []; var object = {valueOf: function() {return 1}, toString: function() {throw "error"}}; x[object] throw "error". Actual: ' + (x[object]));
95 }
96 catch (e) {
97 assert.sameValue(e, "error", 'The value of e is expected to be "error"');
98 }
99
100 try {
101 x = [];
102 var object = {
103 valueOf: function() {
104 return {}
105 },
106 toString: function() {
107 return {}
108 }
109 };
110 x[object];
111 throw new Test262Error('#8.1: x = []; var object = {valueOf: function() {return {}}, toString: function() {return {}}}; x[object] throw TypeError. Actual: ' + (x[object]));
112 }
113 catch (e) {
114 assert.sameValue(
115 e instanceof TypeError,
116 true,
117 'The result of evaluating (e instanceof TypeError) is expected to be true'
118 );
119 }
120
121 reportCompare(0, 0);