-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.test.js
More file actions
38 lines (28 loc) · 1.25 KB
/
ex2.test.js
File metadata and controls
38 lines (28 loc) · 1.25 KB
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
'use strict';
const { simulateGenerationalGC } = require('./ex2');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
function ids(list) {
return list.map(o => o.id).sort((a, b) => a - b);
}
const objects = [
{ id: 1, age: 0 },
{ id: 2, age: 0 },
{ id: 3, age: 1 }
];
// cycle 1
let state = simulateGenerationalGC(objects);
assert(Array.isArray(state.young) && Array.isArray(state.old), 'state must have young and old arrays');
assert(JSON.stringify(ids(state.old)) === JSON.stringify([]), 'no promotion on first cycle');
assert(JSON.stringify(ids(state.young)) === JSON.stringify([1, 2, 3]), 'all objects start in young');
// cycle 2 (age increments again; age>=2 promotes)
state = simulateGenerationalGC(state.young.concat(state.old));
assert(JSON.stringify(ids(state.old)) === JSON.stringify([1, 2, 3]), 'objects with age>=2 must be promoted');
assert(JSON.stringify(ids(state.young)) === JSON.stringify([]), 'promoted objects must be removed from young');
// exclusivity: no object should exist in both spaces
const youngSet = new Set(state.young.map(o => o.id));
for (const o of state.old) {
assert(!youngSet.has(o.id), 'object must not exist in both young and old');
}
console.log('ex2 tests passed');