22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ import 'dart:typed_data' ;
6+
57import 'package:analyzer/src/dart/analysis/cache.dart' ;
8+ import 'package:analyzer_utilities/testing/map_entry_matcher.dart' ;
69import 'package:test/test.dart' ;
710import 'package:test_reflective_loader/test_reflective_loader.dart' ;
811
@@ -12,52 +15,47 @@ main() {
1215 });
1316}
1417
15- List < int > _b (int length) {
16- return List < int >. filled (length, 0 );
18+ Uint8List _b (int length) {
19+ return Uint8List (length);
1720}
1821
1922@reflectiveTest
2023class 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- }
24+ test_get () {
25+ final cache = _newBytesCache (100 );
3226
33- test_get_notFound_retry () {
34- var cache = _newBytesCache ( 100 );
27+ expect (cache. get ( '1' ), isNull);
28+ expect ( cache.map.entries, isEmpty );
3529
36- // Request '1'. Nothing found.
37- expect (cache.get ('1' , _noBytes), isNull);
30+ cache.put ('1' , _b (10 ));
31+ expect (cache.map.entries, [
32+ isMapEntry ('1' , hasLength (10 )),
33+ ]);
3834
39- // Request '1' again.
40- // The previous `null` result should not have been cached.
41- expect (cache.get ('1' , () => _b (40 )), isNotNull);
35+ expect (cache.get ('1' ), hasLength (10 ));
4236 }
4337
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 ));
38+ test_get_reorders () {
39+ final cache = _newBytesCache (100 );
40+
41+ cache.put ('1' , _b (1 ));
42+ cache.put ('2' , _b (2 ));
43+ cache.put ('3' , _b (3 ));
44+ cache.put ('4' , _b (4 ));
45+ expect (cache.map.entries, [
46+ isMapEntry ('1' , hasLength (1 )),
47+ isMapEntry ('2' , hasLength (2 )),
48+ isMapEntry ('3' , hasLength (3 )),
49+ isMapEntry ('4' , hasLength (4 )),
50+ ]);
51+
52+ expect (cache.get ('2' ), hasLength (2 ));
53+ expect (cache.map.entries, [
54+ isMapEntry ('1' , hasLength (1 )),
55+ isMapEntry ('3' , hasLength (3 )),
56+ isMapEntry ('4' , hasLength (4 )),
57+ isMapEntry ('2' , hasLength (2 )),
58+ ]);
6159 }
6260
6361 test_put_evict_first () {
@@ -66,36 +64,39 @@ class CacheTest {
6664 // 40 + 50 < 100
6765 cache.put ('1' , _b (40 ));
6866 cache.put ('2' , _b (50 ));
69- expect (cache.get ('1' , _noBytes), hasLength (40 ));
70- expect (cache.get ('2' , _noBytes), hasLength (50 ));
67+ expect (cache.map.entries, [
68+ isMapEntry ('1' , hasLength (40 )),
69+ isMapEntry ('2' , hasLength (50 )),
70+ ]);
7171
7272 // 40 + 50 + 30 > 100
7373 // So, '1' is evicted.
7474 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 ));
75+ expect (cache.map.entries, [
76+ isMapEntry ('2' , hasLength (50 )),
77+ isMapEntry ('3' , hasLength (30 )),
78+ ]);
7879 }
7980
80- test_put_evict_firstAndSecond () {
81+ test_put_evict_firstSecond () {
8182 var cache = _newBytesCache (100 );
8283
8384 // 10 + 80 < 100
8485 cache.put ('1' , _b (10 ));
8586 cache.put ('2' , _b (80 ));
86- expect (cache.get ('1' , _noBytes), hasLength (10 ));
87- expect (cache.get ('2' , _noBytes), hasLength (80 ));
87+ expect (cache.map.entries, [
88+ isMapEntry ('1' , hasLength (10 )),
89+ isMapEntry ('2' , hasLength (80 )),
90+ ]);
8891
8992 // 10 + 80 + 30 > 100
9093 // So, '1' and '2' are evicted.
9194 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+ expect (cache.map.entries, [
96+ isMapEntry ( '3 ' , hasLength ( 30 )),
97+ ] );
9598 }
9699
97- Cache <String , List <int >> _newBytesCache (int maxSizeBytes) =>
98- Cache <String , List <int >>(maxSizeBytes, (bytes) => bytes.length);
99-
100- static List <int >? _noBytes () => null ;
100+ Cache <String , Uint8List > _newBytesCache (int maxSizeBytes) =>
101+ Cache <String , Uint8List >(maxSizeBytes, (bytes) => bytes.length);
101102}
0 commit comments