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
72
73
74
75
76
77
78
79
80
81
82
83
#[derive(Debug,PartialEq)]
#[allow(dead_code)]
pub struct Coords {
latitude: f64,
longtitude: f64,
}
impl Coords {
pub fn new(latitude: f64, longtitude: f64) -> Result<Coords, String> {
if !Self::is_valid(latitude, longtitude) {
return Err("Invalid coordinates!".to_string());
}
Ok(Coords {
latitude: latitude,
longtitude: longtitude,
})
}
pub fn is_valid(latitude: f64, longtitude: f64) -> bool {
(latitude >= -90f64 && latitude <= 90f64) && (longtitude >= -180f64 && longtitude <= 180f64)
}
}
#[test]
fn is_valid_coords() {
let result = Coords::new(-90_f64, -180_f64).unwrap();
let expected = Coords {
latitude: -90_f64,
longtitude: -180_f64,
};
assert_eq!(expected, result);
}
#[test]
fn boundary_tests() {
assert_eq!(true, Coords::is_valid(-90_f64, -180_f64));
assert_eq!(true, Coords::is_valid(-90_f64, 180_f64));
assert_eq!(true, Coords::is_valid(90_f64, 180_f64));
assert_eq!(true, Coords::is_valid(90_f64, -180_f64));
assert_eq!(false, Coords::is_valid(-91_f64, -180_f64));
assert_eq!(false, Coords::is_valid(91_f64, 180_f64));
assert_eq!(false, Coords::is_valid(-90.99, -180f64));
}