| 
 | 1 | +const LRUCache = require('./lru-cache-3');  | 
 | 2 | + | 
 | 3 | +describe('LRU Cache', () => {  | 
 | 4 | +  let c;  | 
 | 5 | + | 
 | 6 | +  describe('#constructor', () => {  | 
 | 7 | +    it('should initialize', () => {  | 
 | 8 | +      c = new LRUCache();  | 
 | 9 | +      expect(c).toBeDefined();  | 
 | 10 | +    });  | 
 | 11 | + | 
 | 12 | +    it('should initialize', () => {  | 
 | 13 | +      c = new LRUCache(7);  | 
 | 14 | +      expect(c.capacity).toEqual(7);  | 
 | 15 | +    });  | 
 | 16 | +  });  | 
 | 17 | + | 
 | 18 | +  describe('when initialized', () => {  | 
 | 19 | +    beforeEach(() => {  | 
 | 20 | +      c = new LRUCache(2);  | 
 | 21 | +    });  | 
 | 22 | + | 
 | 23 | +    describe('#put', () => {  | 
 | 24 | +      it('should insert new elements', () => {  | 
 | 25 | +        c.put(1, 1);  | 
 | 26 | +        expect(c.size).toEqual(1);  | 
 | 27 | +      });  | 
 | 28 | + | 
 | 29 | +      it('should update existing element', () => {  | 
 | 30 | +        c.put(1, 1);  | 
 | 31 | +        c.put(1, 2);  | 
 | 32 | +        expect(c.size).toEqual(1);  | 
 | 33 | +      });  | 
 | 34 | +    });  | 
 | 35 | + | 
 | 36 | +    describe('#get', () => {  | 
 | 37 | +      it('should get element', () => {  | 
 | 38 | +        c.put(1, 1);  | 
 | 39 | +        expect(c.get(1)).toEqual(1);  | 
 | 40 | +      });  | 
 | 41 | + | 
 | 42 | +      it('should return -1 for non-existing elements', () => {  | 
 | 43 | +        expect(c.get(1)).toEqual(-1);  | 
 | 44 | +      });  | 
 | 45 | + | 
 | 46 | +      it('should not add non-existing number to the top of the list', () => {  | 
 | 47 | +        c.put(1, 1);  | 
 | 48 | +        expect(c.get(8)).toEqual(-1);  | 
 | 49 | +        c.put(2, 2);  | 
 | 50 | +        expect(c.get(9)).toEqual(-1);  | 
 | 51 | +        expect(c.get(1)).toEqual(1);  | 
 | 52 | +        expect(c.get(2)).toEqual(2);  | 
 | 53 | +      });  | 
 | 54 | + | 
 | 55 | +      it('should return -1 for removed elements', () => {  | 
 | 56 | +        c.put(1, 1);  | 
 | 57 | +        c.put(2, 2);  | 
 | 58 | +        c.put(3, 3);  | 
 | 59 | +        expect(c.get(1)).toEqual(-1);  | 
 | 60 | +      });  | 
 | 61 | + | 
 | 62 | +      it('should not remove value if accessed recently', () => {  | 
 | 63 | +        c.put(1, 1);  | 
 | 64 | +        c.put(2, 2);  | 
 | 65 | +        expect(c.get(1)).toEqual(1);  | 
 | 66 | +        c.put(3, 3);  | 
 | 67 | +        expect(c.get(1)).toEqual(1);  | 
 | 68 | +        expect(c.get(2)).toEqual(-1);  | 
 | 69 | +      });  | 
 | 70 | + | 
 | 71 | +      it('should update a value', () => {  | 
 | 72 | +        c.put(1, 1);  | 
 | 73 | +        c.put(1, 2);  | 
 | 74 | +        expect(c.get(1)).toEqual(2);  | 
 | 75 | +      });  | 
 | 76 | +    });  | 
 | 77 | + | 
 | 78 | +    it('should work with size 10', () => {  | 
 | 79 | +      c = new LRUCache(10);  | 
 | 80 | + | 
 | 81 | +      c.put(10, 13);  | 
 | 82 | +      c.put(3, 17);  | 
 | 83 | +      c.put(6, 11);  | 
 | 84 | +      c.put(10, 5);  | 
 | 85 | +      c.put(9, 10);  | 
 | 86 | +      expect(c.get(13)).toEqual(-1);  | 
 | 87 | +      c.put(2, 19);  | 
 | 88 | +      expect(c.get(2)).toEqual(19);  | 
 | 89 | +      expect(c.get(3)).toEqual(17);  | 
 | 90 | +      c.put(5, 25);  | 
 | 91 | +      expect(c.get(8)).toEqual(-1);  | 
 | 92 | +      c.put(9, 22);  | 
 | 93 | +      c.put(5, 5);  | 
 | 94 | +      c.put(1, 30);  | 
 | 95 | +      expect(c.get(11)).toEqual(-1);  | 
 | 96 | +      c.put(9, 12);  | 
 | 97 | +      expect(c.get(7)).toEqual(-1);  | 
 | 98 | +      expect(c.get(5)).toEqual(5);  | 
 | 99 | +      expect(c.get(8)).toEqual(-1);  | 
 | 100 | +      expect(c.get(9)).toEqual(12);  | 
 | 101 | +      c.put(4, 30);  | 
 | 102 | +      c.put(9, 3);  | 
 | 103 | +      expect(c.get(9)).toEqual(3);  | 
 | 104 | +      expect(c.get(10)).toEqual(5);  | 
 | 105 | +      expect(c.get(10)).toEqual(5);  | 
 | 106 | +    });  | 
 | 107 | +  });  | 
 | 108 | +});  | 
0 commit comments