of0.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. use super::parents::*;
  2. use super::rank::Rank;
  3. pub struct ObjectiveFunction0;
  4. pub(crate) trait ObjectiveFunction {
  5. const OCP: u16;
  6. /// Return the new calculated Rank, based on information from the parent.
  7. fn rank(current_rank: Rank, parent_rank: Rank) -> Rank;
  8. /// Return the preferred parent from a given parent set.
  9. fn preferred_parent(parent_set: &ParentSet) -> Option<&Parent>;
  10. }
  11. impl ObjectiveFunction0 {
  12. const OCP: u16 = 0;
  13. const RANK_STRETCH: u16 = 0;
  14. const RANK_FACTOR: u16 = 1;
  15. const RANK_STEP: u16 = 3;
  16. fn rank_increase(parent_rank: Rank) -> u16 {
  17. (Self::RANK_FACTOR * Self::RANK_STEP + Self::RANK_STRETCH)
  18. * parent_rank.min_hop_rank_increase
  19. }
  20. }
  21. impl ObjectiveFunction for ObjectiveFunction0 {
  22. const OCP: u16 = 0;
  23. fn rank(_: Rank, parent_rank: Rank) -> Rank {
  24. assert_ne!(parent_rank, Rank::INFINITE);
  25. Rank::new(
  26. parent_rank.value + Self::rank_increase(parent_rank),
  27. parent_rank.min_hop_rank_increase,
  28. )
  29. }
  30. fn preferred_parent(parent_set: &ParentSet) -> Option<&Parent> {
  31. let mut pref_parent: Option<&Parent> = None;
  32. for parent in parent_set.parents() {
  33. if pref_parent.is_none() || parent.rank() < pref_parent.unwrap().rank() {
  34. pref_parent = Some(parent);
  35. }
  36. }
  37. pref_parent
  38. }
  39. }
  40. #[cfg(test)]
  41. mod tests {
  42. use crate::iface::rpl::consts::DEFAULT_MIN_HOP_RANK_INCREASE;
  43. use super::*;
  44. #[test]
  45. fn rank_increase() {
  46. // 256 (root) + 3 * 256
  47. assert_eq!(
  48. ObjectiveFunction0::rank(Rank::INFINITE, Rank::ROOT),
  49. Rank::new(256 + 3 * 256, DEFAULT_MIN_HOP_RANK_INCREASE)
  50. );
  51. // 1024 + 3 * 256
  52. assert_eq!(
  53. ObjectiveFunction0::rank(
  54. Rank::INFINITE,
  55. Rank::new(1024, DEFAULT_MIN_HOP_RANK_INCREASE)
  56. ),
  57. Rank::new(1024 + 3 * 256, DEFAULT_MIN_HOP_RANK_INCREASE)
  58. );
  59. }
  60. #[test]
  61. #[should_panic]
  62. fn rank_increase_infinite() {
  63. assert_eq!(
  64. ObjectiveFunction0::rank(Rank::INFINITE, Rank::INFINITE),
  65. Rank::INFINITE
  66. );
  67. }
  68. #[test]
  69. fn empty_set() {
  70. assert_eq!(
  71. ObjectiveFunction0::preferred_parent(&ParentSet::default()),
  72. None
  73. );
  74. }
  75. #[test]
  76. fn non_empty_set() {
  77. use crate::wire::Ipv6Address;
  78. let mut parents = ParentSet::default();
  79. parents.add(Parent::new(
  80. Ipv6Address::default(),
  81. 0,
  82. Rank::ROOT,
  83. Default::default(),
  84. Ipv6Address::default(),
  85. ));
  86. let mut address = Ipv6Address::default();
  87. address.0[15] = 1;
  88. parents.add(Parent::new(
  89. address,
  90. 0,
  91. Rank::new(1024, DEFAULT_MIN_HOP_RANK_INCREASE),
  92. Default::default(),
  93. Ipv6Address::default(),
  94. ));
  95. assert_eq!(
  96. ObjectiveFunction0::preferred_parent(&parents),
  97. Some(&Parent::new(
  98. Ipv6Address::default(),
  99. 0,
  100. Rank::ROOT,
  101. Default::default(),
  102. Ipv6Address::default(),
  103. ))
  104. );
  105. }
  106. }