Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/templates/test_lru.h
10278 views
1
/**************************************************************************/
2
/* test_lru.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/templates/lru.h"
34
35
#include "tests/test_macros.h"
36
37
namespace TestLRU {
38
39
TEST_CASE("[LRU] Store and read") {
40
LRUCache<int, int> lru;
41
42
lru.set_capacity(3);
43
lru.insert(1, 1);
44
lru.insert(50, 2);
45
lru.insert(100, 5);
46
47
CHECK(lru.has(1));
48
CHECK(lru.has(50));
49
CHECK(lru.has(100));
50
CHECK(!lru.has(200));
51
52
CHECK(lru.get(1) == 1);
53
CHECK(lru.get(50) == 2);
54
CHECK(lru.get(100) == 5);
55
56
CHECK(lru.getptr(1) != nullptr);
57
CHECK(lru.getptr(1000) == nullptr);
58
59
lru.insert(600, 600); // Erase <50>
60
CHECK(lru.has(600));
61
CHECK(!lru.has(50));
62
}
63
64
TEST_CASE("[LRU] Resize and clear") {
65
LRUCache<int, int> lru;
66
67
lru.set_capacity(3);
68
lru.insert(1, 1);
69
lru.insert(2, 2);
70
lru.insert(3, 3);
71
72
CHECK(lru.get_capacity() == 3);
73
74
lru.set_capacity(5);
75
CHECK(lru.get_capacity() == 5);
76
77
CHECK(lru.has(1));
78
CHECK(lru.has(2));
79
CHECK(lru.has(3));
80
CHECK(!lru.has(4));
81
82
lru.set_capacity(2);
83
CHECK(lru.get_capacity() == 2);
84
85
CHECK(!lru.has(1));
86
CHECK(lru.has(2));
87
CHECK(lru.has(3));
88
CHECK(!lru.has(4));
89
90
lru.clear();
91
CHECK(!lru.has(1));
92
CHECK(!lru.has(2));
93
CHECK(!lru.has(3));
94
CHECK(!lru.has(4));
95
}
96
} // namespace TestLRU
97
98