use cpp_map::prelude::*;
mod common;
use common::*;
#[test]
fn test_insertion_positions() -> Result<(), Box<dyn std::error::Error>> {
for _ in 0..100 {
let mut list = SkipList::default();
let index_10 = list.insert(RoundedTen(10), "Ten")?;
let index_20 = list.insert_with_hint(RoundedTen(20), "Twenty", index_10)?;
assert_eq!(*list.get_v_at(index_20).unwrap(), "Twenty");
let index_0 = list.insert_with_hint(RoundedTen(0), "Zero", index_10)?;
assert_eq!(*list.get_v_at(index_0).unwrap(), "Zero");
let index_15 = list.insert_with_hint(RoundedTen(15), "Fifteen", index_20)?;
assert_eq!(*list.get_v_at(index_15).unwrap(), "Ten");
let mut current = list.first();
let mut values = Vec::new();
while let Some((_key, value)) = list.get_at(current.unwrap()) {
values.push(value.to_string());
current = list.next_pos(current);
if current.is_none() {
break;
}
}
assert_eq!(values, vec!["Zero", "Ten", "Twenty"]);
}
Ok(())
}
#[test]
fn test_hint_edge_cases() -> Result<(), Box<dyn std::error::Error>> {
for _ in 0..100 {
let mut list = SkipList::default();
let index_100 = list.insert(RoundedTen(100), "Hundred")?;
let index_50 = list.insert_with_hint(RoundedTen(50), "Fifty", index_100)?;
assert_eq!(*list.get_v_at(index_50).unwrap(), "Fifty");
let index_25 = list.insert_with_hint(RoundedTen(25), "TwentyFive", index_50)?;
assert_eq!(*list.get_v_at(index_25).unwrap(), "TwentyFive");
let index_75 = list.insert_with_hint(RoundedTen(75), "SeventyFive", index_100)?;
assert_eq!(*list.get_v_at(index_75).unwrap(), "SeventyFive");
let mut values = Vec::new();
let mut current = list.first();
while let Some((_, value)) = list.get_at(current.unwrap()) {
values.push(value.to_string());
current = list.next_pos(current);
if current.is_none() {
break;
}
}
assert_eq!(
values,
vec!["TwentyFive", "Fifty", "SeventyFive", "Hundred"]
);
}
Ok(())
}
#[test]
fn test_duplicate_keys() -> Result<(), Box<dyn std::error::Error>> {
for _ in 0..100 {
let mut list = SkipList::default();
let index_5_1 = list.insert(RoundedTen(5), "Five1")?;
let _index_5_2 = list.insert_with_hint(RoundedTen(5), "Five2", index_5_1)?;
assert_eq!(index_5_1, _index_5_2);
}
Ok(())
}
#[test]
#[allow(clippy::unnecessary_literal_unwrap)]
fn test_insert_remove_sequence() -> Result<(), Box<dyn std::error::Error>> {
for _ in 0..100 {
let mut list = SkipList::default();
let index_10 = list.insert(RoundedTen(10), "Ten")?;
let mut _index_20 = Some(list.insert_with_hint(RoundedTen(20), "Twenty", index_10)?);
let index_30 = Some(list.insert_with_hint(RoundedTen(30), "Thirty", _index_20.unwrap())?);
list.remove_by_index(&mut _index_20);
_index_20 = Some(list.insert_with_hint(RoundedTen(20), "Twenty", index_30.unwrap())?);
let mut values = Vec::new();
let mut current = list.first();
while let Some((_, value)) = list.get_at(current.unwrap()) {
values.push(value.to_string());
current = list.next_pos(current);
if current.is_none() {
break;
}
}
assert_eq!(values, vec!["Ten", "Twenty", "Thirty"]);
}
Ok(())
}
#[test]
fn test_extreme_values() -> Result<(), Box<dyn std::error::Error>> {
for _ in 0..100 {
let mut list = SkipList::default();
let index_min = list.insert(RoundedTen(i32::MIN), "Min")?;
let index_max = list.insert_with_hint(RoundedTen(i32::MAX), "Max", index_min)?;
let _index_mid = list.insert_with_hint(RoundedTen(0), "Mid", index_max)?;
let mut values = Vec::new();
let mut current = list.first();
while let Some((_, value)) = list.get_at(current.unwrap()) {
values.push(value.to_string());
current = list.next_pos(current);
if current.is_none() {
break;
}
}
assert_eq!(values, vec!["Min", "Mid", "Max"]);
}
Ok(())
}