Match serialized bit-packing for vector<bool> to match in-memory layout for vector_view<bool> so that data can be directly mmapped.

This commit is contained in:
Daniel Patterson
2018-10-26 23:37:36 -07:00
parent b1791d1ab3
commit d80318f8ea
3 changed files with 92 additions and 27 deletions
+39 -7
View File
@@ -1,11 +1,15 @@
#include "storage/serialization.hpp"
#include "util/vector_view.hpp"
#include "../common/range_tools.hpp"
#include "../common/temporary_file.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <random>
BOOST_AUTO_TEST_SUITE(serialization)
using namespace osrm;
@@ -15,20 +19,48 @@ 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, 0, 8), 0x74);
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 5, 7), 0x53);
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 6, 8), 0xa9);
BOOST_CHECK_EQUAL(storage::serialization::detail::packBits(v, 11, 1), 0x01);
}
BOOST_AUTO_TEST_CASE(vector_view_pack_test)
{
// Verifies that the packing generated by packBits matches
// what vector_view<bool> expects
// 1. Generate a random bool vector that covers several uint64_t bytes
constexpr unsigned RANDOM_SEED = 42;
std::mt19937 g(RANDOM_SEED);
std::uniform_int_distribution<> binary_distribution(0, 1);
std::vector<bool> v(150);
for (std::size_t i = 0; i < v.size(); ++i)
v[i] = binary_distribution(g) == 1;
// 2. Pack the vector into a contiguous set of bytes
std::uint64_t data[3];
data[0] = storage::serialization::detail::packBits<decltype(v), std::uint64_t>(v, 0, 64);
data[1] = storage::serialization::detail::packBits<decltype(v), std::uint64_t>(v, 64, 64);
data[2] = storage::serialization::detail::packBits<decltype(v), std::uint64_t>(v, 128, 22);
// 3. Make a vector_view of that memory, and see if the bit sequence is
// interpreted correctly by vector_view
util::vector_view<bool> view(data, v.size());
for (std::size_t index = 0; index < v.size(); ++index)
{
BOOST_CHECK_EQUAL(v[index], view[index]);
}
}
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);
storage::serialization::detail::unpackBits(v, 0, 8, 0x74u);
storage::serialization::detail::unpackBits(v, 5, 7, 0x53u);
storage::serialization::detail::unpackBits(v, 6, 8, 0xa9u);
storage::serialization::detail::unpackBits(v, 11, 1, 0x01u);
BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), expected.begin(), expected.end());
}