| 1 |
/* -*- indent-tabs-mode: nil; js-indent-level: 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 |
printBugNumber(BUGNUMBER); |
| 45 |
printStatus (summary); |
| 46 |
|
| 47 |
for (var j in index) {testProperty(index[j]);} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
function testProperty(i) |
| 52 |
{ |
| 53 |
status = getStatus(i); |
| 54 |
|
| 55 |
// try to assign a property using the given index - |
| 56 |
obj[i] = value = (propprefix + i); |
| 57 |
|
| 58 |
// try to read the property back via the index (as number) - |
| 59 |
expect = value; |
| 60 |
actual = obj[i]; |
| 61 |
reportCompare(expect, actual, status); |
| 62 |
|
| 63 |
// try to read the property back via the index as string - |
| 64 |
expect = value; |
| 65 |
actual = obj[String(i)]; |
| 66 |
reportCompare(expect, actual, status); |
| 67 |
} |
| 68 |
|
| 69 |
function positive(n) { return 1 / n > 0; } |
| 70 |
|
| 71 |
function getStatus(i) |
| 72 |
{ |
| 73 |
return statprefix + |
| 74 |
(positive(i) ? i : "-" + -i) + |
| 75 |
statsuffix; |
| 76 |
} |