OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 #include "vm/fixed_cache.h" |
5 #include <string.h> | 6 #include <string.h> |
6 #include "platform/assert.h" | 7 #include "platform/assert.h" |
7 #include "vm/fixed_cache.h" | |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 UNIT_TEST_CASE(FixedCacheEmpty) { | 12 UNIT_TEST_CASE(FixedCacheEmpty) { |
13 FixedCache<int, int, 2> cache; | 13 FixedCache<int, int, 2> cache; |
14 EXPECT(cache.Lookup(0) == NULL); | 14 EXPECT(cache.Lookup(0) == NULL); |
15 EXPECT(cache.Lookup(1) == NULL); | 15 EXPECT(cache.Lookup(1) == NULL); |
16 cache.Insert(1, 2); | 16 cache.Insert(1, 2); |
17 EXPECT(*cache.Lookup(1) == 2); | 17 EXPECT(*cache.Lookup(1) == 2); |
18 EXPECT(cache.Lookup(0) == NULL); | 18 EXPECT(cache.Lookup(0) == NULL); |
19 } | 19 } |
20 | 20 |
21 | |
22 UNIT_TEST_CASE(FixedCacheHalfFull) { | 21 UNIT_TEST_CASE(FixedCacheHalfFull) { |
23 FixedCache<int, const char*, 8> cache; | 22 FixedCache<int, const char*, 8> cache; |
24 // Insert at end. | 23 // Insert at end. |
25 cache.Insert(10, "a"); | 24 cache.Insert(10, "a"); |
26 cache.Insert(20, "b"); | 25 cache.Insert(20, "b"); |
27 cache.Insert(40, "c"); | 26 cache.Insert(40, "c"); |
28 // Insert in the middle. | 27 // Insert in the middle. |
29 cache.Insert(15, "ab"); | 28 cache.Insert(15, "ab"); |
30 cache.Insert(25, "bc"); | 29 cache.Insert(25, "bc"); |
31 // Insert in front. | 30 // Insert in front. |
32 cache.Insert(5, "_"); | 31 cache.Insert(5, "_"); |
33 // Check all items. | 32 // Check all items. |
34 EXPECT(strcmp(*cache.Lookup(5), "_") == 0); | 33 EXPECT(strcmp(*cache.Lookup(5), "_") == 0); |
35 EXPECT(strcmp(*cache.Lookup(10), "a") == 0); | 34 EXPECT(strcmp(*cache.Lookup(10), "a") == 0); |
36 EXPECT(strcmp(*cache.Lookup(20), "b") == 0); | 35 EXPECT(strcmp(*cache.Lookup(20), "b") == 0); |
37 EXPECT(strcmp(*cache.Lookup(40), "c") == 0); | 36 EXPECT(strcmp(*cache.Lookup(40), "c") == 0); |
38 EXPECT(strcmp(*cache.Lookup(25), "bc") == 0); | 37 EXPECT(strcmp(*cache.Lookup(25), "bc") == 0); |
39 // Non-existent - front, middle, end. | 38 // Non-existent - front, middle, end. |
40 EXPECT(cache.Lookup(1) == NULL); | 39 EXPECT(cache.Lookup(1) == NULL); |
41 EXPECT(cache.Lookup(35) == NULL); | 40 EXPECT(cache.Lookup(35) == NULL); |
42 EXPECT(cache.Lookup(50) == NULL); | 41 EXPECT(cache.Lookup(50) == NULL); |
43 } | 42 } |
44 | 43 |
45 | |
46 struct Resource { | 44 struct Resource { |
47 Resource() : id(0) { copies++; } | 45 Resource() : id(0) { copies++; } |
48 explicit Resource(int id_) : id(id_) { copies++; } | 46 explicit Resource(int id_) : id(id_) { copies++; } |
49 | 47 |
50 Resource(const Resource& r) { | 48 Resource(const Resource& r) { |
51 id = r.id; | 49 id = r.id; |
52 copies++; | 50 copies++; |
53 } | 51 } |
54 | 52 |
55 Resource& operator=(const Resource& r) { | 53 Resource& operator=(const Resource& r) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 85 |
88 // Insert at end top - 30 gets replaced by 40. | 86 // Insert at end top - 30 gets replaced by 40. |
89 cache.Insert(40, Resource(16)); | 87 cache.Insert(40, Resource(16)); |
90 EXPECT(cache.Lookup(40)->id == 16); | 88 EXPECT(cache.Lookup(40)->id == 16); |
91 EXPECT(cache.Lookup(30) == NULL); | 89 EXPECT(cache.Lookup(30) == NULL); |
92 } | 90 } |
93 EXPECT(Resource::copies == 0); | 91 EXPECT(Resource::copies == 0); |
94 } | 92 } |
95 | 93 |
96 } // namespace dart | 94 } // namespace dart |
OLD | NEW |