Revision 79ea83d6 host/test/convert_types_test.cpp

b/host/test/convert_types_test.cpp
27 27
using namespace uhd;
28 28

  
29 29
//typedefs for complex types
30
typedef std::complex<boost::uint16_t> sc16_t;
30
typedef std::complex<boost::int16_t> sc16_t;
31 31
typedef std::complex<float> fc32_t;
32 32

  
33 33
//extract pointer to POD since using &vector.front() throws in MSVC
......
158 158
        test_convert_types_fc32(nsamps, io_type, otw_type);
159 159
    }
160 160
}
161

  
162
/***********************************************************************
163
 * Test float to short conversion loopback
164
 **********************************************************************/
165
BOOST_AUTO_TEST_CASE(test_convert_types_fc32_to_sc16){
166
    io_type_t io_type_in(io_type_t::COMPLEX_FLOAT32);
167
    io_type_t io_type_out(io_type_t::COMPLEX_INT16);
168

  
169
    otw_type_t otw_type;
170
    otw_type.byteorder = otw_type_t::BO_NATIVE;
171
    otw_type.width = 16;
172

  
173
    const size_t nsamps = 13;
174
    std::vector<fc32_t> input(nsamps);
175
    BOOST_FOREACH(fc32_t &in, input) in = fc32_t(
176
        (std::rand()/float(RAND_MAX/2)) - 1,
177
        (std::rand()/float(RAND_MAX/2)) - 1
178
    );
179

  
180
    //convert float to dev
181
    std::vector<boost::uint32_t> tmp(nsamps);
182
    transport::convert_io_type_to_otw_type(
183
        pod2ptr(input), io_type_in,
184
        pod2ptr(tmp), otw_type,
185
        nsamps
186
    );
187

  
188
    //convert dev to short
189
    std::vector<sc16_t> output(nsamps);
190
    transport::convert_otw_type_to_io_type(
191
        pod2ptr(tmp), otw_type,
192
        pod2ptr(output), io_type_out,
193
        nsamps
194
    );
195

  
196
    //test that the inputs and outputs match
197
    for (size_t i = 0; i < nsamps; i++){
198
        BOOST_CHECK_CLOSE_FRACTION(input[i].real(), output[i].real()/float(32767), float(0.01));
199
        BOOST_CHECK_CLOSE_FRACTION(input[i].imag(), output[i].imag()/float(32767), float(0.01));
200
    }
201
}
202

  
203
/***********************************************************************
204
 * Test short to float conversion loopback
205
 **********************************************************************/
206
BOOST_AUTO_TEST_CASE(test_convert_types_sc16_to_fc32){
207
    io_type_t io_type_in(io_type_t::COMPLEX_INT16);
208
    io_type_t io_type_out(io_type_t::COMPLEX_FLOAT32);
209

  
210
    otw_type_t otw_type;
211
    otw_type.byteorder = otw_type_t::BO_NATIVE;
212
    otw_type.width = 16;
213

  
214
    const size_t nsamps = 13;
215
    std::vector<sc16_t> input(nsamps);
216
    BOOST_FOREACH(sc16_t &in, input) in = sc16_t(
217
        std::rand()-(RAND_MAX/2),
218
        std::rand()-(RAND_MAX/2)
219
    );
220

  
221
    //convert short to dev
222
    std::vector<boost::uint32_t> tmp(nsamps);
223
    transport::convert_io_type_to_otw_type(
224
        pod2ptr(input), io_type_in,
225
        pod2ptr(tmp), otw_type,
226
        nsamps
227
    );
228

  
229
    //convert dev to float
230
    std::vector<fc32_t> output(nsamps);
231
    transport::convert_otw_type_to_io_type(
232
        pod2ptr(tmp), otw_type,
233
        pod2ptr(output), io_type_out,
234
        nsamps
235
    );
236

  
237
    //test that the inputs and outputs match
238
    for (size_t i = 0; i < nsamps; i++){
239
        BOOST_CHECK_CLOSE_FRACTION(input[i].real()/float(32767), output[i].real(), float(0.01));
240
        BOOST_CHECK_CLOSE_FRACTION(input[i].imag()/float(32767), output[i].imag(), float(0.01));
241
    }
242
}

Also available in: Unified diff