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