1#define BOOST_TEST_MODULE trace_compressed_file
2#include <boost/test/included/unit_test.hpp>
4#include <boost/filesystem.hpp>
5#include <boost/filesystem/fstream.hpp>
19 for (
const auto&
p:
paths) {
27 auto path = bfs::temp_directory_path() / bfs::unique_path();
28 auto os = bfs::ofstream(
path, std::ios_base::out);
31 return paths.emplace_back(std::move(
path)).generic_string();
35 auto path = bfs::temp_directory_path() / bfs::unique_path();
36 auto os = bfs::ofstream(
path, std::ios_base::out|std::ios_base::binary);
38 os.write(
reinterpret_cast<const char*
>(data), size);
40 return paths.emplace_back(std::move(
path)).generic_string();
46typedef std::tuple<uint64_t, std::array<char, 6733>>
test_types;
52 static constexpr bool value =
false;
55 template<
typename T,
size_t S>
56 struct is_std_array<
std::
array<T, S>> {
57 static constexpr bool value =
true;
61 constexpr bool is_std_array_v = is_std_array<T>::value;
65 if constexpr (is_std_array_v<T>) {
67 std::memcpy(result.data(), &
value, std::min<size_t>(
sizeof(
value), result.size()));
76 if constexpr (is_std_array_v<T>) {
77 constexpr size_t input_byte_size = (std::tuple_size_v<T> *
sizeof(
typename T::value_type));
78 constexpr size_t temp_size = (input_byte_size /
sizeof(
uint32_t)) + 1;
80 std::array<uint32_t, temp_size> temp;
81 std::generate(temp.begin(), temp.end(), []() {
82 return (uint32_t)std::rand();
86 std::memcpy(result.data(), temp.data(), input_byte_size);
89 return (
T)std::rand();
95 template<
typename T,
size_t S>
102BOOST_AUTO_TEST_SUITE(compressed_file_tests)
106 auto data = std::vector<T>(128);
107 std::generate(data.begin(), data.end(), [offset=0ULL]()
mutable {
108 auto result = offset;
110 return convert_to<T>(result);
113 auto uncompressed_filename = create_temp_file(data.data(), data.size() *
sizeof(
T));
114 auto compressed_filename = create_temp_file(
nullptr, 0);
119 for (
size_t i = 0; i < data.size(); i++) {
120 const auto& entry = data.at(i);
124 compf.seek((
long)i *
sizeof(
T));
125 compf.read(
reinterpret_cast<char*
>(&
value),
sizeof(
T));
126 BOOST_TEST(
value == entry);
133 auto data = std::vector<T>(128);
134 std::generate(data.begin(), data.end(), [offset=0ULL]()
mutable {
135 auto result = offset;
137 return convert_to<T>(result);
140 auto uncompressed_filename = create_temp_file(data.data(), data.size() *
sizeof(
T));
141 auto compressed_filename = create_temp_file(
nullptr, 0);
148 for(
const auto& entry : data ) {
150 compf.read(
reinterpret_cast<char*
>(&
value),
sizeof(
value));
151 BOOST_TEST(
value == entry);
158 auto data = std::vector<T>(128);
159 std::generate(data.begin(), data.end(), []() {
160 return make_random<T>();
163 auto uncompressed_filename = create_temp_file(data.data(), data.size() *
sizeof(
T));
164 auto compressed_filename = create_temp_file(
nullptr, 0);
169 for (
size_t i = 0; i < data.size(); i++) {
170 auto actual_data = std::vector<T>(128);
173 compf.seek(i *
sizeof(
T));
174 compf.read(
reinterpret_cast<char*
>(actual_data.data()), (actual_data.size() - i) *
sizeof(
T));
176 BOOST_REQUIRE_EQUAL_COLLECTIONS(data.begin() + i, data.end(), actual_data.begin(), actual_data.end() - i);
182 auto data = std::vector<T>(32);
183 std::generate(data.begin(), data.end(), []() {
184 return make_random<T>();
187 auto uncompressed_size = data.size() *
sizeof(
T);
188 auto uncompressed_filename = create_temp_file(data.data(), uncompressed_size);
189 auto compressed_filename = create_temp_file(
nullptr, 0);
197 compressed.
open(
"r");
199 const uint16_t expected_seek_point_count = 0;
200 uint16_t actual_seek_point_count = std::numeric_limits<uint16_t>::max();
201 compressed.
read(
reinterpret_cast<char*
>(&actual_seek_point_count), 2);
202 BOOST_REQUIRE_EQUAL(expected_seek_point_count, actual_seek_point_count);
205 for (
size_t i = 0; i < data.size(); i++) {
206 auto actual_data = std::vector<T>(32);
209 compf.seek(i *
sizeof(
T));
210 compf.read(
reinterpret_cast<char*
>(actual_data.data()), (actual_data.size() - i) *
sizeof(
T));
212 BOOST_REQUIRE_EQUAL_COLLECTIONS(data.begin() + i, data.end(), actual_data.begin(), actual_data.end() - i);
217BOOST_AUTO_TEST_SUITE_END()
void read(char *d, size_t n)
void open(const char *mode)
void set_file_path(fc::path file_path)
wraps boost::filesystem::path to provide platform independent path manipulation.
static bool process(const fc::path &input_path, const fc::path &output_path, size_t seek_point_stride)
uint64_t file_size(const path &p)
std::ostream & operator<<(std::ostream &st, const std::variant< fc::alt_bn128_error, bytes > &err)
#define T(meth, val, expected)
unsigned __int64 uint64_t
std::string create_temp_file(const std::string &contents)
std::string create_temp_file(const void *data, size_t size)
std::list< bfs::path > paths
std::tuple< uint64_t, std::array< char, 6733 > > test_types
BOOST_FIXTURE_TEST_CASE_TEMPLATE(random_access_test, T, test_types, temp_file_fixture)