2 @brief Test baudrate calculator code
7 /***************************************************************************
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; *
13 ***************************************************************************/
17 #define BOOST_TEST_DYN_LINK
18 #include <boost/test/unit_test.hpp>
19 #include <boost/foreach.hpp>
25 extern "C" int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
26 unsigned short *value, unsigned short *index);
28 /// Basic initialization of libftdi for every test
48 BOOST_FIXTURE_TEST_SUITE(Baudrate, BaseFTDIFixture)
50 /// Helper class to store the convert_baudrate_UT_export result
54 unsigned short divisor;
55 unsigned short fractional_bits;
58 calc_result(int actual, unsigned short my_divisor, unsigned short my_fractional_bits, unsigned short my_clock)
59 : actual_baudrate(actual)
61 , fractional_bits(my_fractional_bits)
76 * @brief Test convert_baudrate code against a list of baud rates
78 * @param baudrates Baudrates to check
80 static void test_baudrates(ftdi_context *ftdi, const map<int, calc_result> &baudrates)
82 typedef std::pair<int, calc_result> baudrate_type;
83 BOOST_FOREACH(const baudrate_type &baudrate, baudrates)
85 unsigned short calc_value = 0, calc_index = 0;
86 int calc_baudrate = convert_baudrate_UT_export(baudrate.first, ftdi, &calc_value, &calc_index);
88 const calc_result *res = &baudrate.second;
90 unsigned short divisor = calc_value & 0x3ff;
91 unsigned short fractional_bits = (calc_index & 0x100) ? 4 : (calc_value >> 12);
92 unsigned short clock = (calc_index & 0x100) ? 120 : 48;
94 // Aid debugging since this test is a generic function
95 BOOST_CHECK_MESSAGE(res->actual_baudrate == calc_baudrate && res->divisor == divisor && res->fractional_bits == fractional_bits
96 && res->clock == clock,
97 "\n\nERROR: baudrate calculation failed for --" << baudrate.first << " baud--. Details below: ");
99 BOOST_CHECK_EQUAL(res->actual_baudrate, calc_baudrate);
100 BOOST_CHECK_EQUAL(res->divisor, divisor);
101 BOOST_CHECK_EQUAL(res->fractional_bits, fractional_bits);
102 BOOST_CHECK_EQUAL(res->clock, clock);
106 BOOST_AUTO_TEST_CASE(TypeAMFixedBaudrates)
108 ftdi->type = TYPE_AM;
110 map<int, calc_result> baudrates;
111 baudrates[300] = calc_result(300, 784, 2, 48);
112 baudrates[600] = calc_result(600, 904, 1, 48);
113 baudrates[1200] = calc_result(1200, 452, 0, 48);
114 baudrates[2400] = calc_result(2400, 226, 0, 48);
115 baudrates[4800] = calc_result(4800, 625, 0, 48);
116 baudrates[9600] = calc_result(9600, 312, 4, 48);
117 baudrates[19200] = calc_result(19200, 156, 8, 48);
118 baudrates[38400] = calc_result(38400, 78, 12, 48);
119 baudrates[57600] = calc_result(57554, 52, 12, 48);
120 baudrates[115200] = calc_result(115385, 26, 0, 48);
121 baudrates[230400] = calc_result(230769, 13, 0, 48);
122 baudrates[460800] = calc_result(461538, 6, 4, 48);
123 baudrates[921600] = calc_result(923077, 3, 8, 48);
125 test_baudrates(ftdi, baudrates);
128 BOOST_AUTO_TEST_CASE(Type_BM_232C_R_FixedBaudrates)
130 // Unify testing of chips behaving the same
131 std::vector<enum ftdi_chip_type> test_types;
132 test_types.push_back(TYPE_BM);
133 test_types.push_back(TYPE_2232C);
134 test_types.push_back(TYPE_R);
136 map<int, calc_result> baudrates;
137 baudrates[300] = calc_result(300, 784, 2, 48);
138 baudrates[600] = calc_result(600, 904, 1, 48);
139 baudrates[1200] = calc_result(1200, 452, 0, 48);
140 baudrates[2400] = calc_result(2400, 226, 0, 48);
141 baudrates[4800] = calc_result(4800, 625, 0, 48);
142 baudrates[9600] = calc_result(9600, 312, 4, 48);
143 baudrates[19200] = calc_result(19200, 156, 8, 48);
144 baudrates[38400] = calc_result(38400, 78, 12, 48);
145 baudrates[57600] = calc_result(57553, 52, 12, 48);
146 baudrates[115200] = calc_result(115384, 26, 0, 48);
147 baudrates[230400] = calc_result(230769, 13, 0, 48);
148 baudrates[460800] = calc_result(461538, 6, 4, 48);
149 baudrates[921600] = calc_result(923076, 3, 8, 48);
151 BOOST_FOREACH(const enum ftdi_chip_type &test_chip_type, test_types)
153 ftdi->type = test_chip_type;
154 test_baudrates(ftdi, baudrates);
158 BOOST_AUTO_TEST_CASE(Type_x232H_FixedBaudrates)
160 // Unify testing of chips behaving the same
161 std::vector<enum ftdi_chip_type> test_types;
162 test_types.push_back(TYPE_2232H);
163 test_types.push_back(TYPE_4232H);
164 test_types.push_back(TYPE_232H);
166 map<int, calc_result> baudrates;
167 baudrates[300] = calc_result(300, 784, 2, 48);
168 baudrates[600] = calc_result(600, 904, 1, 48);
169 baudrates[1200] = calc_result(1200, 784, 2, 48);
170 baudrates[2400] = calc_result(2400, 904, 1, 48);
171 baudrates[4800] = calc_result(4800, 452, 0, 48);
172 baudrates[9600] = calc_result(9600, 226, 0, 48);
173 baudrates[19200] = calc_result(19200, 625, 0, 48);
174 baudrates[38400] = calc_result(38400, 312, 4, 48);
175 baudrates[57600] = calc_result(57588, 208, 4, 120);
176 baudrates[115200] = calc_result(115246, 104, 12, 48);
177 baudrates[230400] = calc_result(230215, 52, 12, 48);
178 baudrates[460800] = calc_result(461538, 26, 0, 48);
179 baudrates[921600] = calc_result(923076, 13, 0, 48);
181 BOOST_FOREACH(const enum ftdi_chip_type &test_chip_type, test_types)
183 ftdi->type = test_chip_type;
184 test_baudrates(ftdi, baudrates);
188 BOOST_AUTO_TEST_SUITE_END()