Skip to content

Commit

Permalink
Merge pull request #616 from wjhoward/timescale-unit-tests
Browse files Browse the repository at this point in the history
Timescale unit tests
  • Loading branch information
hatoo authored Nov 30, 2024
2 parents 327c070 + 0676d9a commit 82a751d
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/timescale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,82 @@ impl TimeScale {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn assert_timescale_correct_for_seconds_range(
range: [f64; 2],
expected_timescale: TimeScale,
expected_timescale_str: &str,
expected_timescale_as_secs: f64,
) {
for durations in range {
let timescale = TimeScale::from_elapsed(Duration::from_secs_f64(durations));
assert_eq!(timescale, expected_timescale);
assert_eq!(format!("{}", timescale), expected_timescale_str);
assert_eq!(timescale.as_secs_f64(), expected_timescale_as_secs);
}
}

#[test]
fn test_timescale_ranges() {
assert_timescale_correct_for_seconds_range(
[f64::MIN_POSITIVE, 10.0],
TimeScale::Second,
"second",
1.0,
);
assert_timescale_correct_for_seconds_range(
[10.1, 60.0],
TimeScale::TenSeconds,
"10 seconds",
10.0,
);
assert_timescale_correct_for_seconds_range(
[60.1, 600.0],
TimeScale::Minute,
"minute",
60.0,
);
assert_timescale_correct_for_seconds_range(
[600.1, 3600.0],
TimeScale::TenMinutes,
"10 minutes",
600.0,
);
assert_timescale_correct_for_seconds_range(
[3600.1, 31536000.0],
TimeScale::Hour,
"hour",
3600.0,
);
}

#[test]
fn test_timescale_inc() {
let timescale = TimeScale::from_elapsed(Duration::from_secs_f64(0.1));
let timescale_ten_seconds = timescale.inc();
assert_eq!(timescale_ten_seconds, TimeScale::TenSeconds);
let timescale_minute = timescale_ten_seconds.inc();
assert_eq!(timescale_minute, TimeScale::Minute);
let timescale_ten_minutes = timescale_minute.inc();
assert_eq!(timescale_ten_minutes, TimeScale::TenMinutes);
let timescale_hour = timescale_ten_minutes.inc();
assert_eq!(timescale_hour, TimeScale::Hour);
}

#[test]
fn test_timescale_dec() {
let timescale = TimeScale::from_elapsed(Duration::from_secs_f64(31536000.0));
let timescale_ten_minutes = timescale.dec();
assert_eq!(timescale_ten_minutes, TimeScale::TenMinutes);
let timescale_minute = timescale_ten_minutes.dec();
assert_eq!(timescale_minute, TimeScale::Minute);
let timescale_ten_seconds = timescale_minute.dec();
assert_eq!(timescale_ten_seconds, TimeScale::TenSeconds);
let timescale_second = timescale_ten_seconds.dec();
assert_eq!(timescale_second, TimeScale::Second);
}
}

0 comments on commit 82a751d

Please sign in to comment.