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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { Cu } = require("chrome");
const memory = require("sdk/test/memory");
const { add, remove, has, clear, iterator } = require("sdk/lang/weak-set");
const { setInterval, clearInterval } = require("sdk/timers");
function gc(assert) {
let wait = 1;
let interval = setInterval(function() {
assert.pass("waited " + (wait++ * 0.250) + "secs for gc()..");
}, 250);
return memory.gc().then(() => {
assert.pass("gc completed!");
clearInterval(interval);
});
}
exports['test add/remove/iterate/clear item'] = function*(assert) {
let addItems = {};
let removeItems = {};
let iterateItems = {};
let clearItems = {};
let nonReferencedItems = {};
let item = {};
let addedItems = [{}, {}];
assert.pass("adding things to items");
add(addItems, item);
add(removeItems, item);
add(iterateItems, addedItems[0]);
add(iterateItems, addedItems[1]);
add(iterateItems, addedItems[0]); // weak set shouldn't add this twice
add(clearItems, addedItems[0]);
add(clearItems, addedItems[1]);
add(nonReferencedItems, {});
assert.pass("removing things from removeItems");
remove(removeItems, item);
assert.pass("clear things from clearItems");
clear(clearItems);
assert.pass("starting gc..");
yield gc(assert);
let count = 0;
assert.equal(has(addItems, item), true, 'the addItems is in the weak set');
assert.equal(has(removeItems, item), false, 'the removeItems is not in weak set');
assert.pass("iterating iterateItems..");
for (let item of iterator(iterateItems)) {
assert.equal(item, addedItems[count], "item in the expected order");
count++;
}
assert.equal(count, 2, 'items in the expected number');
assert.pass("iterating clearItems..");
for (let item of iterator(clearItems)) {
assert.fail("the loop should not be executed");
count++
}
for (let item of iterator(nonReferencedItems)) {
assert.fail("the loop should not be executed");
count++
}
assert.equal(count, 2, 'items in the expected number');
};
exports['test adding non object or null item'] = function(assert) {
let items = {};
assert.throws(() => {
add(items, 'foo');
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(items, 0);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(items, undefined);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(items, null);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(items, true);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
};
exports['test adding to non object or null item'] = function(assert) {
let item = {};
assert.throws(() => {
add('foo', item);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(0, item);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(undefined, item);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(null, item);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
assert.throws(() => {
add(true, item);
},
/^\w+ is not a non-null object/,
'only non-null object are allowed');
};
require("sdk/test").run(exports);
|