Fix vector<bool> serialization for tar files and add unit tests
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#include "storage/serialization.hpp"
|
||||
|
||||
#include "../common/range_tools.hpp"
|
||||
#include "../common/temporary_file.hpp"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(serialization)
|
||||
|
||||
using namespace osrm;
|
||||
using namespace osrm::storage;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(pack_test)
|
||||
{
|
||||
std::vector<bool> v = {0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1};
|
||||
|
||||
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 0, 8), 0x2e);
|
||||
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 5, 7), 0x65);
|
||||
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 6, 8), 0x95);
|
||||
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 11, 1), 0x01);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(unpack_test)
|
||||
{
|
||||
std::vector<bool> v(14), expected = {0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1};
|
||||
|
||||
storage::serialization::detail::unpackBits(v, 0, 8, 0x2e);
|
||||
storage::serialization::detail::unpackBits(v, 5, 7, 0x65);
|
||||
storage::serialization::detail::unpackBits(v, 6, 8, 0x95);
|
||||
storage::serialization::detail::unpackBits(v, 11, 1, 0x01);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), expected.begin(), expected.end());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(bin_serialize_bool_vector)
|
||||
{
|
||||
TemporaryFile tmp;
|
||||
{
|
||||
std::vector<std::vector<bool>> data = {
|
||||
{}, {0}, {1, 1, 1}, {1, 1, 0, 0, 1, 1, 0, 0}, {1, 1, 0, 0, 1, 1, 0, 0, 1}};
|
||||
for (const auto &v : data)
|
||||
{
|
||||
{
|
||||
io::FileWriter writer(tmp.path, io::FileWriter::GenerateFingerprint);
|
||||
storage::serialization::write(writer, v);
|
||||
}
|
||||
std::vector<bool> result;
|
||||
io::FileReader reader(tmp.path, io::FileReader::VerifyFingerprint);
|
||||
storage::serialization::read(reader, result);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), result.begin(), result.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(tar_serialize_bool_vector)
|
||||
{
|
||||
TemporaryFile tmp;
|
||||
{
|
||||
std::vector<std::vector<bool>> data = {
|
||||
{}, {0}, {1, 1, 1}, {1, 1, 0, 0, 1, 1, 0, 0}, {1, 1, 0, 0, 1, 1, 0, 0, 1}};
|
||||
for (const auto &v : data)
|
||||
{
|
||||
{
|
||||
tar::FileWriter writer(tmp.path, tar::FileWriter::GenerateFingerprint);
|
||||
storage::serialization::write(writer, "my_boolean_vector", v);
|
||||
}
|
||||
std::vector<bool> result;
|
||||
tar::FileReader reader(tmp.path, tar::FileReader::VerifyFingerprint);
|
||||
storage::serialization::read(reader, "my_boolean_vector", result);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), result.begin(), result.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "storage/tar.hpp"
|
||||
|
||||
#include "../common/range_tools.hpp"
|
||||
#include "../common/temporary_file.hpp"
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
@@ -50,7 +51,7 @@ BOOST_AUTO_TEST_CASE(read_tar_file)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(write_tar_file)
|
||||
{
|
||||
boost::filesystem::path tmp_path(TEST_DATA_DIR "/tar_write_test.tar");
|
||||
TemporaryFile tmp {TEST_DATA_DIR "/tar_write_test.tar"};
|
||||
|
||||
std::uint64_t single_64bit_integer = 0xDEADBEEFAABBCCDD;
|
||||
std::uint32_t single_32bit_integer = 0xDEADBEEF;
|
||||
@@ -60,7 +61,7 @@ BOOST_AUTO_TEST_CASE(write_tar_file)
|
||||
0, 1, 2, 3, 4, 1ULL << 62, 0, 1 << 22, 0xFFFFFFFFFFFFFFFF};
|
||||
|
||||
{
|
||||
storage::tar::FileWriter writer(tmp_path, storage::tar::FileWriter::GenerateFingerprint);
|
||||
storage::tar::FileWriter writer(tmp.path, storage::tar::FileWriter::GenerateFingerprint);
|
||||
writer.WriteOne("foo/single_64bit_integer", single_64bit_integer);
|
||||
writer.WriteOne("bar/single_32bit_integer", single_32bit_integer);
|
||||
writer.WriteElementCount64("baz/bla/64bit_vector", vector_64bit.size());
|
||||
@@ -69,7 +70,7 @@ BOOST_AUTO_TEST_CASE(write_tar_file)
|
||||
writer.WriteFrom("32bit_vector", vector_32bit.data(), vector_32bit.size());
|
||||
}
|
||||
|
||||
storage::tar::FileReader reader(tmp_path, storage::tar::FileReader::VerifyFingerprint);
|
||||
storage::tar::FileReader reader(tmp.path, storage::tar::FileReader::VerifyFingerprint);
|
||||
|
||||
BOOST_CHECK_EQUAL(reader.ReadOne<std::uint32_t>("bar/single_32bit_integer"),
|
||||
single_32bit_integer);
|
||||
|
||||
Reference in New Issue
Block a user