summaryrefslogtreecommitdiff
path: root/tests/test_gpsdclient.c
blob: 7f31ae21d28230582453adc2173aad21ebb5770a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 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.999999995 sec */
    {(1.999999995),
     "  2.00000000",            /* rounded up */
     "  2 00.000000'",          /* rounded up */
     "  1 59' 59.99998\""},
    /* 3.999999999 sec */
    {(3.999999994),
     "  3.99999999",            /* not rounded up */
     "  4 00.000000'",          /* rounded up */
     "  3 59' 59.99998\""},
    /* 5 degree, 1.99999960 arcmin */
    {(5.0 + 1.999999600/60.0),
     "  5.03333333",
     "  5 02.000000'",          /* rounded up */
     "  5 01' 59.99998\""},
    /* 6 degree, 1.99999940 arcmin */
    {(6.0 + 1.999999400/60.0),
     "  6.03333332",
     "  6 01.999999'",          /* not rounded up */
     "  6 01' 59.99996\""},
    /* 7 degree, 59.99999960 arcmin */
    {(7.0 + 59.999999600/60.0),
     "  7.99999999",
     "  8 00.000000'",          /* rounded up */
     "  7 59' 59.99998\""},
    /* 9 degree, 59.99999940 arcmin */
    {(9.0 + 59.999999400/60.0),
     "  9.99999999",
     "  9 59.999999'",          /* not rounded up */
     "  9 59' 59.99996\""},
    /* 11 degree, 1 arcminute, 1.99999600 arcsec */
    {(11.0 + 1.0/60.0 + 1.99999600/3600.0),
     " 11.01722222",
     " 11 01.033333'",
     " 11 01' 02.00000\""},     /* rounded up */
    /* 12 deg, 2 min, 2.99999400 sec */
    {(12.0 + 2.0/60.0 + 2.99999400/3600.0),
     " 12.03416667",
     " 12 02.050000'",
     " 12 02' 02.99999\""},     /* not rounded up */
    /* -44.99999999999 */
    /* fabs() */
    {-44.0,
     " 44.00000000",
     " 44 00.000000'",
     " 44 00' 00.00000\""},
    /* 359.99999999999 */
    {359.99999999999,
     "  0.00000000",         /* rounded up, and rolled over */
     "  0 00.000000'",
     "  0 00' 00.00000\""},
    /* 361 */
    /* nan because out of range */
    {361,
     "nan",
     "nan",
     "nan"},
    /* -361 */
    /* nan because out of range */
    {361,
     "nan",
     "nan",
     "nan"},
};


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);

}