7d5b1922a69a56abc29982a653c6d2a827abded9
[fw/altos] / src / kernel / ao_convert_pa_test.c
1 /*
2  * Copyright © 2012 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <stdint.h>
19 #define AO_CONVERT_TEST
20 typedef int32_t alt_t;
21 #include "ao_host.h"
22 #include "ao_convert_pa.c"
23
24 #define STEP_P  1
25 #define STEP_A  1
26
27 static inline int i_abs(int i) { return i < 0 ? -i : i; }
28
29 int
30 main (int argc, char **argv)
31 {
32         int     i;
33         int32_t p_to_a, p_to_a_to_p;
34         int32_t a_to_p, a_to_p_to_a;
35         int max_p_error = 0, max_p_error_p = -1;
36         int max_a_error = 0, max_a_error_a = -1;
37         int p_error;
38         int a_error;
39         int ret = 0;
40
41         for (i = 0; i < 120000 + STEP_P; i += STEP_P) {
42                 if (i > 120000)
43                         i = 120000;
44                 p_to_a = ao_pa_to_altitude(i);
45                 p_to_a_to_p = ao_altitude_to_pa(p_to_a);
46                 p_error = i_abs(p_to_a_to_p - i);
47                 if (p_error > max_p_error) {
48                         max_p_error = p_error;
49                         max_p_error_p = i;
50                 }
51 //              printf ("pa %d alt %d pa %d\n",
52 //                      i, p_to_a, p_to_a_to_p);
53         }
54         for (i = -1450; i < 40000 + STEP_A; i += STEP_A) {
55                 a_to_p = ao_altitude_to_pa(i);
56                 a_to_p_to_a = ao_pa_to_altitude(a_to_p);
57                 a_error = i_abs(a_to_p_to_a - i);
58                 if (a_error > max_a_error) {
59                         max_a_error = a_error;
60                         max_a_error_a = i;
61                 }
62 //              printf ("alt %d pa %d alt %d\n",
63 //                      i, a_to_p, a_to_p_to_a);
64         }
65         if (max_p_error > 2) {
66                 printf ("max p error %d at %d\n", max_p_error,
67                         max_p_error_p);
68                 ret++;
69         }
70         if (max_a_error > 1) {
71                 printf ("max a error %d at %d\n", max_a_error,
72                         max_a_error_a);
73                 ret++;
74         }
75         return ret;
76 }