libaltos: cjnitest needs altos_flush now
[fw/altos] / src / 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; 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 #include "ao_host.h"
21 #include "ao_convert.c"
22
23 #define STEP    1
24
25 static inline i_abs(int i) { return i < 0 ? -i : i; }
26
27 main ()
28 {
29         int     i;
30         int16_t p_to_a, p_to_a_to_p;
31         int16_t a_to_p, a_to_p_to_a;
32         int max_p_error = 0, max_p_error_p = -1;
33         int max_a_error = 0, max_a_error_a = -1;
34         int p_error;
35         int a_error;
36         int ret = 0;
37
38         for (i = 0; i < 32767 + STEP; i += STEP) {
39                 if (i > 32767)
40                         i = 32767;
41                 p_to_a = ao_pres_to_altitude(i);
42                 p_to_a_to_p = ao_altitude_to_pres(p_to_a);
43                 p_error = i_abs(p_to_a_to_p - i);
44                 if (p_error > max_p_error) {
45                         max_p_error = p_error;
46                         max_p_error_p = i;
47                 }
48 //              printf ("pres %d alt %d pres %d\n",
49 //                      i, p_to_a, p_to_a_to_p);
50         }
51         for (i = -1578; i < 15835 + STEP; i += STEP) {
52                 if (i > 15835)
53                         i = 15835;
54                 a_to_p = ao_altitude_to_pres(i);
55                 a_to_p_to_a = ao_pres_to_altitude(a_to_p);
56                 a_error = i_abs(a_to_p_to_a - i);
57                 if (a_error > max_a_error) {
58                         max_a_error = a_error;
59                         max_a_error_a = i;
60                 }
61 //              printf ("alt %d pres %d alt %d\n",
62 //                      i, a_to_p, a_to_p_to_a);
63         }
64         if (max_p_error > 2) {
65                 printf ("max p error %d at %d\n", max_p_error,
66                         max_p_error_p);
67                 ret++;
68         }
69         if (max_a_error > 1) {
70                 printf ("max a error %d at %d\n", max_a_error,
71                         max_a_error_a);
72                 ret++;
73         }
74         return ret;
75 }