From 566d5e878a192833798beb727fcf43567a3ce14e Mon Sep 17 00:00:00 2001 From: Augusto Date: Sun, 27 Dec 2020 20:14:49 +0000 Subject: [PATCH] Add test and subject of `lalgebra_scalar` --- rust/tests/lalgebra_scalar_test/Cargo.lock | 12 ++++ rust/tests/lalgebra_scalar_test/Cargo.toml | 10 +++ rust/tests/lalgebra_scalar_test/src/main.rs | 73 +++++++++++++++++++++ subjects/lalgebra_scalar/README.md | 39 +++++++++++ 4 files changed, 134 insertions(+) create mode 100644 rust/tests/lalgebra_scalar_test/Cargo.lock create mode 100644 rust/tests/lalgebra_scalar_test/Cargo.toml create mode 100644 rust/tests/lalgebra_scalar_test/src/main.rs create mode 100644 subjects/lalgebra_scalar/README.md diff --git a/rust/tests/lalgebra_scalar_test/Cargo.lock b/rust/tests/lalgebra_scalar_test/Cargo.lock new file mode 100644 index 00000000..c114e7a3 --- /dev/null +++ b/rust/tests/lalgebra_scalar_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "lalgebra_scalar" +version = "0.1.0" + +[[package]] +name = "lalgebra_scalar_test" +version = "0.1.0" +dependencies = [ + "lalgebra_scalar", +] diff --git a/rust/tests/lalgebra_scalar_test/Cargo.toml b/rust/tests/lalgebra_scalar_test/Cargo.toml new file mode 100644 index 00000000..c94f3ae7 --- /dev/null +++ b/rust/tests/lalgebra_scalar_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "lalgebra_scalar_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] +lalgebra_scalar = { path = "../../../../rust-piscine-solutions/lalgebra_scalar"} \ No newline at end of file diff --git a/rust/tests/lalgebra_scalar_test/src/main.rs b/rust/tests/lalgebra_scalar_test/src/main.rs new file mode 100644 index 00000000..93492f97 --- /dev/null +++ b/rust/tests/lalgebra_scalar_test/src/main.rs @@ -0,0 +1,73 @@ +// # Instructions +// A scalar type must implement the operations +// Addition, Subtraction, Multiplication and Division (you might +// also have to use more restrictions). For this use a trait +// inheritance (supertraits) + +// Another condition for a number to be a scalar is to have a zero +// (neutral element in the addition) and a one (neutral element in the +// multiplication). Therefore the Scalar trait will require 2 +// functions zero() and one() + +// After finishing implement the Scalar trait for u32, u64, i32, i64, +// f32, f64 + +use lalgebra_scalar::Scalar; + +#[allow(dead_code)] +fn main() { + println!("{:?}", f64::zero()); + println!("{:?}", i32::zero()); +} + +#[test] +fn scalar_u32() { + let a: u32 = u32::zero(); + assert_eq!(a, 0 as u32); + + let b = u32::one(); + assert_eq!(b, 1 as u32); +} + +#[test] +fn scalar_u64() { + let a = u64::zero(); + assert_eq!(a, 0 as u64); + + let b = u64::one(); + assert_eq!(b, 1 as u64); +} + +#[test] +fn scalar_i32() { + let a: i32 = i32::zero(); + assert_eq!(a, 0 as i32); + + let b = i32::one(); + assert_eq!(b, 1 as i32); +} + +#[test] +fn scalar_i64() { + let a: i64 = i64::zero(); + assert_eq!(a, 0 as i64); + + let b = i64::one(); + assert_eq!(b, 1 as i64); +} + +#[test] +fn scalar_f32() { + let zero = f32::zero(); + assert_eq!(zero, 0.0); + let one = f32::one(); + assert_eq!(one, 1.0); +} + +#[test] +fn scalar_f64() { + let zero = f64::zero(); + assert_eq!(zero, 0.0); + let one = f64::one(); + assert_eq!(one, 1.0); +} diff --git a/subjects/lalgebra_scalar/README.md b/subjects/lalgebra_scalar/README.md new file mode 100644 index 00000000..ff513f4e --- /dev/null +++ b/subjects/lalgebra_scalar/README.md @@ -0,0 +1,39 @@ +## lalgebra_scalar + +### Instructions + +A scalar type must implement the operations Addition, Subtraction, Multiplication and Division (you might also have to use more restrictions). For this use a trait inheritance (supertraits) + +Another condition for a number to be a scalar is to have a zero (neutral element in the addition) and a one (neutral element in the multiplication). Therefore the Scalar trait will require 2 functions zero() and one() + +After finishing implement the Scalar trait for u32, u64, i32, i64, f32, f64 + +### Expected Function + +```rust +pub trait Scalar: _ { + type Item; + fn zero() -> Self::Item; + fn one() -> Self::Item; +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + println!("{:?}", f64::zero()); + println!("{:?}", i32::zero()); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +0.0 +0 +student@ubuntu:~/[[ROOT]]/test$ +```