Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba8f3e8

Browse files
committed
Add tests for LRUCache
1 parent 9b6f8f5 commit ba8f3e8

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const { LRUCache } = require('.');
2+
3+
describe('Algorithms: LRU Cache', () => {
4+
describe('LRUCache Instance', () => {
5+
it('Should be a class', () => {
6+
expect(typeof LRUCache.prototype.constructor).toEqual('function');
7+
});
8+
});
9+
10+
describe('LRUCache API', () => {
11+
let lruCache = new LRUCache(4);
12+
13+
beforeEach(() => {
14+
lruCache = new LRUCache(4);
15+
});
16+
17+
describe('get(key)', () => {
18+
it('Should return false if the LRUCache is empty', () => {
19+
expect(lruCache.get('foo')).toEqual(false);
20+
});
21+
22+
it('Should return cached value if the key exists in the LRUCache', () =>{
23+
lruCache.set('foo', 'bar');
24+
expect(lruCache.get('foo')).toEqual('bar');
25+
});
26+
27+
it('Should move least recently used key to the beginning of the list', () => {
28+
lruCache.set('key1', 'value1');
29+
lruCache.set('key2', 'value2');
30+
31+
expect(lruCache.list.head.next.data['key']).toEqual('key2');
32+
expect(lruCache.list.head.next.data['value']).toEqual('value2');
33+
34+
// The least recently used key is moved at the beginning of the list
35+
lruCache.get('key1');
36+
expect(lruCache.list.head.next.data['key']).toEqual('key1');
37+
expect(lruCache.list.head.next.data['value']).toEqual('value1');
38+
});
39+
});
40+
41+
describe('set(key, value)', () =>{
42+
it('Should append each <key:value> pair to the beginning of list', () => {
43+
lruCache.set('foo', 'bar');
44+
expect(lruCache.list.head.next.data['key']).toEqual('foo');
45+
expect(lruCache.list.head.next.data['value']).toEqual('bar');
46+
});
47+
48+
it('Should update value if key already exists', () => {
49+
lruCache.set('foo', 'bar');
50+
lruCache.set('foo', 'newBar');
51+
expect(lruCache.get('foo')).toEqual('newBar');
52+
});
53+
54+
it('Should remove node at the end if the LRUCache capacity is filled', () => {
55+
lruCache.set('key5', 'value5');
56+
lruCache.set('key4', 'value4');
57+
lruCache.set('key3', 'value3');
58+
lruCache.set('key2', 'value2');
59+
lruCache.set('key1', 'value1');
60+
61+
expect(lruCache.list.length()).toEqual(lruCache.size);
62+
expect(lruCache.list.head.next.data['key']).toEqual('key1');
63+
expect(lruCache.list.head.next.data['value']).toEqual('value1');
64+
expect(lruCache.list.head.next.next.data['key']).toEqual('key2');
65+
expect(lruCache.list.head.next.next.data['value']).toEqual('value2');
66+
expect(lruCache.list.head.next.next.next.data['key']).toEqual('key3');
67+
expect(lruCache.list.head.next.next.next.data['value']).toEqual('value3');
68+
expect(lruCache.list.head.next.next.next.next.data['key']).toEqual('key4');
69+
expect(lruCache.list.head.next.next.next.next.data['value']).toEqual('value4');
70+
});
71+
});
72+
});
73+
});

‎src/_Algorithms_/lru-cache/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class LRUCache {
5050
value,
5151
});
5252
this.map.set(key, this.list.head.next);
53+
return value;
5354
}
5455
return false;
5556
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /