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