Skip to content

Commit

Permalink
rename from _v1/v2/v3/v4 to its actual impl method name
Browse files Browse the repository at this point in the history
  • Loading branch information
drink7036290 committed Dec 9, 2024
1 parent b8dc871 commit 124f320
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod modules;

use criterion::{criterion_group, criterion_main};

use q146_lru_cache::impl_v4::LRUCache;
use q146_lru_cache::impl_intrusive_two_hashmaps::LRUCache;

bench_lru_cache!(q146_with_intrusive_two_hashmaps, LRUCache);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod modules;

use criterion::{criterion_group, criterion_main};

use q146_lru_cache::impl_v1::LRUCache;
use q146_lru_cache::impl_priority_queue::LRUCache;

bench_lru_cache!(q146_with_priority_queue, LRUCache);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod modules;

use criterion::{criterion_group, criterion_main};

use q146_lru_cache::impl_v3::LRUCache;
use q146_lru_cache::impl_two_hashmaps::LRUCache;

bench_lru_cache!(q146_with_two_hashmaps, LRUCache);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod modules;

use criterion::{criterion_group, criterion_main};

use q146_lru_cache::impl_v2::LRUCache;
use q146_lru_cache::impl_vec_hashmap::LRUCache;

bench_lru_cache!(q146_with_vec_hashmap, LRUCache);

Expand Down
8 changes: 4 additions & 4 deletions implementations/q146_lru_cache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod heap_node;
pub mod impl_v1;
pub mod impl_v2;
pub mod impl_v3;
pub mod impl_v4;
pub mod impl_priority_queue;
pub mod impl_two_hashmaps;
pub mod impl_intrusive_two_hashmaps;
pub mod impl_vec_hashmap;
50 changes: 31 additions & 19 deletions implementations/q146_lru_cache/tests/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,46 @@ fn operation_sequence_strategy() -> impl Strategy<Value = Vec<CacheOperation>> {
}

