Skip to content

Commit 97ad610

Browse files
committed
test: make test func static and move tests in the same func
1 parent b98504e commit 97ad610

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

others/lfu_cache.cpp

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -244,63 +244,54 @@ class LFUCache {
244244
} // namespace others
245245

246246
/**
247-
* @brief A simple test case
247+
* @brief self test implementation
248248
* @return void
249249
*/
250-
void test1() {
250+
static void test() {
251251
others::Cache::LFUCache<int, int> cache(5);
252+
253+
// test the initial state of the cache
252254
assert(cache.size() == 0);
253255
assert(cache.capacity() == 5);
254256
assert(cache.empty());
257+
258+
// test insertion in the cache
255259
cache.put(1, 10);
256260
cache.put(-2, 20);
257261

262+
// test the state of cache after inserting some items
258263
assert(cache.size() == 2);
259264
assert(cache.capacity() == 5);
260265
assert(!cache.empty());
261266

262-
cache.put(3, -30);
267+
// test getting items from the cache
268+
assert(cache.get(1) == 10);
269+
assert(cache.get(-2) == 20);
270+
271+
cache.put(-3, -30);
263272
cache.put(4, 40);
264-
cache.put(5, 50);
273+
cache.put(5, -50);
265274
cache.put(6, 60);
266275

276+
// test the state after inserting more items than the capacity
267277
assert(cache.size() == 5);
268278
assert(cache.capacity() == 5);
269279
assert(!cache.empty());
270280

271-
std::cout << "test1 - passed\n";
272-
}
281+
// test retrieval of all items in the cache
282+
assert(cache.get(1) == 10);
283+
assert(cache.get(-2) == 20);
273284

274-
/**
275-
* @brief A simple test case
276-
* @return void
277-
*/
278-
void test2() {
279-
others::Cache::LFUCache<int, int> cache(5);
280-
cache.put(-1, -10);
281-
cache.put(2, 20);
282-
cache.put(3, 30);
285+
// fetching -3 throws runtime_error
286+
// as -3 was evicted being the least frequently used
287+
// when 6 was added
288+
// assert(cache.get(-3) == -30);
283289

284-
assert(cache.get(-1) == -10);
285-
cache.get(2);
286-
cache.get(3);
290+
assert(cache.get(4) == 40);
291+
assert(cache.get(5) == -50);
292+
assert(cache.get(6) == 60);
287293

288-
cache.put(4, 40);
289-
cache.put(5, 50);
290-
cache.put(6, 60);
291-
292-
assert(cache.get(5) == 50);
293-
294-
std::cout << "test2 - passed\n";
295-
}
296-
297-
/**
298-
* @brief self test implementation
299-
* @return void
300-
*/
301-
void test() {
302-
test1();
303-
test2();
294+
std::cout << "test - passed\n";
304295
}
305296

306297
/**

0 commit comments

Comments
 (0)