test: Add tests for the gps module.
[gps-watch.git] / test / gps_test.rs
1 /*
2  * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 use common::gps;
25
26 // Verifies that a single RMC message with position, date and time
27 // does not suffice for getting a valid TimeAndPos result.
28 #[test]
29 fn process_rmc_only() {
30     let input = b"\
31 $GPRMC,110338.000,,1234.5678,N,12345.6789,E,\
32 ,,011116*45";
33
34     let mut input_iter = input.iter();
35
36     let mut tap = gps::TimeAndPos::new();
37     let mut gps = gps::Gps::new();
38
39     let has_fix = gps.update(&mut tap, || {
40         input_iter.next().and_then(|&b| Some(b))
41     });
42
43     assert_eq!(false, has_fix);
44 }
45
46 // Verifies that a single GGA message with position and time
47 // does not suffice for getting a valid TimeAndPos result.
48 #[test]
49 fn process_gga_only() {
50     let input = b"\
51 $GPGGA,110338.000,1234.5678,N,12345.6789,E,\
52 1,04,2.4,190.3,M,48.0,M,,0000*58";
53     let mut input_iter = input.iter();
54
55     let mut tap = gps::TimeAndPos::new();
56     let mut gps = gps::Gps::new();
57
58     let has_fix = gps.update(&mut tap, || {
59         input_iter.next().and_then(|&b| Some(b))
60     });
61
62     assert_eq!(false, has_fix);
63 }
64
65 // Verifies that a GGA message received after a RMC message
66 // does suffice for getting a valid TimeAndPos result.
67 #[test]
68 fn process_rmc_and_gga() {
69     let input = b"\
70 $GPRMC,110338.000,,1234.5678,N,12345.6789,E,\
71 ,,011116*45\
72 $GPGGA,110338.000,1234.5678,N,12345.6789,E,\
73 1,04,2.4,190.3,M,48.0,M,,0000*58";
74     let mut input_iter = input.iter();
75
76     let mut tap = gps::TimeAndPos::new();
77     let mut gps = gps::Gps::new();
78
79     let has_fix = gps.update(&mut tap, || {
80         input_iter.next().and_then(|&b| Some(b))
81     });
82
83     assert_eq!(true, has_fix);
84     assert_eq!(1477998218, tap.unix_time);
85     assert_eq!(tap.latitude, 7545678);
86     assert_eq!(tap.longitude, 74256789);
87 }