|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * Date: 03 December 2000 |
|
8 * |
|
9 * |
|
10 * SUMMARY: This test arose from Bugzilla bug 57043: |
|
11 * "Negative integers as object properties: strange behavior!" |
|
12 * |
|
13 * We check that object properties may be indexed by signed |
|
14 * numeric literals, as in assignments like obj[-1] = 'Hello' |
|
15 * |
|
16 * NOTE: it should not matter whether we provide the literal with |
|
17 * quotes around it or not; e.g. these should be equivalent: |
|
18 * |
|
19 * obj[-1] = 'Hello' |
|
20 * obj['-1'] = 'Hello' |
|
21 */ |
|
22 //----------------------------------------------------------------------------- |
|
23 var BUGNUMBER = 57043; |
|
24 var summary = 'Indexing object properties by signed numerical literals -' |
|
25 var statprefix = 'Adding a property to test object with an index of '; |
|
26 var statsuffix = ', testing it now -'; |
|
27 var propprefix = 'This is property '; |
|
28 var obj = new Object(); |
|
29 var status = ''; var actual = ''; var expect = ''; var value = ''; |
|
30 |
|
31 |
|
32 // various indices to try - |
|
33 var index = |
|
34 [-1073741825, -1073741824, -1073741823, -5000, -507, -3, -2, -1, -0, 0, 1, 2, 3, 1073741823, 1073741824, 1073741825]; |
|
35 |
|
36 |
|
37 //------------------------------------------------------------------------------------------------- |
|
38 test(); |
|
39 //------------------------------------------------------------------------------------------------- |
|
40 |
|
41 |
|
42 function test() |
|
43 { |
|
44 enterFunc ('test'); |
|
45 printBugNumber(BUGNUMBER); |
|
46 printStatus (summary); |
|
47 |
|
48 for (var j in index) {testProperty(index[j]);} |
|
49 |
|
50 exitFunc ('test'); |
|
51 } |
|
52 |
|
53 |
|
54 function testProperty(i) |
|
55 { |
|
56 status = getStatus(i); |
|
57 |
|
58 // try to assign a property using the given index - |
|
59 obj[i] = value = (propprefix + i); |
|
60 |
|
61 // try to read the property back via the index (as number) - |
|
62 expect = value; |
|
63 actual = obj[i]; |
|
64 reportCompare(expect, actual, status); |
|
65 |
|
66 // try to read the property back via the index as string - |
|
67 expect = value; |
|
68 actual = obj[String(i)]; |
|
69 reportCompare(expect, actual, status); |
|
70 } |
|
71 |
|
72 function positive(n) { return 1 / n > 0; } |
|
73 |
|
74 function getStatus(i) |
|
75 { |
|
76 return statprefix + |
|
77 (positive(i) ? i : "-" + -i) + |
|
78 statsuffix; |
|
79 } |