2015-07-14 17:23:54 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Flags: --expose-internals
|
|
|
|
|
2015-09-27 22:31:36 +00:00
|
|
|
require('../common');
|
2015-07-14 17:23:54 +00:00
|
|
|
const assert = require('assert');
|
2019-03-30 21:13:50 +00:00
|
|
|
const FreeList = require('internal/freelist');
|
2015-07-14 17:23:54 +00:00
|
|
|
|
2017-04-25 17:17:07 +00:00
|
|
|
assert.strictEqual(typeof FreeList, 'function');
|
2015-07-14 17:23:54 +00:00
|
|
|
|
2018-10-01 15:11:25 +00:00
|
|
|
const flist1 = new FreeList('flist1', 3, Object);
|
2015-07-14 17:23:54 +00:00
|
|
|
|
|
|
|
// Allocating when empty, should not change the list size
|
2018-10-01 15:11:25 +00:00
|
|
|
const result = flist1.alloc();
|
|
|
|
assert.strictEqual(typeof result, 'object');
|
2015-07-14 17:23:54 +00:00
|
|
|
assert.strictEqual(flist1.list.length, 0);
|
|
|
|
|
|
|
|
// Exhaust the free list
|
2018-10-01 15:11:25 +00:00
|
|
|
assert(flist1.free({ id: 'test1' }));
|
|
|
|
assert(flist1.free({ id: 'test2' }));
|
|
|
|
assert(flist1.free({ id: 'test3' }));
|
2015-07-14 17:23:54 +00:00
|
|
|
|
|
|
|
// Now it should not return 'true', as max length is exceeded
|
2018-10-01 15:11:25 +00:00
|
|
|
assert.strictEqual(flist1.free({ id: 'test4' }), false);
|
|
|
|
assert.strictEqual(flist1.free({ id: 'test5' }), false);
|
2015-07-14 17:23:54 +00:00
|
|
|
|
|
|
|
// At this point 'alloc' should just return the stored values
|
2018-10-01 15:11:25 +00:00
|
|
|
assert.strictEqual(flist1.alloc().id, 'test3');
|
|
|
|
assert.strictEqual(flist1.alloc().id, 'test2');
|
|
|
|
assert.strictEqual(flist1.alloc().id, 'test1');
|