From d47eaa423e4e21fccd29c5e6b882317195cd04f6 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 28 Aug 2024 15:46:49 -0700 Subject: [PATCH] write some ratio tests --- deferred_division/Cargo.lock | 22 ++++++++++++++++++++++ deferred_division/Cargo.toml | 3 +++ deferred_division/src/tests/mod.rs | 4 ++++ deferred_division/src/tests/tests.rs | 15 +++++++++++++++ deferred_division/src/tests/wide.rs | 15 +++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 deferred_division/src/tests/mod.rs create mode 100644 deferred_division/src/tests/tests.rs create mode 100644 deferred_division/src/tests/wide.rs diff --git a/deferred_division/Cargo.lock b/deferred_division/Cargo.lock index 973fcc1..512511f 100644 --- a/deferred_division/Cargo.lock +++ b/deferred_division/Cargo.lock @@ -2,13 +2,35 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bnum" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e31ea183f6ee62ac8b8a8cf7feddd766317adfb13ff469de57ce033efd6a790" + [[package]] name = "deferred_division" version = "0.1.0" dependencies = [ + "fixed_wide", "fixed_wide_traits", ] +[[package]] +name = "fixed_wide" +version = "0.1.0" +dependencies = [ + "bnum", + "fixed_wide_traits", + "typenum", +] + [[package]] name = "fixed_wide_traits" version = "0.1.0" + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" diff --git a/deferred_division/Cargo.toml b/deferred_division/Cargo.toml index 73eafc4..0f2ab79 100644 --- a/deferred_division/Cargo.toml +++ b/deferred_division/Cargo.toml @@ -9,3 +9,6 @@ fixed_wide_traits=["dep:fixed_wide_traits"] [dependencies] fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true } + +[dev-dependencies] +fixed_wide = { version = "0.1.0", path = "../fixed_wide" } diff --git a/deferred_division/src/tests/mod.rs b/deferred_division/src/tests/mod.rs new file mode 100644 index 0000000..a74aed6 --- /dev/null +++ b/deferred_division/src/tests/mod.rs @@ -0,0 +1,4 @@ +mod tests; + +#[cfg(feature="fixed_wide_traits")] +mod wide; diff --git a/deferred_division/src/tests/tests.rs b/deferred_division/src/tests/tests.rs new file mode 100644 index 0000000..e98e832 --- /dev/null +++ b/deferred_division/src/tests/tests.rs @@ -0,0 +1,15 @@ +use crate::ratio::Ratio; + +#[test] +fn ratio(){ + let r=Ratio::new(5,3); + let a=r%1; + assert_eq!(a.num,2); + assert_eq!(a.den,3); + let b=r*2; + assert_eq!(b.num,10); + assert_eq!(b.den,3); + let c=r/2; + assert_eq!(c.num,5); + assert_eq!(c.den,6); +} diff --git a/deferred_division/src/tests/wide.rs b/deferred_division/src/tests/wide.rs new file mode 100644 index 0000000..4246565 --- /dev/null +++ b/deferred_division/src/tests/wide.rs @@ -0,0 +1,15 @@ +use crate::ratio::Ratio; +use fixed_wide_traits::wide::{WideMul,WideDiv}; +use fixed_wide::types::I32F32; +use fixed_wide::types::I64F64; + +#[test] +fn ratio(){ + let r=Ratio::new(I32F32::from(5),I32F32::from(3)); + let a=r.wide_mul(I32F32::from(7)>>2); + assert_eq!(a.num,I64F64::from(7*5)>>2); + assert_eq!(a.den,I32F32::from(3)); + let a=r.wide_div(I32F32::from(7)>>2); + assert_eq!(a.num,I32F32::from(5)); + assert_eq!(a.den,I64F64::from(3*7)>>2); +}