Skip to content

Commit 70e7414

Browse files
committed
test(deep_causality_haft): Added and update test suite to cover all newly added types and traits.
Signed-off-by: Marvin Hansen <marvin.hansen@gmail.com>
1 parent 370380d commit 70e7414

23 files changed

+661
-16
lines changed

deep_causality_haft/src/algebra/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
//! To use these traits, you typically need a type that implements the corresponding HKT witness (from `core` or `extensions`).
4242
//!
4343
//! ```rust
44-
//! use deep_causality_haft::algebra::functor::Functor;
45-
//! use deep_causality_haft::extensions::hkt_vec_ext::VecWitness;
44+
//! use deep_causality_haft::Functor;
45+
//! use deep_causality_haft::VecWitness;
4646
//!
4747
//! let v = vec![1, 2, 3];
4848
//! let v_mapped = VecWitness::fmap(v, |x| x * 2);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_haft::{Adjunction, HKT};
7+
8+
// Simplified Identity for testing
9+
#[derive(Debug, PartialEq, Clone)]
10+
struct Identity<T>(T);
11+
struct IdentityWitness;
12+
impl HKT for IdentityWitness {
13+
type Type<T> = Identity<T>;
14+
}
15+
16+
struct IdentityAdjunction;
17+
18+
impl Adjunction<IdentityWitness, IdentityWitness> for IdentityAdjunction {
19+
fn unit<A>(a: A) -> Identity<Identity<A>> {
20+
Identity(Identity(a))
21+
}
22+
23+
fn counit<A>(fa: Identity<Identity<A>>) -> A {
24+
fa.0.0
25+
}
26+
27+
fn left_adjunct<A, B, F>(_a: A, _f: F) -> Identity<B>
28+
where
29+
F: Fn(Identity<A>) -> B,
30+
{
31+
// Simplified - not fully implemented for test
32+
panic!("Not implemented for test")
33+
}
34+
35+
fn right_adjunct<A, B, F>(_la: Identity<A>, _f: F) -> B
36+
where
37+
F: Fn(A) -> Identity<B>,
38+
{
39+
// Simplified - not fully implemented for test
40+
panic!("Not implemented for test")
41+
}
42+
}
43+
44+
#[test]
45+
fn test_adjunction_identity() {
46+
let val = 42;
47+
let unit = IdentityAdjunction::unit(val);
48+
assert_eq!(unit, Identity(Identity(42)));
49+
50+
let counit = IdentityAdjunction::counit(unit);
51+
assert_eq!(counit, 42);
52+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_haft::{Applicative, OptionWitness, ResultWitness};
7+
8+
#[test]
9+
fn test_applicative_option() {
10+
let pure_val = OptionWitness::pure(5);
11+
assert_eq!(pure_val, Some(5));
12+
13+
let func = Some(|x: i32| x * 2);
14+
let val = Some(10);
15+
let applied = OptionWitness::apply(func, val);
16+
assert_eq!(applied, Some(20));
17+
18+
let none_func: Option<fn(i32) -> i32> = None;
19+
let applied_none_func = OptionWitness::apply(none_func, val);
20+
assert_eq!(applied_none_func, None);
21+
22+
let none_val: Option<i32> = None;
23+
let applied_none_val = OptionWitness::apply(func, none_val);
24+
assert_eq!(applied_none_val, None);
25+
}
26+
27+
#[test]
28+
fn test_applicative_result() {
29+
let pure_val: Result<i32, &str> = ResultWitness::pure(5);
30+
assert_eq!(pure_val, Ok(5));
31+
32+
let func: Result<fn(i32) -> i32, &str> = Ok(|x| x * 2);
33+
let val: Result<i32, &str> = Ok(10);
34+
let applied = ResultWitness::apply(func, val);
35+
assert_eq!(applied, Ok(20));
36+
37+
let err_func: Result<fn(i32) -> i32, &str> = Err("func error");
38+
let applied_err_func = ResultWitness::apply(err_func, val);
39+
assert_eq!(applied_err_func, Err("func error"));
40+
41+
let err_val: Result<i32, &str> = Err("val error");
42+
let applied_err_val = ResultWitness::apply(func, err_val);
43+
assert_eq!(applied_err_val, Err("val error"));
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_haft::{Bifunctor, ResultUnboundWitness};
7+
8+
#[test]
9+
fn test_bifunctor_result() {
10+
let ok_v: Result<i32, String> = Ok(10);
11+
let bimapped_ok = ResultUnboundWitness::bimap(ok_v, |v| v * 2, |e| e);
12+
assert_eq!(bimapped_ok, Ok(20));
13+
14+
let err_v: Result<i32, String> = Err("error".to_string());
15+
let bimapped_err = ResultUnboundWitness::bimap(err_v, |v| v * 2, |e| format!("new {}", e));
16+
assert_eq!(bimapped_err, Err("new error".to_string()));
17+
18+
let err_v2: Result<i32, String> = Err("error".to_string());
19+
let first_mapped = ResultUnboundWitness::first(err_v2, |v| v + 5);
20+
assert_eq!(first_mapped, Err("error".to_string()));
21+
22+
let ok_v2: Result<i32, String> = Ok(10);
23+
let second_mapped = ResultUnboundWitness::second(ok_v2, |e| format!("mapped {}", e));
24+
assert_eq!(second_mapped, Ok(10));
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_haft::{CoMonad, Functor, HKT};
7+
8+
// Mock Identity Comonad (since standard types don't implement Comonad easily in Rust)
9+
#[derive(Debug, PartialEq, Clone)]
10+
struct Identity<T>(T);
11+
12+
struct IdentityWitness;
13+
impl HKT for IdentityWitness {
14+
type Type<T> = Identity<T>;
15+
}
16+
impl Functor<IdentityWitness> for IdentityWitness {
17+
fn fmap<A, B, Func>(fa: Identity<A>, mut f: Func) -> Identity<B>
18+
where
19+
Func: FnMut(A) -> B,
20+
{
21+
Identity(f(fa.0))
22+
}
23+
}
24+
impl CoMonad<IdentityWitness> for IdentityWitness {
25+
fn extract<A>(wa: &Identity<A>) -> A
26+
where
27+
A: Clone,
28+
{
29+
wa.0.clone()
30+
}
31+
fn extend<A, B, Func>(wa: &Identity<A>, mut f: Func) -> Identity<B>
32+
where
33+
Func: FnMut(&Identity<A>) -> B,
34+
{
35+
Identity(f(wa))
36+
}
37+
}
38+
39+
#[test]
40+
fn test_comonad_identity() {
41+
let id = Identity(10);
42+
43+
// Extract
44+
assert_eq!(IdentityWitness::extract(&id), 10);
45+
46+
// Extend
47+
let extended = IdentityWitness::extend(&id, |wa| IdentityWitness::extract(wa) + 1);
48+
assert_eq!(extended, Identity(11));
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_haft::{CyberneticLoop, HKT5Unbound};
7+
8+
// Mock Cybernetic Loop Structure
9+
// (Sensor, Controller, Actuator, Feedback, Meta)
10+
#[derive(Debug, PartialEq, Clone)]
11+
struct System<S, C, A, F, M>(S, C, A, F, M);
12+
13+
struct SystemWitness;
14+
impl HKT5Unbound for SystemWitness {
15+
type Type<S, C, A, F, M> = System<S, C, A, F, M>;
16+
}
17+
18+
impl CyberneticLoop<SystemWitness> for SystemWitness {
19+
fn control_step<S, B, C, A, E, FObserve, FDecide>(
20+
_agent: System<S, B, C, A, E>,
21+
sensor_input: S,
22+
observe_fn: FObserve,
23+
decide_fn: FDecide,
24+
) -> Result<A, E>
25+
where
26+
FObserve: Fn(S, C) -> B,
27+
FDecide: Fn(B, C) -> A,
28+
{
29+
// Simplified - use zero-sized type for context
30+
unsafe {
31+
let context: C = std::mem::zeroed();
32+
let belief = observe_fn(sensor_input, context);
33+
let context2: C = std::mem::zeroed();
34+
let action = decide_fn(belief, context2);
35+
Ok(action)
36+
}
37+
}
38+
}
39+
40+
#[test]
41+
fn test_cybernetic_loop() {
42+
let sys = System(1, 2, 3, 4, 100);
43+
44+
// Test control_step
45+
let result = SystemWitness::control_step(
46+
sys,
47+
10, // sensor input
48+
|s, _c| s + 1, // observe: sensor -> belief
49+
|b, _c| b * 2, // decide: belief -> action
50+
);
51+
52+
assert_eq!(result, Ok(22)); // (10 + 1) * 2 = 22
53+
}
File renamed without changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_haft::{Functor, OptionWitness, ResultWitness, VecWitness};
7+
8+
#[test]
9+
fn test_functor_option() {
10+
let some_v = Some(2);
11+
let mapped = OptionWitness::fmap(some_v, |x| x * 2);
12+
assert_eq!(mapped, Some(4));
13+
14+
let none_v: Option<i32> = None;
15+
let mapped_none = OptionWitness::fmap(none_v, |x| x * 2);
16+
assert_eq!(mapped_none, None);
17+
}
18+
19+
#[test]
20+
fn test_functor_result() {
21+
let ok_v: Result<i32, &str> = Ok(2);
22+
let mapped_ok = ResultWitness::fmap(ok_v, |x| x * 2);
23+
assert_eq!(mapped_ok, Ok(4));
24+
25+
let err_v: Result<i32, &str> = Err("error");
26+
let mapped_err = ResultWitness::fmap(err_v, |x| x * 2);
27+
assert_eq!(mapped_err, Err("error"));
28+
}
29+
30+
#[test]
31+
fn test_functor_vec() {
32+
let v = vec![1, 2, 3];
33+
let mapped_v = VecWitness::fmap(v, |x| x * 2);
34+
assert_eq!(mapped_v, vec![2, 4, 6]);
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
mod adjunction_tests;
7+
mod applicative_tests;
8+
mod bifunctor_tests;
9+
mod comonad_tests;
10+
mod cybernetic_loop_tests;
11+
mod foldable_tests;
12+
mod functor_tests;
13+
mod monad_tests;
14+
mod parametric_monad_tests;
15+
mod profunctor_tests;
16+
mod promonad_tests;
17+
mod riemann_map_tests;
18+
mod traversable_tests;
File renamed without changes.

0 commit comments

Comments
 (0)