Skip to content

Commit

Permalink
add move-to-tail test
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 7, 2022
1 parent abcab87 commit 6536230
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/move-to-tail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const LRU = require('../')
const c = new LRU({ max: 5 })
const t = require('tap')

t.test('list integrity', { bail: true }, t => {
const e = index => ({
index,
prev: c.prev[index],
_: index === c.tail ? 'T' : index === c.head ? 'H' : ('' + index),
next: c.next[index],
head: c.head,
tail: c.tail,
})
const snap = () => {
const a = []
for (let i = 0; i < 5; i++) {
a.push(e(i))
}
return a
}
const integrity = (msg) => {
t.test(msg, { bail: false }, t => {
let fail = false
for (let i = 0; i < c.max; i++) {
if (i !== c.head) {
t.equal(c.next[c.prev[i]], i, 'n[p[i]] === i')
}
if (i !== c.tail) {
t.equal(c.prev[c.next[i]], i, 'p[n[i]] === i')
}
}
t.end()
})
}

for (let i = 0; i < 5; i++) {
c.set(i, i)
}

t.matchSnapshot(snap(), 'list after initial fill')
integrity('after initial fill')
c.moveToTail(2)
t.matchSnapshot(snap(), 'list after moveToTail 2')
integrity('after moveToTail 2')
c.moveToTail(4)
t.matchSnapshot(snap(), 'list after moveToTail 4')
integrity('after moveToTail 4')

t.end()
})

0 comments on commit 6536230

Please sign in to comment.