fn test_lru_cache_with_operations(capacity: i32, operations: Vec<CacheOperation>) {
use q146_lru_cache::impl_v1::LRUCache as LRUCache_v1;
use q146_lru_cache::impl_v2::LRUCache as LRUCache_v2;
use q146_lru_cache::impl_v3::LRUCache as LRUCache_v3;
use q146_lru_cache::impl_v4::LRUCache as LRUCache_v4;
use q146_lru_cache::impl_intrusive_two_hashmaps::LRUCache as LRUCache_intrusive_two_hashmaps;
use q146_lru_cache::impl_priority_queue::LRUCache as LRUCache_priority_queue;
use q146_lru_cache::impl_two_hashmaps::LRUCache as LRUCache_two_hashmaps;
use q146_lru_cache::impl_vec_hashmap::LRUCache as LRUCache_vec_hashmap;

let mut cache_v1 = LRUCache_v1::new(capacity);
let mut cache_v2 = LRUCache_v2::new(capacity);
let mut cache_v3 = LRUCache_v3::new(capacity);
let mut cache_v4 = LRUCache_v4::new(capacity);
let mut cache_priority_queue = LRUCache_priority_queue::new(capacity);
let mut cache_vec_hashmap = LRUCache_vec_hashmap::new(capacity);
let mut cache_two_hashmaps = LRUCache_two_hashmaps::new(capacity);
let mut cache_intrusive_two_hashmaps = LRUCache_intrusive_two_hashmaps::new(capacity);

for operation in operations {
match operation {
CacheOperation::Put { key, value } => {
cache_v1.put(key, value);
cache_v2.put(key, value);
cache_v3.put(key, value);
cache_v4.put(key, value);
cache_priority_queue.put(key, value);
cache_vec_hashmap.put(key, value);
cache_two_hashmaps.put(key, value);
cache_intrusive_two_hashmaps.put(key, value);
}
CacheOperation::Get { key } => {
let result_v1 = cache_v1.get(key);
let result_v2 = cache_v2.get(key);
let result_v3 = cache_v3.get(key);
let result_v4 = cache_v4.get(key);
let result_priority_queue = cache_priority_queue.get(key);
let result_vec_hashmap = cache_vec_hashmap.get(key);
let result_two_hashmaps = cache_two_hashmaps.get(key);
let result_intrusive_two_hashmaps = cache_intrusive_two_hashmaps.get(key);

// Compare results
assert_eq!(result_v1, result_v2, "v1 and v2 differ on get({})", key);
assert_eq!(result_v1, result_v3, "v1 and v3 differ on get({})", key);
assert_eq!(result_v1, result_v4, "v1 and v4 differ on get({})", key);
assert_eq!(
result_priority_queue, result_vec_hashmap,
"v1 and v2 differ on get({})",
key
);
assert_eq!(
result_priority_queue, result_two_hashmaps,
"v1 and v3 differ on get({})",
key
);
assert_eq!(
result_priority_queue, result_intrusive_two_hashmaps,
"v1 and v4 differ on get({})",
key
);
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions implementations/q146_lru_cache/tests/test_all_impl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rstest::rstest;

use q146_lru_cache::impl_v1::LRUCache as LRUCache_v1;
use q146_lru_cache::impl_v2::LRUCache as LRUCache_v2;
use q146_lru_cache::impl_v3::LRUCache as LRUCache_v3;
use q146_lru_cache::impl_v4::LRUCache as LRUCache_v4;
use q146_lru_cache::impl_intrusive_two_hashmaps::LRUCache as LRUCache_intrusive_two_hashmaps;
use q146_lru_cache::impl_priority_queue::LRUCache as LRUCache_priority_queue;
use q146_lru_cache::impl_two_hashmaps::LRUCache as LRUCache_two_hashmaps;
use q146_lru_cache::impl_vec_hashmap::LRUCache as LRUCache_vec_hashmap;

#[rstest]
#[case(vec!["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"],
Expand All @@ -22,10 +22,10 @@ fn test_all_impl(
assert_eq!(cmds[0], "LRUCache");
assert_eq!(expected_list[0], None);

let mut cache_v1 = LRUCache_v1::new(args_list[0][0]);
let mut cache_v2 = LRUCache_v2::new(args_list[0][0]);
let mut cache_v3 = LRUCache_v3::new(args_list[0][0]);
let mut cache_v4 = LRUCache_v4::new(args_list[0][0]);
let mut cache_priority_queue = LRUCache_priority_queue::new(args_list[0][0]);
let mut cache_vec_hashmap = LRUCache_vec_hashmap::new(args_list[0][0]);
let mut cache_two_hashmaps = LRUCache_two_hashmaps::new(args_list[0][0]);
let mut cache_intrusive_two_hashmaps = LRUCache_intrusive_two_hashmaps::new(args_list[0][0]);

for (i, cmd) in cmds.iter().enumerate().skip(1) {
let args = &args_list[i];
Expand All @@ -37,10 +37,10 @@ fn test_all_impl(
assert_eq!(args.len(), 1);
let key = args[0];

assert_eq!(cache_v1.get(key), v);
assert_eq!(cache_v2.get(key), v);
assert_eq!(cache_v3.get(key), v);
assert_eq!(cache_v4.get(key), v);
assert_eq!(cache_priority_queue.get(key), v);
assert_eq!(cache_vec_hashmap.get(key), v);
assert_eq!(cache_two_hashmaps.get(key), v);
assert_eq!(cache_intrusive_two_hashmaps.get(key), v);
}
None => {
panic!("expected value should not be None for cmd \"get\"");
Expand All @@ -55,10 +55,10 @@ fn test_all_impl(
let key = args[0];
let value = args[1];

cache_v1.put(key, value);
cache_v2.put(key, value);
cache_v3.put(key, value);
cache_v4.put(key, value);
cache_priority_queue.put(key, value);
cache_vec_hashmap.put(key, value);
cache_two_hashmaps.put(key, value);
cache_intrusive_two_hashmaps.put(key, value);
}
},
_ => {
Expand Down

0 comments on commit 124f320

Please sign in to comment.