From ea1814025f3bbab7a0aac640e69c56d983a18308 Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Tue, 28 Mar 2023 21:55:26 +0200 Subject: Add integration test for trivial case with lifetime dependency --- tests/trivial_lifetime.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/trivial_lifetime.rs (limited to 'tests') diff --git a/tests/trivial_lifetime.rs b/tests/trivial_lifetime.rs new file mode 100644 index 0000000..fe05234 --- /dev/null +++ b/tests/trivial_lifetime.rs @@ -0,0 +1,37 @@ +//! Tests if a trivial functor, which's lifetime depends on the mapping function, works. +use std::rc::Rc; +use higher::{Functor, Bind}; +use higher_free_macro::free; + +#[derive(Clone)] +struct TrivWithLifetime<'a,A,B>{ + next : RcA + 'a>, +} + +impl<'a,A : 'a,B : 'a> Functor<'a,A> for TrivWithLifetime<'a,A,B> { + type Target = TrivWithLifetime<'a,T,B>; + + fn fmap(self, f: F) -> Self::Target + where + F: Fn(A) -> C + 'a { + TrivWithLifetime{ next : Rc::new(move |x| f((self.next)(x)))} + } +} + +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)); + match f { + FreeTriv::Free(f) => { + let n = (f.next)(-4); + match n { + FreeTriv::Pure(v) => assert_eq!(v,4u32), + FreeTriv::Free(_) => unreachable!(), + } + }, + _ => unreachable!() + } +} \ No newline at end of file -- cgit v1.2.3