Browse Source

Add test and subject of `lalgebra_scalar`

content-update
Augusto 3 years ago
parent
commit
566d5e878a
  1. 12
      rust/tests/lalgebra_scalar_test/Cargo.lock
  2. 10
      rust/tests/lalgebra_scalar_test/Cargo.toml
  3. 73
      rust/tests/lalgebra_scalar_test/src/main.rs
  4. 39
      subjects/lalgebra_scalar/README.md

12
rust/tests/lalgebra_scalar_test/Cargo.lock diff.generated

@ -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",
]

10
rust/tests/lalgebra_scalar_test/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "lalgebra_scalar_test"
version = "0.1.0"
authors = ["Augusto <aug.ornelas@gmail.com>"]
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"}

73
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);
}

39
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$
```
Loading…
Cancel
Save