test/baudrate.cpp: Use orthogonal names for the test-targets
[libftdi] / test / baudrate.cpp
CommitLineData
a87a0712
TJ
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
ac6944cc
TJ
15#include <ftdi.h>
16
a87a0712
TJ
17#define BOOST_TEST_DYN_LINK
18#include <boost/test/unit_test.hpp>
ac6944cc 19#include <boost/foreach.hpp>
3db4042e 20#include <vector>
ac6944cc
TJ
21#include <map>
22
23using namespace std;
24
25extern "C" int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
26 unsigned short *value, unsigned short *index);
27
28/// Basic initialization of libftdi for every test
29class BaseFTDIFixture
30{
31protected:
32 ftdi_context *ftdi;
33
34public:
35 BaseFTDIFixture()
36 : ftdi(NULL)
37 {
38 ftdi = ftdi_new();
39 }
40
41 ~BaseFTDIFixture()
42 {
43 delete ftdi;
44 ftdi = NULL;
45 }
46};
a87a0712 47
c4517f98 48BOOST_FIXTURE_TEST_SUITE(Baudrate, BaseFTDIFixture)
a87a0712 49
ac6944cc
TJ
50/// Helper class to store the convert_baudrate_UT_export result
51struct calc_result
a87a0712 52{
ac6944cc 53 int actual_baudrate;
cb2a07f2
TJ
54 unsigned short divisor;
55 unsigned short fractional_bits;
56 unsigned short clock;
ac6944cc 57
cb2a07f2 58 calc_result(int actual, unsigned short my_divisor, unsigned short my_fractional_bits, unsigned short my_clock)
ac6944cc 59 : actual_baudrate(actual)
cb2a07f2
TJ
60 , divisor(my_divisor)
61 , fractional_bits(my_fractional_bits)
62 , clock(my_clock)
ac6944cc
TJ
63 {
64 }
65
66 calc_result()
67 : actual_baudrate(0)
cb2a07f2
TJ
68 , divisor(0)
69 , fractional_bits(0)
70 , clock(0)
ac6944cc
TJ
71 {
72 }
73};
74
75/**
76 * @brief Test convert_baudrate code against a list of baud rates
77 *
78 * @param baudrates Baudrates to check
79 **/
80static void test_baudrates(ftdi_context *ftdi, const map<int, calc_result> &baudrates)
81{
82 typedef std::pair<int, calc_result> baudrate_type;
83 BOOST_FOREACH(const baudrate_type &baudrate, baudrates)
84 {
85 unsigned short calc_value = 0, calc_index = 0;
86 int calc_baudrate = convert_baudrate_UT_export(baudrate.first, ftdi, &calc_value, &calc_index);
87
88 const calc_result *res = &baudrate.second;
89
9956d428
UB
90 unsigned short divisor = calc_value & 0x3fff;
91 unsigned short fractional_bits = (calc_value >> 14);
92 unsigned short clock = (calc_index & 0x200) ? 120 : 48;
93
94 switch (ftdi->type)
95 {
96 case TYPE_232H:
97 case TYPE_2232H:
98 case TYPE_4232H:
99 fractional_bits |= (calc_index & 0x100) ? 4 : 0;
100 break;
101 case TYPE_R:
102 case TYPE_2232C:
103 case TYPE_BM:
104 fractional_bits |= (calc_index & 0x001) ? 4 : 0;
105 break;
106 default:;
107 }
ac6944cc 108 // Aid debugging since this test is a generic function
cb2a07f2
TJ
109 BOOST_CHECK_MESSAGE(res->actual_baudrate == calc_baudrate && res->divisor == divisor && res->fractional_bits == fractional_bits
110 && res->clock == clock,
c4517f98 111 "\n\nERROR: baudrate calculation failed for --" << baudrate.first << " baud--. Details below: ");
ac6944cc
TJ
112
113 BOOST_CHECK_EQUAL(res->actual_baudrate, calc_baudrate);
cb2a07f2
TJ
114 BOOST_CHECK_EQUAL(res->divisor, divisor);
115 BOOST_CHECK_EQUAL(res->fractional_bits, fractional_bits);
116 BOOST_CHECK_EQUAL(res->clock, clock);
ac6944cc
TJ
117 }
118}
119
120BOOST_AUTO_TEST_CASE(TypeAMFixedBaudrates)
121{
122 ftdi->type = TYPE_AM;
123
124 map<int, calc_result> baudrates;
cb2a07f2
TJ
125 baudrates[300] = calc_result(300, 784, 2, 48);
126 baudrates[600] = calc_result(600, 904, 1, 48);
127 baudrates[1200] = calc_result(1200, 452, 0, 48);
128 baudrates[2400] = calc_result(2400, 226, 0, 48);
129 baudrates[4800] = calc_result(4800, 625, 0, 48);
130 baudrates[9600] = calc_result(9600, 312, 4, 48);
131 baudrates[19200] = calc_result(19200, 156, 8, 48);
132 baudrates[38400] = calc_result(38400, 78, 12, 48);
133 baudrates[57600] = calc_result(57554, 52, 12, 48);
134 baudrates[115200] = calc_result(115385, 26, 0, 48);
135 baudrates[230400] = calc_result(230769, 13, 0, 48);
136 baudrates[460800] = calc_result(461538, 6, 4, 48);
137 baudrates[921600] = calc_result(923077, 3, 8, 48);
ac6944cc
TJ
138
139 test_baudrates(ftdi, baudrates);
140}
141
6645ac5e 142BOOST_AUTO_TEST_CASE(TypeBMFixedBaudrates)
ac6944cc 143{
3db4042e
TJ
144 // Unify testing of chips behaving the same
145 std::vector<enum ftdi_chip_type> test_types;
146 test_types.push_back(TYPE_BM);
147 test_types.push_back(TYPE_2232C);
148 test_types.push_back(TYPE_R);
ac6944cc
TJ
149
150 map<int, calc_result> baudrates;
cb2a07f2
TJ
151 baudrates[300] = calc_result(300, 784, 2, 48);
152 baudrates[600] = calc_result(600, 904, 1, 48);
153 baudrates[1200] = calc_result(1200, 452, 0, 48);
154 baudrates[2400] = calc_result(2400, 226, 0, 48);
155 baudrates[4800] = calc_result(4800, 625, 0, 48);
156 baudrates[9600] = calc_result(9600, 312, 4, 48);
157 baudrates[19200] = calc_result(19200, 156, 8, 48);
158 baudrates[38400] = calc_result(38400, 78, 12, 48);
c4a2bc3a
TJ
159 baudrates[57600] = calc_result(57553, 52, 12, 48);
160 baudrates[115200] = calc_result(115384, 26, 0, 48);
cb2a07f2
TJ
161 baudrates[230400] = calc_result(230769, 13, 0, 48);
162 baudrates[460800] = calc_result(461538, 6, 4, 48);
c4a2bc3a 163 baudrates[921600] = calc_result(923076, 3, 8, 48);
ac6944cc 164
3db4042e
TJ
165 BOOST_FOREACH(const enum ftdi_chip_type &test_chip_type, test_types)
166 {
167 ftdi->type = test_chip_type;
168 test_baudrates(ftdi, baudrates);
169 }
ac6944cc
TJ
170}
171
6645ac5e 172BOOST_AUTO_TEST_CASE(TypeHFixedBaudrates)
ac6944cc 173{
3db4042e
TJ
174 // Unify testing of chips behaving the same
175 std::vector<enum ftdi_chip_type> test_types;
176 test_types.push_back(TYPE_2232H);
177 test_types.push_back(TYPE_4232H);
178 test_types.push_back(TYPE_232H);
ac6944cc
TJ
179
180 map<int, calc_result> baudrates;
cb2a07f2
TJ
181 baudrates[300] = calc_result(300, 784, 2, 48);
182 baudrates[600] = calc_result(600, 904, 1, 48);
c4a2bc3a
TJ
183 baudrates[1200] = calc_result(1200, 784, 2, 48);
184 baudrates[2400] = calc_result(2400, 904, 1, 48);
185 baudrates[4800] = calc_result(4800, 452, 0, 48);
186 baudrates[9600] = calc_result(9600, 226, 0, 48);
187 baudrates[19200] = calc_result(19200, 625, 0, 48);
188 baudrates[38400] = calc_result(38400, 312, 4, 48);
189 baudrates[57600] = calc_result(57588, 208, 4, 120);
190 baudrates[115200] = calc_result(115246, 104, 12, 48);
191 baudrates[230400] = calc_result(230215, 52, 12, 48);
192 baudrates[460800] = calc_result(461538, 26, 0, 48);
193 baudrates[921600] = calc_result(923076, 13, 0, 48);
ac6944cc 194
3db4042e
TJ
195 BOOST_FOREACH(const enum ftdi_chip_type &test_chip_type, test_types)
196 {
197 ftdi->type = test_chip_type;
198 test_baudrates(ftdi, baudrates);
199 }
a87a0712
TJ
200}
201
202BOOST_AUTO_TEST_SUITE_END()