From 95d1e5d3658a664874d2ed97165f5d3713f5288f Mon Sep 17 00:00:00 2001 From: Augusto Date: Fri, 25 Dec 2020 11:49:27 +0000 Subject: [PATCH] Add the exercise division_and_remainder's subject and tests --- .../division_and_remainder_test/Cargo.lock | 12 ++++++ .../division_and_remainder_test/Cargo.toml | 10 +++++ .../division_and_remainder_test/src/main.rs | 35 ++++++++++++++++++ subjects/division_and_remainder/README.md | 37 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 rust/tests/division_and_remainder_test/Cargo.lock create mode 100644 rust/tests/division_and_remainder_test/Cargo.toml create mode 100644 rust/tests/division_and_remainder_test/src/main.rs create mode 100644 subjects/division_and_remainder/README.md diff --git a/rust/tests/division_and_remainder_test/Cargo.lock b/rust/tests/division_and_remainder_test/Cargo.lock new file mode 100644 index 000000000..853701d65 --- /dev/null +++ b/rust/tests/division_and_remainder_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "division_and_remainder" +version = "0.1.0" + +[[package]] +name = "division_and_reminder_test" +version = "0.1.0" +dependencies = [ + "division_and_remainder", +] diff --git a/rust/tests/division_and_remainder_test/Cargo.toml b/rust/tests/division_and_remainder_test/Cargo.toml new file mode 100644 index 000000000..a01bb2d0e --- /dev/null +++ b/rust/tests/division_and_remainder_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "division_and_reminder_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +division_and_remainder = { path = "../../../../rust-piscine-solutions/division_and_remainder" } diff --git a/rust/tests/division_and_remainder_test/src/main.rs b/rust/tests/division_and_remainder_test/src/main.rs new file mode 100644 index 000000000..d07f18119 --- /dev/null +++ b/rust/tests/division_and_remainder_test/src/main.rs @@ -0,0 +1,35 @@ +use division_and_remainder::*; + +fn main() { + let x = 9; + let y = 4; + let (division, remainder) = divide(x, y); + println!( + "\t{}/{}: division = {}, remainder = {}", + x, y, division, remainder + ); +} + +#[test] +#[should_panic] +fn divide_by_0() { + divide(40, 0); +} + +#[test] +fn test_divide() { + let (div, rem) = divide(40, 3); + + assert_eq!(div, 13); + assert_eq!(rem, 1); + + let (div, rem) = divide(389, 39); + + assert_eq!(div, 9); + assert_eq!(rem, 38); + + let (div, rem) = divide(29, 332); + + assert_eq!(div, 0); + assert_eq!(rem, 29); +} diff --git a/subjects/division_and_remainder/README.md b/subjects/division_and_remainder/README.md new file mode 100644 index 000000000..6d345e6ad --- /dev/null +++ b/subjects/division_and_remainder/README.md @@ -0,0 +1,37 @@ +## division_and_remainder + +### Instructions + +Create a function divide that receives two i32 and returns a tuple in which the first element is the result of the integer division between the two numbers and the second is the remainder of the division. + +### Expected Function + +```rust +fn divide(x: i32, y: i32) -> (i32, i32) { + +} +``` + +### Usage + +Here is a program to test you're function + +```rust +fn main() { + let x = 9; + let y = 4; + let (division, remainder) = divide(x, y); + println!( + "\t{}/{}: division = {}, remainder = {}", + x, y, division, remainder + ); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +9/4: division = 2, remainder = 1 +student@ubuntu:~/[[ROOT]]/test$ +```