Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fixed_cache_test.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/fixed_cache.h"
6#include <string.h>
7#include "platform/assert.h"
8#include "vm/unit_test.h"
9
10namespace dart {
11
12UNIT_TEST_CASE(FixedCacheEmpty) {
14 EXPECT(cache.Lookup(0) == nullptr);
15 EXPECT(cache.Lookup(1) == nullptr);
16 cache.Insert(1, 2);
17 EXPECT(*cache.Lookup(1) == 2);
18 EXPECT(cache.Lookup(0) == nullptr);
19}
20
21UNIT_TEST_CASE(FixedCacheHalfFull) {
23 // Insert at end.
24 cache.Insert(10, "a");
25 cache.Insert(20, "b");
26 cache.Insert(40, "c");
27 // Insert in the middle.
28 cache.Insert(15, "ab");
29 cache.Insert(25, "bc");
30 // Insert in front.
31 cache.Insert(5, "_");
32 // Check all items.
33 EXPECT(strcmp(*cache.Lookup(5), "_") == 0);
34 EXPECT(strcmp(*cache.Lookup(10), "a") == 0);
35 EXPECT(strcmp(*cache.Lookup(20), "b") == 0);
36 EXPECT(strcmp(*cache.Lookup(40), "c") == 0);
37 EXPECT(strcmp(*cache.Lookup(25), "bc") == 0);
38 // Nonexistent - front, middle, end.
39 EXPECT(cache.Lookup(1) == nullptr);
40 EXPECT(cache.Lookup(35) == nullptr);
41 EXPECT(cache.Lookup(50) == nullptr);
42}
43
44struct Resource {
45 Resource() : id(0) { copies++; }
46 explicit Resource(int id_) : id(id_) { copies++; }
47
48 Resource(const Resource& r) {
49 id = r.id;
50 copies++;
51 }
52
54 id = r.id;
55 return *this;
56 }
57
59
60 int id;
61 static int copies;
62};
63
64int Resource::copies = 0;
65
66UNIT_TEST_CASE(FixedCacheFullResource) {
67 {
69 cache.Insert(10, Resource(2));
70 cache.Insert(20, Resource(4));
71 cache.Insert(40, Resource(16));
72 cache.Insert(30, Resource(8));
73 EXPECT(cache.Lookup(40)->id == 16);
74 EXPECT(cache.Lookup(5) == nullptr);
75 EXPECT(cache.Lookup(0) == nullptr);
76 // Insert in the front, middle.
77 cache.Insert(5, Resource(1));
78 cache.Insert(15, Resource(3));
79 cache.Insert(25, Resource(6));
80 // 40 got removed by shifting.
81 EXPECT(cache.Lookup(40) == nullptr);
82 EXPECT(cache.Lookup(5)->id == 1);
83 EXPECT(cache.Lookup(15)->id == 3);
84 EXPECT(cache.Lookup(25)->id == 6);
85
86 // Insert at end top - 30 gets replaced by 40.
87 cache.Insert(40, Resource(16));
88 EXPECT(cache.Lookup(40)->id == 16);
89 EXPECT(cache.Lookup(30) == nullptr);
90 }
92}
93
94} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
#define UNIT_TEST_CASE(name)
Definition unit_test.h:23
Resource & operator=(const Resource &r)
Resource(const Resource &r)