diff --git a/benches/bench.rs b/benches/bench.rs index e0b6273..ff818a1 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,18 +1,15 @@ +use issue_77::{climb_stairs, climb_stairs2}; + fn main() { divan::main(); } #[divan::bench(args = [1, 2, 4, 8, 16, 32, 64, 128])] pub fn fibonacci_loop(n: i32) -> i32 { - let mut a = 0; - let mut b = 1; - for _ in 0..n { - (a, b) = (b, a + b); - } - b + climb_stairs2(n) } #[divan::bench(args = [1, 2, 4, 8, 16, 32, 64, 128])] pub fn fibonacci_fold(n: i32) -> i32 { - (0..n).fold((0, 1), |(a, b), _| (b, a + b)).1 + climb_stairs(n) } diff --git a/src/main.rs b/src/lib.rs similarity index 69% rename from src/main.rs rename to src/lib.rs index 76a7df0..721c357 100644 --- a/src/main.rs +++ b/src/lib.rs @@ -1,8 +1,5 @@ -// Rust Bytes Issue 77: Climbing Stairs -pub fn main() {} pub fn climb_stairs2(n: i32) -> i32 { - let mut a = 0; - let mut b = 1; + let (mut a, mut b) = (0, 1); for _ in 0..n { (a, b) = (b, a + b); } @@ -15,55 +12,65 @@ pub fn climb_stairs(n: i32) -> i32 { #[cfg(test)] mod tests { - use super::climb_stairs; + use super::*; #[test] fn test_zero_steps() { assert_eq!(climb_stairs(0), 1); + assert_eq!(climb_stairs2(0), 1); } #[test] fn test_one_step() { assert_eq!(climb_stairs(1), 1); + assert_eq!(climb_stairs2(1), 1); } #[test] fn test_two_steps() { assert_eq!(climb_stairs(2), 2); + assert_eq!(climb_stairs2(2), 2); } #[test] fn test_three_steps() { assert_eq!(climb_stairs(3), 3); + assert_eq!(climb_stairs2(3), 3); } #[test] fn test_four_steps() { assert_eq!(climb_stairs(4), 5); + assert_eq!(climb_stairs2(4), 5); } #[test] fn test_five_steps() { assert_eq!(climb_stairs(5), 8); + assert_eq!(climb_stairs2(5), 8); } #[test] fn test_six_steps() { assert_eq!(climb_stairs(6), 13); + assert_eq!(climb_stairs2(6), 13); } #[test] fn test_small_negative() { assert_eq!(climb_stairs(-1), 1); + assert_eq!(climb_stairs2(-1), 1); } #[test] fn test_seven_steps() { assert_eq!(climb_stairs(7), 21); + assert_eq!(climb_stairs2(7), 21); } #[test] fn test_ten_steps() { assert_eq!(climb_stairs(10), 89); + assert_eq!(climb_stairs2(10), 89); } }