Write a simple unit test for TreeMap and fix bug in remove
This commit is contained in:
parent
cdbcbdfe6d
commit
2adf11e204
1 changed files with 37 additions and 5 deletions
|
@ -5,7 +5,7 @@ use crate::{Bias, Dimension, Item, KeyedItem, SeekTarget, SumTree, Summary};
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TreeMap<K, V>(SumTree<MapEntry<K, V>>)
|
pub struct TreeMap<K, V>(SumTree<MapEntry<K, V>>)
|
||||||
where
|
where
|
||||||
K: Clone + Debug + Default,
|
K: Clone + Debug + Default + Ord,
|
||||||
V: Clone + Debug;
|
V: Clone + Debug;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -41,7 +41,7 @@ impl<K: Clone + Debug + Default + Ord, V: Clone + Debug> TreeMap<K, V> {
|
||||||
let mut cursor = self.0.cursor::<MapKeyRef<'_, K>>();
|
let mut cursor = self.0.cursor::<MapKeyRef<'_, K>>();
|
||||||
let key = MapKeyRef(Some(key));
|
let key = MapKeyRef(Some(key));
|
||||||
let mut new_tree = cursor.slice(&key, Bias::Left, &());
|
let mut new_tree = cursor.slice(&key, Bias::Left, &());
|
||||||
if key.cmp(cursor.start(), &()) == Ordering::Equal {
|
if key.cmp(&cursor.end(&()), &()) == Ordering::Equal {
|
||||||
removed = Some(cursor.item().unwrap().value.clone());
|
removed = Some(cursor.item().unwrap().value.clone());
|
||||||
cursor.next(&());
|
cursor.next(&());
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ impl<K: Clone + Debug + Default + Ord, V: Clone + Debug> TreeMap<K, V> {
|
||||||
|
|
||||||
impl<K, V> Default for TreeMap<K, V>
|
impl<K, V> Default for TreeMap<K, V>
|
||||||
where
|
where
|
||||||
K: Clone + Debug + Default,
|
K: Clone + Debug + Default + Ord,
|
||||||
V: Clone + Debug,
|
V: Clone + Debug,
|
||||||
{
|
{
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
@ -68,13 +68,13 @@ where
|
||||||
|
|
||||||
impl<K, V> Item for MapEntry<K, V>
|
impl<K, V> Item for MapEntry<K, V>
|
||||||
where
|
where
|
||||||
K: Clone + Debug + Default + Clone,
|
K: Clone + Debug + Default + Ord,
|
||||||
V: Clone,
|
V: Clone,
|
||||||
{
|
{
|
||||||
type Summary = MapKey<K>;
|
type Summary = MapKey<K>;
|
||||||
|
|
||||||
fn summary(&self) -> Self::Summary {
|
fn summary(&self) -> Self::Summary {
|
||||||
todo!()
|
self.key()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,3 +118,35 @@ where
|
||||||
self.0.cmp(&cursor_location.0)
|
self.0.cmp(&cursor_location.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_basic() {
|
||||||
|
let mut map = TreeMap::default();
|
||||||
|
assert_eq!(map.iter().collect::<Vec<_>>(), vec![]);
|
||||||
|
|
||||||
|
map.insert(3, "c");
|
||||||
|
assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&3, &"c")]);
|
||||||
|
|
||||||
|
map.insert(1, "a");
|
||||||
|
assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&1, &"a"), (&3, &"c")]);
|
||||||
|
|
||||||
|
map.insert(2, "b");
|
||||||
|
assert_eq!(
|
||||||
|
map.iter().collect::<Vec<_>>(),
|
||||||
|
vec![(&1, &"a"), (&2, &"b"), (&3, &"c")]
|
||||||
|
);
|
||||||
|
|
||||||
|
map.remove(&2);
|
||||||
|
assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&1, &"a"), (&3, &"c")]);
|
||||||
|
|
||||||
|
map.remove(&3);
|
||||||
|
assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&1, &"a")]);
|
||||||
|
|
||||||
|
map.remove(&1);
|
||||||
|
assert_eq!(map.iter().collect::<Vec<_>>(), vec![]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue