Skip to content

Commit 53a7a52

Browse files
424everjswrenn
authored andcommitted
Add tests for with_hasher methods
1 parent d3e55d7 commit 53a7a52

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

tests/test_std.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,31 @@ use rand::{
2020
Rng, SeedableRng,
2121
};
2222
use rand::{seq::SliceRandom, thread_rng};
23+
use std::collections::HashMap;
24+
use std::hash::BuildHasher;
25+
use std::hash::RandomState;
26+
use std::iter::empty;
2327
use std::{cmp::min, fmt::Debug, marker::PhantomData};
2428

29+
// A Hasher which forwards it's calls to RandomState to make sure different hashers
30+
// are accepted in the various *_with_hasher methods.
31+
#[derive(Default)]
32+
struct TestHasher(RandomState);
33+
34+
impl TestHasher {
35+
fn new() -> Self {
36+
TestHasher(RandomState::new())
37+
}
38+
}
39+
40+
impl BuildHasher for TestHasher {
41+
type Hasher = <RandomState as BuildHasher>::Hasher;
42+
43+
fn build_hasher(&self) -> Self::Hasher {
44+
self.0.build_hasher()
45+
}
46+
}
47+
2548
#[test]
2649
fn product3() {
2750
let prod = iproduct!(0..3, 0..2, 0..2);
@@ -76,6 +99,10 @@ fn duplicates_by() {
7699
ys_rev.iter(),
77100
xs.iter().duplicates_by(|x| x[..2].to_string()).rev(),
78101
);
102+
103+
let _ = empty::<u8>()
104+
.duplicates_by_with_hasher(|x| *x, TestHasher::new())
105+
.next();
79106
}
80107

81108
#[test]
@@ -103,6 +130,10 @@ fn duplicates() {
103130
);
104131
let ys_rev = vec![2, 1];
105132
assert_eq!(ys_rev, xs.iter().duplicates().rev().cloned().collect_vec());
133+
134+
let _ = empty::<u8>()
135+
.duplicates_with_hasher(TestHasher::new())
136+
.next();
106137
}
107138

108139
#[test]
@@ -119,6 +150,8 @@ fn unique_by() {
119150
ys_rev.iter(),
120151
xs.iter().unique_by(|x| x[..2].to_string()).rev(),
121152
);
153+
154+
let _ = empty::<u8>().unique_by_with_hasher(|x| *x, TestHasher::new());
122155
}
123156

124157
#[test]
@@ -136,6 +169,8 @@ fn unique() {
136169
it::assert_equal(ys.iter(), xs.iter().rev().unique().rev());
137170
let ys_rev = [1, 0];
138171
it::assert_equal(ys_rev.iter(), xs.iter().unique().rev());
172+
173+
let _ = empty::<u8>().unique_with_hasher(TestHasher::new());
139174
}
140175

141176
#[test]
@@ -301,6 +336,8 @@ fn all_unique() {
301336
assert!("ABCDEFGH".chars().all_unique());
302337
assert!(!"ABCDEFGA".chars().all_unique());
303338
assert!(::std::iter::empty::<usize>().all_unique());
339+
340+
let _ = empty::<u8>().all_unique_with_hasher(TestHasher::new());
304341
}
305342

306343
#[test]
@@ -1597,3 +1634,40 @@ fn multiunzip() {
15971634
)
15981635
);
15991636
}
1637+
1638+
#[test]
1639+
fn into_group_map_with_hasher() {
1640+
let _: HashMap<_, _, TestHasher> =
1641+
empty::<(u8, u8)>().into_group_map_with_hasher(TestHasher::new());
1642+
}
1643+
1644+
#[test]
1645+
fn into_group_map_by_with_hasher() {
1646+
let _: HashMap<_, _, TestHasher> =
1647+
empty::<(u8, u8)>().into_group_map_by_with_hasher(|x| *x, TestHasher::new());
1648+
}
1649+
1650+
#[test]
1651+
fn into_grouping_map_with_hasher() {
1652+
let _: HashMap<_, Vec<_>, TestHasher> = empty::<(u8, u8)>()
1653+
.into_grouping_map_with_hasher(TestHasher::new())
1654+
.collect();
1655+
}
1656+
1657+
#[test]
1658+
fn into_grouping_map_by_with_hasher() {
1659+
let _: HashMap<_, Vec<_>, TestHasher> = empty::<(u8, u8)>()
1660+
.into_grouping_map_by_with_hasher(|x| *x, TestHasher::new())
1661+
.collect();
1662+
}
1663+
1664+
#[test]
1665+
fn counts_with_hasher() {
1666+
let _: HashMap<_, _, TestHasher> = empty::<u8>().counts_with_hasher(TestHasher::new());
1667+
}
1668+
1669+
#[test]
1670+
fn counts_by_with_hasher() {
1671+
let _: HashMap<_, _, TestHasher> =
1672+
empty::<u8>().counts_by_with_hasher(|x| x, TestHasher::new());
1673+
}

0 commit comments

Comments
 (0)