Update baudrate unit test to show divisor/fractional bits/clock (suggested by Uwe)
[libftdi] / test / baudrate.cpp
1 /**@file
2 @brief Test baudrate calculator code
3
4 @author Thomas Jarosch
5 */
6
7 /***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU Lesser General Public License           *
11  *   version 2.1 as published by the Free Software Foundation;             *
12  *                                                                         *
13  ***************************************************************************/
14
15 #include <ftdi.h>
16
17 #define BOOST_TEST_DYN_LINK
18 #include <boost/test/unit_test.hpp>
19 #include <boost/foreach.hpp>
20 #include <map>
21
22 using namespace std;
23
24 extern "C" int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
25                                  unsigned short *value, unsigned short *index);
26
27 /// Basic initialization of libftdi for every test
28 class BaseFTDIFixture
29 {
30 protected:
31     ftdi_context *ftdi;
32
33 public:
34     BaseFTDIFixture()
35         : ftdi(NULL)
36     {
37         ftdi = ftdi_new();
38     }
39
40     ~BaseFTDIFixture()
41     {
42         delete ftdi;
43         ftdi = NULL;
44     }
45 };
46
47 BOOST_FIXTURE_TEST_SUITE(Baudrate, BaseFTDIFixture)
48
49 /// Helper class to store the convert_baudrate_UT_export result
50 struct calc_result
51 {
52     int actual_baudrate;
53     unsigned short divisor;
54     unsigned short fractional_bits;
55     unsigned short clock;
56
57     calc_result(int actual, unsigned short my_divisor, unsigned short my_fractional_bits, unsigned short my_clock)
58         : actual_baudrate(actual)
59         , divisor(my_divisor)
60         , fractional_bits(my_fractional_bits)
61         , clock(my_clock)
62     {
63     }
64
65     calc_result()
66         : actual_baudrate(0)
67         , divisor(0)
68         , fractional_bits(0)
69         , clock(0)
70     {
71     }
72 };
73
74 /**
75  * @brief Test convert_baudrate code against a list of baud rates
76  *
77  * @param baudrates Baudrates to check
78  **/
79 static void test_baudrates(ftdi_context *ftdi, const map<int, calc_result> &baudrates)
80 {
81     typedef std::pair<int, calc_result> baudrate_type;
82     BOOST_FOREACH(const baudrate_type &baudrate, baudrates)
83     {
84         unsigned short calc_value = 0, calc_index = 0;
85         int calc_baudrate = convert_baudrate_UT_export(baudrate.first, ftdi, &calc_value, &calc_index);
86
87         const calc_result *res = &baudrate.second;
88
89         unsigned short divisor = calc_value & 0x3ff;
90         unsigned short fractional_bits = (calc_index & 0x100) ? 4 : (calc_value >> 12);
91         unsigned short clock = (calc_index & 0x100) ? 120 : 48;
92
93         // Aid debugging since this test is a generic function
94         BOOST_CHECK_MESSAGE(res->actual_baudrate == calc_baudrate && res->divisor == divisor && res->fractional_bits == fractional_bits
95                             && res->clock == clock,
96                             "\n\nERROR: baudrate calculation failed for --" << baudrate.first << " baud--. Details below: ");
97
98         BOOST_CHECK_EQUAL(res->actual_baudrate, calc_baudrate);
99         BOOST_CHECK_EQUAL(res->divisor, divisor);
100         BOOST_CHECK_EQUAL(res->fractional_bits, fractional_bits);
101         BOOST_CHECK_EQUAL(res->clock, clock);
102     }
103 }
104
105 BOOST_AUTO_TEST_CASE(TypeAMFixedBaudrates)
106 {
107     ftdi->type = TYPE_AM;
108
109     map<int, calc_result> baudrates;
110     baudrates[300] = calc_result(300, 784, 2, 48);
111     baudrates[600] = calc_result(600, 904, 1, 48);
112     baudrates[1200] = calc_result(1200, 452, 0, 48);
113     baudrates[2400] = calc_result(2400, 226, 0, 48);
114     baudrates[4800] = calc_result(4800, 625, 0, 48);
115     baudrates[9600] = calc_result(9600, 312, 4, 48);
116     baudrates[19200] = calc_result(19200, 156, 8, 48);
117     baudrates[38400] = calc_result(38400, 78, 12, 48);
118     baudrates[57600] = calc_result(57554, 52, 12, 48);
119     baudrates[115200] = calc_result(115385, 26, 0, 48);
120     baudrates[230400] = calc_result(230769, 13, 0, 48);
121     baudrates[460800] = calc_result(461538, 6, 4, 48);
122     baudrates[921600] = calc_result(923077, 3, 8, 48);
123
124     test_baudrates(ftdi, baudrates);
125 }
126
127 BOOST_AUTO_TEST_CASE(TypeBMFixedBaudrates)
128 {
129     ftdi->type = TYPE_BM;
130
131     map<int, calc_result> baudrates;
132     baudrates[300] = calc_result(300, 784, 2, 48);
133     baudrates[600] = calc_result(600, 904, 1, 48);
134     baudrates[1200] = calc_result(1200, 452, 0, 48);
135     baudrates[2400] = calc_result(2400, 226, 0, 48);
136     baudrates[4800] = calc_result(4800, 625, 0, 48);
137     baudrates[9600] = calc_result(9600, 312, 4, 48);
138     baudrates[19200] = calc_result(19200, 156, 8, 48);
139     baudrates[38400] = calc_result(38400, 78, 12, 48);
140     baudrates[57600] = calc_result(57554, 52, 12, 48);
141     baudrates[115200] = calc_result(115385, 26, 0, 48);
142     baudrates[230400] = calc_result(230769, 13, 0, 48);
143     baudrates[460800] = calc_result(461538, 6, 4, 48);
144     baudrates[921600] = calc_result(923077, 3, 8, 48);
145
146     test_baudrates(ftdi, baudrates);
147 }
148
149 BOOST_AUTO_TEST_CASE(Type2232CFixedBaudrates)
150 {
151     ftdi->type = TYPE_2232C;
152
153     map<int, calc_result> baudrates;
154     baudrates[300] = calc_result(300, 784, 2, 48);
155     baudrates[600] = calc_result(600, 904, 1, 48);
156     baudrates[1200] = calc_result(1200, 452, 0, 48);
157     baudrates[2400] = calc_result(2400, 226, 0, 48);
158     baudrates[4800] = calc_result(4800, 625, 0, 48);
159     baudrates[9600] = calc_result(9600, 312, 4, 48);
160     baudrates[19200] = calc_result(19200, 156, 8, 48);
161     baudrates[38400] = calc_result(38400, 78, 12, 48);
162     baudrates[57600] = calc_result(57554, 52, 12, 48);
163     baudrates[115200] = calc_result(115385, 26, 0, 48);
164     baudrates[230400] = calc_result(230769, 13, 0, 48);
165     baudrates[460800] = calc_result(461538, 6, 4, 48);
166     baudrates[921600] = calc_result(923077, 3, 8, 48);
167
168     test_baudrates(ftdi, baudrates);
169 }
170
171 BOOST_AUTO_TEST_CASE(TypeRFixedBaudrates)
172 {
173     ftdi->type = TYPE_R;
174
175     map<int, calc_result> baudrates;
176     baudrates[300] = calc_result(300, 784, 2, 48);
177     baudrates[600] = calc_result(600, 904, 1, 48);
178     baudrates[1200] = calc_result(1200, 452, 0, 48);
179     baudrates[2400] = calc_result(2400, 226, 0, 48);
180     baudrates[4800] = calc_result(4800, 625, 0, 48);
181     baudrates[9600] = calc_result(9600, 312, 4, 48);
182     baudrates[19200] = calc_result(19200, 156, 8, 48);
183     baudrates[38400] = calc_result(38400, 78, 12, 48);
184     baudrates[57600] = calc_result(57554, 52, 12, 48);
185     baudrates[115200] = calc_result(115385, 26, 0, 48);
186     baudrates[230400] = calc_result(230769, 13, 0, 48);
187     baudrates[460800] = calc_result(461538, 6, 4, 48);
188     baudrates[921600] = calc_result(923077, 3, 8, 48);
189
190     test_baudrates(ftdi, baudrates);
191 }
192
193 BOOST_AUTO_TEST_CASE(Type2232HFixedBaudrates)
194 {
195     ftdi->type = TYPE_2232H;
196
197     map<int, calc_result> baudrates;
198     baudrates[300] = calc_result(300, 784, 2, 48);
199     baudrates[600] = calc_result(600, 904, 1, 48);
200     baudrates[1200] = calc_result(1200, 452, 0, 48);
201     baudrates[2400] = calc_result(2400, 226, 0, 48);
202     baudrates[4800] = calc_result(4800, 625, 0, 48);
203     baudrates[9600] = calc_result(9600, 312, 4, 48);
204     baudrates[19200] = calc_result(19200, 156, 8, 48);
205     baudrates[38400] = calc_result(38400, 78, 12, 48);
206     baudrates[57600] = calc_result(57554, 52, 12, 48);
207     baudrates[115200] = calc_result(115385, 26, 0, 48);
208     baudrates[230400] = calc_result(230769, 13, 0, 48);
209     baudrates[460800] = calc_result(461538, 6, 4, 48);
210     baudrates[921600] = calc_result(923077, 3, 8, 48);
211
212     test_baudrates(ftdi, baudrates);
213 }
214
215 BOOST_AUTO_TEST_CASE(Type4232HFixedBaudrates)
216 {
217     ftdi->type = TYPE_4232H;
218
219     map<int, calc_result> baudrates;
220     baudrates[300] = calc_result(300, 784, 2, 48);
221     baudrates[600] = calc_result(600, 904, 1, 48);
222     baudrates[1200] = calc_result(1200, 452, 0, 48);
223     baudrates[2400] = calc_result(2400, 226, 0, 48);
224     baudrates[4800] = calc_result(4800, 625, 0, 48);
225     baudrates[9600] = calc_result(9600, 312, 4, 48);
226     baudrates[19200] = calc_result(19200, 156, 8, 48);
227     baudrates[38400] = calc_result(38400, 78, 12, 48);
228     baudrates[57600] = calc_result(57554, 52, 12, 48);
229     baudrates[115200] = calc_result(115385, 26, 0, 48);
230     baudrates[230400] = calc_result(230769, 13, 0, 48);
231     baudrates[460800] = calc_result(461538, 6, 4, 48);
232     baudrates[921600] = calc_result(923077, 3, 8, 48);
233
234     test_baudrates(ftdi, baudrates);
235 }
236
237 BOOST_AUTO_TEST_CASE(Type232HFixedBaudrates)
238 {
239     ftdi->type = TYPE_232H;
240
241     map<int, calc_result> baudrates;
242     baudrates[300] = calc_result(300, 784, 2, 48);
243     baudrates[600] = calc_result(600, 904, 1, 48);
244     baudrates[1200] = calc_result(1200, 452, 0, 48);
245     baudrates[2400] = calc_result(2400, 226, 0, 48);
246     baudrates[4800] = calc_result(4800, 625, 0, 48);
247     baudrates[9600] = calc_result(9600, 312, 4, 48);
248     baudrates[19200] = calc_result(19200, 156, 8, 48);
249     baudrates[38400] = calc_result(38400, 78, 12, 48);
250     baudrates[57600] = calc_result(57554, 52, 12, 48);
251     baudrates[115200] = calc_result(115385, 26, 0, 48);
252     baudrates[230400] = calc_result(230769, 13, 0, 48);
253     baudrates[460800] = calc_result(461538, 6, 4, 48);
254     baudrates[921600] = calc_result(923077, 3, 8, 48);
255
256     test_baudrates(ftdi, baudrates);
257 }
258
259 BOOST_AUTO_TEST_SUITE_END()