2015-10-01 19:55:33 +00:00
|
|
|
/*!
|
|
|
|
* \file unpack_2bit_samples_test.cc
|
|
|
|
* \brief This file implements unit tests for the unpack_2bit_samples
|
|
|
|
* custom block
|
|
|
|
* \author Cillian O'Driscoll, 2015. cillian.odriscoll (at) gmail.com
|
|
|
|
*
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2015-10-01 19:55:33 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2015-10-01 19:55:33 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2015-10-01 19:55:33 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2015-10-01 19:55:33 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
#include "unpack_2bit_samples.h"
|
|
|
|
#include <gnuradio/blocks/stream_to_vector.h>
|
2015-10-01 19:55:33 +00:00
|
|
|
#include <gnuradio/top_block.h>
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <gtest/gtest.h>
|
2019-08-24 15:34:12 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
2018-08-31 13:08:07 +00:00
|
|
|
#ifdef GR_GREATER_38
|
2018-08-31 13:30:22 +00:00
|
|
|
#include <gnuradio/blocks/vector_sink.h>
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <gnuradio/blocks/vector_source.h>
|
2018-08-31 13:08:07 +00:00
|
|
|
#else
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <gnuradio/blocks/vector_sink_b.h>
|
2015-10-01 19:55:33 +00:00
|
|
|
#include <gnuradio/blocks/vector_source_b.h>
|
|
|
|
#include <gnuradio/blocks/vector_source_s.h>
|
2018-08-31 13:08:07 +00:00
|
|
|
#endif
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<uint8_t> packData(std::vector<int8_t> const &raw_data,
|
|
|
|
bool big_endian)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<uint8_t> packed_data(raw_data.size() / 4);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
int shift = (big_endian ? 6 : 0);
|
2015-10-01 19:55:33 +00:00
|
|
|
unsigned int j = 0;
|
|
|
|
|
2018-12-03 17:03:25 +00:00
|
|
|
for (signed char i : raw_data)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
2018-12-03 17:03:25 +00:00
|
|
|
auto val = static_cast<unsigned>((i - 1) / 2 & 0x03);
|
2018-03-03 01:03:39 +00:00
|
|
|
|
|
|
|
packed_data[j] |= val << shift;
|
|
|
|
|
|
|
|
if (big_endian)
|
|
|
|
{
|
|
|
|
shift -= 2;
|
|
|
|
if (shift < 0)
|
|
|
|
{
|
|
|
|
shift = 6;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shift += 2;
|
|
|
|
if (shift > 6)
|
|
|
|
{
|
|
|
|
shift = 0;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return packed_data;
|
|
|
|
}
|
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
TEST(Unpack2bitSamplesTest, CheckBigEndianByte)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
|
|
|
bool big_endian_bytes = true;
|
|
|
|
size_t item_size = 1;
|
|
|
|
bool big_endian_items = false;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<int8_t> raw_data = {-1, 3, 1, -1, -3, 1, 3, 1};
|
|
|
|
std::vector<uint8_t> packed_data = packData(raw_data, big_endian_bytes);
|
|
|
|
std::vector<uint8_t> unpacked_data;
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::top_block_sptr top_block = gr::make_top_block("Unpack2bitSamplesTest");
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_source_b::sptr source =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_source_b::make(packed_data);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2020-04-02 21:59:35 +00:00
|
|
|
auto unpacker =
|
2015-10-01 19:55:33 +00:00
|
|
|
make_unpack_2bit_samples(big_endian_bytes,
|
2018-03-03 01:03:39 +00:00
|
|
|
item_size,
|
|
|
|
big_endian_items);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
gr::blocks::stream_to_vector::sptr stov =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::stream_to_vector::make(item_size, raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_sink_b::sptr sink =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_sink_b::make(raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
top_block->connect(source, 0, unpacker, 0);
|
2018-03-03 01:03:39 +00:00
|
|
|
top_block->connect(unpacker, 0, stov, 0);
|
2015-10-01 19:55:33 +00:00
|
|
|
top_block->connect(stov, 0, sink, 0);
|
|
|
|
|
|
|
|
top_block->run();
|
|
|
|
top_block->stop();
|
|
|
|
|
|
|
|
unpacked_data = sink->data();
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
EXPECT_EQ(raw_data.size(), unpacked_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
for (size_t i = 0; i < raw_data.size(); ++i)
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(raw_data[i], static_cast<int8_t>(unpacked_data[i]));
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
TEST(Unpack2bitSamplesTest, CheckLittleEndianByte)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
|
|
|
bool big_endian_bytes = false;
|
|
|
|
size_t item_size = 1;
|
|
|
|
bool big_endian_items = false;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<int8_t> raw_data = {-1, 3, 1, -1, -3, 1, 3, 1};
|
|
|
|
std::vector<uint8_t> packed_data = packData(raw_data, big_endian_bytes);
|
|
|
|
std::vector<uint8_t> unpacked_data;
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::top_block_sptr top_block = gr::make_top_block("Unpack2bitSamplesTest");
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_source_b::sptr source =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_source_b::make(packed_data);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2020-04-02 21:59:35 +00:00
|
|
|
auto unpacker =
|
2015-10-01 19:55:33 +00:00
|
|
|
make_unpack_2bit_samples(big_endian_bytes,
|
2018-03-03 01:03:39 +00:00
|
|
|
item_size,
|
|
|
|
big_endian_items);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
gr::blocks::stream_to_vector::sptr stov =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::stream_to_vector::make(item_size, raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_sink_b::sptr sink =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_sink_b::make(raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
top_block->connect(source, 0, unpacker, 0);
|
2018-03-03 01:03:39 +00:00
|
|
|
top_block->connect(unpacker, 0, stov, 0);
|
2015-10-01 19:55:33 +00:00
|
|
|
top_block->connect(stov, 0, sink, 0);
|
|
|
|
|
|
|
|
top_block->run();
|
|
|
|
top_block->stop();
|
|
|
|
|
|
|
|
unpacked_data = sink->data();
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
EXPECT_EQ(raw_data.size(), unpacked_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
for (size_t i = 0; i < raw_data.size(); ++i)
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(raw_data[i], static_cast<int8_t>(unpacked_data[i]));
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
TEST(Unpack2bitSamplesTest, CheckBigEndianShortBigEndianByte)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
|
|
|
bool big_endian_bytes = true;
|
|
|
|
size_t item_size = 2;
|
|
|
|
bool big_endian_items = true;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<int8_t> raw_data = {-1, 3, 1, -1, -3, 1, 3, 1};
|
|
|
|
std::vector<uint8_t> packed_data = packData(raw_data, big_endian_bytes);
|
2015-10-01 19:55:33 +00:00
|
|
|
// change the order of each pair of bytes:
|
2019-08-24 15:34:12 +00:00
|
|
|
for (size_t ii = 0; ii < packed_data.size(); ii += item_size)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
unsigned int kk = ii + item_size - 1;
|
|
|
|
unsigned int jj = ii;
|
|
|
|
while (kk > jj)
|
|
|
|
{
|
|
|
|
uint8_t tmp = packed_data[jj];
|
|
|
|
packed_data[jj] = packed_data[kk];
|
|
|
|
packed_data[kk] = tmp;
|
|
|
|
--kk;
|
|
|
|
++jj;
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now create a new big endian buffer:
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<int16_t> packed_data_short(
|
|
|
|
reinterpret_cast<int16_t *>(&packed_data[0]),
|
|
|
|
reinterpret_cast<int16_t *>(&packed_data[0]) + packed_data.size() / item_size);
|
2017-07-11 10:22:05 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<uint8_t> unpacked_data;
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::top_block_sptr top_block = gr::make_top_block("Unpack2bitSamplesTest");
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_source_s::sptr source =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_source_s::make(packed_data_short);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2020-04-02 21:59:35 +00:00
|
|
|
auto unpacker =
|
2015-10-01 19:55:33 +00:00
|
|
|
make_unpack_2bit_samples(big_endian_bytes,
|
2018-03-03 01:03:39 +00:00
|
|
|
item_size,
|
|
|
|
big_endian_items);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
gr::blocks::stream_to_vector::sptr stov =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::stream_to_vector::make(1, raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_sink_b::sptr sink =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_sink_b::make(raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
top_block->connect(source, 0, unpacker, 0);
|
2018-03-03 01:03:39 +00:00
|
|
|
top_block->connect(unpacker, 0, stov, 0);
|
2015-10-01 19:55:33 +00:00
|
|
|
top_block->connect(stov, 0, sink, 0);
|
|
|
|
|
|
|
|
top_block->run();
|
|
|
|
top_block->stop();
|
|
|
|
|
|
|
|
unpacked_data = sink->data();
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
EXPECT_EQ(raw_data.size(), unpacked_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
for (size_t i = 0; i < raw_data.size(); ++i)
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(raw_data[i], static_cast<int8_t>(unpacked_data[i]));
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
TEST(Unpack2bitSamplesTest, CheckBigEndianShortLittleEndianByte)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
|
|
|
bool big_endian_bytes = false;
|
|
|
|
size_t item_size = 2;
|
|
|
|
bool big_endian_items = true;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<int8_t> raw_data = {-1, 3, 1, -1, -3, 1, 3, 1};
|
|
|
|
std::vector<uint8_t> packed_data = packData(raw_data, big_endian_bytes);
|
2015-10-01 19:55:33 +00:00
|
|
|
// change the order of each pair of bytes:
|
2019-08-24 15:34:12 +00:00
|
|
|
for (size_t ii = 0; ii < packed_data.size(); ii += item_size)
|
2015-10-01 19:55:33 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
unsigned int kk = ii + item_size - 1;
|
|
|
|
unsigned int jj = ii;
|
|
|
|
while (kk > jj)
|
|
|
|
{
|
|
|
|
uint8_t tmp = packed_data[jj];
|
|
|
|
packed_data[jj] = packed_data[kk];
|
|
|
|
packed_data[kk] = tmp;
|
|
|
|
--kk;
|
|
|
|
++jj;
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now create a new big endian buffer:
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<int16_t> packed_data_short(
|
|
|
|
reinterpret_cast<int16_t *>(&packed_data[0]),
|
|
|
|
reinterpret_cast<int16_t *>(&packed_data[0]) + packed_data.size() / item_size);
|
2017-07-11 10:22:05 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::vector<uint8_t> unpacked_data;
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::top_block_sptr top_block = gr::make_top_block("Unpack2bitSamplesTest");
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_source_s::sptr source =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_source_s::make(packed_data_short);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2020-04-02 21:59:35 +00:00
|
|
|
auto unpacker =
|
2015-10-01 19:55:33 +00:00
|
|
|
make_unpack_2bit_samples(big_endian_bytes,
|
2018-03-03 01:03:39 +00:00
|
|
|
item_size,
|
|
|
|
big_endian_items);
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
gr::blocks::stream_to_vector::sptr stov =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::stream_to_vector::make(1, raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2017-07-11 10:22:05 +00:00
|
|
|
gr::blocks::vector_sink_b::sptr sink =
|
2018-03-03 01:03:39 +00:00
|
|
|
gr::blocks::vector_sink_b::make(raw_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
|
|
|
top_block->connect(source, 0, unpacker, 0);
|
2018-03-03 01:03:39 +00:00
|
|
|
top_block->connect(unpacker, 0, stov, 0);
|
2015-10-01 19:55:33 +00:00
|
|
|
top_block->connect(stov, 0, sink, 0);
|
|
|
|
|
|
|
|
top_block->run();
|
|
|
|
top_block->stop();
|
|
|
|
|
|
|
|
unpacked_data = sink->data();
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
EXPECT_EQ(raw_data.size(), unpacked_data.size());
|
2015-10-01 19:55:33 +00:00
|
|
|
|
2019-08-24 15:34:12 +00:00
|
|
|
for (size_t i = 0; i < raw_data.size(); ++i)
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ(raw_data[i], static_cast<int8_t>(unpacked_data[i]));
|
|
|
|
}
|
2015-10-01 19:55:33 +00:00
|
|
|
}
|