Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Sep 3, 2024
1 parent 1f32f62 commit 3a43dcd
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions crossbeam-skiplist/tests/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,3 +910,49 @@ fn drops() {
assert_eq!(KEYS.load(Ordering::SeqCst), 8);
assert_eq!(VALUES.load(Ordering::SeqCst), 7);
}

#[test]
fn comparable_get() {
use equivalent::{Comparable, Equivalent};

#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct Foo {
a: u64,
b: u32,
}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct FooRef<'a> {
data: &'a [u8]
}

impl Equivalent<Foo> for FooRef<'_> {
fn equivalent(&self, key: &Foo) -> bool {
let a = u64::from_be_bytes(self.data[..8].try_into().unwrap());
let b = u32::from_be_bytes(self.data[8..].try_into().unwrap());
a == key.a && b == key.b
}
}

impl Comparable<Foo> for FooRef<'_> {
fn compare(&self, key: &Foo) -> std::cmp::Ordering {
let a = u64::from_be_bytes(self.data[..8].try_into().unwrap());
let b = u32::from_be_bytes(self.data[8..].try_into().unwrap());
Foo { a, b }.cmp(key)
}
}

let s = SkipList::new(epoch::default_collector().clone());
let foo = Foo { a: 1, b: 2 };

let g = &epoch::pin();
s.insert(foo, 12, g);

let buf = 1u64.to_be_bytes().iter().chain(2u32.to_be_bytes().iter()).copied().collect::<Vec<_>>();
let foo_ref = FooRef { data: &buf };

let ent = s.get(&foo_ref, g).unwrap();
assert_eq!(ent.key().a, 1);
assert_eq!(ent.key().b, 2);
assert_eq!(*ent.value(), 12);
}

0 comments on commit 3a43dcd

Please sign in to comment.