TAMER: sym::tests: Generate with macro
This will be used for generating the common tests between HashSet and HashMap implementations. This is my first macro in Rust. There does not seem to be a way to concatenate identifiers (!), so I'm placing them within modules instead. That ended up working out just fine, since then I can use a type to provide the SUT.master
parent
e4e0089815
commit
9a98644213
|
@ -480,6 +480,7 @@ where
|
||||||
/// see [`HashSet::with_capacity`].
|
/// see [`HashSet::with_capacity`].
|
||||||
///
|
///
|
||||||
/// See also `intern` benchmarks.
|
/// See also `intern` benchmarks.
|
||||||
|
#[inline]
|
||||||
pub fn with_capacity(capacity: usize) -> Self {
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
map: HashSet::with_capacity_and_hasher(
|
map: HashSet::with_capacity_and_hasher(
|
||||||
|
@ -593,11 +594,28 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod hash_set_interner {
|
macro_rules! each_interner {
|
||||||
use super::*;
|
($($(#[$attr:meta])* fn $name:ident() $body:block)*) => {
|
||||||
|
macro_rules! interner {
|
||||||
|
($mod:ident: $type:ty) => {
|
||||||
|
mod $mod {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
/// Two different string values in memory should be interned to the
|
type Sut = $type;
|
||||||
/// same object if they represent the same data.
|
|
||||||
|
$(
|
||||||
|
$(#[$attr])*
|
||||||
|
fn $name() $body
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interner!(common_hash_set: HashSetInterner<SymbolRc, RandomState>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
each_interner! {
|
||||||
#[test]
|
#[test]
|
||||||
fn recognizes_equal_strings() {
|
fn recognizes_equal_strings() {
|
||||||
let a = "foo";
|
let a = "foo";
|
||||||
|
@ -605,7 +623,7 @@ mod test {
|
||||||
let c = "bar";
|
let c = "bar";
|
||||||
let d = c.to_string();
|
let d = c.to_string();
|
||||||
|
|
||||||
let mut sut = HashSetInterner::<SymbolRc>::new();
|
let mut sut = Sut::new();
|
||||||
|
|
||||||
let (ia, ib, ic, id) =
|
let (ia, ib, ic, id) =
|
||||||
(sut.intern(a), sut.intern(&b), sut.intern(c), sut.intern(&d));
|
(sut.intern(a), sut.intern(&b), sut.intern(c), sut.intern(&d));
|
||||||
|
@ -628,7 +646,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn length_increases_with_each_new_intern() {
|
fn length_increases_with_each_new_intern() {
|
||||||
let mut sut = HashSetInterner::<SymbolRc>::new();
|
let mut sut = Sut::new();
|
||||||
|
|
||||||
assert_eq!(0, sut.len(), "invalid empty len");
|
assert_eq!(0, sut.len(), "invalid empty len");
|
||||||
|
|
||||||
|
@ -645,7 +663,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_check_wither_string_is_interned() {
|
fn can_check_wither_string_is_interned() {
|
||||||
let mut sut = HashSetInterner::<SymbolRc>::new();
|
let mut sut = Sut::new();
|
||||||
|
|
||||||
assert!(!sut.contains("foo"), "recognize missing value");
|
assert!(!sut.contains("foo"), "recognize missing value");
|
||||||
sut.intern("foo");
|
sut.intern("foo");
|
||||||
|
@ -654,7 +672,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn intern_soft() {
|
fn intern_soft() {
|
||||||
let mut sut = HashSetInterner::<SymbolRc>::new();
|
let mut sut = Sut::new();
|
||||||
|
|
||||||
assert_eq!(None, sut.intern_soft("foo"));
|
assert_eq!(None, sut.intern_soft("foo"));
|
||||||
|
|
||||||
|
@ -665,7 +683,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn new_with_capacity() {
|
fn new_with_capacity() {
|
||||||
let n = 512;
|
let n = 512;
|
||||||
let sut = HashSetInterner::<SymbolRc>::with_capacity(n);
|
let sut = Sut::with_capacity(n);
|
||||||
|
|
||||||
// note that this is not publicly available
|
// note that this is not publicly available
|
||||||
assert!(sut.map.capacity() >= n);
|
assert!(sut.map.capacity() >= n);
|
||||||
|
|
Loading…
Reference in New Issue