From 074f2bf662cef5a8c71131c3d4395b913af733eb Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 24 Feb 2020 13:49:09 -0500 Subject: [PATCH] Update docs, more tests --- benches/stock_functions.rs | 10 +++-- src/lib.rs | 77 ++++++++++++++++++++++---------------- src/num.rs | 2 + tests/fractions.rs | 6 +++ 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/benches/stock_functions.rs b/benches/stock_functions.rs index f920b18..5314200 100644 --- a/benches/stock_functions.rs +++ b/benches/stock_functions.rs @@ -53,11 +53,13 @@ fn bench_gcd(c: &mut Criterion) { for input in [(small_1, small), (med_1, med), (max_1, max)].iter() { let param = format!("{},{}", input.0, input.1); - group.bench_with_input(BenchmarkId::new("Binary", ¶m), input, |b, input| { - b.iter(|| u128::gcd(black_box(input.0), black_box(input.1))) + group.bench_with_input(BenchmarkId::new("Binary", ¶m), input, |bench, input| { + let (a, b) = input; + bench.iter(|| u128::gcd(black_box(*a), black_box(*b))) }); - group.bench_with_input(BenchmarkId::new("Euclid", ¶m), input, |b, input| { - b.iter(|| u128::e_gcd(black_box(input.0), black_box(input.1))) + group.bench_with_input(BenchmarkId::new("Euclid", ¶m), input, |bench, input| { + let (a, b) = input; + bench.iter(|| u128::e_gcd(black_box(*a), black_box(*b))) }); } group.finish(); diff --git a/src/lib.rs b/src/lib.rs index d36cee8..36a4d84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,19 +9,19 @@ pub mod num; pub mod rational; /// Calculate a number in the fibonacci sequence, -/// using a lookup table for better worst-case performance. +/// using recursion and a lookup table /// /// Can calculate up to 186 using native unsigned 128 bit integers. /// /// Example: /// ```rust -/// use rusty_numbers::fibonacci; +/// use rusty_numbers::mem_fibonacci; /// -/// let valid = fibonacci(45); // Some(1134903170) -/// # assert_eq!(1134903170, fibonacci(45).unwrap()); +/// let valid = mem_fibonacci(45); // Some(1134903170) +/// # assert_eq!(1134903170, mem_fibonacci(45).unwrap()); /// # assert!(valid.is_some()); /// -/// let invalid = fibonacci(187); // None +/// let invalid = mem_fibonacci(187); // None /// # assert!(invalid.is_none()); /// ``` #[inline] @@ -31,34 +31,34 @@ pub fn mem_fibonacci(n: usize) -> Option { table[1] = 1; table[2] = 1; - _mem_fibonacci(n, &mut table) -} - -/// Actual calculating function for `fibonacci` -#[inline] -fn _mem_fibonacci(n: usize, table: &mut [u128]) -> Option { - if n < 2 { - // The first values are predefined. - return Some(table[n]); - } - - if n > 186 { - return None; - } - - match table[n] { - // The lookup array starts out zeroed, so a zero - // is a not yet calculated value - 0 => { - let a = _mem_fibonacci(n - 1, table)?; - let b = _mem_fibonacci(n - 2, table)?; - - table[n] = a + b; - - Some(table[n]) + /// Actual calculating function for `fibonacci` + #[inline] + fn f(n: usize, table: &mut [u128]) -> Option { + if n < 2 { + // The first values are predefined. + return Some(table[n]); + } + + if n > 186 { + return None; + } + + match table[n] { + // The lookup array starts out zeroed, so a zero + // is a not yet calculated value + 0 => { + let a = f(n - 1, table)?; + let b = f(n - 2, table)?; + + table[n] = a + b; + + Some(table[n]) + } + x => Some(x), } - x => Some(x), } + + f(n, &mut table) } /// Calculate a number in the fibonacci sequence, @@ -82,9 +82,20 @@ pub fn rec_fibonacci(n: usize) -> Option { } /// Calculate a number in the fibonacci sequence, -/// using iteration /// -/// Can calculate up to 186 +/// Can calculate up to 186 using native unsigned 128 bit integers. +/// +/// Example: +/// ```rust +/// use rusty_numbers::fibonacci; +/// +/// let valid = fibonacci(45); // Some(1134903170) +/// # assert_eq!(1134903170, fibonacci(45).unwrap()); +/// # assert!(valid.is_some()); +/// +/// let invalid = fibonacci(187); // None +/// # assert!(invalid.is_none()); +/// ``` #[inline] pub fn fibonacci(n: usize) -> Option { let mut a: u128 = 0; diff --git a/src/num.rs b/src/num.rs index 1b554b8..8039452 100644 --- a/src/num.rs +++ b/src/num.rs @@ -91,6 +91,8 @@ pub trait Int: /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic /// overflow would occur. If an overflow would have occurred then the wrapped value is returned. + /// + /// The wrapped value is the amount less than the minimum value as a positive number fn left_overflowing_sub(self, rhs: Self) -> (Self, bool); /// Convert to an unsigned number diff --git a/tests/fractions.rs b/tests/fractions.rs index f7eef4f..4a93a67 100644 --- a/tests/fractions.rs +++ b/tests/fractions.rs @@ -1,6 +1,7 @@ #![cfg_attr(tarpaulin, skip)] use rusty_numbers::frac; +use rusty_numbers::rational::Frac; #[test] fn mul_test() { @@ -64,6 +65,11 @@ fn zero_denom() { frac!(1 / 0); } +#[test] +fn fraction_reducing() { + assert_eq!(frac!(1u8/2), Frac::new_unreduced(48u8, 96u8).reduce()); +} + #[test] fn op_assign() { // Addition