| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 import 'package:front_end/src/byte_store/cache.dart'; |
| 6 import 'package:test/test.dart'; |
| 7 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 8 |
| 9 main() { |
| 10 defineReflectiveSuite(() { |
| 11 defineReflectiveTests(CacheTest); |
| 12 }); |
| 13 } |
| 14 |
| 15 List<int> _b(int length) { |
| 16 return new List<int>(length); |
| 17 } |
| 18 |
| 19 @reflectiveTest |
| 20 class CacheTest { |
| 21 test_get_notFound_evict() { |
| 22 var cache = _newBytesCache(100); |
| 23 |
| 24 // Request '1'. Nothing found. |
| 25 expect(cache.get('1', _noBytes), isNull); |
| 26 |
| 27 // Add enough data to the store to force an eviction. |
| 28 cache.put('2', _b(40)); |
| 29 cache.put('3', _b(40)); |
| 30 cache.put('4', _b(40)); |
| 31 } |
| 32 |
| 33 test_get_notFound_retry() { |
| 34 var cache = _newBytesCache(100); |
| 35 |
| 36 // Request '1'. Nothing found. |
| 37 expect(cache.get('1', _noBytes), isNull); |
| 38 |
| 39 // Request '1' again. |
| 40 // The previous `null` result should not have been cached. |
| 41 expect(cache.get('1', () => _b(40)), isNotNull); |
| 42 } |
| 43 |
| 44 test_get_put_evict() { |
| 45 var cache = _newBytesCache(100); |
| 46 |
| 47 // Keys: [1, 2]. |
| 48 cache.put('1', _b(40)); |
| 49 cache.put('2', _b(50)); |
| 50 |
| 51 // Request '1', so now it is the most recently used. |
| 52 // Keys: [2, 1]. |
| 53 cache.get('1', _noBytes); |
| 54 |
| 55 // 40 + 50 + 30 > 100 |
| 56 // So, '2' is evicted. |
| 57 cache.put('3', _b(30)); |
| 58 expect(cache.get('1', _noBytes), hasLength(40)); |
| 59 expect(cache.get('2', _noBytes), isNull); |
| 60 expect(cache.get('3', _noBytes), hasLength(30)); |
| 61 } |
| 62 |
| 63 test_put_evict_first() { |
| 64 var cache = _newBytesCache(100); |
| 65 |
| 66 // 40 + 50 < 100 |
| 67 cache.put('1', _b(40)); |
| 68 cache.put('2', _b(50)); |
| 69 expect(cache.get('1', _noBytes), hasLength(40)); |
| 70 expect(cache.get('2', _noBytes), hasLength(50)); |
| 71 |
| 72 // 40 + 50 + 30 > 100 |
| 73 // So, '1' is evicted. |
| 74 cache.put('3', _b(30)); |
| 75 expect(cache.get('1', _noBytes), isNull); |
| 76 expect(cache.get('2', _noBytes), hasLength(50)); |
| 77 expect(cache.get('3', _noBytes), hasLength(30)); |
| 78 } |
| 79 |
| 80 test_put_evict_firstAndSecond() { |
| 81 var cache = _newBytesCache(100); |
| 82 |
| 83 // 10 + 80 < 100 |
| 84 cache.put('1', _b(10)); |
| 85 cache.put('2', _b(80)); |
| 86 expect(cache.get('1', _noBytes), hasLength(10)); |
| 87 expect(cache.get('2', _noBytes), hasLength(80)); |
| 88 |
| 89 // 10 + 80 + 30 > 100 |
| 90 // So, '1' and '2' are evicted. |
| 91 cache.put('3', _b(30)); |
| 92 expect(cache.get('1', _noBytes), isNull); |
| 93 expect(cache.get('2', _noBytes), isNull); |
| 94 expect(cache.get('3', _noBytes), hasLength(30)); |
| 95 } |
| 96 |
| 97 Cache<String, List<int>> _newBytesCache(int maxSizeBytes) => |
| 98 new Cache<String, List<int>>(maxSizeBytes, (bytes) => bytes.length); |
| 99 |
| 100 static List<int> _noBytes() => null; |
| 101 } |
| OLD | NEW |