1 var test = require("tap").test
4 test("basic", function (t) {
5 var cache = new LRU({max: 10})
6 cache.set("key", "value")
7 t.equal(cache.get("key"), "value")
8 t.equal(cache.get("nada"), undefined)
9 t.equal(cache.length, 1)
10 t.equal(cache.max, 10)
14 test("least recently set", function (t) {
15 var cache = new LRU(2)
19 t.equal(cache.get("c"), "C")
20 t.equal(cache.get("b"), "B")
21 t.equal(cache.get("a"), undefined)
25 test("lru recently gotten", function (t) {
26 var cache = new LRU(2)
31 t.equal(cache.get("c"), "C")
32 t.equal(cache.get("b"), undefined)
33 t.equal(cache.get("a"), "A")
37 test("del", function (t) {
38 var cache = new LRU(2)
41 t.equal(cache.get("a"), undefined)
45 test("max", function (t) {
46 var cache = new LRU(3)
48 // test changing the max, verify that the LRU items get dropped.
50 for (var i = 0; i < 100; i ++) cache.set(i, i)
51 t.equal(cache.length, 100)
52 for (var i = 0; i < 100; i ++) {
53 t.equal(cache.get(i), i)
56 t.equal(cache.length, 3)
57 for (var i = 0; i < 97; i ++) {
58 t.equal(cache.get(i), undefined)
60 for (var i = 98; i < 100; i ++) {
61 t.equal(cache.get(i), i)
64 // now remove the max restriction, and try again.
66 for (var i = 0; i < 100; i ++) cache.set(i, i)
67 t.equal(cache.length, 100)
68 for (var i = 0; i < 100; i ++) {
69 t.equal(cache.get(i), i)
71 // should trigger an immediate resize
73 t.equal(cache.length, 3)
74 for (var i = 0; i < 97; i ++) {
75 t.equal(cache.get(i), undefined)
77 for (var i = 98; i < 100; i ++) {
78 t.equal(cache.get(i), i)
83 test("reset", function (t) {
84 var cache = new LRU(10)
88 t.equal(cache.length, 0)
89 t.equal(cache.max, 10)
90 t.equal(cache.get("a"), undefined)
91 t.equal(cache.get("b"), undefined)
96 // Note: `<cache>.dump()` is a debugging tool only. No guarantees are made
97 // about the format/layout of the response.
98 test("dump", function (t) {
99 var cache = new LRU(10)
100 var d = cache.dump();
101 t.equal(Object.keys(d).length, 0, "nothing in dump for empty cache")
103 var d = cache.dump() // { a: { key: "a", value: "A", lu: 0 } }
105 t.equal(d.a.key, "a")
106 t.equal(d.a.value, "A")
113 t.equal(d.b.key, "b")
114 t.equal(d.b.value, "B")
121 test("basic with weighed length", function (t) {
122 var cache = new LRU({
124 length: function (item) { return item.size }
126 cache.set("key", {val: "value", size: 50})
127 t.equal(cache.get("key").val, "value")
128 t.equal(cache.get("nada"), undefined)
129 t.equal(cache.lengthCalculator(cache.get("key")), 50)
130 t.equal(cache.length, 50)
131 t.equal(cache.max, 100)
136 test("weighed length item too large", function (t) {
137 var cache = new LRU({
139 length: function (item) { return item.size }
141 t.equal(cache.max, 10)
143 // should fall out immediately
144 cache.set("key", {val: "value", size: 50})
146 t.equal(cache.length, 0)
147 t.equal(cache.get("key"), undefined)
151 test("least recently set with weighed length", function (t) {
152 var cache = new LRU({
154 length: function (item) { return item.length }
158 cache.set("c", "CCC")
159 cache.set("d", "DDDD")
160 t.equal(cache.get("d"), "DDDD")
161 t.equal(cache.get("c"), "CCC")
162 t.equal(cache.get("b"), undefined)
163 t.equal(cache.get("a"), undefined)
167 test("lru recently gotten with weighed length", function (t) {
168 var cache = new LRU({
170 length: function (item) { return item.length }
174 cache.set("c", "CCC")
177 cache.set("d", "DDDD")
178 t.equal(cache.get("c"), undefined)
179 t.equal(cache.get("d"), "DDDD")
180 t.equal(cache.get("b"), "BB")
181 t.equal(cache.get("a"), "A")
185 test("set returns proper booleans", function(t) {
186 var cache = new LRU({
188 length: function (item) { return item.length }
191 t.equal(cache.set("a", "A"), true)
193 // should return false for max exceeded
194 t.equal(cache.set("b", "donuts"), false)
196 t.equal(cache.set("b", "B"), true)
197 t.equal(cache.set("c", "CCCC"), true)
201 test("drop the old items", function(t) {
202 var cache = new LRU({
209 setTimeout(function () {
211 t.equal(cache.get("a"), "A")
214 setTimeout(function () {
217 t.notOk(cache.get("a"))
220 setTimeout(function () {
221 t.notOk(cache.get("b"))
222 t.equal(cache.get("c"), "C")
225 setTimeout(function () {
226 t.notOk(cache.get("c"))
231 test("disposal function", function(t) {
233 var cache = new LRU({
235 dispose: function (k, n) {
250 test("disposal function on too big of item", function(t) {
252 var cache = new LRU({
254 length: function (k) {
257 dispose: function (k, n) {
263 t.equal(disposed, false)
264 cache.set("obj", obj)
265 t.equal(disposed, obj)
269 test("has()", function(t) {
270 var cache = new LRU({
275 cache.set('foo', 'bar')
276 t.equal(cache.has('foo'), true)
277 cache.set('blu', 'baz')
278 t.equal(cache.has('foo'), false)
279 t.equal(cache.has('blu'), true)
280 setTimeout(function() {
281 t.equal(cache.has('blu'), false)
286 test("stale", function(t) {
287 var cache = new LRU({
292 cache.set('foo', 'bar')
293 t.equal(cache.get('foo'), 'bar')
294 t.equal(cache.has('foo'), true)
295 setTimeout(function() {
296 t.equal(cache.has('foo'), false)
297 t.equal(cache.get('foo'), 'bar')
298 t.equal(cache.get('foo'), undefined)
303 test("lru update via set", function(t) {
304 var cache = LRU({ max: 2 });
312 t.equal(cache.get('foo'), undefined)
313 t.equal(cache.get('bar'), undefined)
314 t.equal(cache.get('baz'), 3)
315 t.equal(cache.get('qux'), 4)
319 test("least recently set w/ peek", function (t) {
320 var cache = new LRU(2)
323 t.equal(cache.peek("a"), "A")
325 t.equal(cache.get("c"), "C")
326 t.equal(cache.get("b"), "B")
327 t.equal(cache.get("a"), undefined)
331 test("pop the least used item", function (t) {
332 var cache = new LRU(3)
339 t.equal(cache.length, 3)
340 t.equal(cache.max, 3)
342 // Ensure we pop a, c, b
346 t.equal(last.key, "a")
347 t.equal(last.value, "A")
348 t.equal(cache.length, 2)
349 t.equal(cache.max, 3)
352 t.equal(last.key, "c")
353 t.equal(last.value, "C")
354 t.equal(cache.length, 1)
355 t.equal(cache.max, 3)
358 t.equal(last.key, "b")
359 t.equal(last.value, "B")
360 t.equal(cache.length, 0)
361 t.equal(cache.max, 3)
365 t.equal(cache.length, 0)
366 t.equal(cache.max, 3)