summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/basic/dictionary-delete-compact.js
blob: 73887c978c160a83ceeb9ad22b662d10e7e97f11 (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
72
73
74
// Stress test dictionary object/map property deletion/addition/compaction.

const numProps = 1000;

// Delete a range of properties and check correctness.
function deleteRange(deleteStart, deleteEnd) {
    for (var i = 0; i < numProps; i++) {
        o["x" + i] = i;
    }
    for (var i = deleteStart; i < deleteEnd; i++) {
        delete o["x" + i];
    }
    assertEq(Object.getOwnPropertyNames(o).length,
             numProps - (deleteEnd - deleteStart));
    for (var i = 0; i < numProps; i++) {
        if (deleteStart <= i && i < deleteEnd) {
            assertEq(("x" + i) in o, false);
        } else {
            assertEq(o["x" + i], i);
        }
    }
}

// For every "stride" properties, delete all of them except one.
function deleteMany(stride) {
    for (var i = 0; i < numProps; i++) {
        o["x" + i] = i;
    }
    var propsNotDeleted = 0;
    for (var i = 0; i < numProps; i++) {
        if ((i % stride) === 1) {
            propsNotDeleted++;
            continue;
        }
        delete o["x" + i];
    }
    assertEq(Object.getOwnPropertyNames(o).length, propsNotDeleted);
    for (var i = 0; i < numProps; i++) {
        if ((i % stride) !== 1) {
            assertEq(("x" + i) in o, false);
        } else {
            assertEq(o["x" + i], i);
        }
    }
}

var o = {};

function test(useFreshObject) {
    function testOne(f) {
        if (useFreshObject) {
            o = {};
        }
        f();
    }

    for (var i = 6; i < 12; i++) {
        testOne(_ => deleteRange(i, 1000));
    }
    testOne(_ => deleteRange(100, 1000));
    testOne(_ => deleteRange(0, 1000));
    testOne(_ => deleteRange(1, 1000));
    testOne(_ => deleteRange(8, 990));

    testOne(_ => deleteMany(3));
    testOne(_ => deleteMany(7));
    testOne(_ => deleteMany(8));
    testOne(_ => deleteMany(15));
    testOne(_ => deleteMany(111));
}

test(true);
o = {};
test(false);