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+ } ) ;
0 commit comments