Updated test #64
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
struct Vector2 {
|
||||
x i32,
|
||||
y i32,
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub fn magnitude(*self) i32 {
|
||||
self.x * self.x + self.y * self.y
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_inherent_impl() {
|
||||
let v = Vector2 { x: 3, y: 4 };
|
||||
assert_eq!(v.magnitude(), 25);
|
||||
}
|
||||
|
||||
trait Display {
|
||||
fn display(*self) String;
|
||||
}
|
||||
|
||||
impl Display for Vector2 {
|
||||
fn display(*self) String {
|
||||
format!("({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_trait_impl_block() {
|
||||
let v = Vector2 { x: 1, y: 2 };
|
||||
let s = v.display();
|
||||
assert_eq!(s.len() > 0, true);
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub fn add(*self, other Vector2) Vector2 {
|
||||
Vector2 {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_multiple_impls() {
|
||||
let a = Vector2 { x: 1, y: 2 };
|
||||
let b = Vector2 { x: 3, y: 4 };
|
||||
let c = a.add(b);
|
||||
assert_eq!(c.x, 4);
|
||||
assert_eq!(c.y, 6);
|
||||
}
|
||||
|
||||
pub fn run_tests() {
|
||||
test_inherent_impl();
|
||||
test_trait_impl_block();
|
||||
test_multiple_impls();
|
||||
}
|
||||
Reference in New Issue
Block a user