Skip to content

Commit

Permalink
Refactor epoch.rs as a module (#298)
Browse files Browse the repository at this point in the history
* Start refactor of epoch.rs

* Moved Epoch Python to its own file

* Move epoch tests to kani

* Split epoch.rs into epoch module and across several files

* Fix J1900 and J2000 ref epochs, but still need more work for 282
  • Loading branch information
ChristopherRabotin authored Apr 25, 2024
1 parent 926214e commit a9d214f
Show file tree
Hide file tree
Showing 17 changed files with 3,689 additions and 3,633 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde = { version = "1.0.155", optional = true }
serde_derive = { version = "1.0.155", optional = true }
pyo3 = { version = "0.21.1", features = [
"extension-module",
"inventory",
"multiple-pymethods",
], optional = true }
num-traits = { version = "0.2.15", default-features = false, features = [
"libm",
Expand Down
File renamed without changes.
109 changes: 57 additions & 52 deletions src/duration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use pyo3::prelude::pyclass;
use num_traits::Float;

#[cfg(kani)]
mod kani;
mod kani_verif;

pub const DAYS_PER_CENTURY_U64: u64 = 36_525;
pub const NANOSECONDS_PER_MICROSECOND: u64 = 1_000;
Expand Down Expand Up @@ -723,55 +723,60 @@ impl PartialOrd<Unit> for Duration {
}
}

#[test]
#[cfg(feature = "serde")]
fn test_serdes() {
let dt = Duration::from_seconds(10.1);
let content = r#"{"centuries":0,"nanoseconds":10100000000}"#;
assert_eq!(content, serde_json::to_string(&dt).unwrap());
let parsed: Duration = serde_json::from_str(content).unwrap();
assert_eq!(dt, parsed);
}

#[test]
fn test_bounds() {
let min = Duration::MIN;
assert_eq!(min.centuries, i16::MIN);
assert_eq!(min.nanoseconds, 0);

let max = Duration::MAX;
assert_eq!(max.centuries, i16::MAX);
assert_eq!(max.nanoseconds, NANOSECONDS_PER_CENTURY);

let min_p = Duration::MIN_POSITIVE;
assert_eq!(min_p.centuries, 0);
assert_eq!(min_p.nanoseconds, 1);

let min_n = Duration::MIN_NEGATIVE;
assert_eq!(min_n.centuries, -1);
assert_eq!(min_n.nanoseconds, NANOSECONDS_PER_CENTURY - 1);

let min_n1 = Duration::MIN - 1 * Unit::Nanosecond;
assert_eq!(min_n1, Duration::MIN);

let max_n1 = Duration::MAX - 1 * Unit::Nanosecond;
assert_eq!(max_n1.centuries, i16::MAX);
assert_eq!(max_n1.nanoseconds, NANOSECONDS_PER_CENTURY - 1);
}

#[test]
fn test_decompose() {
let d = -73000.days();
let out_days = d.to_unit(Unit::Day);
assert_eq!(out_days, -73000.0);
let (sign, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) =
d.decompose();
assert_eq!(sign, -1);
assert_eq!(days, 73000);
assert_eq!(hours, 0);
assert_eq!(minutes, 0);
assert_eq!(seconds, 0);
assert_eq!(milliseconds, 0);
assert_eq!(microseconds, 0);
assert_eq!(nanoseconds, 0);
#[cfg(test)]
mod ut_duration {
use super::{Duration, TimeUnits, Unit, NANOSECONDS_PER_CENTURY};

#[test]
#[cfg(feature = "serde")]
fn test_serdes() {
let dt = Duration::from_seconds(10.1);
let content = r#"{"centuries":0,"nanoseconds":10100000000}"#;
assert_eq!(content, serde_json::to_string(&dt).unwrap());
let parsed: Duration = serde_json::from_str(content).unwrap();
assert_eq!(dt, parsed);
}

#[test]
fn test_bounds() {
let min = Duration::MIN;
assert_eq!(min.centuries, i16::MIN);
assert_eq!(min.nanoseconds, 0);

let max = Duration::MAX;
assert_eq!(max.centuries, i16::MAX);
assert_eq!(max.nanoseconds, NANOSECONDS_PER_CENTURY);

let min_p = Duration::MIN_POSITIVE;
assert_eq!(min_p.centuries, 0);
assert_eq!(min_p.nanoseconds, 1);

let min_n = Duration::MIN_NEGATIVE;
assert_eq!(min_n.centuries, -1);
assert_eq!(min_n.nanoseconds, NANOSECONDS_PER_CENTURY - 1);

let min_n1 = Duration::MIN - 1 * Unit::Nanosecond;
assert_eq!(min_n1, Duration::MIN);

let max_n1 = Duration::MAX - 1 * Unit::Nanosecond;
assert_eq!(max_n1.centuries, i16::MAX);
assert_eq!(max_n1.nanoseconds, NANOSECONDS_PER_CENTURY - 1);
}

#[test]
fn test_decompose() {
let d = -73000.days();
let out_days = d.to_unit(Unit::Day);
assert_eq!(out_days, -73000.0);
let (sign, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) =
d.decompose();
assert_eq!(sign, -1);
assert_eq!(days, 73000);
assert_eq!(hours, 0);
assert_eq!(minutes, 0);
assert_eq!(seconds, 0);
assert_eq!(milliseconds, 0);
assert_eq!(microseconds, 0);
assert_eq!(nanoseconds, 0);
}
}
2 changes: 1 addition & 1 deletion src/duration/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Documentation: https://nyxspace.com/
*/

// Here lives all of the implementations that are only built with the std flag
// Here lives all of the implementations that are only built with the pyhon feature.

use super::{Duration, Unit};

Expand Down
Loading

0 comments on commit a9d214f

Please sign in to comment.