From 1bb5e4a96bab2e0e9cdba60b49ee25eafab1381e Mon Sep 17 00:00:00 2001 From: Augusto Date: Thu, 24 Dec 2020 23:04:27 +0000 Subject: [PATCH] Add exercise `find_factorial`'s subject and tests --- rust/tests/find_factorial_test/Cargo.lock | 12 +++++++ rust/tests/find_factorial_test/Cargo.toml | 10 ++++++ rust/tests/find_factorial_test/src/main.rs | 34 ++++++++++++++++++ subjects/find_factorial/README.md | 40 ++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 rust/tests/find_factorial_test/Cargo.lock create mode 100644 rust/tests/find_factorial_test/Cargo.toml create mode 100644 rust/tests/find_factorial_test/src/main.rs create mode 100644 subjects/find_factorial/README.md diff --git a/rust/tests/find_factorial_test/Cargo.lock b/rust/tests/find_factorial_test/Cargo.lock new file mode 100644 index 00000000..f8022035 --- /dev/null +++ b/rust/tests/find_factorial_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "find_factorial" +version = "0.1.0" + +[[package]] +name = "find_factorial_test" +version = "0.1.0" +dependencies = [ + "find_factorial", +] diff --git a/rust/tests/find_factorial_test/Cargo.toml b/rust/tests/find_factorial_test/Cargo.toml new file mode 100644 index 00000000..93a57a2f --- /dev/null +++ b/rust/tests/find_factorial_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "find_factorial_test" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +find_factorial = { path = "../../../../rust-piscine-solutions/find_factorial"} diff --git a/rust/tests/find_factorial_test/src/main.rs b/rust/tests/find_factorial_test/src/main.rs new file mode 100644 index 00000000..b288520e --- /dev/null +++ b/rust/tests/find_factorial_test/src/main.rs @@ -0,0 +1,34 @@ +// Complete this function to return the factorial of a given number + +pub fn factorial(num: u64) -> u64 { + match num { + 0 | 1 => 1, + _ => factorial(num - 1) * num, + } +} + +fn main() { + println!("The factorial of 0 = {}", factorial(0)); + println!("The factorial of 1 = {}", factorial(1)); + println!("The factorial of 5 = {}", factorial(5)); + println!("The factorial of 10 = {}", factorial(10)); + println!("The factorial of 19 = {}", factorial(19)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn factorial_of_1() { + assert_eq!(1, factorial(0)); + assert_eq!(1, factorial(1)); + assert_eq!(120, factorial(5)); + assert_eq!(40320, factorial(8)); + assert_eq!(3628800, factorial(10)); + assert_eq!(87178291200, factorial(14)); + assert_eq!(6402373705728000, factorial(18)); + assert_eq!(121645100408832000, factorial(19)); + assert_eq!(2432902008176640000, factorial(20)); + } +} diff --git a/subjects/find_factorial/README.md b/subjects/find_factorial/README.md new file mode 100644 index 00000000..dfb88d15 --- /dev/null +++ b/subjects/find_factorial/README.md @@ -0,0 +1,40 @@ +## find_factorial + +### Instruccions + +Complete the function `factorial` to return the factorial of a given number + +### Expected Function + +```rust +pub fn factorial(num: u64) -> u64 { +} +``` + +### Usage + +Here is a possible program to test your function : + +```rust +use find_factorial::factorial; + +fn main() { + println!("The factorial of 0 = {}", factorial(0)); + println!("The factorial of 1 = {}", factorial(1)); + println!("The factorial of 5 = {}", factorial(5)); + println!("The factorial of 10 = {}", factorial(10)); + println!("The factorial of 19 = {}", factorial(19)); +} +``` + +And its output: + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +The factorial of 0 = 1 +The factorial of 1 = 1 +The factorial of 5 = 120 +The factorial of 10 = 3628800 +The factorial of 19 = 121645100408832000 +student@ubuntu:~/[[ROOT]]/test$ +```