| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.index.lru_cache; | 5 library test.index.lru_cache; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/index/lru_cache.dart'; | 7 import 'package:analysis_server/src/index/lru_cache.dart'; |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 | 9 |
| 10 import '../reflective_tests.dart'; | 10 import '../reflective_tests.dart'; |
| 11 | 11 |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 groupSep = ' | '; | 14 groupSep = ' | '; |
| 15 group('FixedStringCodecTest', () { | 15 group('FixedStringCodecTest', () { |
| 16 runReflectiveTests(_LRUCacheTest); | 16 runReflectiveTests(_LRUCacheTest); |
| 17 }); | 17 }); |
| 18 } | 18 } |
| 19 | 19 |
| 20 | 20 |
| 21 @ReflectiveTestCase() | 21 @ReflectiveTestCase() |
| 22 class _LRUCacheTest { | 22 class _LRUCacheTest { |
| 23 LRUCache<int, String> cache = new LRUCache<int, String>(3); | 23 LRUCache<int, String> cache = new LRUCache<int, String>(3); |
| 24 | 24 |
| 25 void test_evict() { | 25 void test_evict_notGet() { |
| 26 List<int> evictedKeys = new List<int>(); | 26 List<int> evictedKeys = new List<int>(); |
| 27 List<String> evictedValues = new List<String>(); | 27 List<String> evictedValues = new List<String>(); |
| 28 cache = new LRUCache<int, String>(3, (int key, String value) { | 28 cache = new LRUCache<int, String>(3, (int key, String value) { |
| 29 evictedKeys.add(key); | 29 evictedKeys.add(key); |
| 30 evictedValues.add(value); | 30 evictedValues.add(value); |
| 31 }); | 31 }); |
| 32 // fill | 32 // fill |
| 33 cache.put(1, 'A'); | 33 cache.put(1, 'A'); |
| 34 cache.put(2, 'B'); | 34 cache.put(2, 'B'); |
| 35 cache.put(3, 'C'); | 35 cache.put(3, 'C'); |
| 36 // access '1' and '3' | 36 // access '1' and '3' |
| 37 cache.get(1); | 37 cache.get(1); |
| 38 cache.get(3); | 38 cache.get(3); |
| 39 // put '4', evict '2' | 39 // put '4', evict '2' |
| 40 cache.put(4, 'D'); | 40 cache.put(4, 'D'); |
| 41 expect(cache.get(1), 'A'); | 41 expect(cache.get(1), 'A'); |
| 42 expect(cache.get(2), isNull); | 42 expect(cache.get(2), isNull); |
| 43 expect(cache.get(3), 'C'); | 43 expect(cache.get(3), 'C'); |
| 44 expect(cache.get(4), 'D'); | 44 expect(cache.get(4), 'D'); |
| 45 // check eviction listener | 45 // check eviction listener |
| 46 expect(evictedKeys, contains(2)); | 46 expect(evictedKeys, contains(2)); |
| 47 expect(evictedValues, contains('B')); | 47 expect(evictedValues, contains('B')); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void test_evict_notPut() { |
| 51 List<int> evictedKeys = new List<int>(); |
| 52 List<String> evictedValues = new List<String>(); |
| 53 cache = new LRUCache<int, String>(3, (int key, String value) { |
| 54 evictedKeys.add(key); |
| 55 evictedValues.add(value); |
| 56 }); |
| 57 // fill |
| 58 cache.put(1, 'A'); |
| 59 cache.put(2, 'B'); |
| 60 cache.put(3, 'C'); |
| 61 // put '1' and '3' |
| 62 cache.put(1, 'AA'); |
| 63 cache.put(3, 'CC'); |
| 64 // put '4', evict '2' |
| 65 cache.put(4, 'D'); |
| 66 expect(cache.get(1), 'AA'); |
| 67 expect(cache.get(2), isNull); |
| 68 expect(cache.get(3), 'CC'); |
| 69 expect(cache.get(4), 'D'); |
| 70 // check eviction listener |
| 71 expect(evictedKeys, contains(2)); |
| 72 expect(evictedValues, contains('B')); |
| 73 } |
| 74 |
| 50 void test_putGet() { | 75 void test_putGet() { |
| 51 // fill | 76 // fill |
| 52 cache.put(1, 'A'); | 77 cache.put(1, 'A'); |
| 53 cache.put(2, 'B'); | 78 cache.put(2, 'B'); |
| 54 cache.put(3, 'C'); | 79 cache.put(3, 'C'); |
| 55 // check | 80 // check |
| 56 expect(cache.get(1), 'A'); | 81 expect(cache.get(1), 'A'); |
| 57 expect(cache.get(2), 'B'); | 82 expect(cache.get(2), 'B'); |
| 58 expect(cache.get(3), 'C'); | 83 expect(cache.get(3), 'C'); |
| 59 expect(cache.get(4), isNull); | 84 expect(cache.get(4), isNull); |
| 60 } | 85 } |
| 61 | 86 |
| 62 void test_remove() { | 87 void test_remove() { |
| 63 cache.put(1, 'A'); | 88 cache.put(1, 'A'); |
| 64 cache.put(2, 'B'); | 89 cache.put(2, 'B'); |
| 65 cache.put(3, 'C'); | 90 cache.put(3, 'C'); |
| 66 // remove | 91 // remove |
| 67 cache.remove(1); | 92 cache.remove(1); |
| 68 cache.remove(3); | 93 cache.remove(3); |
| 69 // check | 94 // check |
| 70 expect(cache.get(1), isNull); | 95 expect(cache.get(1), isNull); |
| 71 expect(cache.get(2), 'B'); | 96 expect(cache.get(2), 'B'); |
| 72 expect(cache.get(3), isNull); | 97 expect(cache.get(3), isNull); |
| 73 } | 98 } |
| 74 } | 99 } |
| OLD | NEW |