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/src/dart/analysis/byte_store.dart'; | |
6 import 'package:test/test.dart'; | |
7 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
8 import 'package:typed_mock/typed_mock.dart'; | |
9 | |
10 main() { | |
11 defineReflectiveSuite(() { | |
12 defineReflectiveTests(MemoryCachingByteStoreTest); | |
13 defineReflectiveTests(NullByteStoreTest); | |
14 }); | |
15 } | |
16 | |
17 List<int> _b(int length) { | |
18 return new List<int>(length); | |
19 } | |
20 | |
21 @reflectiveTest | |
22 class MemoryCachingByteStoreTest { | |
23 test_get_notFound_evict() { | |
24 var store = new _TestByteStore(); | |
25 var cachingStore = new MemoryCachingByteStore(store, 100); | |
26 | |
27 // Request '1'. Nothing found. | |
28 cachingStore.get('1'); | |
29 | |
30 // Add enough data to the store to force an eviction. | |
31 cachingStore.put('2', _b(40)); | |
32 cachingStore.put('3', _b(40)); | |
33 cachingStore.put('4', _b(40)); | |
34 } | |
35 | |
36 test_get_notFound_retry() { | |
37 var mockStore = new _TestByteStore(); | |
38 var baseStore = new MemoryCachingByteStore(mockStore, 1000); | |
39 var cachingStore = new MemoryCachingByteStore(baseStore, 100); | |
40 | |
41 // Request '1'. Nothing found. | |
42 expect(cachingStore.get('1'), isNull); | |
43 | |
44 // Add data to the base store, bypassing the caching store. | |
45 baseStore.put('1', _b(40)); | |
46 | |
47 // Request '1' again. The previous `null` result should not have been | |
48 // cached. | |
49 expect(cachingStore.get('1'), isNotNull); | |
50 } | |
51 | |
52 test_get_put_evict() { | |
53 var store = new _TestByteStore(); | |
54 var cachingStore = new MemoryCachingByteStore(store, 100); | |
55 | |
56 // Keys: [1, 2]. | |
57 cachingStore.put('1', _b(40)); | |
58 cachingStore.put('2', _b(50)); | |
59 | |
60 // Request '1', so now it is the most recently used. | |
61 // Keys: [2, 1]. | |
62 cachingStore.get('1'); | |
63 | |
64 // 40 + 50 + 30 > 100 | |
65 // So, '2' is evicted. | |
66 cachingStore.put('3', _b(30)); | |
67 expect(cachingStore.get('1'), hasLength(40)); | |
68 expect(cachingStore.get('2'), isNull); | |
69 expect(cachingStore.get('3'), hasLength(30)); | |
70 } | |
71 | |
72 test_put_evict_first() { | |
73 var store = new _TestByteStore(); | |
74 var cachingStore = new MemoryCachingByteStore(store, 100); | |
75 | |
76 // 40 + 50 < 100 | |
77 cachingStore.put('1', _b(40)); | |
78 cachingStore.put('2', _b(50)); | |
79 expect(cachingStore.get('1'), hasLength(40)); | |
80 expect(cachingStore.get('2'), hasLength(50)); | |
81 | |
82 // 40 + 50 + 30 > 100 | |
83 // So, '1' is evicted. | |
84 cachingStore.put('3', _b(30)); | |
85 expect(cachingStore.get('1'), isNull); | |
86 expect(cachingStore.get('2'), hasLength(50)); | |
87 expect(cachingStore.get('3'), hasLength(30)); | |
88 } | |
89 | |
90 test_put_evict_firstAndSecond() { | |
91 var store = new _TestByteStore(); | |
92 var cachingStore = new MemoryCachingByteStore(store, 100); | |
93 | |
94 // 10 + 80 < 100 | |
95 cachingStore.put('1', _b(10)); | |
96 cachingStore.put('2', _b(80)); | |
97 expect(cachingStore.get('1'), hasLength(10)); | |
98 expect(cachingStore.get('2'), hasLength(80)); | |
99 | |
100 // 10 + 80 + 30 > 100 | |
101 // So, '1' and '2' are evicted. | |
102 cachingStore.put('3', _b(30)); | |
103 expect(cachingStore.get('1'), isNull); | |
104 expect(cachingStore.get('2'), isNull); | |
105 expect(cachingStore.get('3'), hasLength(30)); | |
106 } | |
107 } | |
108 | |
109 @reflectiveTest | |
110 class NullByteStoreTest { | |
111 test_get() { | |
112 var store = new NullByteStore(); | |
113 | |
114 expect(store.get('1'), isNull); | |
115 | |
116 store.put('1', _b(10)); | |
117 expect(store.get('1'), isNull); | |
118 } | |
119 } | |
120 | |
121 class _TestByteStore extends TypedMock implements ByteStore {} | |
OLD | NEW |