-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (67 loc) · 2.38 KB
/
main.cpp
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
#include <iostream>
#include <cassert>
#include <HybridCache.h> // Include your HybridCache header file
// Define test cases
void test_insert_and_retrieve() {
HybridCache<int, std::string> cache;
// Insert some key-value pairs
cache.insert(1, "Value1");
cache.insert(2, "Value2");
cache.insert(3, "Value3");
// Retrieve values and check
assert(cache.get(1) == "Value1");
assert(cache.get(2) == "Value2");
assert(cache.get(3) == "Value3");
std::cout << "Insert and retrieve test passed!" << std::endl;
}
void test_multi_block() {
HybridCache<int, std::string> cache;
// Insert key-value pairs until memory is full
std::string longString(BLOCK_SIZE / 6, 'x');
for (int i = 0; i < BLOCK_SIZE / (sizeof(int) + longString.size()); ++i) {
cache.insert(i, longString);
}
// Insert one more entry, should trigger eviction
cache.insert(100, longString);
std::string temp = cache.get(0);
// Ensure that the evicted block is no longer in memory
assert(cache.get(0) == longString); // Assuming default-constructed ValueType represents absence
std::cout << "Multi-block get test passed!" << std::endl;
}
void test_eviction() {
HybridCache<int, std::string> cache;
// Insert key-value pairs until memory is full
std::string longString(BLOCK_SIZE / 6, 'x');
for (int i = 0; i < 2 * BLOCK_SIZE / (sizeof(int) + longString.size()); ++i) {
cache.insert(i, longString);
}
cache.insert(100, longString);
// Ensure that the evicted block is no longer in memory, but promote from disk
assert(cache.get(0) == longString); // Assuming default-constructed ValueType represents absence
std::cout << "Eviction test passed!" << std::endl;
}
void test_load_previous_cache() {
// Create a new cache
HybridCache<int, std::string> cache (
MEMORY_CACHE_SIZE,
DISK_CACHE_SIZE,
BLOCK_SIZE,
"hybrid.cache",
"hybrid.meta",
true,
MAX_BF_SIZE,
BLOOM_FILTER_BITS_PER_KEY
);
std::cout << "Load previous cache test passed!" << std::endl;
}
// Add more test cases as needed
int main() {
// Run test cases
test_insert_and_retrieve();
test_multi_block();
test_eviction();
test_load_previous_cache();
// Add more test cases here
std::cout << "All tests passed!" << std::endl;
return 0;
}