From 1d0244520c9fc0493023a0f298fc0d89451c5cbc Mon Sep 17 00:00:00 2001 From: statiolake Date: Fri, 15 Nov 2019 22:55:51 +0900 Subject: [PATCH 1/2] Add `num-traits` to Cargo.toml The `num-derive` requires `num-traits` to be explicitly included in Cargo.toml. (cf. https://github.com/rust-num/num-derive/issues/34) This issue may be fixed in https://github.com/rust-num/num-derive/pull/35. --- Cargo.lock | 1 + Cargo.toml | 2 ++ tests/test_num_derive.rs | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/test_num_derive.rs diff --git a/Cargo.lock b/Cargo.lock index cb1190f..f3be31c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,7 @@ dependencies = [ "nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "permutohedron 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 0adb0d8..a95eba1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ default = ["jemalloc"] # 多バイト長整数、分数、複素数など num = "=0.2.0" num-derive = "=0.3.0" +# num-derive が依存するため必要 +num-traits = "=0.2.9" # 多次元配列 ndarray = "=0.13.0" diff --git a/tests/test_num_derive.rs b/tests/test_num_derive.rs new file mode 100644 index 0000000..03d1754 --- /dev/null +++ b/tests/test_num_derive.rs @@ -0,0 +1,23 @@ +use num_derive::{FromPrimitive, Num, NumCast, NumOps, One, ToPrimitive, Zero}; + +#[derive( + Debug, + Eq, + PartialEq, + PartialOrd, + Ord, + FromPrimitive, + ToPrimitive, + One, + Zero, + Num, + NumCast, + NumOps, +)] +struct Weight(i32); + +fn main() { + let w1 = Weight(3); + let w2 = Weight(4); + println!("{:?}", w1 + w2); // => "Weight(7)" +} From 0d59e70e3e58aa6698d743a59f988cae1111cf6f Mon Sep 17 00:00:00 2001 From: statiolake Date: Sat, 16 Nov 2019 02:20:33 +0900 Subject: [PATCH 2/2] Fix test for num_derive There was a test file, but actually no test in the file! ;) --- tests/test_num_derive.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tests/test_num_derive.rs b/tests/test_num_derive.rs index 03d1754..ac5a14b 100644 --- a/tests/test_num_derive.rs +++ b/tests/test_num_derive.rs @@ -2,22 +2,36 @@ use num_derive::{FromPrimitive, Num, NumCast, NumOps, One, ToPrimitive, Zero}; #[derive( Debug, - Eq, + Clone, + Copy, PartialEq, + Eq, PartialOrd, Ord, FromPrimitive, ToPrimitive, - One, Zero, + One, Num, NumCast, NumOps, )] struct Weight(i32); -fn main() { - let w1 = Weight(3); +#[test] +fn check_ops() { + let w1 = Weight(7); let w2 = Weight(4); - println!("{:?}", w1 + w2); // => "Weight(7)" + + assert_eq!(w1 + w2, Weight(11)); + assert_eq!(w1 - w2, Weight(3)); + assert_eq!(w1 * w2, Weight(28)); + assert_eq!(w1 / w2, Weight(1)); + assert_eq!(w1 % w2, Weight(3)); + assert!(w1 > w2); + assert!(w2 < w1); + assert!(w1 >= w1); + assert!(w1 <= w1); + assert!(w1 != w2); + assert!(w1 == w1); }