Skip to content

Commit

Permalink
Improve readability and add some tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
thomask7b committed Nov 12, 2024
1 parent b865859 commit 2cf4ab7
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/bitcoin/d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
config,
descriptors::LianaDescriptor,
};
use utils::{block_before_date};
use utils::block_before_date;

use std::{
cmp,
Expand Down Expand Up @@ -1274,7 +1274,7 @@ impl SyncProgress {

if progress == 1.0 && self.blocks != self.headers {
// Don't return 100% progress until syncing is fully complete.
0.999
0.9999
} else {
progress
}
Expand Down Expand Up @@ -1571,18 +1571,33 @@ impl From<&&Json> for MempoolEntryFees {
mod tests {
use super::*;

fn sync_progress(percentage: f64, fully_sync: bool) -> SyncProgress {
let (headers, blocks) = if fully_sync {
(1_000, 1_000)
} else {
(1_000, 999)
};

SyncProgress {
percentage,
headers,
blocks,
}
}

#[test]
fn test_rounded_up_progress() {
assert_eq!(SyncProgress::new(0.6, 1_000, 1_000).rounded_up_progress(), 0.6);
assert_eq!(SyncProgress::new(0.67891, 1_000, 1_000).rounded_up_progress(), 0.6789);
assert_eq!(SyncProgress::new(0.98, 1_000, 1_000).rounded_up_progress(), 0.98);
assert_eq!(SyncProgress::new(0.998, 1_000, 1_000).rounded_up_progress(), 0.998);
assert_eq!(SyncProgress::new(0.9476, 1_000, 1_000).rounded_up_progress(), 0.9476);
assert_eq!(SyncProgress::new(0.9998, 1_000, 1_000).rounded_up_progress(), 0.9998);
assert_eq!(SyncProgress::new(0.99998, 1_000, 1_000).rounded_up_progress(), 1.0);
assert_eq!(SyncProgress::new(0.9991, 1_000, 1_000).rounded_up_progress(), 0.9991);
assert_eq!(SyncProgress::new(0.99991, 1_000, 1_000).rounded_up_progress(), 1.0);
assert_eq!(SyncProgress::new(1.2, 1_000, 1_000).rounded_up_progress(), 1.0);
assert_eq!(SyncProgress::new(1.0, 1_000, 999).rounded_up_progress(), 0.999);
assert_eq!(sync_progress(0.6, false).rounded_up_progress(), 0.6);
assert_eq!(sync_progress(0.67891, false).rounded_up_progress(), 0.6789);
assert_eq!(sync_progress(0.98, false).rounded_up_progress(), 0.98);
assert_eq!(sync_progress(0.998, false).rounded_up_progress(), 0.998);
assert_eq!(sync_progress(0.9476, false).rounded_up_progress(), 0.9476);
assert_eq!(sync_progress(0.9998, false).rounded_up_progress(), 0.9998);
assert_eq!(sync_progress(0.99998, false).rounded_up_progress(), 0.9999);
assert_eq!(sync_progress(0.9991, false).rounded_up_progress(), 0.9991);
assert_eq!(sync_progress(0.99991, false).rounded_up_progress(), 0.9999);
assert_eq!(sync_progress(1.2, true).rounded_up_progress(), 1.0);
assert_eq!(sync_progress(1.0, false).rounded_up_progress(), 0.9999);
assert_eq!(sync_progress(1.0, true).rounded_up_progress(), 1.0);
}
}

0 comments on commit 2cf4ab7

Please sign in to comment.