feat(KvStore): new, get, set, remove functions implementation
- Added a new test for empty key
This commit is contained in:
17
src/lib.rs
17
src/lib.rs
@@ -1,20 +1,27 @@
|
|||||||
pub struct KvStore {}
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct KvStore {
|
||||||
|
hash_map: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
impl KvStore {
|
impl KvStore {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
unimplemented!()
|
Self {
|
||||||
|
hash_map: HashMap::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(&mut self, key: String, value: String) {
|
pub fn set(&mut self, key: String, value: String) {
|
||||||
unimplemented!()
|
self.hash_map.insert(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&mut self, key: String) -> Option<String> {
|
pub fn get(&mut self, key: String) -> Option<String> {
|
||||||
unimplemented!()
|
self.hash_map.get(&key).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, key: String) {
|
pub fn remove(&mut self, key: String) {
|
||||||
unimplemented!()
|
self.hash_map.remove(&key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -153,3 +153,11 @@ fn remove_key() {
|
|||||||
store.remove("key1".to_owned());
|
store.remove("key1".to_owned());
|
||||||
assert_eq!(store.get("key1".to_owned()), None);
|
assert_eq!(store.get("key1".to_owned()), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_empty_key() {
|
||||||
|
let mut store = KvStore::new();
|
||||||
|
|
||||||
|
store.set("".to_owned(), "value1".to_owned());
|
||||||
|
assert_eq!(store.get("".to_owned()), Some("value1".to_owned()));
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user