Initial commit
[yaffs-website] / node_modules / lru-cache / test / basic.js
1 var test = require("tap").test
2   , LRU = require("../")
3
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)
11   t.end()
12 })
13
14 test("least recently set", function (t) {
15   var cache = new LRU(2)
16   cache.set("a", "A")
17   cache.set("b", "B")
18   cache.set("c", "C")
19   t.equal(cache.get("c"), "C")
20   t.equal(cache.get("b"), "B")
21   t.equal(cache.get("a"), undefined)
22   t.end()
23 })
24
25 test("lru recently gotten", function (t) {
26   var cache = new LRU(2)
27   cache.set("a", "A")
28   cache.set("b", "B")
29   cache.get("a")
30   cache.set("c", "C")
31   t.equal(cache.get("c"), "C")
32   t.equal(cache.get("b"), undefined)
33   t.equal(cache.get("a"), "A")
34   t.end()
35 })
36
37 test("del", function (t) {
38   var cache = new LRU(2)
39   cache.set("a", "A")
40   cache.del("a")
41   t.equal(cache.get("a"), undefined)
42   t.end()
43 })
44
45 test("max", function (t) {
46   var cache = new LRU(3)
47
48   // test changing the max, verify that the LRU items get dropped.
49   cache.max = 100
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)
54   }
55   cache.max = 3
56   t.equal(cache.length, 3)
57   for (var i = 0; i < 97; i ++) {
58     t.equal(cache.get(i), undefined)
59   }
60   for (var i = 98; i < 100; i ++) {
61     t.equal(cache.get(i), i)
62   }
63
64   // now remove the max restriction, and try again.
65   cache.max = "hello"
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)
70   }
71   // should trigger an immediate resize
72   cache.max = 3
73   t.equal(cache.length, 3)
74   for (var i = 0; i < 97; i ++) {
75     t.equal(cache.get(i), undefined)
76   }
77   for (var i = 98; i < 100; i ++) {
78     t.equal(cache.get(i), i)
79   }
80   t.end()
81 })
82
83 test("reset", function (t) {
84   var cache = new LRU(10)
85   cache.set("a", "A")
86   cache.set("b", "B")
87   cache.reset()
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)
92   t.end()
93 })
94
95
96 test("basic with weighed length", function (t) {
97   var cache = new LRU({
98     max: 100,
99     length: function (item) { return item.size }
100   })
101   cache.set("key", {val: "value", size: 50})
102   t.equal(cache.get("key").val, "value")
103   t.equal(cache.get("nada"), undefined)
104   t.equal(cache.lengthCalculator(cache.get("key")), 50)
105   t.equal(cache.length, 50)
106   t.equal(cache.max, 100)
107   t.end()
108 })
109
110
111 test("weighed length item too large", function (t) {
112   var cache = new LRU({
113     max: 10,
114     length: function (item) { return item.size }
115   })
116   t.equal(cache.max, 10)
117
118   // should fall out immediately
119   cache.set("key", {val: "value", size: 50})
120
121   t.equal(cache.length, 0)
122   t.equal(cache.get("key"), undefined)
123   t.end()
124 })
125
126 test("least recently set with weighed length", function (t) {
127   var cache = new LRU({
128     max:8,
129     length: function (item) { return item.length }
130   })
131   cache.set("a", "A")
132   cache.set("b", "BB")
133   cache.set("c", "CCC")
134   cache.set("d", "DDDD")
135   t.equal(cache.get("d"), "DDDD")
136   t.equal(cache.get("c"), "CCC")
137   t.equal(cache.get("b"), undefined)
138   t.equal(cache.get("a"), undefined)
139   t.end()
140 })
141
142 test("lru recently gotten with weighed length", function (t) {
143   var cache = new LRU({
144     max: 8,
145     length: function (item) { return item.length }
146   })
147   cache.set("a", "A")
148   cache.set("b", "BB")
149   cache.set("c", "CCC")
150   cache.get("a")
151   cache.get("b")
152   cache.set("d", "DDDD")
153   t.equal(cache.get("c"), undefined)
154   t.equal(cache.get("d"), "DDDD")
155   t.equal(cache.get("b"), "BB")
156   t.equal(cache.get("a"), "A")
157   t.end()
158 })
159
160 test("lru recently updated with weighed length", function (t) {
161   var cache = new LRU({
162     max: 8,
163     length: function (item) { return item.length }
164   })
165   cache.set("a", "A")
166   cache.set("b", "BB")
167   cache.set("c", "CCC")
168   t.equal(cache.length, 6) //CCC BB A
169   cache.set("a", "+A")
170   t.equal(cache.length, 7) //+A CCC BB
171   cache.set("b", "++BB")
172   t.equal(cache.length, 6) //++BB +A
173   t.equal(cache.get("c"), undefined)
174
175   cache.set("c", "oversized")
176   t.equal(cache.length, 6) //++BB +A
177   t.equal(cache.get("c"), undefined)
178
179   cache.set("a", "oversized")
180   t.equal(cache.length, 4) //++BB
181   t.equal(cache.get("a"), undefined)
182   t.equal(cache.get("b"), "++BB")
183   t.end()
184 })
185
186 test("set returns proper booleans", function(t) {
187   var cache = new LRU({
188     max: 5,
189     length: function (item) { return item.length }
190   })
191
192   t.equal(cache.set("a", "A"), true)
193
194   // should return false for max exceeded
195   t.equal(cache.set("b", "donuts"), false)
196
197   t.equal(cache.set("b", "B"), true)
198   t.equal(cache.set("c", "CCCC"), true)
199   t.end()
200 })
201
202 test("drop the old items", function(t) {
203   var cache = new LRU({
204     max: 5,
205     maxAge: 50
206   })
207
208   cache.set("a", "A")
209
210   setTimeout(function () {
211     cache.set("b", "b")
212     t.equal(cache.get("a"), "A")
213   }, 25)
214
215   setTimeout(function () {
216     cache.set("c", "C")
217     // timed out
218     t.notOk(cache.get("a"))
219   }, 60 + 25)
220
221   setTimeout(function () {
222     t.notOk(cache.get("b"))
223     t.equal(cache.get("c"), "C")
224   }, 90)
225
226   setTimeout(function () {
227     t.notOk(cache.get("c"))
228     t.end()
229   }, 155)
230 })
231
232 test("individual item can have it's own maxAge", function(t) {
233   var cache = new LRU({
234     max: 5,
235     maxAge: 50
236   })
237
238   cache.set("a", "A", 20)
239   setTimeout(function () {
240     t.notOk(cache.get("a"))
241     t.end()
242   }, 25)
243 })
244
245 test("individual item can have it's own maxAge > cache's", function(t) {
246   var cache = new LRU({
247     max: 5,
248     maxAge: 20
249   })
250
251   cache.set("a", "A", 50)
252   setTimeout(function () {
253     t.equal(cache.get("a"), "A")
254     t.end()
255   }, 25)
256 })
257
258 test("disposal function", function(t) {
259   var disposed = false
260   var cache = new LRU({
261     max: 1,
262     dispose: function (k, n) {
263       disposed = n
264     }
265   })
266
267   cache.set(1, 1)
268   cache.set(2, 2)
269   t.equal(disposed, 1)
270   cache.set(3, 3)
271   t.equal(disposed, 2)
272   cache.reset()
273   t.equal(disposed, 3)
274   t.end()
275 })
276
277 test("disposal function on too big of item", function(t) {
278   var disposed = false
279   var cache = new LRU({
280     max: 1,
281     length: function (k) {
282       return k.length
283     },
284     dispose: function (k, n) {
285       disposed = n
286     }
287   })
288   var obj = [ 1, 2 ]
289
290   t.equal(disposed, false)
291   cache.set("obj", obj)
292   t.equal(disposed, obj)
293   t.end()
294 })
295
296 test("has()", function(t) {
297   var cache = new LRU({
298     max: 1,
299     maxAge: 10
300   })
301
302   cache.set('foo', 'bar')
303   t.equal(cache.has('foo'), true)
304   cache.set('blu', 'baz')
305   t.equal(cache.has('foo'), false)
306   t.equal(cache.has('blu'), true)
307   setTimeout(function() {
308     t.equal(cache.has('blu'), false)
309     t.end()
310   }, 15)
311 })
312
313 test("stale", function(t) {
314   var cache = new LRU({
315     maxAge: 10,
316     stale: true
317   })
318
319   cache.set('foo', 'bar')
320   t.equal(cache.get('foo'), 'bar')
321   t.equal(cache.has('foo'), true)
322   setTimeout(function() {
323     t.equal(cache.has('foo'), false)
324     t.equal(cache.get('foo'), 'bar')
325     t.equal(cache.get('foo'), undefined)
326     t.end()
327   }, 15)
328 })
329
330 test("lru update via set", function(t) {
331   var cache = LRU({ max: 2 });
332
333   cache.set('foo', 1);
334   cache.set('bar', 2);
335   cache.del('bar');
336   cache.set('baz', 3);
337   cache.set('qux', 4);
338
339   t.equal(cache.get('foo'), undefined)
340   t.equal(cache.get('bar'), undefined)
341   t.equal(cache.get('baz'), 3)
342   t.equal(cache.get('qux'), 4)
343   t.end()
344 })
345
346 test("least recently set w/ peek", function (t) {
347   var cache = new LRU(2)
348   cache.set("a", "A")
349   cache.set("b", "B")
350   t.equal(cache.peek("a"), "A")
351   cache.set("c", "C")
352   t.equal(cache.get("c"), "C")
353   t.equal(cache.get("b"), "B")
354   t.equal(cache.get("a"), undefined)
355   t.end()
356 })
357
358 test("pop the least used item", function (t) {
359   var cache = new LRU(3)
360   , last
361
362   cache.set("a", "A")
363   cache.set("b", "B")
364   cache.set("c", "C")
365
366   t.equal(cache.length, 3)
367   t.equal(cache.max, 3)
368
369   // Ensure we pop a, c, b
370   cache.get("b", "B")
371
372   last = cache.pop()
373   t.equal(last.key, "a")
374   t.equal(last.value, "A")
375   t.equal(cache.length, 2)
376   t.equal(cache.max, 3)
377
378   last = cache.pop()
379   t.equal(last.key, "c")
380   t.equal(last.value, "C")
381   t.equal(cache.length, 1)
382   t.equal(cache.max, 3)
383
384   last = cache.pop()
385   t.equal(last.key, "b")
386   t.equal(last.value, "B")
387   t.equal(cache.length, 0)
388   t.equal(cache.max, 3)
389
390   last = cache.pop()
391   t.equal(last, null)
392   t.equal(cache.length, 0)
393   t.equal(cache.max, 3)
394
395   t.end()
396 })