-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlruCache.test.ts
More file actions
52 lines (46 loc) · 1.68 KB
/
Copy pathlruCache.test.ts
File metadata and controls
52 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { createLRUCache } from '../lruCache';
describe('createLRUCache', () => {
it('returns undefined for a missing key', () => {
const cache = createLRUCache<string, number>(8);
expect(cache.get('missing')).toBeUndefined();
});
it('returns the value previously set under a key', () => {
const cache = createLRUCache<string, number>(8);
cache.set('a', 1);
expect(cache.get('a')).toBe(1);
});
it('evicts the least-recently-used entry once maxSize is reached', () => {
const cache = createLRUCache<string, number>(2);
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3); // evicts 'a'
expect(cache.get('a')).toBeUndefined();
expect(cache.get('b')).toBe(2);
expect(cache.get('c')).toBe(3);
});
it('touches an entry on get so it is no longer the LRU', () => {
const cache = createLRUCache<string, number>(2);
cache.set('a', 1);
cache.set('b', 2);
cache.get('a'); // touch 'a' — 'b' is now LRU
cache.set('c', 3); // evicts 'b'
expect(cache.get('a')).toBe(1);
expect(cache.get('b')).toBeUndefined();
expect(cache.get('c')).toBe(3);
});
it('updates an existing key in place without evicting other entries', () => {
const cache = createLRUCache<string, number>(2);
cache.set('a', 1);
cache.set('b', 2);
cache.set('b', 99); // replace — must not evict 'a'
expect(cache.get('a')).toBe(1);
expect(cache.get('b')).toBe(99);
});
it('evicts the previous entry on every new key when maxSize is 1', () => {
const cache = createLRUCache<string, number>(1);
cache.set('a', 1);
cache.set('b', 2);
expect(cache.get('a')).toBeUndefined();
expect(cache.get('b')).toBe(2);
});
});