summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/object/values-entries-indexed.js
blob: f4d801c272d3536a038a07bcf3e3880c0d02beed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function assertSameEntries(actual, expected) {
    assertEq(actual.length, expected.length);
    for (let i = 0; i < expected.length; ++i)
        assertEqArray(actual[i], expected[i]);
}

// Test Object.keys/values/entries on objects with indexed properties.

// Empty dense elements, test with array and plain object.
{
    let array = [];
    assertEqArray(Object.keys(array), []);
    assertEqArray(Object.values(array), []);
    assertSameEntries(Object.entries(array), []);

    let object = {};
    assertEqArray(Object.keys(object), []);
    assertEqArray(Object.values(object), []);
    assertSameEntries(Object.entries(object), []);
}

// Dense elements only, test with array and plain object.
{
    let array = [1, 2, 3];
    assertEqArray(Object.keys(array), ["0", "1", "2"]);
    assertEqArray(Object.values(array), [1, 2, 3]);
    assertSameEntries(Object.entries(array), [["0", 1], ["1", 2], ["2", 3]]);

    let object = {0: 4, 1: 5, 2: 6};
    assertEqArray(Object.keys(object), ["0", "1", "2"]);
    assertEqArray(Object.values(object), [4, 5, 6]);
    assertSameEntries(Object.entries(object), [["0", 4], ["1", 5], ["2", 6]]);
}

// Extra indexed properties only, test with array and plain object.
{
    let array = [];
    Object.defineProperty(array, 0, {configurable: true, enumerable: true, value: 123});

    assertEqArray(Object.keys(array), ["0"]);
    assertEqArray(Object.values(array), [123]);
    assertSameEntries(Object.entries(array), [["0", 123]]);

    let object = [];
    Object.defineProperty(object, 0, {configurable: true, enumerable: true, value: 123});

    assertEqArray(Object.keys(object), ["0"]);
    assertEqArray(Object.values(object), [123]);
    assertSameEntries(Object.entries(object), [["0", 123]]);
}

// Dense and extra indexed properties, test with array and plain object.
{
    let array = [1, 2, 3];
    Object.defineProperty(array, 0, {writable: false});

    assertEqArray(Object.keys(array), ["0", "1", "2"]);
    assertEqArray(Object.values(array), [1, 2, 3]);
    assertSameEntries(Object.entries(array), [["0", 1], ["1", 2], ["2", 3]]);

    let object = {0: 4, 1: 5, 2: 6};
    Object.defineProperty(object, 0, {writable: false});

    assertEqArray(Object.keys(object), ["0", "1", "2"]);
    assertEqArray(Object.values(object), [4, 5, 6]);
    assertSameEntries(Object.entries(object), [["0", 4], ["1", 5], ["2", 6]]);
}


if (typeof reportCompare === "function")
    reportCompare(true, true);