|
| 1 | +package space_age |
| 2 | + |
| 3 | +import "base:intrinsics" |
| 4 | +import "core:log" |
| 5 | +import "core:testing" |
| 6 | + |
| 7 | +expect_almost :: proc( |
| 8 | + value, expected: $T, |
| 9 | + tolerance := 0.01, |
| 10 | + loc := #caller_location, |
| 11 | +) -> bool where intrinsics.type_is_float(T) { |
| 12 | + ok := abs(expected - value) <= tolerance |
| 13 | + // Instead of using testing.expect...(), you can also fail a test |
| 14 | + // by logging an error. |
| 15 | + if !ok { |
| 16 | + log.errorf("expected %.2f, got %.2f", expected, value, location = loc) |
| 17 | + } |
| 18 | + return ok |
| 19 | +} |
| 20 | + |
| 21 | +@(test) |
| 22 | +/// description = age on Earth |
| 23 | +test_age_on_earth :: proc(t: ^testing.T) { |
| 24 | + expect_almost(age(.Earth, 1_000_000_000), 31.69) |
| 25 | +} |
| 26 | + |
| 27 | +@(test) |
| 28 | +/// description = age on Mercury |
| 29 | +test_age_on_mercury :: proc(t: ^testing.T) { |
| 30 | + expect_almost(age(.Mercury, 2_134_835_688), 280.88) |
| 31 | +} |
| 32 | + |
| 33 | +@(test) |
| 34 | +/// description = age on Venus |
| 35 | +test_age_on_venus :: proc(t: ^testing.T) { |
| 36 | + expect_almost(age(.Venus, 189_839_836), 9.78) |
| 37 | +} |
| 38 | + |
| 39 | +@(test) |
| 40 | +/// description = age on Mars |
| 41 | +test_age_on_mars :: proc(t: ^testing.T) { |
| 42 | + expect_almost(age(.Mars, 2_129_871_239), 35.88) |
| 43 | +} |
| 44 | + |
| 45 | +@(test) |
| 46 | +/// description = age on Jupiter |
| 47 | +test_age_on_jupiter :: proc(t: ^testing.T) { |
| 48 | + expect_almost(age(.Jupiter, 901_876_382), 2.41) |
| 49 | +} |
| 50 | + |
| 51 | +@(test) |
| 52 | +/// description = age on Saturn |
| 53 | +test_age_on_saturn :: proc(t: ^testing.T) { |
| 54 | + expect_almost(age(.Saturn, 2_000_000_000), 2.15) |
| 55 | +} |
| 56 | + |
| 57 | +@(test) |
| 58 | +/// description = age on Uranus |
| 59 | +test_age_on_uranus :: proc(t: ^testing.T) { |
| 60 | + expect_almost(age(.Uranus, 1_210_123_456), 0.46) |
| 61 | +} |
| 62 | + |
| 63 | +@(test) |
| 64 | +/// description = age on Neptune |
| 65 | +test_age_on_neptune :: proc(t: ^testing.T) { |
| 66 | + expect_almost(age(.Neptune, 1_821_023_456), 0.35) |
| 67 | +} |
0 commit comments