| OLD | NEW |
| (Empty) | |
| 1 library cache_spec; |
| 2 |
| 3 import '../_specs.dart'; |
| 4 |
| 5 main() => describe('CacheFactory', () { |
| 6 |
| 7 describe('cache', () { |
| 8 Cache<String, Object> cache; |
| 9 |
| 10 beforeEach(() { |
| 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'); |
| 23 }); |
| 24 |
| 25 |
| 26 it('should remove entries via remove', () { |
| 27 cache.put('k1', 'foo'); |
| 28 cache.put('k2', 'bar'); |
| 29 |
| 30 cache.remove('k2'); |
| 31 |
| 32 expect(cache.get('k1')).toBe('foo'); |
| 33 expect(cache.get('k2')).toBeNull(); |
| 34 |
| 35 cache.remove('k1'); |
| 36 |
| 37 expect(cache.get('k1')).toBeNull(); |
| 38 expect(cache.get('k2')).toBeNull(); |
| 39 }); |
| 40 |
| 41 |
| 42 it('should return null when entry does not exist', () { |
| 43 expect(cache.remove('non-existent')).toBeNull(); |
| 44 }); |
| 45 |
| 46 |
| 47 // TODO(chirayu): to implement |
| 48 // it('should stringify keys', () { |
| 49 // cache.put('123', 'foo'); |
| 50 // cache.put(123, 'bar'); |
| 51 |
| 52 // expect(cache.get('123')).toBe('bar'); |
| 53 // expect(cache.info().size).toBe(1); |
| 54 |
| 55 // cache.remove(123); |
| 56 // expect(cache.info().size).toBe(0); |
| 57 // }); |
| 58 |
| 59 |
| 60 it("should return value from put", () { |
| 61 var obj = {}; |
| 62 expect(cache.put('k1', obj)).toBe(obj); |
| 63 }); |
| 64 }); |
| 65 |
| 66 |
| 67 describe('put, get & remove', () { |
| 68 |
| 69 it('should add cache entries via add and retrieve them via get', inject(()
{ |
| 70 var obj = {'bar':'baz'}; |
| 71 cache.put('key1', 'bar'); |
| 72 cache.put('key2', obj); |
| 73 |
| 74 expect(cache.get('key2')).toBe(obj); |
| 75 expect(cache.get('key1')).toBe('bar'); |
| 76 })); |
| 77 |
| 78 |
| 79 it('should remove entries via remove', inject(() { |
| 80 cache.put('k1', 'foo'); |
| 81 cache.put('k2', 'bar'); |
| 82 |
| 83 cache.remove('k2'); |
| 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 |
| 112 cache.removeAll(); |
| 113 |
| 114 expect(cache.get('id1')).toBeNull(); |
| 115 expect(cache.get('id2')).toBeNull(); |
| 116 expect(cache.get('id3')).toBeNull(); |
| 117 })); |
| 118 }); |
| 119 }); |
| 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 |