// Copyright 2017 Kyle Mayes // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. extern crate dice; use std::ops::{Add, Div, Mul, Sub}; use dice::{Ratio}; macro_rules! assert_binary_eq { ($left:expr, $right:expr, $output:expr, $op:ident, $checked_op:ident) => ({ let left = Ratio::new($left.0, $left.1); let right = Ratio::new($right.0, $right.1); let output = Ratio::new($output.0, $output.1); assert_eq!(left.$op(right), output); assert_eq!(left.$checked_op(right), Some(output)); }); } #[test] fn test_ratio() { let ratio = Ratio::new(7, 22); assert_eq!(ratio.percentage(0), "31%"); assert_eq!(ratio.percentage(1), "31.8%"); assert_eq!(ratio.percentage(2), "31.81%"); assert_eq!(ratio.percentage(3), "31.818%"); assert_eq!(ratio.real(0), "0"); assert_eq!(ratio.real(1), "0.3"); assert_eq!(ratio.real(2), "0.31"); assert_eq!(ratio.real(3), "0.318"); assert_eq!(ratio.real(4), "0.3181"); assert_eq!(ratio.real(5), "0.31818"); let ratio = Ratio::new(-7, 22); assert_eq!(ratio.percentage(0), "-31%"); assert_eq!(ratio.percentage(1), "-31.8%"); assert_eq!(ratio.percentage(2), "-31.81%"); assert_eq!(ratio.percentage(3), "-31.818%"); assert_eq!(ratio.real(0), "-0"); assert_eq!(ratio.real(1), "-0.3"); assert_eq!(ratio.real(2), "-0.31"); assert_eq!(ratio.real(3), "-0.318"); assert_eq!(ratio.real(4), "-0.3181"); assert_eq!(ratio.real(5), "-0.31818"); let ratio = Ratio::new(22, 7); assert_eq!(ratio.percentage(0), "314%"); assert_eq!(ratio.percentage(1), "314.2%"); assert_eq!(ratio.percentage(2), "314.28%"); assert_eq!(ratio.percentage(3), "314.285%"); assert_eq!(ratio.real(0), "3"); assert_eq!(ratio.real(1), "3.1"); assert_eq!(ratio.real(2), "3.14"); assert_eq!(ratio.real(3), "3.142"); assert_eq!(ratio.real(4), "3.1428"); assert_eq!(ratio.real(5), "3.14285"); let ratio = Ratio::new(-22, 7); assert_eq!(ratio.percentage(0), "-314%"); assert_eq!(ratio.percentage(1), "-314.2%"); assert_eq!(ratio.percentage(2), "-314.28%"); assert_eq!(ratio.percentage(3), "-314.285%"); assert_eq!(ratio.real(0), "-3"); assert_eq!(ratio.real(1), "-3.1"); assert_eq!(ratio.real(2), "-3.14"); assert_eq!(ratio.real(3), "-3.142"); assert_eq!(ratio.real(4), "-3.1428"); assert_eq!(ratio.real(5), "-3.14285"); assert!(Ratio::new(2, 7) < Ratio::new(3, 7)); assert!(Ratio::new(-2, 7) > Ratio::new(-3, 7)); assert!(Ratio::new(7, 2) > Ratio::new(7, 3)); assert!(Ratio::new(-7, 2) < Ratio::new(-7, 3)); assert!(Ratio::new(3, 7) < Ratio::new(4, 9)); assert!(Ratio::new(-3, 7) > Ratio::new(-4, 9)); assert!(Ratio::new(4, 9) > Ratio::new(3, 7)); assert!(Ratio::new(-4, 9) < Ratio::new(-3, 7)); assert_binary_eq!((3, 7), (7, 23), (118, 161), add, checked_add); assert_binary_eq!((7, 23), (3, 7), (118, 161), add, checked_add); assert_binary_eq!((3, 7), (7, 23), (69, 49), div, checked_div); assert_binary_eq!((7, 23), (3, 7), (49, 69), div, checked_div); assert_binary_eq!((3, 7), (7, 23), (3, 23), mul, checked_mul); assert_binary_eq!((7, 23), (3, 7), (3, 23), mul, checked_mul); assert_binary_eq!((3, 7), (7, 23), (20, 161), sub, checked_sub); assert_binary_eq!((7, 23), (3, 7), (-20, 161), sub, checked_sub); }