-
Notifications
You must be signed in to change notification settings - Fork 931
/
Copy pathlru-cache.spec.js
120 lines (105 loc) · 2.83 KB
/
lru-cache.spec.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const LRUCache = require('./lru-cache-3');
describe('LRU Cache', () => {
let c;
describe('#constructor', () => {
it('should initialize', () => {
c = new LRUCache();
expect(c).toBeDefined();
});
it('should initialize with capacity', () => {
c = new LRUCache(7);
expect(c.capacity).toEqual(7);
});
});
describe('when initialized', () => {
beforeEach(() => {
c = new LRUCache(2);
});
describe('#put', () => {
it('should insert new elements', () => {
c.put(1, 1);
expect(c.size).toEqual(1);
});
it('should update existing element', () => {
c.put(1, 1);
c.put(1, 2);
expect(c.size).toEqual(1);
});
});
describe('#get', () => {
it('should get element', () => {
c.put(1, 1);
expect(c.get(1)).toEqual(1);
});
it('should return -1 for non-existing elements', () => {
expect(c.get(1)).toEqual(-1);
});
it('should not add non-existing number to the top of the list', () => {
c.put(1, 1);
expect(c.get(8)).toEqual(-1);
c.put(2, 2);
expect(c.get(9)).toEqual(-1);
expect(c.get(1)).toEqual(1);
expect(c.get(2)).toEqual(2);
});
it('should return -1 for removed elements', () => {
c.put(1, 1);
c.put(2, 2);
c.put(3, 3);
expect(c.get(1)).toEqual(-1);
});
it('should not remove value if accessed recently', () => {
c.put(1, 1);
c.put(2, 2);
expect(c.get(1)).toEqual(1);
c.put(3, 3);
expect(c.get(1)).toEqual(1);
expect(c.get(2)).toEqual(-1);
});
it('should update a value', () => {
c.put(1, 1);
c.put(1, 2);
expect(c.get(1)).toEqual(2);
});
});
it('should work with updates', () => {
// ["LRUCache","put","put","put","put","get","get"]
// [[2],[2,1],[1,1],[2,3],[4,1],[1],[2]]
c = new LRUCache(2);
c.put(2, 1);
c.put(1, 1);
c.put(2, 3);
c.put(4, 1);
c.get(1);
c.get(2);
});
it('should work with size 10', () => {
c = new LRUCache(10);
c.put(10, 13);
c.put(3, 17);
c.put(6, 11);
c.put(10, 5);
c.put(9, 10);
expect(c.get(13)).toEqual(-1);
c.put(2, 19);
expect(c.get(2)).toEqual(19);
expect(c.get(3)).toEqual(17);
c.put(5, 25);
expect(c.get(8)).toEqual(-1);
c.put(9, 22);
c.put(5, 5);
c.put(1, 30);
expect(c.get(11)).toEqual(-1);
c.put(9, 12);
expect(c.get(7)).toEqual(-1);
expect(c.get(5)).toEqual(5);
expect(c.get(8)).toEqual(-1);
expect(c.get(9)).toEqual(12);
c.put(4, 30);
c.put(9, 3);
expect(c.get(9)).toEqual(3);
expect(c.get(10)).toEqual(5);
expect(c.get(10)).toEqual(5);
});
});
});