pub const MILLIS_PER_SEC: u64 = 1_000;
pub const MICROS_PER_MILLI: u64 = 1_000;
pub const MICROS_PER_SEC: u64 = MILLIS_PER_SEC * MICROS_PER_MILLI;
pub const NANOS_PER_MICRO: u64 = 1_000;
pub const NANOS_PER_MILLI: u64 = NANOS_PER_MICRO * MICROS_PER_MILLI;
pub const NANOS_PER_SEC: u64 = NANOS_PER_MILLI * MILLIS_PER_SEC;
pub const BITS_PER_BYTE: u32 = u8::BITS;
pub const KIB: u64 = 1 << 10;
pub const MIB: u64 = KIB << 10;
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
(a, b) = (b, a % b);
}
a.max(1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gcd_reduces_and_is_never_zero() {
assert_eq!(gcd(48, 18), 6);
assert_eq!(gcd(7, 0), 7);
assert_eq!(gcd(0, 0), 1);
assert_eq!(gcd(90_000, 1_001), 1);
}
#[test]
fn the_ratios_are_the_decimal_units() {
assert_eq!(MICROS_PER_SEC, 1_000_000);
assert_eq!(NANOS_PER_MILLI, 1_000_000);
assert_eq!(NANOS_PER_SEC, 1_000_000_000);
assert_eq!((BITS_PER_BYTE, KIB, MIB), (8, 1_024, 1_048_576));
assert_eq!(
MICROS_PER_SEC,
u64::from(crate::timescale::Timescale::MICROS.get())
);
}
}