| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library test.src.util.lru_map; |
| 6 |
| 7 import 'package:analyzer/src/util/lru_map.dart'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 |
| 10 import '../../reflective_tests.dart'; |
| 11 |
| 12 |
| 13 main() { |
| 14 groupSep = ' | '; |
| 15 runReflectiveTests(_LRUCacheTest); |
| 16 } |
| 17 |
| 18 |
| 19 @reflectiveTest |
| 20 class _LRUCacheTest { |
| 21 LRUMap<int, String> cache = new LRUMap<int, String>(3); |
| 22 |
| 23 void test_evict_notGet() { |
| 24 List<int> evictedKeys = new List<int>(); |
| 25 List<String> evictedValues = new List<String>(); |
| 26 cache = new LRUMap<int, String>(3, (int key, String value) { |
| 27 evictedKeys.add(key); |
| 28 evictedValues.add(value); |
| 29 }); |
| 30 // fill |
| 31 cache.put(1, 'A'); |
| 32 cache.put(2, 'B'); |
| 33 cache.put(3, 'C'); |
| 34 // access '1' and '3' |
| 35 cache.get(1); |
| 36 cache.get(3); |
| 37 // put '4', evict '2' |
| 38 cache.put(4, 'D'); |
| 39 expect(cache.get(1), 'A'); |
| 40 expect(cache.get(2), isNull); |
| 41 expect(cache.get(3), 'C'); |
| 42 expect(cache.get(4), 'D'); |
| 43 // check eviction listener |
| 44 expect(evictedKeys, contains(2)); |
| 45 expect(evictedValues, contains('B')); |
| 46 } |
| 47 |
| 48 void test_evict_notPut() { |
| 49 List<int> evictedKeys = new List<int>(); |
| 50 List<String> evictedValues = new List<String>(); |
| 51 cache = new LRUMap<int, String>(3, (int key, String value) { |
| 52 evictedKeys.add(key); |
| 53 evictedValues.add(value); |
| 54 }); |
| 55 // fill |
| 56 cache.put(1, 'A'); |
| 57 cache.put(2, 'B'); |
| 58 cache.put(3, 'C'); |
| 59 // put '1' and '3' |
| 60 cache.put(1, 'AA'); |
| 61 cache.put(3, 'CC'); |
| 62 // put '4', evict '2' |
| 63 cache.put(4, 'D'); |
| 64 expect(cache.get(1), 'AA'); |
| 65 expect(cache.get(2), isNull); |
| 66 expect(cache.get(3), 'CC'); |
| 67 expect(cache.get(4), 'D'); |
| 68 // check eviction listener |
| 69 expect(evictedKeys, contains(2)); |
| 70 expect(evictedValues, contains('B')); |
| 71 } |
| 72 |
| 73 void test_putGet() { |
| 74 // fill |
| 75 cache.put(1, 'A'); |
| 76 cache.put(2, 'B'); |
| 77 cache.put(3, 'C'); |
| 78 // check |
| 79 expect(cache.get(1), 'A'); |
| 80 expect(cache.get(2), 'B'); |
| 81 expect(cache.get(3), 'C'); |
| 82 expect(cache.get(4), isNull); |
| 83 } |
| 84 |
| 85 void test_remove() { |
| 86 cache.put(1, 'A'); |
| 87 cache.put(2, 'B'); |
| 88 cache.put(3, 'C'); |
| 89 // remove |
| 90 cache.remove(1); |
| 91 cache.remove(3); |
| 92 // check |
| 93 expect(cache.get(1), isNull); |
| 94 expect(cache.get(2), 'B'); |
| 95 expect(cache.get(3), isNull); |
| 96 } |
| 97 } |
| OLD | NEW |