|
3 | 3 | * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved. |
4 | 4 | */ |
5 | 5 |
|
| 6 | +use deep_causality::utils_test::test_utils; |
6 | 7 | use deep_causality::utils_test::test_utils::*; |
7 | 8 | use deep_causality::*; |
8 | 9 |
|
@@ -119,3 +120,77 @@ fn test_to_vec() { |
119 | 120 | let col = get_test_causality_vec(); |
120 | 121 | assert_eq!(3, col.to_vec().len()); |
121 | 122 | } |
| 123 | + |
| 124 | +// --- Tests for CausableReasoning Trait --- |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn test_evaluate_deterministic_propagation_error_non_deterministic_effect() { |
| 128 | + // Setup: A collection containing a non-deterministic (probabilistic) causaloid. |
| 129 | + let true_causaloid = test_utils::get_test_causaloid_deterministic_true(); |
| 130 | + let probabilistic_causaloid = test_utils::get_test_causaloid_probabilistic(); |
| 131 | + let coll: Vec<BaseCausaloid> = vec![true_causaloid, probabilistic_causaloid]; |
| 132 | + |
| 133 | + // Act: Evaluate with deterministic propagation, which expects only deterministic effects. |
| 134 | + let evidence = Evidence::Numerical(0.0); |
| 135 | + let result = coll.evaluate_deterministic_propagation(&evidence); |
| 136 | + |
| 137 | + // Assert: This covers the error branch for non-deterministic effects. |
| 138 | + assert!(result.is_err()); |
| 139 | + let err_msg = result.unwrap_err().to_string(); |
| 140 | + assert!(err_msg.contains("encountered a non-deterministic effect: Probabilistic(0.0)")); |
| 141 | +} |
| 142 | + |
| 143 | +#[test] |
| 144 | +fn test_evaluate_probabilistic_propagation_success() { |
| 145 | + // Setup: Create two causaloids with specific probabilities to test multiplication. |
| 146 | + fn causal_fn_half(_: &Evidence) -> Result<PropagatingEffect, CausalityError> { |
| 147 | + Ok(PropagatingEffect::Probabilistic(0.5)) |
| 148 | + } |
| 149 | + let p1 = Causaloid::new(1, causal_fn_half, "p=0.5"); |
| 150 | + |
| 151 | + fn causal_fn_quarter(_: &Evidence) -> Result<PropagatingEffect, CausalityError> { |
| 152 | + Ok(PropagatingEffect::Probabilistic(0.25)) |
| 153 | + } |
| 154 | + let p2 = Causaloid::new(2, causal_fn_quarter, "p=0.25"); |
| 155 | + |
| 156 | + let coll: Vec<BaseCausaloid> = vec![p1, p2]; |
| 157 | + |
| 158 | + // Act: Evaluate with probabilistic propagation. |
| 159 | + let evidence = Evidence::Numerical(0.0); |
| 160 | + let effect = coll.evaluate_probabilistic_propagation(&evidence).unwrap(); |
| 161 | + |
| 162 | + // Assert: This covers the main logic branch, ensuring probabilities are multiplied. |
| 163 | + assert_eq!(effect, PropagatingEffect::Probabilistic(0.125)); |
| 164 | +} |
| 165 | + |
| 166 | +#[test] |
| 167 | +fn test_evaluate_probabilistic_propagation_with_halting() { |
| 168 | + // Setup: A collection with a probabilistic causaloid followed by a halting one. |
| 169 | + let probabilistic_causaloid = test_utils::get_test_causaloid_probabilistic(); |
| 170 | + let halting_causaloid = test_utils::get_test_causaloid_halting(); |
| 171 | + let coll: Vec<BaseCausaloid> = vec![probabilistic_causaloid, halting_causaloid]; |
| 172 | + |
| 173 | + // Act: Evaluate with probabilistic propagation. |
| 174 | + let evidence = Evidence::Numerical(0.0); |
| 175 | + let effect = coll.evaluate_probabilistic_propagation(&evidence).unwrap(); |
| 176 | + |
| 177 | + // Assert: This covers the Halting branch, which should take precedence. |
| 178 | + assert_eq!(effect, PropagatingEffect::Halting); |
| 179 | +} |
| 180 | + |
| 181 | +#[test] |
| 182 | +fn test_evaluate_probabilistic_propagation_error_contextual_link() { |
| 183 | + // Setup: A collection containing a ContextualLink causaloid, which is invalid for this function. |
| 184 | + let probabilistic_causaloid = test_utils::get_test_causaloid_probabilistic(); |
| 185 | + let contextual_link_causaloid = test_utils::get_test_causaloid_contextual_link(); |
| 186 | + let coll: Vec<BaseCausaloid> = vec![probabilistic_causaloid, contextual_link_causaloid]; |
| 187 | + |
| 188 | + // Act: Evaluate with probabilistic propagation. |
| 189 | + let evidence = Evidence::Numerical(0.0); |
| 190 | + let result = coll.evaluate_probabilistic_propagation(&evidence); |
| 191 | + |
| 192 | + // Assert: This covers the error branch for invalid ContextualLink effects. |
| 193 | + assert!(result.is_err()); |
| 194 | + let err_msg = result.unwrap_err().to_string(); |
| 195 | + assert!(err_msg.contains("Encountered a ContextualLink in a probabilistic chain evaluation.")); |
| 196 | +} |
0 commit comments