aboutsummaryrefslogtreecommitdiff
path: root/tests/vector.rs
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2023-04-02 21:38:17 +0200
committerAndreas Grois <andi@grois.info>2023-04-02 21:53:09 +0200
commit418f514fc46f45ae2901753e3398adb33664bed9 (patch)
treef8edc0df880a00d4964877d4b835c0eb9e481210 /tests/vector.rs
parent02d01dd1b544a576caeb8da42912c5db904b94bd (diff)
I, for one, welcome our new clippy overlords
Diffstat (limited to 'tests/vector.rs')
-rw-r--r--tests/vector.rs149
1 files changed, 49 insertions, 100 deletions
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!()