Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Unified Diff: pkg/front_end/test/src/byte_store/cache_test.dart

Issue 2998363002: Generalize LRU Cache to any objects. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/front_end/test/src/byte_store/byte_store_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/test/src/byte_store/cache_test.dart
diff --git a/pkg/front_end/test/src/byte_store/cache_test.dart b/pkg/front_end/test/src/byte_store/cache_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6221e43bcaaeab31cb5b4fdae03775cd154c3dd0
--- /dev/null
+++ b/pkg/front_end/test/src/byte_store/cache_test.dart
@@ -0,0 +1,101 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:front_end/src/byte_store/cache.dart';
+import 'package:test/test.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(CacheTest);
+ });
+}
+
+List<int> _b(int length) {
+ return new List<int>(length);
+}
+
+@reflectiveTest
+class CacheTest {
+ test_get_notFound_evict() {
+ var cache = _newBytesCache(100);
+
+ // Request '1'. Nothing found.
+ expect(cache.get('1', _noBytes), isNull);
+
+ // Add enough data to the store to force an eviction.
+ cache.put('2', _b(40));
+ cache.put('3', _b(40));
+ cache.put('4', _b(40));
+ }
+
+ test_get_notFound_retry() {
+ var cache = _newBytesCache(100);
+
+ // Request '1'. Nothing found.
+ expect(cache.get('1', _noBytes), isNull);
+
+ // Request '1' again.
+ // The previous `null` result should not have been cached.
+ expect(cache.get('1', () => _b(40)), isNotNull);
+ }
+
+ test_get_put_evict() {
+ var cache = _newBytesCache(100);
+
+ // Keys: [1, 2].
+ cache.put('1', _b(40));
+ cache.put('2', _b(50));
+
+ // Request '1', so now it is the most recently used.
+ // Keys: [2, 1].
+ cache.get('1', _noBytes);
+
+ // 40 + 50 + 30 > 100
+ // So, '2' is evicted.
+ cache.put('3', _b(30));
+ expect(cache.get('1', _noBytes), hasLength(40));
+ expect(cache.get('2', _noBytes), isNull);
+ expect(cache.get('3', _noBytes), hasLength(30));
+ }
+
+ test_put_evict_first() {
+ var cache = _newBytesCache(100);
+
+ // 40 + 50 < 100
+ cache.put('1', _b(40));
+ cache.put('2', _b(50));
+ expect(cache.get('1', _noBytes), hasLength(40));
+ expect(cache.get('2', _noBytes), hasLength(50));
+
+ // 40 + 50 + 30 > 100
+ // So, '1' is evicted.
+ cache.put('3', _b(30));
+ expect(cache.get('1', _noBytes), isNull);
+ expect(cache.get('2', _noBytes), hasLength(50));
+ expect(cache.get('3', _noBytes), hasLength(30));
+ }
+
+ test_put_evict_firstAndSecond() {
+ var cache = _newBytesCache(100);
+
+ // 10 + 80 < 100
+ cache.put('1', _b(10));
+ cache.put('2', _b(80));
+ expect(cache.get('1', _noBytes), hasLength(10));
+ expect(cache.get('2', _noBytes), hasLength(80));
+
+ // 10 + 80 + 30 > 100
+ // So, '1' and '2' are evicted.
+ cache.put('3', _b(30));
+ expect(cache.get('1', _noBytes), isNull);
+ expect(cache.get('2', _noBytes), isNull);
+ expect(cache.get('3', _noBytes), hasLength(30));
+ }
+
+ Cache<String, List<int>> _newBytesCache(int maxSizeBytes) =>
+ new Cache<String, List<int>>(maxSizeBytes, (bytes) => bytes.length);
+
+ static List<int> _noBytes() => null;
+}
« no previous file with comments | « pkg/front_end/test/src/byte_store/byte_store_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698