From bc90af936ba57295f32e1afcf6fa81b2917b4ea7 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 20 Feb 2025 09:21:30 -0800 Subject: [PATCH] fix: error during construction if sequence_length is not finite --- Cargo.lock | 2 +- src/sys/table_collection.rs | 21 +++++++++++++++++++++ src/table_collection.rs | 9 +-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 931653c0..b4ae9977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -648,7 +648,7 @@ dependencies = [ [[package]] name = "tskit" -version = "0.15.0-alpha.1" +version = "0.15.0-alpha.2" dependencies = [ "anyhow", "bincode", diff --git a/src/sys/table_collection.rs b/src/sys/table_collection.rs index 81f2ea9e..114c8073 100644 --- a/src/sys/table_collection.rs +++ b/src/sys/table_collection.rs @@ -16,6 +16,12 @@ pub struct TableCollection(TskBox); impl TableCollection { pub fn new(sequence_length: f64) -> Result { + if !sequence_length.is_finite() || sequence_length <= 0.0 { + return Err(TskitError::ValueError { + got: sequence_length.to_string(), + expected: "sequence_length >= 0.0".to_string(), + }); + } let mut tsk = TskBox::new(|tc: *mut tsk_table_collection_t| unsafe { tsk_table_collection_init(tc, 0) })?; @@ -114,3 +120,18 @@ impl TableCollection { self.0.into_raw() } } + +#[test] +fn test_nan_sequence_length() { + assert!(TableCollection::new(f64::NAN).is_err()) +} + +#[test] +fn test_inf_sequence_length() { + assert!(TableCollection::new(f64::INFINITY).is_err()) +} + +#[test] +fn test_neg_inf_sequence_length() { + assert!(TableCollection::new(f64::NEG_INFINITY).is_err()) +} diff --git a/src/table_collection.rs b/src/table_collection.rs index 78c6e154..f8c770d9 100644 --- a/src/table_collection.rs +++ b/src/table_collection.rs @@ -87,14 +87,7 @@ impl TableCollection { /// let tables = tskit::TableCollection::new(-55.0).unwrap(); /// ``` pub fn new>(sequence_length: P) -> Result { - let sequence_length = sequence_length.into(); - if sequence_length <= 0. { - return Err(TskitError::ValueError { - got: f64::from(sequence_length).to_string(), - expected: "sequence_length >= 0.0".to_string(), - }); - } - let mut inner = LLTableCollection::new(sequence_length.into())?; + let mut inner = LLTableCollection::new(sequence_length.into().into())?; let views = crate::table_views::TableViews::new_from_ll_table_collection(&mut inner)?; Ok(Self { inner,