diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/multiple_generics.rs | 49 | ||||
| -rw-r--r-- | tests/multiple_generics_lifetime.rs | 35 | ||||
| -rw-r--r-- | tests/trivial.rs | 8 | ||||
| -rw-r--r-- | tests/trivial_lifetime.rs | 8 | ||||
| -rw-r--r-- | tests/vector.rs | 149 | ||||
| -rw-r--r-- | tests/with_lifetimes.rs | 2 |
6 files changed, 104 insertions, 147 deletions
diff --git a/tests/multiple_generics.rs b/tests/multiple_generics.rs index 2f560dd..c8c76ba 100644 --- a/tests/multiple_generics.rs +++ b/tests/multiple_generics.rs @@ -1,11 +1,21 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::all)] //! Tests if multiple generic parameters work, for the case that lifetimes are independent of mapping functions. -//! For simplicity, it just creates a FreeResult +//! For simplicity, it just creates a `FreeResult` based on `Result`. use higher_free_macro::free; use higher::{Functor, Bind, Apply}; free!(FreeResult<O,E>, Result<FreeResult<O,E>,E>); + +//just to appease clippy without disabling the lint.... +macro_rules! assert_nearly_equal { + ($a:expr, $b:expr, $c:expr) => { + assert!((($a)-($b)).abs() < $c) + }; +} + #[test] fn test_multiple_generics(){ let m : FreeResult<_, String> = FreeResult::lift_f(Ok(37u32)); @@ -16,26 +26,16 @@ fn test_multiple_generics(){ match m { FreeResult::Free(b) => { match *b { - Ok(f) => { - match f { - FreeResult::Free(b) => { - match *b { - Ok(f) => { - match f{ - FreeResult::Pure(x) => assert_eq!(x, 37*6), - _ => unreachable!() - } - } - _ => unreachable!() - } - }, + Ok(FreeResult::Free(b)) => { + match *b { + Ok(FreeResult::Pure(x)) => assert_eq!(x, 37*6), _ => unreachable!() } } _ => unreachable!() } } - _ => unreachable!() + FreeResult::Pure(_) => unreachable!() } } @@ -46,41 +46,36 @@ fn test_multiple_generics2(){ match m{ FreeResult::Free(m) => { match &*m { - Ok(m) => { - match m{ - FreeResult::Free(m) => { - match &**m { - Err(e) => assert_eq!(e, "An early out."), - _ => unreachable!() - } - } + Ok(FreeResult::Free(m)) => { + match &**m { + Err(e) => assert_eq!(e, "An early out."), _ => unreachable!() } }, _ => unreachable!() } }, - _ => unreachable!() + FreeResult::Pure(_) => unreachable!() } } #[test] fn test_multiple_generics3(){ let m : FreeResult<_, String> = FreeResult::lift_f(Ok(37u32)); - let f : FreeResult<_, String> = FreeResult::Pure(|x : u32| -> f32 {(x as f32)*0.5f32}).fmap(Into::into); + let f : FreeResult<_, String> = FreeResult::Pure(|x : u32| -> f64 {f64::from(x)*0.5f64}).fmap(Into::into); let m = m.apply(f); match m{ FreeResult::Free(m) => { match &*m{ Ok(k) => { match k { - FreeResult::Pure(k) => assert_eq!(18.5f32, *k), + FreeResult::Pure(k) => assert_nearly_equal!(18.5f64, *k, f64::EPSILON), FreeResult::Free(_) => unreachable!(), } } Err(_) => unreachable!(), } }, - _ => unreachable!() + FreeResult::Pure(_) => unreachable!() } }
\ No newline at end of file diff --git a/tests/multiple_generics_lifetime.rs b/tests/multiple_generics_lifetime.rs index ddc0628..48a9bd1 100644 --- a/tests/multiple_generics_lifetime.rs +++ b/tests/multiple_generics_lifetime.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::all)] //! Tests if multiple generic parameters work, if the return value's lifetime depends on the mapping function lifetime. use std::rc::Rc; @@ -24,10 +26,10 @@ free!(<'xx>, FreeTest<'xx,'yy,AA,BB>, TestFunctor<'xx, 'yy, FreeTest<'xx, 'yy, A #[test] fn test_lifetime_multiple_generics(){ - let m = FreeTest::lift_f(TestFunctor{ data : &"Listening to NSP while writing this.", next : Rc::new(|x| (x as f32)*0.5f32)}); - let f = FreeTest::Pure(|x : f32| -> bool {x > 0.7f32} ).fmap(Into::into); - let m = m.apply(f); - match m { + let free_monad = FreeTest::lift_f(TestFunctor{ data : &"Listening to NSP while writing this.", next : Rc::new(|x| f64::from(x)*0.5f64)}); + let functions = FreeTest::Pure(|x : f64| -> bool {x > 0.7f64} ).fmap(Into::into); + let free_monad_after_apply = free_monad.apply(functions); + match free_monad_after_apply { FreeTest::Free(m) => { assert_eq!(m.data, &"Listening to NSP while writing this."); let x = m.next.clone(); @@ -43,24 +45,31 @@ fn test_lifetime_multiple_generics(){ FreeTest::Free(_) => unreachable!(), } }, - _ => unreachable!() + FreeTest::Pure(_) => unreachable!() } } +//just to appease clippy without disabling the lint.... +macro_rules! assert_nearly_equal { + ($a:expr, $b:expr, $c:expr) => { + assert!((($a)-($b)).abs() < $c) + }; +} + #[test] fn test_lifetime_multiple_generics_bind(){ - let m = FreeTest::lift_f(TestFunctor{ data : &"Listening to Soilwork while writing this.", next : Rc::new(|x| (x as f32)*0.5f32)}); - let m = m.bind(|x : f32| -> FreeTest<_,_> { + let m = FreeTest::lift_f(TestFunctor{ data : &"Listening to Soilwork while writing this.", next : Rc::new(|x| f64::from(x)*0.5f64)}); + let m = m.bind(|x : f64| -> FreeTest<_,_> { if x < 0.0 { - FreeTest::Pure(x.abs().floor() as u32) + FreeTest::Pure(x.abs().floor()) } else { - FreeTest::lift_f(TestFunctor{data : &"Now it's Little Big.", next : Rc::new(move |y| (y as u32) + (x.ceil() as u32))}) + FreeTest::lift_f(TestFunctor{data : &"Now it's Little Big.", next : Rc::new(move |y| f64::from(y) + x.ceil())}) }}); match m{ FreeTest::Free(m) => { assert_eq!(m.data, &"Listening to Soilwork while writing this."); match (m.next)(-3){ - FreeTest::Pure(v) => assert_eq!(v, 1), + FreeTest::Pure(v) => assert_nearly_equal!(v, 1f64, f64::EPSILON), FreeTest::Free(_) => unreachable!(), } match (m.next)(3){ @@ -68,14 +77,12 @@ fn test_lifetime_multiple_generics_bind(){ FreeTest::Free(v) => { assert_eq!(v.data, &"Now it's Little Big."); match (v.next)(5) { - FreeTest::Pure(v) => { - assert_eq!(v, 7) - }, + FreeTest::Pure(v) => assert_nearly_equal!(v, 7f64, f64::EPSILON), FreeTest::Free(_) => unreachable!(), } }, } }, - _ => unreachable!() + FreeTest::Pure(_) => unreachable!() } }
\ No newline at end of file diff --git a/tests/trivial.rs b/tests/trivial.rs index 5468fe4..5bc1e91 100644 --- a/tests/trivial.rs +++ b/tests/trivial.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::all)] //! A trivial test functor. Not holding any data, so this is basically just a linked list of free-nodes. use higher_free_macro::free; use higher::{Functor, Bind, Apply}; @@ -24,16 +26,16 @@ fn test_trivial_functor() { TrivialFunctor(f) => { match f{ TrivialFreeMonad::Pure(x) => assert_eq!(x, 37*6), - _ => unreachable!() + TrivialFreeMonad::Free(_) => unreachable!() } } } }, - _ => unreachable!() + TrivialFreeMonad::Pure(_) => unreachable!() } } } } - _ => unreachable!() + TrivialFreeMonad::Pure(_) => unreachable!() } }
\ No newline at end of file diff --git a/tests/trivial_lifetime.rs b/tests/trivial_lifetime.rs index fe05234..4f7d094 100644 --- a/tests/trivial_lifetime.rs +++ b/tests/trivial_lifetime.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::all)] //! Tests if a trivial functor, which's lifetime depends on the mapping function, works. use std::rc::Rc; use higher::{Functor, Bind}; @@ -22,8 +24,8 @@ free!(<'a>, FreeTriv<'a,A,B>, TrivWithLifetime<'a,FreeTriv<'a,A,B>,B>); #[test] fn test_trivial_with_lifetime(){ - let f = FreeTriv::lift_f(TrivWithLifetime{next : Rc::new(|x : i32| x.abs() as u32)}); - let f = f.bind(|x| FreeTriv::Pure(x)); + let f = FreeTriv::lift_f(TrivWithLifetime{next : Rc::new(i32::unsigned_abs)}); + let f = f.bind(FreeTriv::Pure); match f { FreeTriv::Free(f) => { let n = (f.next)(-4); @@ -32,6 +34,6 @@ fn test_trivial_with_lifetime(){ FreeTriv::Free(_) => unreachable!(), } }, - _ => unreachable!() + FreeTriv::Pure(_) => unreachable!() } }
\ No newline at end of file diff --git a/tests/vector.rs b/tests/vector.rs index 74cc913..2b2f4b2 100644 --- a/tests/vector.rs +++ b/tests/vector.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::all)] //! Tests if creating a Free Monad for a Vec works. Not sure if this is useful in any way. //! It is a nice illustration that Free Monads are tree-like though. @@ -6,117 +8,64 @@ use higher::{Functor, Bind, Apply}; free!(FreeVec<A>, Vec<FreeVec<A>>); + +//just to appease clippy without disabling the lint.... +macro_rules! assert_nearly_equal { + ($a:expr, $b:expr, $c:expr) => { + assert!((($a)-($b)).abs() < $c) + }; +} + #[test] fn test_vector(){ - let fv = FreeVec::lift_f(vec![2,3,4]); - let fv = fv.fmap(|x| x*2); - let fv = fv.bind(|x| if x%3 == 0 {FreeVec::Pure(x)} else {FreeVec::lift_f(vec![x,x+1])}); - let f = FreeVec::lift_f(vec![(|x| (x as f32) / 3.0) as fn(u32)->f32, (|x| (x+2) as f32) as fn(u32)->f32]); - let r = fv.apply(f.fmap(Into::into)); - match r { + let free_monad = FreeVec::lift_f(vec![2,3,4]); + let free_monad_after_fmap = free_monad.fmap(|x| x*2); + let free_monad_after_bind = free_monad_after_fmap.bind(|x| if x%3 == 0 {FreeVec::Pure(x)} else {FreeVec::lift_f(vec![x,x+1])}); + let functions = FreeVec::lift_f(vec![(|x| f64::from(x) / 3.0) as fn(u32)->f64, (|x| f64::from(x+2)) as fn(u32)->f64]); + let free_monad_after_apply = free_monad_after_bind.apply(functions.fmap(Into::into)); + match free_monad_after_apply { FreeVec::Free(v) => { match &**v{ - [a,b] => { - match a { - FreeVec::Free(v) => { - match &***v { - [a,b,c] => { - match a { - FreeVec::Free(v) => { - match &***v { - [a,b] => { - match a{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(4.0f32/3.0f32, *v)} - } - match b{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(5.0f32/3.0f32, *v)} - } - }, - _ => unreachable!() - } - } - FreeVec::Pure(_) => unreachable!(), - } - match b { - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => assert_eq!(2.0f32, *v), - } - match c { - FreeVec::Free(v) => { - match &***v { - [a,b] => { - match a{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(8.0f32/3.0f32, *v)} - } - match b{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(3.0f32, *v)} - } - }, - _ => unreachable!() - } - } - FreeVec::Pure(_) => unreachable!(), - } + [FreeVec::Free(left), FreeVec::Free(right)] => { + match &***left { + [FreeVec::Free(left), FreeVec::Pure(middle), FreeVec::Free(right)] => { + match &***left { + [FreeVec::Pure(left), FreeVec::Pure(right)] => { + assert_nearly_equal!(4.0f64/3.0f64, *left, f64::EPSILON); + assert_nearly_equal!(5.0f64/3.0f64, *right, f64::EPSILON); + }, + _ => unreachable!() + } + assert_nearly_equal!(2.0f64, *middle, f64::EPSILON); + match &***right { + [FreeVec::Pure(left),FreeVec::Pure(right)] => { + assert_nearly_equal!(8.0f64/3.0f64, *left, f64::EPSILON); + assert_nearly_equal!(3.0f64, *right, f64::EPSILON); }, _ => unreachable!() } - } - FreeVec::Pure(_) => unreachable!() + }, + _ => unreachable!() } - - match b { - FreeVec::Free(v) => { - match &***v { - [a,b,c] => { - match a { - FreeVec::Free(v) => { - match &***v { - [a,b] => { - match a{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(6.0f32, *v)} - } - match b{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(7.0f32, *v)} - } - }, - _ => unreachable!() - } - } - FreeVec::Pure(_) => unreachable!(), - } - match b { - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => assert_eq!(8.0f32, *v), - } - match c { - FreeVec::Free(v) => { - match &***v { - [a,b] => { - match a{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(10.0f32, *v)} - } - match b{ - FreeVec::Free(_) => unreachable!(), - FreeVec::Pure(v) => {assert_eq!(11.0f32, *v)} - } - }, - _ => unreachable!() - } - } - FreeVec::Pure(_) => unreachable!(), - } + match &***right { + [FreeVec::Free(left),FreeVec::Pure(middle),FreeVec::Free(right)] => { + match &***left { + [FreeVec::Pure(left),FreeVec::Pure(right)] => { + assert_nearly_equal!(6.0f64, *left, f64::EPSILON); + assert_nearly_equal!(7.0f64, *right, f64::EPSILON); + }, + _ => unreachable!() + } + assert_nearly_equal!(8.0f64, *middle, f64::EPSILON); + match &***right { + [FreeVec::Pure(left),FreeVec::Pure(right)] => { + assert_nearly_equal!(10.0f64, *left, f64::EPSILON); + assert_nearly_equal!(11.0f64, *right, f64::EPSILON); }, _ => unreachable!() } - } - FreeVec::Pure(_) => unreachable!() + }, + _ => unreachable!() } }, _ => unreachable!() diff --git a/tests/with_lifetimes.rs b/tests/with_lifetimes.rs index 2e87878..697fc74 100644 --- a/tests/with_lifetimes.rs +++ b/tests/with_lifetimes.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::all)] //! Test for the case that the Functor the Free Monad is based on has lifetime parameters that do not depend on the //! lifetime of the mapping function in the Functor implementation. |
