diff options
Diffstat (limited to 'rust/space-age/tests/space-age.rs')
| -rw-r--r-- | rust/space-age/tests/space-age.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/rust/space-age/tests/space-age.rs b/rust/space-age/tests/space-age.rs new file mode 100644 index 0000000..acc45d4 --- /dev/null +++ b/rust/space-age/tests/space-age.rs | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | use space_age::*; | ||
| 2 | |||
| 3 | fn assert_in_delta(expected: f64, actual: f64) { | ||
| 4 | let diff: f64 = (expected - actual).abs(); | ||
| 5 | let delta: f64 = 0.01; | ||
| 6 | if diff > delta { | ||
| 7 | panic!( | ||
| 8 | "Your result of {} should be within {} of the expected result {}", | ||
| 9 | actual, delta, expected | ||
| 10 | ) | ||
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | #[test] | ||
| 15 | fn earth_age() { | ||
| 16 | let duration = Duration::from(1_000_000_000); | ||
| 17 | assert_in_delta(31.69, Earth::years_during(&duration)); | ||
| 18 | } | ||
| 19 | |||
| 20 | #[test] | ||
| 21 | fn mercury_age() { | ||
| 22 | let duration = Duration::from(2_134_835_688); | ||
| 23 | assert_in_delta(280.88, Mercury::years_during(&duration)); | ||
| 24 | } | ||
| 25 | |||
| 26 | #[test] | ||
| 27 | fn venus_age() { | ||
| 28 | let duration = Duration::from(189_839_836); | ||
| 29 | assert_in_delta(9.78, Venus::years_during(&duration)); | ||
| 30 | } | ||
| 31 | |||
| 32 | #[test] | ||
| 33 | fn mars_age() { | ||
| 34 | let duration = Duration::from(2_129_871_239); | ||
| 35 | assert_in_delta(35.88, Mars::years_during(&duration)); | ||
| 36 | } | ||
| 37 | |||
| 38 | #[test] | ||
| 39 | fn jupiter_age() { | ||
| 40 | let duration = Duration::from(901_876_382); | ||
| 41 | assert_in_delta(2.41, Jupiter::years_during(&duration)); | ||
| 42 | } | ||
| 43 | |||
| 44 | #[test] | ||
| 45 | fn saturn_age() { | ||
| 46 | let duration = Duration::from(2_000_000_000); | ||
| 47 | assert_in_delta(2.15, Saturn::years_during(&duration)); | ||
| 48 | } | ||
| 49 | |||
| 50 | #[test] | ||
| 51 | fn uranus_age() { | ||
| 52 | let duration = Duration::from(1_210_123_456); | ||
| 53 | assert_in_delta(0.46, Uranus::years_during(&duration)); | ||
| 54 | } | ||
| 55 | |||
| 56 | #[test] | ||
| 57 | fn neptune_age() { | ||
| 58 | let duration = Duration::from(1_821_023_456); | ||
| 59 | assert_in_delta(0.35, Neptune::years_during(&duration)); | ||
| 60 | } | ||
