Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor epochrs as a module #298

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading