| OLD | NEW |
| 1 library cache_spec; | 1 library cache_spec; |
| 2 | 2 |
| 3 import '../_specs.dart'; | 3 import '../_specs.dart'; |
| 4 | 4 |
| 5 void main() { | 5 main() => describe('CacheFactory', () { |
| 6 describe('CacheFactory', () { | |
| 7 | 6 |
| 8 describe('cache', () { | 7 describe('cache', () { |
| 9 Cache<String, Object> cache; | 8 Cache<String, Object> cache; |
| 10 | 9 |
| 11 beforeEach(() { | 10 beforeEach(() { |
| 12 cache = new LruCache<String, Object>(); | 11 cache = new LruCache<String, Object>(); |
| 12 }); |
| 13 |
| 14 |
| 15 describe('put, get & remove', () { |
| 16 it('should add cache entries via add and retrieve them via get', () { |
| 17 var obj = {'bar':'baz'}; |
| 18 cache.put('key1', 'bar'); |
| 19 cache.put('key2', obj); |
| 20 |
| 21 expect(cache.get('key2')).toBe(obj); |
| 22 expect(cache.get('key1')).toBe('bar'); |
| 13 }); | 23 }); |
| 14 | 24 |
| 15 | 25 |
| 16 describe('put, get & remove', () { | 26 it('should remove entries via remove', () { |
| 17 it('should add cache entries via add and retrieve them via get', () { | 27 cache.put('k1', 'foo'); |
| 18 var obj = {'bar':'baz'}; | 28 cache.put('k2', 'bar'); |
| 19 cache.put('key1', 'bar'); | |
| 20 cache.put('key2', obj); | |
| 21 | 29 |
| 22 expect(cache.get('key2')).toBe(obj); | 30 cache.remove('k2'); |
| 23 expect(cache.get('key1')).toBe('bar'); | |
| 24 }); | |
| 25 | 31 |
| 32 expect(cache.get('k1')).toBe('foo'); |
| 33 expect(cache.get('k2')).toBeNull(); |
| 26 | 34 |
| 27 it('should remove entries via remove', () { | 35 cache.remove('k1'); |
| 28 cache.put('k1', 'foo'); | |
| 29 cache.put('k2', 'bar'); | |
| 30 | 36 |
| 31 cache.remove('k2'); | 37 expect(cache.get('k1')).toBeNull(); |
| 32 | 38 expect(cache.get('k2')).toBeNull(); |
| 33 expect(cache.get('k1')).toBe('foo'); | |
| 34 expect(cache.get('k2')).toBeNull(); | |
| 35 | |
| 36 cache.remove('k1'); | |
| 37 | |
| 38 expect(cache.get('k1')).toBeNull(); | |
| 39 expect(cache.get('k2')).toBeNull(); | |
| 40 }); | |
| 41 | |
| 42 | |
| 43 it('should return null when entry does not exist', () { | |
| 44 expect(cache.remove('non-existent')).toBeNull(); | |
| 45 }); | |
| 46 | |
| 47 | |
| 48 // TODO(chirayu): to implement | |
| 49 // it('should stringify keys', () { | |
| 50 // cache.put('123', 'foo'); | |
| 51 // cache.put(123, 'bar'); | |
| 52 | |
| 53 // expect(cache.get('123')).toBe('bar'); | |
| 54 // expect(cache.info().size).toBe(1); | |
| 55 | |
| 56 // cache.remove(123); | |
| 57 // expect(cache.info().size).toBe(0); | |
| 58 // }); | |
| 59 | |
| 60 | |
| 61 it("should return value from put", () { | |
| 62 var obj = {}; | |
| 63 expect(cache.put('k1', obj)).toBe(obj); | |
| 64 }); | |
| 65 }); | 39 }); |
| 66 | 40 |
| 67 | 41 |
| 68 describe('put, get & remove', () { | 42 it('should return null when entry does not exist', () { |
| 69 | 43 expect(cache.remove('non-existent')).toBeNull(); |
| 70 it('should add cache entries via add and retrieve them via get', inject(
() { | |
| 71 var obj = {'bar':'baz'}; | |
| 72 cache.put('key1', 'bar'); | |
| 73 cache.put('key2', obj); | |
| 74 | |
| 75 expect(cache.get('key2')).toBe(obj); | |
| 76 expect(cache.get('key1')).toBe('bar'); | |
| 77 })); | |
| 78 | |
| 79 | |
| 80 it('should remove entries via remove', inject(() { | |
| 81 cache.put('k1', 'foo'); | |
| 82 cache.put('k2', 'bar'); | |
| 83 | |
| 84 cache.remove('k2'); | |
| 85 | |
| 86 expect(cache.get('k1')).toBe('foo'); | |
| 87 expect(cache.get('k2')).toBeNull(); | |
| 88 | |
| 89 cache.remove('k1'); | |
| 90 | |
| 91 expect(cache.get('k1')).toBeNull(); | |
| 92 expect(cache.get('k2')).toBeNull(); | |
| 93 })); | |
| 94 | |
| 95 | |
| 96 it('should return null when entry does not exist', inject(() { | |
| 97 expect(cache.remove('non-existent')).toBeNull(); | |
| 98 })); | |
| 99 | |
| 100 it("should return value from put", inject(() { | |
| 101 var obj = {}; | |
| 102 expect(cache.put('k1', obj)).toBe(obj); | |
| 103 })); | |
| 104 }); | 44 }); |
| 105 | 45 |
| 106 | 46 |
| 107 describe('removeAll', () { | 47 // TODO(chirayu): to implement |
| 108 it('should blow away all data', inject(() { | 48 // it('should stringify keys', () { |
| 109 cache.put('id1', 1); | 49 // cache.put('123', 'foo'); |
| 110 cache.put('id2', 2); | 50 // cache.put(123, 'bar'); |
| 111 cache.put('id3', 3); | |
| 112 | 51 |
| 113 cache.removeAll(); | 52 // expect(cache.get('123')).toBe('bar'); |
| 53 // expect(cache.info().size).toBe(1); |
| 114 | 54 |
| 115 expect(cache.get('id1')).toBeNull(); | 55 // cache.remove(123); |
| 116 expect(cache.get('id2')).toBeNull(); | 56 // expect(cache.info().size).toBe(0); |
| 117 expect(cache.get('id3')).toBeNull(); | 57 // }); |
| 118 })); | 58 |
| 59 |
| 60 it("should return value from put", () { |
| 61 var obj = {}; |
| 62 expect(cache.put('k1', obj)).toBe(obj); |
| 119 }); | 63 }); |
| 120 }); | 64 }); |
| 121 | 65 |
| 122 // TODO(chirayu): Add a lot more tests and tests and don't rely on toString(
) | 66 |
| 123 describe('LRU cache', () { | 67 describe('put, get & remove', () { |
| 124 it('should have LRU behavior with ordering keys and eviction', inject(() { | 68 |
| 125 var cache = new LruCache<int, int>(capacity: 4); | 69 it('should add cache entries via add and retrieve them via get', inject(()
{ |
| 126 cache.put(1, 10); | 70 var obj = {'bar':'baz'}; |
| 127 cache.put(2, 20); | 71 cache.put('key1', 'bar'); |
| 128 cache.put(3, 30); | 72 cache.put('key2', obj); |
| 129 cache.put(4, 40); | 73 |
| 130 expect(cache.get(2)).toEqual(20); | 74 expect(cache.get('key2')).toBe(obj); |
| 131 cache.put(5, 50); | 75 expect(cache.get('key1')).toBe('bar'); |
| 132 cache.put(6, 60); | 76 })); |
| 133 expect(cache.get(5)).toEqual(50); | 77 |
| 134 cache.put(7, 70); | 78 |
| 135 cache.put(8, 80); | 79 it('should remove entries via remove', inject(() { |
| 136 // 1 has been evicted. | 80 cache.put('k1', 'foo'); |
| 137 expect(cache.get(1)).toBeNull(); | 81 cache.put('k2', 'bar'); |
| 138 // The order of items is LRU to MRU. | 82 |
| 139 expect("$cache").toEqual( | 83 cache.remove('k2'); |
| 140 r"[LruCache<int, int>: capacity=4, size=4, items={6: 60, 5: 50, 7: 7
0, 8: 80}]"); | 84 |
| 85 expect(cache.get('k1')).toBe('foo'); |
| 86 expect(cache.get('k2')).toBeNull(); |
| 87 |
| 88 cache.remove('k1'); |
| 89 |
| 90 expect(cache.get('k1')).toBeNull(); |
| 91 expect(cache.get('k2')).toBeNull(); |
| 92 })); |
| 93 |
| 94 |
| 95 it('should return null when entry does not exist', inject(() { |
| 96 expect(cache.remove('non-existent')).toBeNull(); |
| 97 })); |
| 98 |
| 99 it("should return value from put", inject(() { |
| 100 var obj = {}; |
| 101 expect(cache.put('k1', obj)).toBe(obj); |
| 102 })); |
| 103 }); |
| 104 |
| 105 |
| 106 describe('removeAll', () { |
| 107 it('should blow away all data', inject(() { |
| 108 cache.put('id1', 1); |
| 109 cache.put('id2', 2); |
| 110 cache.put('id3', 3); |
| 111 |
| 141 cache.removeAll(); | 112 cache.removeAll(); |
| 142 expect("$cache").toEqual(r"[LruCache<int, int>: capacity=4, size=0, item
s={}]"); | |
| 143 | 113 |
| 144 var stats = cache.stats(); | 114 expect(cache.get('id1')).toBeNull(); |
| 145 expect(stats.capacity).toEqual(4); | 115 expect(cache.get('id2')).toBeNull(); |
| 146 expect(stats.size).toEqual(0); | 116 expect(cache.get('id3')).toBeNull(); |
| 147 expect(stats.hits).toEqual(2); | |
| 148 expect(stats.misses).toEqual(1); | |
| 149 })); | 117 })); |
| 150 }); | 118 }); |
| 151 }); | 119 }); |
| 152 } | 120 |
| 121 // TODO(chirayu): Add a lot more tests and tests and don't rely on toString() |
| 122 describe('LRU cache', () { |
| 123 it('should have LRU behavior with ordering keys and eviction', inject(() { |
| 124 var cache = new LruCache<int, int>(capacity: 4); |
| 125 cache.put(1, 10); |
| 126 cache.put(2, 20); |
| 127 cache.put(3, 30); |
| 128 cache.put(4, 40); |
| 129 expect(cache.get(2)).toEqual(20); |
| 130 cache.put(5, 50); |
| 131 cache.put(6, 60); |
| 132 expect(cache.get(5)).toEqual(50); |
| 133 cache.put(7, 70); |
| 134 cache.put(8, 80); |
| 135 // 1 has been evicted. |
| 136 expect(cache.get(1)).toBeNull(); |
| 137 // The order of items is LRU to MRU. |
| 138 expect("$cache").toEqual( |
| 139 r"[LruCache<int, int>: capacity=4, size=4, items={6: 60, 5: 50, 7: 70,
8: 80}]"); |
| 140 cache.removeAll(); |
| 141 expect("$cache").toEqual(r"[LruCache<int, int>: capacity=4, size=0, items=
{}]"); |
| 142 |
| 143 var stats = cache.stats(); |
| 144 expect(stats.capacity).toEqual(4); |
| 145 expect(stats.size).toEqual(0); |
| 146 expect(stats.hits).toEqual(2); |
| 147 expect(stats.misses).toEqual(1); |
| 148 })); |
| 149 }); |
| 150 }); |
| OLD | NEW |