corrade-nucleus-nucleons – Blame information for rev 20
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 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, key) { |
||
100 | t.isa(key, 'string') |
||
101 | return item.size |
||
102 | } |
||
103 | }) |
||
104 | cache.set("key", {val: "value", size: 50}) |
||
105 | t.equal(cache.get("key").val, "value") |
||
106 | t.equal(cache.get("nada"), undefined) |
||
107 | t.equal(cache.lengthCalculator(cache.get("key"), 'key'), 50) |
||
108 | t.equal(cache.length, 50) |
||
109 | t.equal(cache.max, 100) |
||
110 | t.end() |
||
111 | }) |
||
112 | |||
113 | |||
114 | test("weighed length item too large", function (t) { |
||
115 | var cache = new LRU({ |
||
116 | max: 10, |
||
117 | length: function (item) { return item.size } |
||
118 | }) |
||
119 | t.equal(cache.max, 10) |
||
120 | |||
121 | // should fall out immediately |
||
122 | cache.set("key", {val: "value", size: 50}) |
||
123 | |||
124 | t.equal(cache.length, 0) |
||
125 | t.equal(cache.get("key"), undefined) |
||
126 | t.end() |
||
127 | }) |
||
128 | |||
129 | test("least recently set with weighed length", function (t) { |
||
130 | var cache = new LRU({ |
||
131 | max:8, |
||
132 | length: function (item) { return item.length } |
||
133 | }) |
||
134 | cache.set("a", "A") |
||
135 | cache.set("b", "BB") |
||
136 | cache.set("c", "CCC") |
||
137 | cache.set("d", "DDDD") |
||
138 | t.equal(cache.get("d"), "DDDD") |
||
139 | t.equal(cache.get("c"), "CCC") |
||
140 | t.equal(cache.get("b"), undefined) |
||
141 | t.equal(cache.get("a"), undefined) |
||
142 | t.end() |
||
143 | }) |
||
144 | |||
145 | test("lru recently gotten with weighed length", function (t) { |
||
146 | var cache = new LRU({ |
||
147 | max: 8, |
||
148 | length: function (item) { return item.length } |
||
149 | }) |
||
150 | cache.set("a", "A") |
||
151 | cache.set("b", "BB") |
||
152 | cache.set("c", "CCC") |
||
153 | cache.get("a") |
||
154 | cache.get("b") |
||
155 | cache.set("d", "DDDD") |
||
156 | t.equal(cache.get("c"), undefined) |
||
157 | t.equal(cache.get("d"), "DDDD") |
||
158 | t.equal(cache.get("b"), "BB") |
||
159 | t.equal(cache.get("a"), "A") |
||
160 | t.end() |
||
161 | }) |
||
162 | |||
163 | test("lru recently updated with weighed length", function (t) { |
||
164 | var cache = new LRU({ |
||
165 | max: 8, |
||
166 | length: function (item) { return item.length } |
||
167 | }) |
||
168 | cache.set("a", "A") |
||
169 | cache.set("b", "BB") |
||
170 | cache.set("c", "CCC") |
||
171 | t.equal(cache.length, 6) //CCC BB A |
||
172 | cache.set("a", "+A") |
||
173 | t.equal(cache.length, 7) //+A CCC BB |
||
174 | cache.set("b", "++BB") |
||
175 | t.equal(cache.length, 6) //++BB +A |
||
176 | t.equal(cache.get("c"), undefined) |
||
177 | |||
178 | cache.set("c", "oversized") |
||
179 | t.equal(cache.length, 6) //++BB +A |
||
180 | t.equal(cache.get("c"), undefined) |
||
181 | |||
182 | cache.set("a", "oversized") |
||
183 | t.equal(cache.length, 4) //++BB |
||
184 | t.equal(cache.get("a"), undefined) |
||
185 | t.equal(cache.get("b"), "++BB") |
||
186 | t.end() |
||
187 | }) |
||
188 | |||
189 | test("set returns proper booleans", function(t) { |
||
190 | var cache = new LRU({ |
||
191 | max: 5, |
||
192 | length: function (item) { return item.length } |
||
193 | }) |
||
194 | |||
195 | t.equal(cache.set("a", "A"), true) |
||
196 | |||
197 | // should return false for max exceeded |
||
198 | t.equal(cache.set("b", "donuts"), false) |
||
199 | |||
200 | t.equal(cache.set("b", "B"), true) |
||
201 | t.equal(cache.set("c", "CCCC"), true) |
||
202 | t.end() |
||
203 | }) |
||
204 | |||
205 | test("drop the old items", function(t) { |
||
206 | var cache = new LRU({ |
||
207 | max: 5, |
||
208 | maxAge: 50 |
||
209 | }) |
||
210 | |||
211 | cache.set("a", "A") |
||
212 | |||
213 | setTimeout(function () { |
||
214 | cache.set("b", "b") |
||
215 | t.equal(cache.get("a"), "A") |
||
216 | }, 25) |
||
217 | |||
218 | setTimeout(function () { |
||
219 | cache.set("c", "C") |
||
220 | // timed out |
||
221 | t.notOk(cache.get("a")) |
||
222 | }, 60 + 25) |
||
223 | |||
224 | setTimeout(function () { |
||
225 | t.notOk(cache.get("b")) |
||
226 | t.equal(cache.get("c"), "C") |
||
227 | }, 90) |
||
228 | |||
229 | setTimeout(function () { |
||
230 | t.notOk(cache.get("c")) |
||
231 | t.end() |
||
232 | }, 155) |
||
233 | }) |
||
234 | |||
235 | test("individual item can have its own maxAge", function(t) { |
||
236 | var cache = new LRU({ |
||
237 | max: 5, |
||
238 | maxAge: 50 |
||
239 | }) |
||
240 | |||
241 | cache.set("a", "A", 20) |
||
242 | setTimeout(function () { |
||
243 | t.notOk(cache.get("a")) |
||
244 | t.end() |
||
245 | }, 25) |
||
246 | }) |
||
247 | |||
248 | test("individual item can have its own maxAge > cache's", function(t) { |
||
249 | var cache = new LRU({ |
||
250 | max: 5, |
||
251 | maxAge: 20 |
||
252 | }) |
||
253 | |||
254 | cache.set("a", "A", 50) |
||
255 | setTimeout(function () { |
||
256 | t.equal(cache.get("a"), "A") |
||
257 | t.end() |
||
258 | }, 25) |
||
259 | }) |
||
260 | |||
261 | test("disposal function", function(t) { |
||
262 | var disposed = false |
||
263 | var cache = new LRU({ |
||
264 | max: 1, |
||
265 | dispose: function (k, n) { |
||
266 | disposed = n |
||
267 | } |
||
268 | }) |
||
269 | |||
270 | cache.set(1, 1) |
||
271 | cache.set(2, 2) |
||
272 | t.equal(disposed, 1) |
||
273 | cache.set(3, 3) |
||
274 | t.equal(disposed, 2) |
||
275 | cache.reset() |
||
276 | t.equal(disposed, 3) |
||
277 | t.end() |
||
278 | }) |
||
279 | |||
280 | test("disposal function on too big of item", function(t) { |
||
281 | var disposed = false |
||
282 | var cache = new LRU({ |
||
283 | max: 1, |
||
284 | length: function (k) { |
||
285 | return k.length |
||
286 | }, |
||
287 | dispose: function (k, n) { |
||
288 | disposed = n |
||
289 | } |
||
290 | }) |
||
291 | var obj = [ 1, 2 ] |
||
292 | |||
293 | t.equal(disposed, false) |
||
294 | cache.set("obj", obj) |
||
295 | t.equal(disposed, obj) |
||
296 | t.end() |
||
297 | }) |
||
298 | |||
299 | test("has()", function(t) { |
||
300 | var cache = new LRU({ |
||
301 | max: 1, |
||
302 | maxAge: 10 |
||
303 | }) |
||
304 | |||
305 | cache.set('foo', 'bar') |
||
306 | t.equal(cache.has('foo'), true) |
||
307 | cache.set('blu', 'baz') |
||
308 | t.equal(cache.has('foo'), false) |
||
309 | t.equal(cache.has('blu'), true) |
||
310 | setTimeout(function() { |
||
311 | t.equal(cache.has('blu'), false) |
||
312 | t.end() |
||
313 | }, 15) |
||
314 | }) |
||
315 | |||
316 | test("stale", function(t) { |
||
317 | var cache = new LRU({ |
||
318 | maxAge: 10, |
||
319 | stale: true |
||
320 | }) |
||
321 | |||
322 | cache.set('foo', 'bar') |
||
323 | t.equal(cache.get('foo'), 'bar') |
||
324 | t.equal(cache.has('foo'), true) |
||
325 | setTimeout(function() { |
||
326 | t.equal(cache.has('foo'), false) |
||
327 | t.equal(cache.get('foo'), 'bar') |
||
328 | t.equal(cache.get('foo'), undefined) |
||
329 | t.end() |
||
330 | }, 15) |
||
331 | }) |
||
332 | |||
333 | test("lru update via set", function(t) { |
||
334 | var cache = LRU({ max: 2 }); |
||
335 | |||
336 | cache.set('foo', 1); |
||
337 | cache.set('bar', 2); |
||
338 | cache.del('bar'); |
||
339 | cache.set('baz', 3); |
||
340 | cache.set('qux', 4); |
||
341 | |||
342 | t.equal(cache.get('foo'), undefined) |
||
343 | t.equal(cache.get('bar'), undefined) |
||
344 | t.equal(cache.get('baz'), 3) |
||
345 | t.equal(cache.get('qux'), 4) |
||
346 | t.end() |
||
347 | }) |
||
348 | |||
349 | test("least recently set w/ peek", function (t) { |
||
350 | var cache = new LRU(2) |
||
351 | cache.set("a", "A") |
||
352 | cache.set("b", "B") |
||
353 | t.equal(cache.peek("a"), "A") |
||
354 | cache.set("c", "C") |
||
355 | t.equal(cache.get("c"), "C") |
||
356 | t.equal(cache.get("b"), "B") |
||
357 | t.equal(cache.get("a"), undefined) |
||
358 | t.end() |
||
359 | }) |
||
360 | |||
361 | test("pop the least used item", function (t) { |
||
362 | var cache = new LRU(3) |
||
363 | , last |
||
364 | |||
365 | cache.set("a", "A") |
||
366 | cache.set("b", "B") |
||
367 | cache.set("c", "C") |
||
368 | |||
369 | t.equal(cache.length, 3) |
||
370 | t.equal(cache.max, 3) |
||
371 | |||
372 | // Ensure we pop a, c, b |
||
373 | cache.get("b", "B") |
||
374 | |||
375 | last = cache.pop() |
||
376 | t.equal(last.key, "a") |
||
377 | t.equal(last.value, "A") |
||
378 | t.equal(cache.length, 2) |
||
379 | t.equal(cache.max, 3) |
||
380 | |||
381 | last = cache.pop() |
||
382 | t.equal(last.key, "c") |
||
383 | t.equal(last.value, "C") |
||
384 | t.equal(cache.length, 1) |
||
385 | t.equal(cache.max, 3) |
||
386 | |||
387 | last = cache.pop() |
||
388 | t.equal(last.key, "b") |
||
389 | t.equal(last.value, "B") |
||
390 | t.equal(cache.length, 0) |
||
391 | t.equal(cache.max, 3) |
||
392 | |||
393 | last = cache.pop() |
||
394 | t.equal(last, null) |
||
395 | t.equal(cache.length, 0) |
||
396 | t.equal(cache.max, 3) |
||
397 | |||
398 | t.end() |
||
399 | }) |
||
400 | |||
401 | test("get and set only accepts strings and numbers as keys", function(t) { |
||
402 | var cache = new LRU() |
||
403 | |||
404 | cache.set("key", "value") |
||
405 | cache.set(123, 456) |
||
406 | |||
407 | t.equal(cache.get("key"), "value") |
||
408 | t.equal(cache.get(123), 456) |
||
409 | |||
410 | t.end() |
||
411 | }) |
||
412 | |||
413 | test("peek with wierd keys", function(t) { |
||
414 | var cache = new LRU() |
||
415 | |||
416 | cache.set("key", "value") |
||
417 | cache.set(123, 456) |
||
418 | |||
419 | t.equal(cache.peek("key"), "value") |
||
420 | t.equal(cache.peek(123), 456) |
||
421 | |||
422 | t.equal(cache.peek({ |
||
423 | toString: function() { return "key" } |
||
424 | }), undefined) |
||
425 | |||
426 | t.end() |
||
427 | }) |