From be31365fb1f54747d7b2ec1e7c53420da02a560f Mon Sep 17 00:00:00 2001 From: Mitsuharu Seki Date: Tue, 10 Oct 2017 07:49:59 +0900 Subject: [PATCH 1/2] Add support for num-complex --- .travis.yml | 10 +++++----- Cargo.toml | 4 ++++ README.md | 11 +++++++++++ src/complex.rs | 17 +++++++++++++++++ src/lib.rs | 6 ++++++ tests/lib.rs | 24 ++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/complex.rs diff --git a/.travis.yml b/.travis.yml index 39d6711..0a52914 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,17 +46,17 @@ before_script: fi script: -- cargo build +- cargo build --features=num-complex - | if [ "${TRAVIS_OS_NAME}" = "linux" ]; then - cargo kcov --coveralls + cargo kcov --coveralls --features=num-complex else - cargo test + cargo test --features=num-complex fi -- cargo test --release +- cargo test --release --features=num-complex - | if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then - cargo bench --verbose + cargo bench --verbose --features=num-complex fi - | if [ "${TRAVIS_OS_NAME}" = "linux" ]; then diff --git a/Cargo.toml b/Cargo.toml index 8c5b789..9f6df37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,7 @@ documentation = "https://docs.rs/nearly_eq/" name = "nearly_eq" bench = false test = true + +[dependencies.num-complex] +optional = true +version = ">=0.0.0" diff --git a/README.md b/README.md index a8800ae..99b01bd 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,14 @@ [![Coverage Status](https://coveralls.io/repos/github/chalharu/rust-nearly-eq/badge.svg)](https://coveralls.io/github/chalharu/rust-nearly-eq) rust crate: nearly equal + + +## If you are using the num-complex crate +To depend on the `nearly_eq` crate with this feature enabled, put the following in +your project's `Cargo.toml` file: + +```toml +[dependencies.nearly_eq] +features = ["num-complex"] +version = ... # Whichever version you are using +``` \ No newline at end of file diff --git a/src/complex.rs b/src/complex.rs new file mode 100644 index 0000000..96b7499 --- /dev/null +++ b/src/complex.rs @@ -0,0 +1,17 @@ +//! # Licensing +//! This Source Code is subject to the terms of the Mozilla Public License +//! version 2.0 (the "License"). You can obtain a copy of the License at +//! http://mozilla.org/MPL/2.0/. + +use num_complex::Complex; +use NearlyEq; + +impl> NearlyEq, B> for Complex { + fn eps() -> B { + C::eps() + } + + fn eq(&self, other: &Complex, eps: &B) -> bool { + self.re.eq(&other.re, eps) && self.im.eq(&other.im, eps) + } +} diff --git a/src/lib.rs b/src/lib.rs index 2442201..7427e5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,15 @@ //! version 2.0 (the "License"). You can obtain a copy of the License at //! http://mozilla.org/MPL/2.0/. +#[cfg(feature = "num-complex")] +extern crate num_complex; + #[macro_use] mod assert; +#[cfg(feature = "num-complex")] +mod complex; + /// Trait for nearly equality comparisons. pub trait NearlyEq { fn eps() -> Diff; diff --git a/tests/lib.rs b/tests/lib.rs index 124e541..000e3dc 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,6 +1,12 @@ +#[cfg(feature = "num-complex")] +extern crate num_complex; + #[macro_use] extern crate nearly_eq; +#[cfg(feature = "num-complex")] +use num_complex::Complex; + #[test] fn it_should_not_panic_if_values_are_nearly_equal() { assert_nearly_eq!(8f32, 8f32 + 1e-7); @@ -88,3 +94,21 @@ fn bad_compare_with_array() { let right = [1f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]; assert_nearly_eq!(left, right); } + + +#[test] +#[cfg(feature = "num-complex")] +fn compare_with_complex() { + let left = Complex::new(1.0f64, 0.0); + let right = Complex::new(1.0f64, 1e-12); + assert_nearly_eq!(left, right); +} + +#[test] +#[should_panic] +#[cfg(feature = "num-complex")] +fn bad_compare_with_complex() { + let left = Complex::new(1.0f64, 0.0); + let right = Complex::new(1.0f64, 1e-8); + assert_nearly_eq!(left, right); +} From 74ba06b3526074e0fc2bf0c83297fba6863d12f6 Mon Sep 17 00:00:00 2001 From: Mitsuharu Seki Date: Tue, 10 Oct 2017 07:51:27 +0900 Subject: [PATCH 2/2] modify version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9f6df37..1012d61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nearly_eq" -version = "0.1.1" +version = "0.1.2" authors = ["Mitsuharu Seki "] repository = "https://github.com/chalharu/rust-nearly-eq" keywords = ["assert"]