summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGary E. Miller <gem@rellim.com>2019-02-21 15:13:28 -0800
committerGary E. Miller <gem@rellim.com>2019-02-21 15:13:28 -0800
commitc40b62c5b409aa97acfa397e94dfb40198c62ea0 (patch)
tree63ad985e1efb15587eecb811cdc876d7268f93f4 /tests
parent83e111fa7b4467500de853b790157e75f88bb64a (diff)
downloadgpsd-c40b62c5b409aa97acfa397e94dfb40198c62ea0.tar.gz
deg_to_s(): Fix bad conversion. Add test cases.
Bug and test case by: Stephen Moshier <steve@moshier.net>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_gpsdclient.c98
1 files changed, 98 insertions, 0 deletions
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);
+
+}
+