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 <string.h> | 5 #include <string.h> |
6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
7 #include "vm/fixed_cache.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 { |
(...skipping 26 matching lines...) Expand all Loading... |
37 EXPECT(strcmp(*cache.Lookup(40), "c") == 0); | 37 EXPECT(strcmp(*cache.Lookup(40), "c") == 0); |
38 EXPECT(strcmp(*cache.Lookup(25), "bc") == 0); | 38 EXPECT(strcmp(*cache.Lookup(25), "bc") == 0); |
39 // Non-existent - front, middle, end. | 39 // Non-existent - front, middle, end. |
40 EXPECT(cache.Lookup(1) == NULL); | 40 EXPECT(cache.Lookup(1) == NULL); |
41 EXPECT(cache.Lookup(35) == NULL); | 41 EXPECT(cache.Lookup(35) == NULL); |
42 EXPECT(cache.Lookup(50) == NULL); | 42 EXPECT(cache.Lookup(50) == NULL); |
43 } | 43 } |
44 | 44 |
45 | 45 |
46 struct Resource { | 46 struct Resource { |
47 Resource() : id(0), stuff(NULL) {} | 47 Resource() : id(0) { copies++; } |
48 explicit Resource(int id_) : id(id_), stuff(new int) {} | 48 explicit Resource(int id_) : id(id_) { copies++; } |
| 49 |
| 50 Resource(const Resource& r) { |
| 51 id = r.id; |
| 52 copies++; |
| 53 } |
| 54 |
| 55 Resource& operator=(const Resource& r) { |
| 56 id = r.id; |
| 57 return *this; |
| 58 } |
| 59 |
| 60 ~Resource() { copies--; } |
49 | 61 |
50 int id; | 62 int id; |
51 int* stuff; | 63 static int copies; |
52 }; | 64 }; |
53 | 65 |
54 static void freeResource(Resource* res) { | 66 int Resource::copies = 0; |
55 delete res->stuff; | |
56 } | |
57 | 67 |
| 68 UNIT_TEST_CASE(FixedCacheFullResource) { |
| 69 { |
| 70 FixedCache<int, Resource, 6> cache; |
| 71 cache.Insert(10, Resource(2)); |
| 72 cache.Insert(20, Resource(4)); |
| 73 cache.Insert(40, Resource(16)); |
| 74 cache.Insert(30, Resource(8)); |
| 75 EXPECT(cache.Lookup(40)->id == 16); |
| 76 EXPECT(cache.Lookup(5) == NULL); |
| 77 EXPECT(cache.Lookup(0) == NULL); |
| 78 // Insert in the front, middle. |
| 79 cache.Insert(5, Resource(1)); |
| 80 cache.Insert(15, Resource(3)); |
| 81 cache.Insert(25, Resource(6)); |
| 82 // 40 got removed by shifting. |
| 83 EXPECT(cache.Lookup(40) == NULL); |
| 84 EXPECT(cache.Lookup(5)->id == 1); |
| 85 EXPECT(cache.Lookup(15)->id == 3); |
| 86 EXPECT(cache.Lookup(25)->id == 6); |
58 | 87 |
59 UNIT_TEST_CASE(FixedCacheFullDeleter) { | 88 // Insert at end top - 30 gets replaced by 40. |
60 FixedCache<int, Resource, 6> cache(freeResource); | 89 cache.Insert(40, Resource(16)); |
61 cache.Insert(10, Resource(2)); | 90 EXPECT(cache.Lookup(40)->id == 16); |
62 cache.Insert(20, Resource(4)); | 91 EXPECT(cache.Lookup(30) == NULL); |
63 cache.Insert(40, Resource(16)); | 92 } |
64 cache.Insert(30, Resource(8)); | 93 EXPECT(Resource::copies == 0); |
65 EXPECT(cache.Lookup(40)->id == 16); | |
66 EXPECT(cache.Lookup(5) == NULL); | |
67 EXPECT(cache.Lookup(0) == NULL); | |
68 // Insert in the front, middle. | |
69 cache.Insert(5, Resource(1)); | |
70 cache.Insert(15, Resource(3)); | |
71 cache.Insert(25, Resource(6)); | |
72 // 40 got removed by shifting. | |
73 EXPECT(cache.Lookup(40) == NULL); | |
74 EXPECT(cache.Lookup(5)->id == 1); | |
75 EXPECT(cache.Lookup(15)->id == 3); | |
76 EXPECT(cache.Lookup(25)->id == 6); | |
77 | |
78 // Insert at end top - 30 gets replaced by 40. | |
79 cache.Insert(40, Resource(16)); | |
80 EXPECT(cache.Lookup(40)->id == 16); | |
81 EXPECT(cache.Lookup(30) == NULL); | |
82 } | 94 } |
83 | 95 |
84 } // namespace dart | 96 } // namespace dart |
OLD | NEW |