blob: f961002ac33da6a5665dd978cdd2864e7c07f1b8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
mod shapes;
use shapes::{Rectangle, Shape, Triangle};
fn area(shape: &Shape) -> i32 {
match shape {
Shape::Rectangle(rectangle) => rectangle.width * rectangle.height,
Shape::Triangle(triangle) => triangle.width * triangle.height / 2,
}
}
#[cfg(test)]
mod test {
use super::shapes::{Rectangle, Shape, Triangle};
#[test]
fn rectangle() {
let rectangle = Shape::Rectangle(Rectangle { width: 10, height: 25 });
let area = super::area(&rectangle);
assert_eq!(area, 250);
}
#[test]
fn triangle() {
let triangle = Shape::Triangle(Triangle { width: 10, height: 25 });
let area = super::area(&triangle);
assert_eq!(area, 125);
}
}
|