Chromium Code Reviews| 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:analyzer/file_system/file_system.dart'; | |
| 6 import 'package:analyzer/file_system/memory_file_system.dart'; | |
| 7 import 'package:analyzer/src/dart/analysis/file_byte_store.dart'; | |
| 8 import 'package:test/test.dart'; | |
| 9 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 10 | |
| 11 main() { | |
| 12 defineReflectiveSuite(() { | |
| 13 defineReflectiveTests(FileByteStoreTest); | |
| 14 }); | |
| 15 } | |
| 16 | |
| 17 @reflectiveTest | |
| 18 class FileByteStoreTest { | |
| 19 final MemoryResourceProvider provider = new MemoryResourceProvider(); | |
| 20 Folder folder; | |
| 21 FileByteStore store; | |
| 22 | |
| 23 void setUp() { | |
| 24 folder = provider.newFolder('/my/cache'); | |
| 25 } | |
| 26 | |
| 27 test_evict_afterGet() { | |
| 28 store = new FileByteStore(folder, '__temp', 100); | |
| 29 | |
| 30 store.put('1', _b(40)); | |
| 31 store.put('2', _b(50)); | |
| 32 store.get('1'); | |
| 33 | |
| 34 store.put('3', _b(30)); | |
| 35 _assertExists('1', 40); | |
| 36 _assertNotExists('2'); | |
| 37 _assertExists('3', 30); | |
| 38 } | |
| 39 | |
| 40 test_evict_first() { | |
| 41 store = new FileByteStore(folder, '__temp', 100); | |
| 42 | |
| 43 store.put('1', _b(40)); | |
| 44 store.put('2', _b(50)); | |
| 45 _assertExists('1', 40); | |
| 46 _assertExists('2', 50); | |
| 47 | |
| 48 store.put('3', _b(30)); | |
| 49 _assertNotExists('1'); | |
| 50 _assertExists('2', 50); | |
| 51 _assertExists('3', 30); | |
| 52 } | |
| 53 | |
| 54 test_evict_firstAndSecond() { | |
| 55 store = new FileByteStore(folder, '__temp', 100); | |
| 56 | |
| 57 store.put('1', _b(10)); | |
| 58 store.put('2', _b(80)); | |
| 59 _assertExists('1', 10); | |
| 60 _assertExists('2', 80); | |
| 61 | |
| 62 store.put('3', _b(30)); | |
| 63 _assertNotExists('1'); | |
| 64 _assertNotExists('2'); | |
| 65 _assertExists('3', 30); | |
| 66 } | |
| 67 | |
| 68 test_evict_preExisting() { | |
| 69 folder.getChildAssumingFile('1').writeAsBytesSync(_b(40)); | |
| 70 folder.getChildAssumingFile('2').writeAsBytesSync(_b(50)); | |
| 71 store = new FileByteStore(folder, '__temp', 100); | |
| 72 | |
| 73 // Access '1', so now the queue looks [2, 1]. | |
| 74 store.get('1'); | |
| 75 | |
| 76 // 40 + 50 + 30 > 100 | |
| 77 // So, '2' is removed. | |
| 78 store.put('3', _b(30)); | |
| 79 _assertExists('1', 40); | |
| 80 _assertNotExists('2'); | |
| 81 _assertExists('3', 30); | |
| 82 } | |
| 83 | |
| 84 test_get_null() { | |
| 85 store = new FileByteStore(folder, '__temp', 1 << 10); | |
| 86 List<int> bytes = store.get('foo'); | |
| 87 expect(bytes, isNull); | |
| 88 } | |
| 89 | |
| 90 test_put_get() { | |
| 91 store = new FileByteStore(folder, '__temp', 1 << 10); | |
| 92 List<int> bytes = [0, 1, 2, 3, 4]; | |
| 93 store.put('foo', bytes); | |
| 94 expect(store.get('foo'), bytes); | |
| 95 } | |
| 96 | |
| 97 void _assertExists(String key, int length) { | |
| 98 expect(folder.getChildAssumingFile(key).exists, isTrue); | |
| 99 expect(store.get(key), hasLength(length)); | |
| 100 } | |
| 101 | |
| 102 void _assertNotExists(String key) { | |
| 103 expect(folder.getChildAssumingFile(key).exists, isFalse); | |
| 104 expect(store.get(key), isNull); | |
| 105 } | |
| 106 | |
| 107 static List<int> _b(int length) { | |
|
Brian Wilkerson
2016/11/10 22:12:04
Really ?!? If we can't just write "new List<int>",
| |
| 108 return new List<int>(length); | |
| 109 } | |
| 110 } | |
| OLD | NEW |