capability_grower_configuration_comparison/
compare_density.rs

1// ---------------- [ File: capability-grower-configuration-comparison/src/compare_density.rs ]
2crate::ix!();
3
4pub trait CompareDensity {
5    fn compare_density(&self, other: &GrowerTreeConfiguration) -> CompareOutcome;
6}
7
8impl CompareDensity for GrowerTreeConfiguration {
9    fn compare_density(&self, other: &GrowerTreeConfiguration) -> CompareOutcome {
10        let d1 = *self.density();
11        let d2 = *other.density();
12        if d1 == d2 {
13            CompareOutcome::Exact
14        } else {
15            let diff = (d1 as i32 - d2 as i32).abs();
16            if diff > 10 {
17                CompareOutcome::Incompatible
18            } else {
19                let penalty = 0.05 * diff as f32;
20                let score = (1.0 - penalty).max(0.0);
21                CompareOutcome::Partial(score)
22            }
23        }
24    }
25}
26
27#[cfg(test)]
28mod compare_density_tests {
29    use super::*;
30
31    #[traced_test]
32    fn same_exact() {
33        let a = GrowerTreeConfiguration::base_config(5, 3, 2);
34        let b = GrowerTreeConfiguration::base_config(5, 1, 2);
35        assert_eq!(a.compare_density(&b), CompareOutcome::Exact);
36    }
37
38    #[traced_test]
39    fn difference_small_partial() {
40        let a = GrowerTreeConfiguration::base_config(5, 3, 2);
41        let mut b = a.clone();
42        b = b.to_builder().density(3).build().unwrap(); // from 2 =>3
43        let out = a.compare_density(&b);
44        match out {
45            CompareOutcome::Partial(_) => {},
46            other => panic!("expected partial, got {:?}", other),
47        }
48    }
49
50    #[traced_test]
51    fn difference_large_incompatible() {
52        let a = GrowerTreeConfiguration::base_config(5, 3, 2);
53        let mut b = a.clone();
54        b = b.to_builder().density(15).build().unwrap();
55        assert_eq!(a.compare_density(&b), CompareOutcome::Incompatible);
56    }
57}