Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_compressed_file.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE trace_compressed_file
2#include <boost/test/included/unit_test.hpp>
3#include <list>
4#include <boost/filesystem.hpp>
5#include <boost/filesystem/fstream.hpp>
6
9
10using namespace sysio;
11using namespace sysio::trace_api;
12
13namespace bfs = boost::filesystem;
14
17
19 for (const auto& p: paths) {
20 if (bfs::exists(p)) {
21 bfs::remove(p);
22 }
23 }
24 }
25
26 std::string create_temp_file( const std::string& contents ) {
27 auto path = bfs::temp_directory_path() / bfs::unique_path();
28 auto os = bfs::ofstream(path, std::ios_base::out);
29 os << contents;
30 os.close();
31 return paths.emplace_back(std::move(path)).generic_string();
32 }
33
34 std::string create_temp_file( const void* data, size_t size ) {
35 auto path = bfs::temp_directory_path() / bfs::unique_path();
36 auto os = bfs::ofstream(path, std::ios_base::out|std::ios_base::binary);
37 if (data && size)
38 os.write(reinterpret_cast<const char*>(data), size);
39 os.close();
40 return paths.emplace_back(std::move(path)).generic_string();
41 }
42
43 std::list<bfs::path> paths;
44};
45
46typedef std::tuple<uint64_t, std::array<char, 6733>> test_types;
47
48namespace {
49
50 template<typename T>
51 struct is_std_array {
52 static constexpr bool value = false;
53 };
54
55 template<typename T, size_t S>
56 struct is_std_array<std::array<T, S>> {
57 static constexpr bool value = true;
58 };
59
60 template<typename T>
61 constexpr bool is_std_array_v = is_std_array<T>::value;
62
63 template<typename T>
64 T convert_to(uint64_t value) {
65 if constexpr (is_std_array_v<T>) {
66 T result({0});
67 std::memcpy(result.data(), &value, std::min<size_t>(sizeof(value), result.size()));
68 return result;
69 } else {
70 return T(value);
71 }
72 }
73
74 template<typename T>
75 T make_random() {
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;
79
80 std::array<uint32_t, temp_size> temp;
81 std::generate(temp.begin(), temp.end(), []() {
82 return (uint32_t)std::rand();
83 });
84
85 T result;
86 std::memcpy(result.data(), temp.data(), input_byte_size);
87 return result;
88 } else {
89 return (T)std::rand();
90 }
91 }
92}
93
94namespace std {
95 template<typename T, size_t S>
96 std::ostream& operator<<(std::ostream &os, const std::array<T,S> &array) {
98 return os;
99 }
100}
101
102BOOST_AUTO_TEST_SUITE(compressed_file_tests)
103
105 // generate a large dataset where ever 8 bytes is the offset to that 8 bytes of data
106 auto data = std::vector<T>(128);
107 std::generate(data.begin(), data.end(), [offset=0ULL]() mutable {
108 auto result = offset;
109 offset+=sizeof(T);
110 return convert_to<T>(result);
111 });
112
113 auto uncompressed_filename = create_temp_file(data.data(), data.size() * sizeof(T));
114 auto compressed_filename = create_temp_file(nullptr, 0);
115
116 BOOST_TEST(compressed_file::process(uncompressed_filename, compressed_filename, 512));
117
118 // test that you can read all of the offsets from the compressed form by opening and seeking to them
119 for (size_t i = 0; i < data.size(); i++) {
120 const auto& entry = data.at(i);
121 auto compf = compressed_file(compressed_filename);
122 compf.open();
123 T value;
124 compf.seek((long)i * sizeof(T));
125 compf.read(reinterpret_cast<char*>(&value), sizeof(T));
126 BOOST_TEST(value == entry);
127 compf.close();
128 }
129}
130
132 // generate a large dataset where ever 8 bytes is the offset to that 8 bytes of data
133 auto data = std::vector<T>(128);
134 std::generate(data.begin(), data.end(), [offset=0ULL]() mutable {
135 auto result = offset;
136 offset+=sizeof(T);
137 return convert_to<T>(result);
138 });
139
140 auto uncompressed_filename = create_temp_file(data.data(), data.size() * sizeof(T));
141 auto compressed_filename = create_temp_file(nullptr, 0);
142
143 BOOST_TEST(compressed_file::process(uncompressed_filename, compressed_filename, 512));
144
145 // test that you can read all of the offsets from the compressed form sequentially
146 auto compf = compressed_file(compressed_filename);
147 compf.open();
148 for( const auto& entry : data ) {
149 T value;
150 compf.read(reinterpret_cast<char*>(&value), sizeof(value));
151 BOOST_TEST(value == entry);
152 }
153 compf.close();
154}
155
157 // generate a large dataset where ever 8 bytes is the offset to that 8 bytes of data
158 auto data = std::vector<T>(128);
159 std::generate(data.begin(), data.end(), []() {
160 return make_random<T>();
161 });
162
163 auto uncompressed_filename = create_temp_file(data.data(), data.size() * sizeof(T));
164 auto compressed_filename = create_temp_file(nullptr, 0);
165
166 BOOST_TEST(compressed_file::process(uncompressed_filename, compressed_filename, 512));
167
168 // test that you can read all of the offsets from the compressed form through the end of the file
169 for (size_t i = 0; i < data.size(); i++) {
170 auto actual_data = std::vector<T>(128);
171 auto compf = compressed_file(compressed_filename);
172 compf.open();
173 compf.seek(i * sizeof(T));
174 compf.read(reinterpret_cast<char*>(actual_data.data()), (actual_data.size() - i) * sizeof(T));
175 compf.close();
176 BOOST_REQUIRE_EQUAL_COLLECTIONS(data.begin() + i, data.end(), actual_data.begin(), actual_data.end() - i);
177 }
178}
179
181 // generate a large dataset where ever 8 bytes is the offset to that 8 bytes of data
182 auto data = std::vector<T>(32);
183 std::generate(data.begin(), data.end(), []() {
184 return make_random<T>();
185 });
186
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);
190
191 // set a stride of the whole file which should result in no seek points
192 BOOST_TEST(compressed_file::process(uncompressed_filename, compressed_filename, uncompressed_size));
193
194 // verify that no seek points were created
195 fc::cfile compressed;
196 compressed.set_file_path(compressed_filename);
197 compressed.open("r");
198 compressed.seek(fc::file_size(compressed_filename) - 2);
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);
203
204 // test that you can read all of the offsets from the compressed form through the end of the file
205 for (size_t i = 0; i < data.size(); i++) {
206 auto actual_data = std::vector<T>(32);
207 auto compf = compressed_file(compressed_filename);
208 compf.open();
209 compf.seek(i * sizeof(T));
210 compf.read(reinterpret_cast<char*>(actual_data.data()), (actual_data.size() - i) * sizeof(T));
211 compf.close();
212 BOOST_REQUIRE_EQUAL_COLLECTIONS(data.begin() + i, data.end(), actual_data.begin(), actual_data.end() - i);
213 }
214}
215
216
217BOOST_AUTO_TEST_SUITE_END()
const mie::Vuint & p
Definition bn.cpp:27
T data[N]
Definition array.hpp:37
void read(char *d, size_t n)
Definition cfile.hpp:114
void seek(long loc)
Definition cfile.hpp:87
void open(const char *mode)
Definition cfile.hpp:65
void set_file_path(fc::path file_path)
Definition cfile.hpp:37
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)
os_t os
uint64_t file_size(const path &p)
Definition name.hpp:106
std::ostream & operator<<(std::ostream &st, const std::variant< fc::alt_bn128_error, bytes > &err)
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
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)