-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement num_integer::Integer (#20)
* Add num_integer dep #19 * Quick implementation of Integer easy parts #19 * Add gcd/lcm, tests for Integer divisions * Add basic gcd/lcm tests * Bump version number
- Loading branch information
Showing
4 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "fixed-bigint" | ||
version = "0.1.5" | ||
version = "0.1.6" | ||
authors = ["kaidokert <[email protected]>"] | ||
documentation = "https://docs.rs/fixed-bigint" | ||
edition = "2018" | ||
|
@@ -20,6 +20,10 @@ exclude = ["/.github/*"] | |
version = "0.2.14" | ||
default-features = false | ||
|
||
[dependencies.num-integer] | ||
version = "0.1.44" | ||
default-features = false | ||
|
||
[profile.dev] | ||
opt-level = 0 | ||
debug = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// 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. | ||
|
||
#[cfg(test)] | ||
use fixed_bigint::FixedUInt; | ||
use num_integer::Integer; | ||
|
||
#[test] | ||
fn test_evenodd() { | ||
fn even_odd<T: num_integer::Integer + From<u8>>() { | ||
let tests = [(0u8, true), (1u8, false), (2u8, true), (255u8, false)]; | ||
for &(num, even) in &tests { | ||
let f: T = num.into(); | ||
assert_eq!(f.is_even(), even); | ||
assert_eq!(f.is_odd(), !even); | ||
} | ||
} | ||
even_odd::<u8>(); | ||
even_odd::<FixedUInt<u8, 1>>(); | ||
even_odd::<FixedUInt<u8, 2>>(); | ||
even_odd::<FixedUInt<u16, 1>>(); | ||
} | ||
|
||
#[test] | ||
fn test_divides() { | ||
fn divides<T: num_integer::Integer + From<u8>>() { | ||
let tests = [(6u8, 3u8, true), (8, 2, true), (8, 1, true), (17, 2, false)]; | ||
for &(multiple, multiplier, expected) in &tests { | ||
assert_eq!(multiple.is_multiple_of(&multiplier), expected); | ||
assert_eq!(multiple.divides(&multiplier), expected); | ||
} | ||
let divrem = [ | ||
(6u8, 3u8, 2u8, 0u8), | ||
(8, 2, 4, 0), | ||
(7, 2, 3, 1), | ||
(89, 13, 6, 11), | ||
]; | ||
for &(multiple, divider, div, rem) in &divrem { | ||
let (divres, remres) = multiple.div_rem(÷r); | ||
assert_eq!(divres, div); | ||
assert_eq!(remres, rem); | ||
|
||
assert_eq!(multiple.div_floor(÷r), divres); | ||
assert_eq!(multiple.mod_floor(÷r), remres); | ||
} | ||
} | ||
divides::<u8>(); | ||
divides::<FixedUInt<u8, 1>>(); | ||
divides::<FixedUInt<u8, 2>>(); | ||
divides::<FixedUInt<u16, 1>>(); | ||
} | ||
|
||
// TODO: Test GCD / LCM | ||
#[test] | ||
fn test_gcd_lcm() { | ||
fn gcd_lcm<T: num_integer::Integer + From<u8>>() { | ||
let gcd_tests = [ | ||
(8u8, 12u8, 4u8), | ||
(1, 1, 1), | ||
(100, 100, 100), | ||
(99, 98, 1), | ||
(99, 90, 9), | ||
]; | ||
for &(a, b, expected) in &gcd_tests { | ||
assert_eq!(a.gcd(&b), expected); | ||
} | ||
let lcm_tests = [ | ||
(10u8, 2u8, 10u8), | ||
(1, 1, 1), | ||
(4, 6, 12), | ||
(7, 12, 84), | ||
(14, 12, 84), | ||
(255, 255, 255), | ||
]; | ||
for &(a, b, expected) in &lcm_tests { | ||
assert_eq!(a.lcm(&b), expected); | ||
} | ||
} | ||
gcd_lcm::<u8>(); | ||
gcd_lcm::<FixedUInt<u8, 1>>(); | ||
gcd_lcm::<FixedUInt<u8, 2>>(); | ||
gcd_lcm::<FixedUInt<u16, 1>>(); | ||
} |