test: Add tests for the gps module.
[gps-watch.git] / test / gps_test.rs
diff --git a/test/gps_test.rs b/test/gps_test.rs
new file mode 100644 (file)
index 0000000..bed56a7
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+use common::gps;
+
+// Verifies that a single RMC message with position, date and time
+// does not suffice for getting a valid TimeAndPos result.
+#[test]
+fn process_rmc_only() {
+    let input = b"\
+$GPRMC,110338.000,,1234.5678,N,12345.6789,E,\
+,,011116*45";
+
+    let mut input_iter = input.iter();
+
+    let mut tap = gps::TimeAndPos::new();
+    let mut gps = gps::Gps::new();
+
+    let has_fix = gps.update(&mut tap, || {
+        input_iter.next().and_then(|&b| Some(b))
+    });
+
+    assert_eq!(false, has_fix);
+}
+
+// Verifies that a single GGA message with position and time
+// does not suffice for getting a valid TimeAndPos result.
+#[test]
+fn process_gga_only() {
+    let input = b"\
+$GPGGA,110338.000,1234.5678,N,12345.6789,E,\
+1,04,2.4,190.3,M,48.0,M,,0000*58";
+    let mut input_iter = input.iter();
+
+    let mut tap = gps::TimeAndPos::new();
+    let mut gps = gps::Gps::new();
+
+    let has_fix = gps.update(&mut tap, || {
+        input_iter.next().and_then(|&b| Some(b))
+    });
+
+    assert_eq!(false, has_fix);
+}
+
+// Verifies that a GGA message received after a RMC message
+// does suffice for getting a valid TimeAndPos result.
+#[test]
+fn process_rmc_and_gga() {
+    let input = b"\
+$GPRMC,110338.000,,1234.5678,N,12345.6789,E,\
+,,011116*45\
+$GPGGA,110338.000,1234.5678,N,12345.6789,E,\
+1,04,2.4,190.3,M,48.0,M,,0000*58";
+    let mut input_iter = input.iter();
+
+    let mut tap = gps::TimeAndPos::new();
+    let mut gps = gps::Gps::new();
+
+    let has_fix = gps.update(&mut tap, || {
+        input_iter.next().and_then(|&b| Some(b))
+    });
+
+    assert_eq!(true, has_fix);
+    assert_eq!(1477998218, tap.unix_time);
+    assert_eq!(tap.latitude, 7545678);
+    assert_eq!(tap.longitude, 74256789);
+}