diff options
author | Gary E. Miller <gem@rellim.com> | 2019-02-21 15:13:28 -0800 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2019-02-21 15:13:28 -0800 |
commit | c40b62c5b409aa97acfa397e94dfb40198c62ea0 (patch) | |
tree | 63ad985e1efb15587eecb811cdc876d7268f93f4 | |
parent | 83e111fa7b4467500de853b790157e75f88bb64a (diff) | |
download | gpsd-c40b62c5b409aa97acfa397e94dfb40198c62ea0.tar.gz |
deg_to_s(): Fix bad conversion. Add test cases.
Bug and test case by: Stephen Moshier <steve@moshier.net>
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | SConstruct | 20 | ||||
-rw-r--r-- | gpsdclient.c | 2 | ||||
-rw-r--r-- | tests/test_gpsdclient.c | 98 |
4 files changed, 118 insertions, 3 deletions
@@ -27,6 +27,7 @@ valgrind-audit.py test_bits test_float test_geoid +test_gpsdclient test_gpsmm test_json test_libgps @@ -1377,6 +1377,9 @@ test_float = env.Program('tests/test_float', ['tests/test_float.c']) test_geoid = env.Program('tests/test_geoid', ['tests/test_geoid.c'], LIBS=['gpsd', 'gps_static'], parse_flags=gpsdflags) +test_gpsdclient = env.Program('tests/test_gpsdclient', + ['tests/test_gpsdclient.c'], + LIBS=['gps_static', 'm']) test_matrix = env.Program('tests/test_matrix', ['tests/test_matrix.c'], LIBS=['gpsd', 'gps_static'], parse_flags=gpsdflags) @@ -1408,8 +1411,16 @@ else: test_gpsmm = env.Program('tests/test_gpsmm', ['tests/test_gpsmm.cpp'], LIBS=['gps_static'], parse_flags=["-lm"] + rtlibs + dbusflags) -testprogs = [test_bits, test_float, test_geoid, test_libgps, test_matrix, - test_mktime, test_packet, test_timespec, test_trig] +testprogs = [test_bits, + test_float, + test_geoid, + test_gpsdclient, + test_libgps, + test_matrix, + test_mktime, + test_packet, + test_timespec, + test_trig] if env['socket_export']: testprogs.append(test_json) if env["libgpsmm"]: @@ -2050,6 +2061,11 @@ bits_regress = Utility('bits-regress', [test_bits], [ '$SRCDIR/tests/test_bits --quiet' ]) +# Unit-test the deg_to_str() extractor +bits_regress = Utility('deg-regress', [test_gpsdclient], [ + '$SRCDIR/tests/test_gpsdclient' +]) + # Unit-test the bitfield extractor matrix_regress = Utility('matrix-regress', [test_matrix], [ '$SRCDIR/tests/test_matrix --quiet' diff --git a/gpsdclient.c b/gpsdclient.c index 0c8af48e..880206ef 100644 --- a/gpsdclient.c +++ b/gpsdclient.c @@ -72,7 +72,7 @@ char *deg_to_str(enum deg_str_type type, double f) /* else DD MM SS.sss */ fdsec = modf(fsec * 60, &fsec); sec = (int)fsec; - dsec = (int)(fdsec * 10000.0); + dsec = (int)(fdsec * 100000.0); (void)snprintf(str, sizeof(str), "%3d %02d' %02d.%05d\"", deg, min, sec, dsec); diff --git a/tests/test_gpsdclient.c b/tests/test_gpsdclient.c new file mode 100644 index 00000000..95308cd4 --- /dev/null +++ b/tests/test_gpsdclient.c @@ -0,0 +1,98 @@ +/* test for gpsdclient.c: function deg_to_str + * + * Consider rounding off also: + * dsec = (int)(fdsec * 10000.0 + 0.5); + * + * This file is Copyright (c) 2010 by the GPSD project + * SPDX-License-Identifier: BSD-2-clause +*/ + +/* first so the #defs work */ +#include "../gpsd_config.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> /* for getopt() */ +#include "../gpsdclient.h" +#include "../revision.h" + +struct test { + double deg; + char dd[20]; + char ddmm[20]; + char ddmmss[20]; +}; + +struct test tests[] = { + /* 1 degree, 1 arcminute, 1.999 arcsec */ + {(1.0 + 1.0/60.0 + 1.999/3600.0), + " 1.01722194", + " 1 01.033316'", + " 1 01' 01.99899\""}, + /* 1 deg, 2 min, 2.0999 sec */ + {(1.0 + 2.0/60.0 + 2.999/3600.0), + " 1.03416638", + " 1 02.049983'", + " 1 02' 02.99900\""}, +}; + + +int main(int argc, char **argv) +{ + char *s; + unsigned int i; + int verbose = 0; + int fail_count = 0; + int option; + + while ((option = getopt(argc, argv, "h?vV")) != -1) { + switch (option) { + default: + fail_count = 1; + /* FALLTHROUGH */ + case '?': + /* FALLTHROUGH */ + case 'h': + (void)fputs("usage: test_gpsdclient [-v] [-V]\n", stderr); + exit(fail_count); + case 'V': + (void)fprintf( stderr, "test_gpsdclient %s\n", + VERSION); + exit(EXIT_SUCCESS); + case 'v': + verbose = 1; + break; + } + } + + + for (i = 0; i < (sizeof(tests)/sizeof(struct test)); i++) { + s = deg_to_str (deg_dd, tests[i].deg); + if (0 != strcmp(s, tests[i].dd)) { + printf("ERROR: %s s/b %s\n", s, tests[i].dd); + fail_count++; + } + if (0 < verbose) { + printf("%s s/b %s\n", s, tests[i].dd); + } + s = deg_to_str (deg_ddmm, tests[i].deg); + if (0 != strcmp(s, tests[i].ddmm)) { + printf("ERROR: %s s/b %s\n", s, tests[i].ddmm); + fail_count++; + } + if (0 < verbose) { + printf("%s s/b %s\n", s, tests[i].ddmm); + } + s = deg_to_str (deg_ddmmss, tests[i].deg); + if (0 != strcmp(s, tests[i].ddmmss)) { + printf("ERROR: %s s/b %s\n", s, tests[i].ddmmss); + fail_count++; + } + if (0 < verbose) { + printf("%s s/b %s\n", s, tests[i].ddmmss); + } + } + exit(fail_count); + +} + |