Switch from GPLv2 to GPLv2+
[fw/altos] / src / kernel / ao_convert_test.c
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdint.h>
20 #define AO_CONVERT_TEST
21 #define AO_NEED_ALTITUDE_TO_PRES 1
22 #include "ao_host.h"
23 #include "ao_convert.c"
24
25 #define STEP    1
26
27 static inline int i_abs(int i) { return i < 0 ? -i : i; }
28
29 int main (int argc, char **argv)
30 {
31         int     i;
32         int16_t p_to_a, p_to_a_to_p;
33         int16_t a_to_p, a_to_p_to_a;
34         int max_p_error = 0, max_p_error_p = -1;
35         int max_a_error = 0, max_a_error_a = -1;
36         int p_error;
37         int a_error;
38         int ret = 0;
39
40         for (i = 0; i < 32767 + STEP; i += STEP) {
41                 if (i > 32767)
42                         i = 32767;
43                 p_to_a = ao_pres_to_altitude(i);
44                 p_to_a_to_p = ao_altitude_to_pres(p_to_a);
45                 p_error = i_abs(p_to_a_to_p - i);
46                 if (p_error > max_p_error) {
47                         max_p_error = p_error;
48                         max_p_error_p = i;
49                 }
50 //              printf ("pres %d alt %d pres %d\n",
51 //                      i, p_to_a, p_to_a_to_p);
52         }
53         for (i = -1578; i < 15835 + STEP; i += STEP) {
54                 if (i > 15835)
55                         i = 15835;
56                 a_to_p = ao_altitude_to_pres(i);
57                 a_to_p_to_a = ao_pres_to_altitude(a_to_p);
58                 a_error = i_abs(a_to_p_to_a - i);
59                 if (a_error > max_a_error) {
60                         max_a_error = a_error;
61                         max_a_error_a = i;
62                 }
63 //              printf ("alt %d pres %d alt %d\n",
64 //                      i, a_to_p, a_to_p_to_a);
65         }
66         if (max_p_error > 2) {
67                 printf ("max p error %d at %d\n", max_p_error,
68                         max_p_error_p);
69                 ret++;
70         }
71         if (max_a_error > 1) {
72                 printf ("max a error %d at %d\n", max_a_error,
73                         max_a_error_a);
74                 ret++;
75         }
76         return ret;
77 }