Skip to content

Commit 25ba2d4

Browse files
committed
Increased test coverage
Signed-off-by: Marvin Hansen <[email protected]>
1 parent e4021e1 commit 25ba2d4

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

deep_causality/tests/types/causal_types/causaloid/causaloid_tests.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,61 @@ fn test_explain_collection_with_sub_explain_error() {
395395
let err_msg = result.unwrap_err().to_string();
396396
assert!(err_msg.contains("has not been evaluated"));
397397
}
398+
399+
#[test]
400+
fn test_evaluate_singleton_with_context() {
401+
let id: IdentificationValue = 1;
402+
let description = "tests a causaloid with a context";
403+
let context = get_base_context();
404+
405+
fn contextual_causal_fn(
406+
evidence: &Evidence,
407+
ctx: &Arc<BaseContext>,
408+
) -> Result<PropagatingEffect, CausalityError> {
409+
let obs = unpack_evidence(evidence)?;
410+
// Get contextoid by ID. In get_base_context, the node at index 0 has ID 1.
411+
let contextoid = ctx.get_node(0).expect("Could not find contextoid");
412+
// Extract a value from the contextoid.
413+
let val = contextoid.id() as f64; // This will be 1.0
414+
// Relate the observation (obs) to the data (val) from the contextoid.
415+
let is_active = obs.ge(&val);
416+
Ok(PropagatingEffect::Deterministic(is_active))
417+
}
418+
419+
let causaloid: BaseCausaloid =
420+
Causaloid::new_with_context(id, contextual_causal_fn, Arc::new(context), description);
421+
422+
// Evaluate with evidence that should result in true (1.5 >= 1.0)
423+
let evidence_true = Evidence::Numerical(1.5);
424+
let effect_true = causaloid.evaluate(&evidence_true).unwrap();
425+
assert_eq!(effect_true, PropagatingEffect::Deterministic(true));
426+
427+
// Evaluate with evidence that should result in false (0.5 < 1.0)
428+
let evidence_false = Evidence::Numerical(0.5);
429+
let effect_false = causaloid.evaluate(&evidence_false).unwrap();
430+
assert_eq!(effect_false, PropagatingEffect::Deterministic(false));
431+
}
432+
433+
#[test]
434+
fn test_evaluate_collection_ignores_other_effects() {
435+
// Setup: A collection with causaloids that have effects other than Deterministic or Halting.
436+
let probabilistic_causaloid = test_utils::get_test_causaloid_probabilistic();
437+
let contextual_link_causaloid = test_utils::get_test_causaloid_contextual_link();
438+
let false_causaloid = test_utils::get_test_causaloid_deterministic_false();
439+
440+
let causal_coll = vec![
441+
probabilistic_causaloid,
442+
contextual_link_causaloid,
443+
false_causaloid,
444+
];
445+
let collection_causaloid =
446+
Causaloid::from_causal_collection(103, Arc::new(causal_coll), "Ignore Others Collection");
447+
448+
// Act
449+
let evidence = Evidence::Numerical(0.0);
450+
let effect = collection_causaloid.evaluate(&evidence).unwrap();
451+
452+
// Assert: The aggregation logic should ignore Probabilistic and ContextualLink,
453+
// resulting in an overall effect of Deterministic(false).
454+
assert_eq!(effect, PropagatingEffect::Deterministic(false));
455+
}

0 commit comments

Comments
 (0)