From 9f6bcff890cb245719828a3c384095ad79eb2a1a Mon Sep 17 00:00:00 2001 From: Augusto Date: Sun, 27 Dec 2020 23:59:10 +0000 Subject: [PATCH] Add subject and test for exercise `matrix_ops` --- rust/tests/matrix_ops_test/Cargo.lock | 19 +++++++++ rust/tests/matrix_ops_test/Cargo.toml | 10 +++++ rust/tests/matrix_ops_test/src/main.rs | 51 +++++++++++++++++++++++ subjects/matrix_ops/README.md | 56 ++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 rust/tests/matrix_ops_test/Cargo.lock create mode 100644 rust/tests/matrix_ops_test/Cargo.toml create mode 100644 rust/tests/matrix_ops_test/src/main.rs create mode 100644 subjects/matrix_ops/README.md diff --git a/rust/tests/matrix_ops_test/Cargo.lock b/rust/tests/matrix_ops_test/Cargo.lock new file mode 100644 index 00000000..7fb91365 --- /dev/null +++ b/rust/tests/matrix_ops_test/Cargo.lock @@ -0,0 +1,19 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "lalgebra_scalar" +version = "0.1.0" + +[[package]] +name = "matrix" +version = "0.1.0" +dependencies = [ + "lalgebra_scalar", +] + +[[package]] +name = "matrix_test" +version = "0.1.0" +dependencies = [ + "matrix", +] diff --git a/rust/tests/matrix_ops_test/Cargo.toml b/rust/tests/matrix_ops_test/Cargo.toml new file mode 100644 index 00000000..9aba827c --- /dev/null +++ b/rust/tests/matrix_ops_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "matrix_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] +matrix = { path = "../../../../rust-piscine-solutions/matrix"} diff --git a/rust/tests/matrix_ops_test/src/main.rs b/rust/tests/matrix_ops_test/src/main.rs new file mode 100644 index 00000000..e291dbf3 --- /dev/null +++ b/rust/tests/matrix_ops_test/src/main.rs @@ -0,0 +1,51 @@ +// In this exercise you will define the basic operations with a matrix +// starting by implementing the `std::ops::Add` trait + +// Define the operation + (by defining the trait std::ops::Add) for +// two matrices remember that two matrices can only be added if they +// have the same size. Therefore the add method must handle the +// possibility of failure by returning an Option + +use matrix::Matrix; + +fn main() { + let matrix = Matrix(vec![vec![8, 1], vec![9, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1], vec![1, 1]]); + println!("{:?}", matrix + matrix_2); + + let matrix = Matrix(vec![vec![1, 3], vec![2, 5]]); + let matrix_2 = Matrix(vec![vec![3, 1], vec![1, 1]]); + println!("{:?}", matrix - matrix_2); + + let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]); + println!("{:?}", matrix - matrix_2); + + let matrix = Matrix(vec![vec![1, 3], vec![9, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]); + println!("{:?}", matrix + matrix_2); +} + +#[test] +fn addition() { + let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1], vec![1, 1]]); + let expected = Matrix(vec![vec![2, 2], vec![2, 2]]); + assert_eq!(matrix + matrix_2, Some(expected)); + + let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]); + assert_eq!(matrix + matrix_2, None); +} + +#[test] +fn subtraction() { + let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1], vec![1, 1]]); + let expected = Matrix(vec![vec![0, 0], vec![0, 0]]); + assert_eq!(matrix - matrix_2, Some(expected)); + + let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]); + assert_eq!(matrix - matrix_2, None); +} diff --git a/subjects/matrix_ops/README.md b/subjects/matrix_ops/README.md new file mode 100644 index 00000000..3b7550f7 --- /dev/null +++ b/subjects/matrix_ops/README.md @@ -0,0 +1,56 @@ +## matrix_ops + +### Instructions + +In this exercise you will define the basic operations with a matrix starting by implementing the `std::ops::Add` trait + +Define the operation + (by defining the trait std::ops::Add) for two matrices remember that two matrices can only be added if they have the same size. Therefore the add method must handle the possibility of failure by returning an Option + +### Expected Function + +```rust +use std::ops::{ Add, Sub }; + +impl Add for Matrix { + +} + +impl Sub for Matrix { + +} +``` + +### Usage + +Here is a program to test your function + +```rust +fn main() { + let matrix = Matrix(vec![vec![8, 1], vec![9, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1], vec![1, 1]]); + println!("{:?}", matrix + matrix_2); + + let matrix = Matrix(vec![vec![1, 3], vec![2, 5]]); + let matrix_2 = Matrix(vec![vec![3, 1], vec![1, 1]]); + println!("{:?}", matrix - matrix_2); + + let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]); + println!("{:?}", matrix - matrix_2); + + let matrix = Matrix(vec![vec![1, 3], vec![9, 1]]); + let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]); + println!("{:?}", matrix + matrix_2); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +Some(Matrix([[9, 2], [10, 2]])) +Some(Matrix([[-2, 2], [1, 4]])) +None +None +student@ubuntu:~/[[ROOT]]/test$ +```