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