|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#[cfg(test)] |
| 16 | +use fixed_bigint::FixedUInt; |
| 17 | +use num_integer::Integer; |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn test_evenodd() { |
| 21 | + fn even_odd<T: num_integer::Integer + From<u8>>() { |
| 22 | + let tests = [(0u8, true), (1u8, false), (2u8, true), (255u8, false)]; |
| 23 | + for &(num, even) in &tests { |
| 24 | + let f: T = num.into(); |
| 25 | + assert_eq!(f.is_even(), even); |
| 26 | + assert_eq!(f.is_odd(), !even); |
| 27 | + } |
| 28 | + } |
| 29 | + even_odd::<u8>(); |
| 30 | + even_odd::<FixedUInt<u8, 1>>(); |
| 31 | + even_odd::<FixedUInt<u8, 2>>(); |
| 32 | + even_odd::<FixedUInt<u16, 1>>(); |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn test_divides() { |
| 37 | + fn divides<T: num_integer::Integer + From<u8>>() { |
| 38 | + let tests = [(6u8, 3u8, true), (8, 2, true), (8, 1, true), (17, 2, false)]; |
| 39 | + for &(multiple, multiplier, expected) in &tests { |
| 40 | + assert_eq!(multiple.is_multiple_of(&multiplier), expected); |
| 41 | + assert_eq!(multiple.divides(&multiplier), expected); |
| 42 | + } |
| 43 | + let divrem = [ |
| 44 | + (6u8, 3u8, 2u8, 0u8), |
| 45 | + (8, 2, 4, 0), |
| 46 | + (7, 2, 3, 1), |
| 47 | + (89, 13, 6, 11), |
| 48 | + ]; |
| 49 | + for &(multiple, divider, div, rem) in &divrem { |
| 50 | + let (divres, remres) = multiple.div_rem(÷r); |
| 51 | + assert_eq!(divres, div); |
| 52 | + assert_eq!(remres, rem); |
| 53 | + |
| 54 | + assert_eq!(multiple.div_floor(÷r), divres); |
| 55 | + assert_eq!(multiple.mod_floor(÷r), remres); |
| 56 | + } |
| 57 | + } |
| 58 | + divides::<u8>(); |
| 59 | + divides::<FixedUInt<u8, 1>>(); |
| 60 | + divides::<FixedUInt<u8, 2>>(); |
| 61 | + divides::<FixedUInt<u16, 1>>(); |
| 62 | +} |
| 63 | + |
| 64 | +// TODO: Test GCD / LCM |
| 65 | +#[test] |
| 66 | +fn test_gcd_lcm() { |
| 67 | + fn gcd_lcm<T: num_integer::Integer + From<u8>>() { |
| 68 | + let gcd_tests = [ |
| 69 | + (8u8, 12u8, 4u8), |
| 70 | + (1, 1, 1), |
| 71 | + (100, 100, 100), |
| 72 | + (99, 98, 1), |
| 73 | + (99, 90, 9), |
| 74 | + ]; |
| 75 | + for &(a, b, expected) in &gcd_tests { |
| 76 | + assert_eq!(a.gcd(&b), expected); |
| 77 | + } |
| 78 | + let lcm_tests = [ |
| 79 | + (10u8, 2u8, 10u8), |
| 80 | + (1, 1, 1), |
| 81 | + (4, 6, 12), |
| 82 | + (7, 12, 84), |
| 83 | + (14, 12, 84), |
| 84 | + (255, 255, 255), |
| 85 | + ]; |
| 86 | + for &(a, b, expected) in &lcm_tests { |
| 87 | + assert_eq!(a.lcm(&b), expected); |
| 88 | + } |
| 89 | + } |
| 90 | + gcd_lcm::<u8>(); |
| 91 | + gcd_lcm::<FixedUInt<u8, 1>>(); |
| 92 | + gcd_lcm::<FixedUInt<u8, 2>>(); |
| 93 | + gcd_lcm::<FixedUInt<u16, 1>>(); |
| 94 | +} |
0 commit comments