| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/lru-cache/test/ |
Upload File : |
var test = require('tap').test
var LRU = require('../')
test('forEach', function (t) {
var l = new LRU(5)
for (var i = 0; i < 10; i ++) {
l.set(i, i.toString(2))
}
var i = 9
l.forEach(function (val, key, cache) {
t.equal(cache, l)
t.equal(key, i)
t.equal(val, i.toString(2))
i -= 1
})
// get in order of most recently used
l.get(6)
l.get(8)
var order = [ 8, 6, 9, 7, 5 ]
var i = 0
l.forEach(function (val, key, cache) {
var j = order[i ++]
t.equal(cache, l)
t.equal(key, j)
t.equal(val, j.toString(2))
})
t.equal(i, order.length)
i = 0
order.reverse()
l.rforEach(function (val, key, cache) {
var j = order[i ++]
t.equal(cache, l)
t.equal(key, j)
t.equal(val, j.toString(2))
})
t.equal(i, order.length)
t.end()
})
test('keys() and values()', function (t) {
var l = new LRU(5)
for (var i = 0; i < 10; i ++) {
l.set(i, i.toString(2))
}
t.similar(l.keys(), [9, 8, 7, 6, 5])
t.similar(l.values(), ['1001', '1000', '111', '110', '101'])
// get in order of most recently used
l.get(6)
l.get(8)
t.similar(l.keys(), [8, 6, 9, 7, 5])
t.similar(l.values(), ['1000', '110', '1001', '111', '101'])
t.end()
})
test('all entries are iterated over', function(t) {
var l = new LRU(5)
for (var i = 0; i < 10; i ++) {
l.set(i.toString(), i.toString(2))
}
var i = 0
l.forEach(function (val, key, cache) {
if (i > 0) {
cache.del(key)
}
i += 1
})
t.equal(i, 5)
t.equal(l.keys().length, 1)
t.end()
})
test('all stale entries are removed', function(t) {
var l = new LRU({ max: 5, maxAge: -5, stale: true })
for (var i = 0; i < 10; i ++) {
l.set(i.toString(), i.toString(2))
}
var i = 0
l.forEach(function () {
i += 1
})
t.equal(i, 5)
t.equal(l.keys().length, 0)
t.end()
})
test('expires', function (t) {
var l = new LRU({
max: 10,
maxAge: 50
})
for (var i = 0; i < 10; i++) {
l.set(i.toString(), i.toString(2), ((i % 2) ? 25 : undefined))
}
var i = 0
var order = [ 8, 6, 4, 2, 0 ]
setTimeout(function () {
l.forEach(function (val, key, cache) {
var j = order[i++]
t.equal(cache, l)
t.equal(key, j.toString())
t.equal(val, j.toString(2))
})
t.equal(i, order.length);
setTimeout(function () {
var count = 0;
l.forEach(function (val, key, cache) { count++; })
t.equal(0, count);
t.end()
}, 25)
}, 26)
})