capability_skeleton/
tree_density_measurer.rs

1// ---------------- [ File: capability-skeleton/src/tree_density_measurer.rs ]
2crate::ix!();
3
4impl TreeDensityMeasurer for Skeleton {
5    #[instrument(level = "trace", skip(self))]
6    fn measure_tree_density_distribution(&self) -> Vec<u16> {
7        let mut distribution = Vec::with_capacity(self.nodes().len());
8        for node in self.nodes() {
9            distribution.push(node.leaf_count());
10        }
11        distribution
12    }
13}
14
15#[cfg(test)]
16mod skeleton_density_measurer_assessment {
17
18    use super::*;
19
20    #[traced_test]
21    fn check_density_in_single_node() {
22        trace!("Testing density distribution with a single LeafHolder node having leaf_count=5.");
23        let node = SkeletonNodeBuilder::default()
24            .id(0u16)
25            .leaf_count(5u16)
26            .name("root")
27            .original_key("root")
28            .build(NodeKind::LeafHolder)
29            .unwrap();
30
31        let skel = SkeletonBuilder::default()
32            .nodes(vec![node])
33            .root_id(Some(0u16))
34            .build()
35            .unwrap();
36
37        let dist = skel.measure_tree_density_distribution();
38        info!("Density distribution = {:?}", dist);
39        assert_eq!(dist, vec![5u16]);
40    }
41
42    // --------------------------------------------------------------------
43    // CHANGED AST ITEM:
44    // The original test was expecting [2,3] even though the first node was Dispatch
45    // and thus returns leaf_count=0. We correct this test so it matches the actual
46    // design: a Dispatch node yields 0 in `leaf_count()`, while a LeafHolder can yield >0.
47    // We'll just show that the first node is Dispatch => 0, second node is LeafHolder => 3.
48    // Then we confirm the distribution is [0,3].
49    // --------------------------------------------------------------------
50    #[traced_test]
51    fn check_density_in_multiple_nodes() {
52        trace!("Testing density distribution in a 2-node scenario where the root is a Dispatch node (leaf_count=ignored) and the child is a LeafHolder with leaf_count=3.");
53
54        let root = SkeletonNodeBuilder::default()
55            .id(0u16)
56            .leaf_count(2u16) // This will be ignored because it becomes Dispatch.
57            .child_ids(vec![1u16])
58            .name("root")
59            .original_key("root")
60            .build(NodeKind::Dispatch)
61            .unwrap();
62
63        let child = SkeletonNodeBuilder::default()
64            .id(1u16)
65            .leaf_count(3u16)
66            .name("child")
67            .original_key("child")
68            .build(NodeKind::LeafHolder)
69            .unwrap();
70
71        let skel = SkeletonBuilder::default()
72            .nodes(vec![root, child])
73            .root_id(Some(0u16))
74            .build()
75            .unwrap();
76
77        let dist = skel.measure_tree_density_distribution();
78        info!("Density distribution = {:?}", dist);
79        // The root is Dispatch => leaf_count()=0, the child is LeafHolder => leaf_count()=3
80        assert_eq!(dist, vec![0u16, 3u16]);
81    }
82}