#![cfg(feature = "std")] use mockall::predicate::eq; use nearly::{ NearlyEqEps, NearlyEqTol, NearlyEqUlps, NearlyOrdEps, NearlyOrdTol, NearlyOrdUlps, Tolerance, }; use paste::paste; use std::collections::{BTreeMap, HashMap}; mod common; use common::{MockLhs, Rhs}; macro_rules! checkpoint { ($container: expr) => { for i in $container.iter_mut() { i.1.checkpoint(); } }; } macro_rules! lhs_value { (hashmap) => { HashMap::from([ (0, MockLhs::new()), (1, MockLhs::new()), (2, MockLhs::new()), ]) }; (btree_map) => { BTreeMap::from([ (0, MockLhs::new()), (1, MockLhs::new()), (2, MockLhs::new()), ]) }; } macro_rules! lhs_value_short { (hashmap) => { HashMap::from([(0, MockLhs::new()), (1, MockLhs::new())]) }; (btree_map) => { BTreeMap::from([(0, MockLhs::new()), (1, MockLhs::new())]) }; } macro_rules! rhs_value { (hashmap) => { HashMap::from([(0, Rhs(3)), (1, Rhs(7)), (2, Rhs(11))]) }; (btree_map) => { BTreeMap::from([(0, Rhs(3)), (1, Rhs(7)), (2, Rhs(11))]) }; } macro_rules! rhs_value_short { (hashmap) => { HashMap::from([(0, Rhs(3)), (1, Rhs(7))]) }; (btree_map) => { BTreeMap::from([(0, Rhs(3)), (1, Rhs(7))]) }; } macro_rules! get_type { ($inner: ty, hashmap) => { HashMap }; ($inner: ty, btree_map) => { BTreeMap } } macro_rules! lhs_type { ($coll: tt) => { get_type!(MockLhs, $coll) }; } macro_rules! rhs_type { ($coll: tt) => { get_type!(Rhs, $coll) }; } macro_rules! get_element { ($container: expr, $idx: expr) => { *$container.get_mut(&$idx).expect("Invalid key") }; } macro_rules! impl_test { ($lhs: tt, $rhs: tt) => { impl_test_fn!($lhs, $rhs, eq); impl_test_fn!($lhs, $rhs, lt); impl_test_fn!($lhs, $rhs, le); impl_test_fn!($lhs, $rhs, gt); impl_test_fn!($lhs, $rhs, ge); }; } macro_rules! impl_test_fn { ($lhs: tt, $rhs: tt, $fn: ident) => { paste! { #[test] fn []() { let mut a: lhs_type!($lhs) = lhs_value!($lhs); let b: rhs_type!($rhs) = rhs_value!($rhs); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(0.1)) .times(1) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(0.1)) .times(1) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(0.1)) .times(1) .return_const(true); assert!(a.[](&b, &0.1)); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(0.1)) .times(1) .return_const(false); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(0.1)) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(0.1)) .return_const(true); assert!(!a.[](&b, &0.1)); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(0.1)) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(0.1)) .times(1) .return_const(false); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(0.1)) .return_const(true); assert!(!a.[](&b, &0.1)); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(0.1)) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(0.1)) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(0.1)) .times(1) .return_const(false); assert!(!a.[](&b, &0.1)); } #[test] fn []() { #[allow(unused_mut)] let mut a: lhs_type!($lhs) = lhs_value!($lhs); let b: rhs_type!($rhs) = rhs_value!($rhs); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(5)) .times(1) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(5)) .times(1) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(5)) .times(1) .return_const(true); assert!(a.[](&b, &5)); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(5)) .times(1) .return_const(false); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(5)) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(5)) .return_const(true); assert!(!a.[](&b, &5)); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(5)) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(5)) .times(1) .return_const(false); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(5)) .return_const(true); assert!(!a.[](&b, &5)); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(5)) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(5)) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(5)) .times(1) .return_const(false); assert!(!a.[](&b, &5)); } #[test] fn []() { let mut a: lhs_type!($lhs) = lhs_value!($lhs); let b: rhs_type!($rhs) = rhs_value!($rhs); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(Tolerance::::new(0.1, 5))) .times(1) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(Tolerance::::new(0.1, 5))) .times(1) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(Tolerance::::new(0.1, 5))) .times(1) .return_const(true); assert!(a.[](&b, &Tolerance::::new(0.1, 5))); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(Tolerance::::new(0.1, 5))) .times(1) .return_const(false); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(Tolerance::::new(0.1, 5))) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(Tolerance::::new(0.1, 5))) .return_const(true); assert!(!a.[](&b, &Tolerance::::new(0.1, 5))); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(Tolerance::::new(0.1, 5))) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(Tolerance::::new(0.1, 5))) .times(1) .return_const(false); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(Tolerance::::new(0.1, 5))) .return_const(true); assert!(!a.[](&b, &Tolerance::::new(0.1, 5))); checkpoint!(a); get_element!(a, 0).[]() .with(eq(Rhs(3)), eq(Tolerance::::new(0.1, 5))) .return_const(true); get_element!(a, 1).[]() .with(eq(Rhs(7)), eq(Tolerance::::new(0.1, 5))) .return_const(true); get_element!(a, 2).[]() .with(eq(Rhs(11)), eq(Tolerance::::new(0.1, 5))) .times(1) .return_const(false); assert!(!a.[](&b, &Tolerance::::new(0.1, 5))); } #[test] fn []() { { let mut a: lhs_type!($lhs) = lhs_value!($lhs); let b: rhs_type!($rhs) = rhs_value_short!($rhs); assert!(a.len() > b.len()); get_element!(a, 0).[]().times(0); get_element!(a, 1).[]().times(0); get_element!(a, 2).[]().times(0); assert!(!a.[](&b, &0.1)); } { let mut a: lhs_type!($lhs) = lhs_value_short!($lhs); let b: rhs_type!($rhs) = rhs_value!($rhs); assert!(a.len() < b.len()); get_element!(a, 0).[]().times(0); get_element!(a, 1).[]().times(0); assert!(!a.[](&b, &0.1)); } } #[test] fn []() { { let mut a: lhs_type!($lhs) = lhs_value!($lhs); let b: rhs_type!($rhs) = rhs_value_short!($rhs); assert!(a.len() > b.len()); get_element!(a, 0).[]().times(0); get_element!(a, 1).[]().times(0); get_element!(a, 2).[]().times(0); assert!(!a.[](&b, &5)); } { let mut a: lhs_type!($lhs) = lhs_value_short!($lhs); let b: rhs_type!($rhs) = rhs_value!($rhs); assert!(a.len() < b.len()); get_element!(a, 0).[]().times(0); get_element!(a, 1).[]().times(0); assert!(!a.[](&b, &5)); } } #[test] fn []() { { let mut a: lhs_type!($lhs) = lhs_value!($lhs); let b: rhs_type!($rhs) = rhs_value_short!($rhs); assert!(a.len() > b.len()); get_element!(a, 0).[]().times(0); get_element!(a, 1).[]().times(0); get_element!(a, 2).[]().times(0); assert!(!a.[](&b, &Tolerance::::new(0.1, 5))); } { let mut a: lhs_type!($lhs) = lhs_value_short!($lhs); let b: rhs_type!($rhs) = rhs_value!($rhs); assert!(a.len() < b.len()); get_element!(a, 0).[]().times(0); get_element!(a, 1).[]().times(0); assert!(!a.[](&b, &Tolerance::::new(0.1, 5))); } } } }; } impl_test!(btree_map, btree_map); impl_test!(hashmap, hashmap);