Skip to content

Commit f9df820

Browse files
author
Timothée Haudebourg
committed
Added tests for the Equivalent trait.
1 parent 9fd5856 commit f9df820

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/equivalent_trait.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use hashbrown::Equivalent;
2+
use hashbrown::HashMap;
3+
4+
use std::hash::Hash;
5+
6+
#[derive(Debug, Hash)]
7+
pub struct Pair<A, B>(pub A, pub B);
8+
9+
impl<A, B, C, D> PartialEq<(A, B)> for Pair<C, D>
10+
where
11+
C: PartialEq<A>,
12+
D: PartialEq<B>,
13+
{
14+
fn eq(&self, rhs: &(A, B)) -> bool {
15+
self.0 == rhs.0 && self.1 == rhs.1
16+
}
17+
}
18+
19+
impl<A, B, X> Equivalent<X> for Pair<A, B>
20+
where
21+
Pair<A, B>: PartialEq<X>,
22+
A: Hash + Eq,
23+
B: Hash + Eq,
24+
{
25+
fn equivalent(&self, other: &X) -> bool {
26+
*self == *other
27+
}
28+
}
29+
30+
#[test]
31+
fn test_lookup() {
32+
let s = String::from;
33+
let mut map = HashMap::new();
34+
map.insert((s("a"), s("b")), 1);
35+
map.insert((s("a"), s("x")), 2);
36+
37+
assert!(map.contains_key(&Pair("a", "b")));
38+
assert!(!map.contains_key(&Pair("b", "a")));
39+
}
40+
41+
#[test]
42+
fn test_string_str() {
43+
let s = String::from;
44+
let mut map = HashMap::new();
45+
map.insert(s("a"), 1);
46+
map.insert(s("b"), 2);
47+
map.insert(s("x"), 3);
48+
map.insert(s("y"), 4);
49+
50+
assert!(map.contains_key("a"));
51+
assert!(!map.contains_key("z"));
52+
assert_eq!(map.remove("b"), Some(2));
53+
}

0 commit comments

Comments
 (0)