-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathtests.rs
More file actions
181 lines (167 loc) · 5.96 KB
/
Copy pathtests.rs
File metadata and controls
181 lines (167 loc) · 5.96 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use super::{LeastFrequentlyUsedPriorityQueue, LeastRecentlyUsedPriorityQueue};
// ---------------- LRU tests -----------------
#[test]
fn lru_insert_all_all_existing() {
let mut lru = LeastRecentlyUsedPriorityQueue::new(4);
lru.insert_all(["a", "b", "c"]);
assert_eq!(lru.inserted_new_count(), 3);
assert_eq!(lru.removed_count(), 0);
lru.insert_all(["b", "c"]); // no new keys
assert_eq!(lru.inserted_new_count(), 3);
assert_eq!(lru.removed_count(), 0);
assert_eq!(lru.max_size(), 3);
}
#[test]
fn lru_insert_all_some_new_no_eviction() {
let mut lru = LeastRecentlyUsedPriorityQueue::new(5);
lru.insert_all(["a", "b"]);
lru.insert_all(["b", "c", "d"]); // adds c,d
assert_eq!(lru.inserted_new_count(), 4); // a,b,c,d
assert_eq!(lru.removed_count(), 0);
assert_eq!(lru.max_size(), 4);
}
#[test]
fn lru_insert_all_with_eviction() {
let mut lru = LeastRecentlyUsedPriorityQueue::new(3);
lru.insert_all(["a", "b"]);
lru.insert_all(["b", "c", "d"]); // evicts a, inserts c,d
assert_eq!(lru.inserted_new_count(), 4); // a,b,c,d
assert_eq!(lru.removed_count(), 1); // a
// Set currently holds b,c,d
assert_eq!(lru.max_size(), 3);
}
#[test]
fn lru_insert_all_complex_eviction() {
let mut lru = LeastRecentlyUsedPriorityQueue::new(4);
lru.insert_all(["a", "b", "c"]);
// Touch a to make it most recent, order (a, c, b, ... LRU is b)
lru.insert_all(["a"]);
lru.insert_all(["a", "d", "e", "f"]); // need to keep a,d,e,f; evict b & c
assert_eq!(lru.inserted_new_count(), 6); // a,b,c,d,e,f
assert_eq!(lru.removed_count(), 2); // b,c
assert_eq!(lru.max_size(), 4);
}
#[test]
fn lru_insert_all_duplicates_in_input() {
let mut lru = LeastRecentlyUsedPriorityQueue::new(5);
lru.insert_all(["a", "b", "b", "c", "a", "d"]); // duplicates should not inflate count
assert_eq!(lru.inserted_new_count(), 4); // a,b,c,d
assert_eq!(lru.removed_count(), 0);
assert_eq!(lru.max_size(), 4);
}
#[test]
fn lru_insert_all_recency_update_only() {
let mut lru = LeastRecentlyUsedPriorityQueue::new(3);
lru.insert_all(["x", "y", "z"]);
assert_eq!(lru.inserted_new_count(), 3);
lru.insert_all(["z", "y"]); // no new insertions
assert_eq!(lru.inserted_new_count(), 3);
assert_eq!(lru.removed_count(), 0);
assert_eq!(lru.max_size(), 3);
// Adding one new triggers eviction of least recently used (x)
lru.insert_all(["y", "w"]);
assert_eq!(lru.inserted_new_count(), 4); // w added
assert_eq!(lru.removed_count(), 1); // x removed
}
// ---------------- LFU tests -----------------
#[test]
fn lfu_basic_frequency_eviction() {
let mut lfu = LeastFrequentlyUsedPriorityQueue::new(3);
lfu.insert_all(["a"]);
lfu.insert_all(["b"]);
lfu.insert_all(["c"]);
// Bump b twice
lfu.insert_all(["b"]);
lfu.insert_all(["b"]);
// Insert d -> should evict a (oldest among lowest freq =1 keys a & c)
lfu.insert_all(["d"]);
assert!(!lfu.map.contains_key(&"a"));
assert!(lfu.map.contains_key(&"b"));
assert!(lfu.map.contains_key(&"c"));
assert!(lfu.map.contains_key(&"d"));
}
#[test]
fn lfu_update_existing_value() {
let mut lfu = LeastFrequentlyUsedPriorityQueue::new(1);
lfu.insert_all(["a"]);
lfu.insert_all(["a"]); // bump freq
assert!(lfu.map.contains_key(&"a"));
lfu.insert_all(["b"]); // eviction of a
assert!(!lfu.map.contains_key(&"a"));
assert!(lfu.map.contains_key(&"b"));
}
#[test]
fn lfu_zero_capacity() {
let mut lfu: LeastFrequentlyUsedPriorityQueue<&str> = LeastFrequentlyUsedPriorityQueue::new(0);
lfu.insert_all(["a"]); // ignored
assert_eq!(lfu.inserted_new_count(), 0);
assert_eq!(lfu.removed_count(), 0);
assert_eq!(lfu.max_size(), 0);
}
#[test]
fn lfu_eviction_prefers_lowest_freq_oldest() {
let mut lfu = LeastFrequentlyUsedPriorityQueue::new(3);
lfu.insert_all(["x"]);
lfu.insert_all(["y"]);
lfu.insert_all(["z"]);
// bump x twice to freq=3
lfu.insert_all(["x"]);
lfu.insert_all(["x"]);
// Insert w -> evict y (oldest among y,z with lowest freq=1)
lfu.insert_all(["w"]);
assert!(!lfu.map.contains_key(&"y"));
assert!(lfu.map.contains_key(&"x"));
assert!(lfu.map.contains_key(&"z"));
assert!(lfu.map.contains_key(&"w"));
}
#[test]
fn lfu_insert_all() {
let mut lfu = LeastFrequentlyUsedPriorityQueue::new(3);
lfu.insert_all(["a"]);
lfu.insert_all(["b"]);
lfu.insert_all(["b"]); // bump b
lfu.insert_all(["c"]);
// frequencies: a:1, b:2, c:1 (a oldest among freq1)
lfu.insert_all(["d", "e"]); // need space for 2 new: evict a then c
assert!(lfu.map.contains_key(&"d"));
assert!(lfu.map.contains_key(&"e"));
// b should remain (highest freq)
assert!(lfu.map.contains_key(&"b"));
assert!(!lfu.map.contains_key(&"a"));
assert!(!lfu.map.contains_key(&"c"));
assert_eq!(lfu.map.len(), 3);
}
#[test]
fn lfu_counters() {
let mut lfu = LeastFrequentlyUsedPriorityQueue::new(3);
lfu.insert_all(["a"]);
lfu.insert_all(["b"]);
lfu.insert_all(["c"]);
assert_eq!(lfu.inserted_new_count(), 3);
assert_eq!(lfu.max_size(), 3);
lfu.insert_all(["b"]); // bump b
lfu.insert_all(["d", "e"]); // inserts d,e removes a,c
assert_eq!(lfu.inserted_new_count(), 5);
assert_eq!(lfu.removed_count(), 2);
// Another eviction
lfu.insert_all(["f"]); // inserts f removes one of (d,e)
assert_eq!(lfu.inserted_new_count(), 6);
assert_eq!(lfu.map.len(), 3);
assert!(lfu.removed_count() >= 3);
assert_eq!(lfu.max_size(), 3);
}
#[test]
fn lfu_max_size_tracking() {
let mut lfu = LeastFrequentlyUsedPriorityQueue::new(4);
assert_eq!(lfu.max_size(), 0);
lfu.insert_all(["a"]);
lfu.insert_all(["b"]);
lfu.insert_all(["c"]);
assert_eq!(lfu.max_size(), 3);
lfu.insert_all(["c"]); // duplicate bump
assert_eq!(lfu.max_size(), 3);
lfu.insert_all(["d"]);
assert_eq!(lfu.max_size(), 4);
lfu.insert_all(["e", "f"]); // still capacity 4
assert_eq!(lfu.max_size(), 4);
}