Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / 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 // 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")
102   cache.set("a", "A")
103   var d = cache.dump()  // { a: { key: "a", value: "A", lu: 0 } }
104   t.ok(d.a)
105   t.equal(d.a.key, "a")
106   t.equal(d.a.value, "A")
107   t.equal(d.a.lu, 0)
108
109   cache.set("b", "B")
110   cache.get("b")
111   d = cache.dump()
112   t.ok(d.b)
113   t.equal(d.b.key, "b")
114   t.equal(d.b.value, "B")
115   t.equal(d.b.lu, 2)
116
117   t.end()
118 })
119
120
121 test("basic with weighed length", function (t) {
122   var cache = new LRU({
123     max: 100,
124     length: function (item) { return item.size }
125   })
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)
132   t.end()
133 })
134
135
136 test("weighed length item too large", function (t) {
137   var cache = new LRU({
138     max: 10,
139     length: function (item) { return item.size }
140   })
141   t.equal(cache.max, 10)
142
143   // should fall out immediately
144   cache.set("key", {val: "value", size: 50})
145
146   t.equal(cache.length, 0)
147   t.equal(cache.get("key"), undefined)
148   t.end()
149 })
150
151 test("least recently set with weighed length", function (t) {
152   var cache = new LRU({
153     max:8,
154     length: function (item) { return item.length }
155   })
156   cache.set("a", "A")
157   cache.set("b", "BB")
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)
164   t.end()
165 })
166
167 test("lru recently gotten with weighed length", function (t) {
168   var cache = new LRU({
169     max: 8,
170     length: function (item) { return item.length }
171   })
172   cache.set("a", "A")
173   cache.set("b", "BB")
174   cache.set("c", "CCC")
175   cache.get("a")
176   cache.get("b")
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")
182   t.end()
183 })
184
185 test("set returns proper booleans", function(t) {
186   var cache = new LRU({
187     max: 5,
188     length: function (item) { return item.length }
189   })
190
191   t.equal(cache.set("a", "A"), true)
192
193   // should return false for max exceeded
194   t.equal(cache.set("b", "donuts"), false)
195
196   t.equal(cache.set("b", "B"), true)
197   t.equal(cache.set("c", "CCCC"), true)
198   t.end()
199 })
200
201 test("drop the old items", function(t) {
202   var cache = new LRU({
203     max: 5,
204     maxAge: 50
205   })
206
207   cache.set("a", "A")
208
209   setTimeout(function () {
210     cache.set("b", "b")
211     t.equal(cache.get("a"), "A")
212   }, 25)
213
214   setTimeout(function () {
215     cache.set("c", "C")
216     // timed out
217     t.notOk(cache.get("a"))
218   }, 60)
219
220   setTimeout(function () {
221     t.notOk(cache.get("b"))
222     t.equal(cache.get("c"), "C")
223   }, 90)
224
225   setTimeout(function () {
226     t.notOk(cache.get("c"))
227     t.end()
228   }, 155)
229 })
230
231 test("disposal function", function(t) {
232   var disposed = false
233   var cache = new LRU({
234     max: 1,
235     dispose: function (k, n) {
236       disposed = n
237     }
238   })
239
240   cache.set(1, 1)
241   cache.set(2, 2)
242   t.equal(disposed, 1)
243   cache.set(3, 3)
244   t.equal(disposed, 2)
245   cache.reset()
246   t.equal(disposed, 3)
247   t.end()
248 })
249
250 test("disposal function on too big of item", function(t) {
251   var disposed = false
252   var cache = new LRU({
253     max: 1,
254     length: function (k) {
255       return k.length
256     },
257     dispose: function (k, n) {
258       disposed = n
259     }
260   })
261   var obj = [ 1, 2 ]
262
263   t.equal(disposed, false)
264   cache.set("obj", obj)
265   t.equal(disposed, obj)
266   t.end()
267 })
268
269 test("has()", function(t) {
270   var cache = new LRU({
271     max: 1,
272     maxAge: 10
273   })
274
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)
282     t.end()
283   }, 15)
284 })
285
286 test("stale", function(t) {
287   var cache = new LRU({
288     maxAge: 10,
289     stale: true
290   })
291
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)
299     t.end()
300   }, 15)
301 })
302
303 test("lru update via set", function(t) {
304   var cache = LRU({ max: 2 });
305
306   cache.set('foo', 1);
307   cache.set('bar', 2);
308   cache.del('bar');
309   cache.set('baz', 3);
310   cache.set('qux', 4);
311
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)
316   t.end()
317 })