-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (70 loc) · 1.84 KB
/
main.cpp
File metadata and controls
90 lines (70 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cassert>
#include <iostream>
#include <vector>
#include "lru_cache.h"
static void expectEq(const std::vector<int>& actual,
const std::vector<int>& expected) {
assert(actual == expected);
}
static void testBasicEviction() {
LRUCache cache(2);
assert(cache.empty());
assert(cache.capacity() == 2);
cache.put(1, 10);
cache.put(2, 20);
assert(cache.size() == 2);
expectEq(cache.keys(), {2, 1});
assert(cache.get(1) == 10);
expectEq(cache.keys(), {1, 2});
cache.put(3, 30);
assert(cache.get(2) == -1);
assert(cache.get(1) == 10);
assert(cache.get(3) == 30);
expectEq(cache.keys(), {3, 1});
}
static void testUpdateExistingKey() {
LRUCache cache(2);
cache.put(1, 10);
cache.put(2, 20);
cache.put(1, 100);
assert(cache.get(1) == 100);
expectEq(cache.keys(), {1, 2});
cache.put(3, 30);
assert(cache.get(2) == -1);
assert(cache.get(1) == 100);
assert(cache.get(3) == 30);
}
static void testCapacityOne() {
LRUCache cache(1);
cache.put(1, 10);
assert(cache.get(1) == 10);
cache.put(2, 20);
assert(cache.get(1) == -1);
assert(cache.get(2) == 20);
expectEq(cache.keys(), {2});
}
static void testZeroCapacity() {
LRUCache cache(0);
cache.put(1, 10);
assert(cache.empty());
assert(cache.get(1) == -1);
expectEq(cache.keys(), {});
}
static void testGetMissingDoesNotChangeOrder() {
LRUCache cache(3);
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
expectEq(cache.keys(), {3, 2, 1});
assert(cache.get(99) == -1);
expectEq(cache.keys(), {3, 2, 1});
}
int main() {
testBasicEviction();
testUpdateExistingKey();
testCapacityOne();
testZeroCapacity();
testGetMissingDoesNotChangeOrder();
std::cout << "All LRU cache tests passed.\n";
return 0;
}