micropeak: Add -Xlint:unchecked to javac line
[fw/altos] / src / test / ao_ms5607_convert_test.c
1 /*
2  * Copyright © 2013 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 #define __xdata
19 #define __data
20 #define __pdata
21 #define __reentrant
22
23 #include <stdint.h>
24 #include <ao_ms5607.h>
25
26 struct ao_ms5607_prom ms5607_prom = {
27         0x002c,
28         0xa6e0,
29         0x988e,
30         0x6814,
31         0x5eff,
32         0x8468,
33         0x6c86,
34         0xa271,
35 };
36
37 int32_t D1_mm = 6179630;
38 int32_t D2_mm = 8933155;
39
40 #include <ao_ms5607_convert.c>
41 #define ao_ms5607_convert ao_ms5607_convert_8051
42 #include <ao_ms5607_convert_8051.c>
43 #include <ao_int64.c>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 struct ao_ms5607_sample ao_sample = {
48         6179630,
49         8933155
50 };
51
52 int errors;
53
54 void test(int trial, struct ao_ms5607_sample *sample)
55 {
56         struct ao_ms5607_value  value, value_8051;
57
58         ao_ms5607_convert(sample, &value);
59         ao_ms5607_convert_8051(sample, &value_8051);
60         if (value.temp != value_8051.temp || value.pres != value_8051.pres) {
61                 ++errors;
62                 printf ("trial %d: %d, %d -> %d, %d (should be %d, %d)\n",
63                         trial,
64                         sample->pres, sample->temp,
65                         value_8051.pres, value_8051.temp,
66                         value.pres, value.temp);
67         }
68 }
69
70 #define TESTS   10000000
71
72 #include <stdlib.h>
73
74 static int32_t rand24(void) { return random() & 0xffffff; }
75
76 int
77 main(int argc, char **argv)
78 {
79         struct ao_ms5607_sample sample;
80         int     i, start;
81
82         if (argv[1])
83                 start = atoi(argv[1]);
84         else
85                 start = 0;
86
87         srandom(10000);
88         test(-1, &ao_sample);
89         for (i = 0; i < TESTS; i++) {
90                 sample.pres = rand24();
91                 sample.temp = rand24();
92                 if (i >= start)
93                         test(i, &sample);
94         }
95         return errors;
96 }