rust/fpnum/src/lib.rs
changeset 13911 9ae1184886db
parent 13909 cd39e87d7a80
child 13917 ac1801fe51d9
--- a/rust/fpnum/src/lib.rs	Sat Oct 13 18:32:41 2018 +0200
+++ b/rust/fpnum/src/lib.rs	Sat Oct 13 20:01:09 2018 +0300
@@ -270,6 +270,12 @@
     }
 }
 
+macro_rules! fp {
+    (-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
+    ($n: tt / $d: tt) => { FPNum::new($n, $d) };
+    ($n: tt) => { FPNum::from($n) };
+}
+
 /* TODO:
  Distance
  DistanceI
@@ -281,7 +287,7 @@
 #[cfg(test)]
 #[test]
 fn basics() {
-    let n = FPNum::new(15, 2);
+    let n = fp!(15/2);
     assert!(n.is_positive());
     assert!(!n.is_negative());
 
@@ -290,7 +296,7 @@
 
     assert_eq!(-(-n), n);
     assert_eq!((-n).abs(), n);
-    assert_eq!(-n, FPNum::new(-15, 2));
+    assert_eq!(-n, fp!(-15/2));
 
     assert_eq!(n.round(), 7);
     assert_eq!((-n).round(), -7);
@@ -298,8 +304,8 @@
 
 #[test]
 fn zero() {
-    let z = FPNum::from(0);
-    let n = FPNum::new(15, 2);
+    let z = fp!(0);
+    let n = fp!(15/2);
 
     assert!(z.is_zero());
     assert!(z.is_positive());
@@ -310,9 +316,9 @@
 
 #[test]
 fn ord() {
-    let z = FPNum::from(0);;
-    let n1_5 = FPNum::new(3, 2);
-    let n2_25 = FPNum::new(9, 4);
+    let z = fp!(0);
+    let n1_5 = fp!(3/2);
+    let n2_25 = fp!(9/4);
 
     assert!(!(z > z));
     assert!(!(z < z));
@@ -323,12 +329,12 @@
 
 #[test]
 fn arith() {
-    let n1_5 = FPNum::new(3, 2);
-    let n2_25 = FPNum::new(9, 4);
-    let n_0_15 = FPNum::new(-15, 100);
+    let n1_5 = fp!(3/2);
+    let n2_25 = fp!(9/4);
+    let n_0_15 = fp!(-15/100);
 
-    assert_eq!(n1_5 + n1_5, FPNum::from(3));
-    assert_eq!(-n1_5 - n1_5, FPNum::from(-3));
+    assert_eq!(n1_5 + n1_5, fp!(3));
+    assert_eq!(-n1_5 - n1_5, fp!(-3));
 
     assert_eq!(n1_5 * n1_5, n2_25);
     assert_eq!(-n1_5 * -n1_5, n2_25);