Skip to content

Commit

Permalink
move test suite to tap
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm committed Dec 9, 2011
1 parent 83b610b commit 01a58a6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
73 changes: 0 additions & 73 deletions lib/lru-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,76 +76,3 @@ function LRUCache (maxLength) {
lruWalk()
}
}

if (!process || !module || module !== process.mainModule) return undefined

var l = LRUCache(3)
, assert = require("assert")

l.set(1, 1)
l.set(2, 1)
l.set(3, 1)
l.set(4, 1)
l.set(5, 1)
l.set(6, 1)

assert.equal(l.get(1), undefined)
assert.equal(l.get(2), undefined)
assert.equal(l.get(3), undefined)
assert.equal(l.get(4), 1)
assert.equal(l.get(5), 1)
assert.equal(l.get(6), 1)

// now keep re-getting the 6 so it remains the most recently used.
// in this case, we'll have 6, 7, 8, 9, 10, 11, so the ending length = 5
l.set(7, 1)
l.get(6)
l.set(8, 1)
l.get(6)
l.set(9, 1)
l.get(6)
l.set(10, 1)
l.get(6)
l.set(11, 1)
assert.equal(l.length, 3)
assert.equal(l.get(4), undefined)
assert.equal(l.get(5), undefined)
assert.equal(l.get(6), 1)
assert.equal(l.get(7), undefined)
assert.equal(l.get(8), undefined)
assert.equal(l.get(9), undefined)
assert.equal(l.get(10), 1)
assert.equal(l.get(11), 1)

// test changing the maxLength, verify that the LRU items get dropped.
l.maxLength = 100
for (var i = 0; i < 100; i ++) l.set(i, i)
assert.equal(l.length, 100)
for (var i = 0; i < 100; i ++) {
assert.equal(l.get(i), i)
}
l.maxLength = 3
assert.equal(l.length, 3)
for (var i = 0; i < 97; i ++) {
assert.equal(l.get(i), undefined)
}
for (var i = 98; i < 100; i ++) {
assert.equal(l.get(i), i)
}

// now remove the maxLength restriction, and try again.
l.maxLength = "hello"
for (var i = 0; i < 100; i ++) l.set(i, i)
assert.equal(l.length, 100)
for (var i = 0; i < 100; i ++) {
assert.equal(l.get(i), i)
}
// should trigger an immediate resize
l.maxLength = 3
assert.equal(l.length, 3)
for (var i = 0; i < 97; i ++) {
assert.equal(l.get(i), undefined)
}
for (var i = 98; i < 100; i ++) {
assert.equal(l.get(i), i)
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
, "description" : "A cache object that deletes the least-recently-used items."
, "version" : "1.0.4"
, "author" : "Isaac Z. Schlueter <i@izs.me>"
, "scripts" : { "test" : "node lib/lru-cache.js" }
, "scripts" : { "test" : "./node_modules/.bin/tap test" }
, "main" : "lib/lru-cache"
, "repository" : "git://github.com/isaacs/node-lru-cache.git"
, "devDependencies" :
{ "tap" : "0.1" }
, "licenses" :
[ { "type" : "MIT"
, "url" : "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE"
Expand Down
81 changes: 81 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var test = require('tap').test
, LRU = require('../')

test('basic', function (t) {
var cache = new LRU(10)
cache.set("key", "value")
t.equal(cache.get("key"), "value")
t.equal(cache.get("nada"), undefined)
t.equal(cache.length, 1)
t.equal(cache.maxLength, 10)
t.end()
})

test('least recently set', function (t) {
var cache = new LRU(2)
cache.set("a", "A")
cache.set("b", "B")
cache.set("c", "C")
t.equal(cache.get("c"), "C")
t.equal(cache.get("b"), "B")
t.equal(cache.get("a"), undefined)
t.end()
})

test('lru recently gotten', function (t) {
var cache = new LRU(2)
cache.set("a", "A")
cache.set("b", "B")
cache.get("a")
cache.set("c", "C")
t.equal(cache.get("c"), "C")
t.equal(cache.get("b"), undefined)
t.equal(cache.get("a"), "A")
t.end()
})

test('del', function (t) {
var cache = new LRU(2)
cache.set("a", "A")
cache.del("a")
t.equal(cache.get("a"), undefined)
t.end()
})

test('maxLength', function (t) {
var cache = new LRU(3)

// test changing the maxLength, verify that the LRU items get dropped.
cache.maxLength = 100
for (var i = 0; i < 100; i ++) cache.set(i, i)
t.equal(cache.length, 100)
for (var i = 0; i < 100; i ++) {
t.equal(cache.get(i), i)
}
cache.maxLength = 3
t.equal(cache.length, 3)
for (var i = 0; i < 97; i ++) {
t.equal(cache.get(i), undefined)
}
for (var i = 98; i < 100; i ++) {
t.equal(cache.get(i), i)
}

// now remove the maxLength restriction, and try again.
cache.maxLength = "hello"
for (var i = 0; i < 100; i ++) cache.set(i, i)
t.equal(cache.length, 100)
for (var i = 0; i < 100; i ++) {
t.equal(cache.get(i), i)
}
// should trigger an immediate resize
cache.maxLength = 3
t.equal(cache.length, 3)
for (var i = 0; i < 97; i ++) {
t.equal(cache.get(i), undefined)
}
for (var i = 98; i < 100; i ++) {
t.equal(cache.get(i), i)
}
t.end()
})

0 comments on commit 01a58a6

Please sign in to comment.