capability_grower_configuration_comparison/
compare_sub_branch_ordering.rs

1// ---------------- [ File: capability-grower-configuration-comparison/src/compare_sub_branch_ordering.rs ]
2crate::ix!();
3
4pub trait CompareSubBranchOrdering {
5    fn compare_sub_branch_ordering(&self, other: &GrowerTreeConfiguration) -> CompareOutcome;
6}
7
8impl CompareSubBranchOrdering for GrowerTreeConfiguration {
9    fn compare_sub_branch_ordering(&self, other: &GrowerTreeConfiguration) -> CompareOutcome {
10        match (self.ordering(), other.ordering()) {
11            (None, None) => CompareOutcome::Exact,
12            (None, Some(_)) | (Some(_), None) => CompareOutcome::Partial(0.5),
13            (Some(a), Some(b)) => {
14                if a == b {
15                    CompareOutcome::Exact
16                } else {
17                    CompareOutcome::Partial(0.3)
18                }
19            }
20        }
21    }
22}
23
24#[cfg(test)]
25mod compare_sub_branch_ordering_tests {
26    use super::*;
27
28    #[traced_test]
29    fn both_none() {
30        let a = GrowerTreeConfiguration::default();
31        let b = GrowerTreeConfiguration::default();
32        assert_eq!(a.compare_sub_branch_ordering(&b), CompareOutcome::Exact);
33    }
34
35    #[traced_test]
36    fn same_variant() {
37        let a = GrowerTreeConfiguration::default()
38            .to_builder().ordering(Some(SubBranchOrdering::Alphabetical)).build().unwrap();
39        let b = a.clone();
40        assert_eq!(a.compare_sub_branch_ordering(&b), CompareOutcome::Exact);
41    }
42
43    #[traced_test]
44    fn different_variant_partial() {
45        let a = GrowerTreeConfiguration::default()
46            .to_builder().ordering(Some(SubBranchOrdering::Random)).build().unwrap();
47        let b = a.to_builder().ordering(Some(SubBranchOrdering::DifficultyDescending)).build().unwrap();
48        let out = a.compare_sub_branch_ordering(&b);
49        match out {
50            CompareOutcome::Partial(_) => {},
51            other => panic!("expected partial, got {:?}", other),
52        }
53    }
54
55    #[traced_test]
56    fn one_none_one_some_partial() {
57        let a = GrowerTreeConfiguration::default();
58        let b = a.to_builder().ordering(Some(SubBranchOrdering::Chronological)).build().unwrap();
59        match a.compare_sub_branch_ordering(&b) {
60            CompareOutcome::Partial(_) => {},
61            other => panic!("expected partial, got {:?}", other),
62        }
63    }
64}