-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01_shape.rs
More file actions
71 lines (59 loc) · 1.95 KB
/
Copy path01_shape.rs
File metadata and controls
71 lines (59 loc) · 1.95 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Run this file with `cargo test --test 01_shape`.
//! TODO: Create a trait `Shape` with methods for calculating the area and perimeter of a geometrical
//! object. Then create two simple geometrical objects (`Rectangle` and `Circle`) and implement
//! the `Shape` trait for both of them.
/// Below you can find a set of unit tests.
#[cfg(test)]
mod tests {
use crate::{Circle, Rectangle, Shape};
use std::f64::consts::PI;
#[test]
fn rectangle1() {
let rectangle = Rectangle::new(5.0, 3.0);
assert_almost_eq(rectangle.area(), 15.0);
assert_almost_eq(rectangle.perimeter(), 16.0);
}
#[test]
fn rectangle2() {
let rectangle = Rectangle::new(0.3, 1982.3);
assert_almost_eq(rectangle.area(), 594.69);
assert_almost_eq(rectangle.perimeter(), 3965.2);
}
#[test]
fn rectangle3() {
let rectangle = Rectangle::new(0.0, 1.0);
assert_almost_eq(rectangle.area(), 0.0);
assert_almost_eq(rectangle.perimeter(), 2.0);
}
#[test]
fn circle1() {
let rectangle = Circle::new(5.0);
assert_almost_eq(rectangle.area(), 25.0 * PI);
assert_almost_eq(rectangle.perimeter(), 10.0 * PI);
}
#[test]
fn circle2() {
let rectangle = Circle::new(122038.12);
assert_almost_eq(rectangle.area(), 46788690454.10);
assert_almost_eq(rectangle.perimeter(), 766788.122);
}
#[test]
fn circle3() {
let rectangle = Circle::new(0.0);
assert_almost_eq(rectangle.area(), 0.0);
assert_almost_eq(rectangle.perimeter(), 0.0);
}
#[test]
fn test_implements_trait() {
fn take_shape<T: Shape>(_: T) {}
take_shape(Circle::new(1.0));
take_shape(Rectangle::new(1.0, 1.0));
}
#[track_caller]
fn assert_almost_eq(value: f64, expected: f64) {
assert!(
(value - expected).abs() < 0.01,
"{value} does not equal {expected}"
);
}
}