Skip to content

Commit 8e147b2

Browse files
committed
stable_set: add tests for map, clean up tests
1 parent e859e65 commit 8e147b2

File tree

5 files changed

+351
-54
lines changed

5 files changed

+351
-54
lines changed

stable_set/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ pub use stable_set::StableSet;
2323
pub use stable_map::StableMap;
2424

2525
pub mod stable_set;
26-
pub mod stable_map;
26+
pub mod stable_map;
27+
28+
#[cfg(test)]
29+
mod test_set;
30+
#[cfg(test)]
31+
mod test_map;

stable_set/src/stable_map.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ use core::hash::Hash;
77
use index_table::{IndexTable, SmallIndex};
88
use std::{borrow::Borrow, hash::BuildHasher, ops::RangeBounds};
99

10-
#[path = "test_map.rs"]
11-
mod test_map;
12-
1310
/// A hash map that maintains the order of its elements.
1411
#[derive(Clone)]
1512
pub struct StableMap<K, V, S, W=u32> {
@@ -260,20 +257,14 @@ impl<K: Hash + Eq, V, S: BuildHasher, W: SmallIndex> StableMap<K, V, S, W> {
260257
/// Removes the entry with the specified index from the set and returns its key and value, if it exists.
261258
///
262259
/// The last item is put in its place.
263-
pub fn swap_remove_index_full(&mut self, index: usize) -> Option<(K, V)> {
260+
pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
264261
let hash = self.build_hasher.hash_one(&self.items.get(index)?.0);
265262
self.index_table
266263
.find_entry(hash, |i| i == index)
267264
.unwrap()
268265
.remove();
269266
Some(self.swap_remove_finish(index))
270267
}
271-
/// Removes the entry with the specified index from the set and returns its value, if it exists.
272-
///
273-
/// The last item is put in its place.
274-
pub fn swap_remove_index(&mut self, index: usize) -> Option<V> {
275-
self.swap_remove_index_full(index).map(|x| x.1)
276-
}
277268
/// Removes all items for which `f` evaluates to `false`.
278269
///
279270
/// `f` is guaranteed to be called exactly once for each item and in order, and may mutate the values.
@@ -554,7 +545,7 @@ impl<K: Hash, V, S: BuildHasher, W: SmallIndex> OccupiedEntry<'_, K, V, S, W> {
554545
}
555546

556547
impl<'a, K, V, S, W: SmallIndex> Entry<'a, K, V, S, W> {
557-
/// Returns a refernece to the key of the entry.
548+
/// Returns a reference to the key of the entry.
558549
pub fn key(&self) -> &K {
559550
match self {
560551
Entry::Vacant(entry) => entry.key(),
@@ -613,7 +604,7 @@ impl<'a, K, V, S, W: SmallIndex> Entry<'a, K, V, S, W> {
613604

614605
impl<K: Hash, V, S: BuildHasher, W: SmallIndex> StableMap<K, V, S, W> {
615606
#[cfg(test)]
616-
fn check(&self) {
607+
pub(crate) fn check(&self) {
617608
assert!(self.index_table.len() == self.items.len());
618609
for (index, (key, _)) in self.items.iter().enumerate() {
619610
let hash = self.build_hasher.hash_one(key);

stable_set/src/stable_set.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ use core::hash::Hash;
77
use index_table::{IndexTable, SmallIndex};
88
use std::{borrow::Borrow, hash::BuildHasher, ops::RangeBounds};
99

10-
#[path = "test_set.rs"]
11-
mod test_set;
12-
1310
/// A hash set that maintains the order of its elements.
1411
///
1512
/// In `StableSet<T, S, W>`,
@@ -381,7 +378,7 @@ impl<T: Hash + Eq, S: BuildHasher + Default, W: SmallIndex> FromIterator<T> for
381378

382379
impl<T: Hash, S: BuildHasher, W: SmallIndex> StableSet<T, S, W> {
383380
#[cfg(test)]
384-
fn check(&self) {
381+
pub(crate) fn check(&self) {
385382
assert!(self.index_table.len() == self.items.len());
386383
for (index, item) in self.items.iter().enumerate() {
387384
let hash = self.build_hasher.hash_one(item);

0 commit comments

Comments
 (0)