Просмотр исходного кода

consensus: add unit test for bounds on f (#192)

* consensus: add unit test for bounds on f

Added test f_is_bounded to src/validator/consensus/pid.rs

Test by running:
cargo test --release --all-features f_is_bounded

* fix CI error (cargo fmt)

---------

Co-authored-by: y <y>
greptile 3 лет назад
Родитель
Сommit
970698f4a9
1 измененных файлов с 26 добавлено и 0 удалено
  1. 26 0
      src/validator/consensus/pid.rs

+ 26 - 0
src/validator/consensus/pid.rs

@@ -114,3 +114,29 @@ fn calculate_sigmas(f: Float10, total_tokens: Float10) -> (pallas::Base, pallas:
 
     (sigma1, sigma2)
 }
+
+#[cfg(test)]
+mod tests {
+    use super::{calculate_f, Slot};
+    // use super::Float10;
+    use super::{MAX_F, MIN_F};
+
+    #[test]
+    fn f_is_bounded() {
+        // Method: calculate_f takes a slot previous_slot as an argument.
+        // This slot's f value is summed with other low numbers to produce f.
+        // By setting the previous_slot's f to a very large value, we can check
+        // that calculate_f is properly bounding the result of the sum.
+        let mut slot = Slot::default();
+        slot.pid.f = -1_000_000.0;
+        let (f, _) = calculate_f(&slot, 0);
+        assert!(f >= *MIN_F);
+        assert!(f <= *MAX_F);
+
+        let mut slot = Slot::default();
+        slot.pid.f = 1_000_000.0;
+        let (f, _) = calculate_f(&slot, 0);
+        assert!(f >= *MIN_F);
+        assert!(f <= *MAX_F);
+    }
+}