From: Tilman Sauerbeck Date: Wed, 8 Jan 2020 17:36:47 +0000 (+0100) Subject: test: Add tests for the gps module. X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=6f2603fafd5b07040e0202cbb6808511f86c07c6;p=gps-watch.git test: Add tests for the gps module. --- diff --git a/SConscript.test b/SConscript.test index 28ddfd7..57593ad 100644 --- a/SConscript.test +++ b/SConscript.test @@ -10,6 +10,7 @@ SConscript('SConscript.libcommon.rs', exports='env', duplicate=0) test_source_files = [ 'test/main.rs', # Must be listed first (see below). + 'test/gps_test.rs', ] test_env = env.Clone() diff --git a/test/gps_test.rs b/test/gps_test.rs new file mode 100644 index 0000000..bed56a7 --- /dev/null +++ b/test/gps_test.rs @@ -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); +} diff --git a/test/main.rs b/test/main.rs index 0e0c124..011e185 100644 --- a/test/main.rs +++ b/test/main.rs @@ -22,3 +22,10 @@ */ extern crate common; + +mod gps_test; + +#[no_mangle] +extern fn get_ticks() -> u32 { + 0 +}