git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
98 lines
2.5 KiB
C++
98 lines
2.5 KiB
C++
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
/**
|
|
* @file
|
|
* @brief test file for the BaseTrack class
|
|
* @author Dominik Meyer <dmeyer@hsu-hh.de>
|
|
* @date 2018-11-21
|
|
* @copyright 2018 no yet defined
|
|
* @test testing conversion fromSimpleToByteArray and fromByteArrayToSimple
|
|
*/
|
|
#include <BC/BC.hpp>
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <cstdlib>
|
|
#include <cstdint>
|
|
#include <algorithm>
|
|
|
|
int main()
|
|
{
|
|
std::vector<unsigned char> testVector;
|
|
// initialize random number generator
|
|
std::srand(std::time(nullptr));
|
|
int numberRuns=100000;
|
|
|
|
// start with BC::DataTypes::deviceIdType
|
|
BC::DataTypes::deviceIdType dit = std::rand() % UINT32_MAX;
|
|
BC::DataTypes::deviceIdType dit2;
|
|
|
|
for(int i=0; i<numberRuns; i++)
|
|
{
|
|
dit = std::rand() % UINT32_MAX;
|
|
testVector = BC::DataTypes::Convert::fromSimpleToByteArray(dit);
|
|
|
|
dit2=BC::DataTypes::Convert::fromByteArrayToSimple<BC::DataTypes::deviceIdType>(testVector);
|
|
|
|
if ( dit != dit2 )
|
|
{
|
|
std::cout << "Test failed" << std::endl;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
// test fromSimpleVectorToByteArray and back
|
|
std::vector<uint32_t> data;
|
|
std::vector<uint32_t> data2;
|
|
std::vector<unsigned char> v;
|
|
uint32_t nrElements = std::rand() % 100000 +1000;
|
|
for(uint32_t i=0; i<nrElements; i++)
|
|
{
|
|
data.push_back(std::rand() % UINT32_MAX);
|
|
}
|
|
|
|
v=BC::DataTypes::Convert::fromSimpleContainerToByteArray(data);
|
|
data2=BC::DataTypes::Convert::fromByteArrayToSimpleContainer<std::vector<uint32_t>,uint32_t>(v);
|
|
|
|
if (data.size() != data2.size())
|
|
{
|
|
std::cout << "Convert of uint32_t vector failed. Sizes do not match" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
// test if elements are the same
|
|
if (!std::equal(data.begin(), data.end(), data2.begin()))
|
|
{
|
|
std::cout << "Convert of uint32_t vector failed. elements do not match" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
|
|
|
|
//test vector of string conversion
|
|
std::vector<std::string> strings;
|
|
std::vector<std::string> strings2;
|
|
|
|
for(uint32_t i=0; i<20; i++)
|
|
{
|
|
strings.push_back("hallo");
|
|
}
|
|
|
|
v.clear();
|
|
v=BC::DataTypes::Convert::fromStringContainerToByteArray(strings);
|
|
strings2=BC::DataTypes::Convert::fromByteArrayToStringContainer<std::vector<std::string>>(v);
|
|
|
|
// test if elements are the same
|
|
if (!std::equal(strings.begin(), strings.end(), strings2.begin()))
|
|
{
|
|
std::cout << "Convert of string vector failed. elements do not match" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|