shared_follows_laws
Some checks failed
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.

This commit is contained in:
AF 2023-10-15 17:34:09 +00:00
parent 31e6ac9429
commit 807ff5e589
9 changed files with 78 additions and 16 deletions

View File

@ -214,4 +214,9 @@ mod composition_tests {
fn monad_follows_laws() { fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap(); test_suite::monad_follows_laws::<T>().unwrap();
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -166,12 +166,12 @@ mod effect_tests {
use super::{test_suite, tests, Effect, EffectInstance, WithEffect}; use super::{test_suite, tests, Effect, EffectInstance, WithEffect};
#[derive(Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
enum TestEffect { enum TestEffect {
Pure, Pure,
Sample, Sample,
Parallel(Box<Self>, Box<Self>), Parallel(Arc<Self>, Arc<Self>),
Sequential(Box<Self>, Box<Self>), Sequential(Arc<Self>, Arc<Self>),
} }
impl TestEffect { impl TestEffect {
@ -179,25 +179,27 @@ mod effect_tests {
matches!(self, Self::Pure) matches!(self, Self::Pure)
} }
fn simplify(self) -> Self { fn simplify(&self) -> Arc<Self> {
match self { match self {
Self::Pure => Self::Pure, Self::Pure => Self::Pure.into(),
Self::Sample => Self::Sample, Self::Sample => Self::Sample.into(),
Self::Parallel(a, b) if a.is_pure() => *b, Self::Parallel(a, b) if a.is_pure() => b.clone(),
Self::Parallel(a, b) if b.is_pure() => *a, Self::Parallel(a, b) if b.is_pure() => a.clone(),
Self::Sequential(a, b) if a.is_pure() => *b, Self::Sequential(a, b) if a.is_pure() => b.clone(),
Self::Sequential(a, b) if b.is_pure() => *a, Self::Sequential(a, b) if b.is_pure() => a.clone(),
Self::Parallel(a, b) => match *a { Self::Parallel(a, b) => match a.as_ref() {
Self::Parallel(aa, ab) => { Self::Parallel(aa, ab) => {
Self::Parallel(aa, Self::Parallel(ab, b).into()).simplify() Self::Parallel(aa.clone(), Self::Parallel(ab.clone(), b.clone()).into())
.simplify()
} }
a => Self::Parallel(a.simplify().into(), b.simplify().into()), a => Self::Parallel(a.simplify(), b.simplify()).into(),
}, },
Self::Sequential(a, b) => match *a { Self::Sequential(a, b) => match a.as_ref() {
TestEffect::Sequential(aa, ab) => { TestEffect::Sequential(aa, ab) => {
Self::Sequential(aa, Self::Sequential(ab, b).into()).simplify() Self::Sequential(aa.clone(), Self::Sequential(ab.clone(), b.clone()).into())
.simplify()
} }
a => Self::Sequential(a.simplify().into(), b.simplify().into()), a => Self::Sequential(a.simplify().clone(), b.simplify().clone()).into(),
}, },
} }
} }
@ -248,4 +250,9 @@ mod effect_tests {
fn monad_follows_laws() { fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap(); test_suite::monad_follows_laws::<T>().unwrap();
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -184,4 +184,9 @@ mod future_tests {
Selected::B(_, b) => assert_eq!(b, 2), Selected::B(_, b) => assert_eq!(b, 2),
} }
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -185,4 +185,9 @@ mod option_tests {
fn monad_follows_laws() { fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap(); test_suite::monad_follows_laws::<T>().unwrap();
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -280,4 +280,9 @@ mod result_tests {
fn monad_follows_laws() { fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap(); test_suite::monad_follows_laws::<T>().unwrap();
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -147,4 +147,9 @@ mod solo_tests {
fn monad_follows_laws() { fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap(); test_suite::monad_follows_laws::<T>().unwrap();
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -256,4 +256,9 @@ mod tryfuture_tests {
Selected::B(_, b) => assert_eq!(b, 2), Selected::B(_, b) => assert_eq!(b, 2),
} }
} }
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
} }

View File

@ -1,5 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use shared::SharedFunctor;
use super::tests::*; use super::tests::*;
use super::*; use super::*;
@ -119,3 +121,11 @@ pub fn monad_follows_laws<'a, T: Monad<'a> + FunctorTestSuite<'a>>() -> R {
}); });
res res
} }
pub fn shared_follows_laws<'a, T: SharedFunctor<'a> + FunctorTestSuite<'a>>() -> R {
let mut res = R::default();
T::sample(|pa| {
res += shared_is_same_as_original::<T, _>(move || pa(2));
});
res
}

View File

@ -5,6 +5,7 @@ use std::{
use applicative_select::Selected; use applicative_select::Selected;
use controlflow::IterativeWrapped; use controlflow::IterativeWrapped;
use shared::SharedFunctor;
use super::*; use super::*;
@ -448,3 +449,17 @@ pub fn select_of_equal_is_same<
fa0(), fa0(),
) )
} }
pub fn shared_is_same_as_original<
'a,
T: SharedFunctor<'a> + Eqr<'a>,
A: 'a + Send + Sync + Clone + Debug + PartialEq,
>(
fa0: impl 'a + Fn() -> T::F<A>,
) -> R {
T::eqr(
"shared same as original",
T::unshare(T::share(fa0())),
fa0(),
)
}