capability_grower_configuration_comparison/
compare_depth.rs

1// ---------------- [ File: capability-grower-configuration-comparison/src/compare_depth.rs ]
2crate::ix!();
3
4pub trait CompareDepth {
5    fn compare_depth(&self, other: &GrowerTreeConfiguration) -> CompareOutcome;
6}
7
8impl CompareDepth for GrowerTreeConfiguration {
9    fn compare_depth(&self, other: &GrowerTreeConfiguration) -> CompareOutcome {
10        let d1 = self.depth();
11        let d2 = other.depth();
12        if d1 == d2 {
13            CompareOutcome::Exact
14        } else {
15            let diff = (*d1 as i32 - *d2 as i32).abs();
16            // E.g. big difference => Incompatible, smaller => partial
17            if diff > 3 {
18                CompareOutcome::Incompatible
19            } else {
20                // partial with some decreasing score
21                let penalty = 0.2 * diff as f32;
22                let score = (1.0 - penalty).max(0.0);
23                CompareOutcome::Partial(score)
24            }
25        }
26    }
27}
28
29#[cfg(test)]
30mod compare_depth_tests {
31    use super::*;
32    use traced_test::traced_test;
33
34    #[traced_test]
35    fn same_depth_returns_exact() {
36        let a = GrowerTreeConfiguration::base_config(5, 2, 2);
37        let b = GrowerTreeConfiguration::base_config(5, 3, 2);
38        let outcome = a.compare_depth(&b);
39        assert_eq!(outcome, CompareOutcome::Exact);
40    }
41
42    #[traced_test]
43    fn difference_small_returns_partial() {
44        let a = GrowerTreeConfiguration::base_config(5, 2, 2);
45        let b = GrowerTreeConfiguration::base_config(6, 2, 2); // diff=1 => partial
46        let outcome = a.compare_depth(&b);
47        assert!(matches!(outcome, CompareOutcome::Partial(_)));
48
49        let c = GrowerTreeConfiguration::base_config(7, 2, 2); // diff=2 => partial
50        let o2 = a.compare_depth(&c);
51        assert!(matches!(o2, CompareOutcome::Partial(_)));
52    }
53
54    #[traced_test]
55    fn difference_large_returns_incompatible() {
56        let a = GrowerTreeConfiguration::base_config(5, 2, 2);
57        let b = GrowerTreeConfiguration::base_config(9, 2, 2); // diff=4 => incompatible
58        let outcome = a.compare_depth(&b);
59        assert_eq!(outcome, CompareOutcome::Incompatible);
60    }
61}