From 7e285290fe03a01708eb79a2fbf41d9acf9f1403 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 3 Apr 2024 21:03:35 +0200 Subject: [PATCH] test in memory comp --- src/fragment_store/test_file_zstd.cpp | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/fragment_store/test_file_zstd.cpp b/src/fragment_store/test_file_zstd.cpp index 817639d..cecddd7 100644 --- a/src/fragment_store/test_file_zstd.cpp +++ b/src/fragment_store/test_file_zstd.cpp @@ -1,6 +1,7 @@ #include "./file2_zstd.hpp" #include +#include #include #include @@ -37,6 +38,105 @@ const static ByteSpan data_test_text3{ }; int main(void) { + { // first do a simple mem backed test + std::vector buffer; + { // write + File2MemW f_w_mem{buffer}; + assert(f_w_mem.isGood()); + + File2ZSTDW f_w_zstd{f_w_mem}; + assert(f_w_zstd.isGood()); + + bool res = f_w_zstd.write(data_test_text1); + assert(res); + assert(f_w_zstd.isGood()); + + // write another frame of the same data + res = f_w_zstd.write(data_test_text2); + assert(res); + assert(f_w_zstd.isGood()); + + // write larger frame + res = f_w_zstd.write(data_test_text3); + assert(res); + assert(f_w_zstd.isGood()); + } + + std::cout << "in mem size: " << buffer.size() << "\n"; + + { // read + File2MemR f_r_mem{ByteSpan{buffer}}; + assert(f_r_mem.isGood()); + + File2ZSTDR f_r_zstd{f_r_mem}; + assert(f_r_zstd.isGood()); + + // reads return owning buffers + + { // readback data_test_text1 + auto r_res_var = f_r_zstd.read(data_test_text1.size); + + //assert(f_r_zstd.isGood()); + //assert(f_r_file.isGood()); + assert(std::holds_alternative>(r_res_var)); + const auto& r_res_vec = std::get>(r_res_var); + + //std::cout << "decomp: " << std::string_view{reinterpret_cast(r_res_vec.data()), r_res_vec.size()}; + + assert(r_res_vec.size() == data_test_text1.size); + assert(std::equal(data_test_text1.cbegin(), data_test_text1.cend(), r_res_vec.cbegin())); + } + + { // readback data_test_text2 + auto r_res_var = f_r_zstd.read(data_test_text2.size); + + //assert(f_r_zstd.isGood()); + //assert(f_r_file.isGood()); + assert(std::holds_alternative>(r_res_var)); + const auto& r_res_vec = std::get>(r_res_var); + + //std::cout << "decomp: " << std::string_view{reinterpret_cast(r_res_vec.data()), r_res_vec.size()}; + + assert(r_res_vec.size() == data_test_text2.size); + assert(std::equal( + data_test_text2.cbegin(), + data_test_text2.cend(), + r_res_vec.cbegin() + )); + } + + { // readback data_test_text3 + auto r_res_var = f_r_zstd.read(data_test_text3.size); + + //assert(f_r_zstd.isGood()); + //assert(f_r_file.isGood()); + assert(std::holds_alternative>(r_res_var)); + const auto& r_res_vec = std::get>(r_res_var); + + //std::cout << "decomp: " << std::string_view{reinterpret_cast(r_res_vec.data()), r_res_vec.size()}; + + assert(r_res_vec.size() == data_test_text3.size); + assert(std::equal( + data_test_text3.cbegin(), + data_test_text3.cend(), + r_res_vec.cbegin() + )); + } + + { // assert eof somehow + // since its eof, reading a single byte should return a zero sized buffer + auto r_res_var = f_r_zstd.read(1); + if (std::holds_alternative>(r_res_var)) { + assert(std::get>(r_res_var).empty()); + } else if (std::holds_alternative(r_res_var)) { + assert(std::get(r_res_var).empty()); + } else { + assert(false); + } + } + } + } + const auto temp_dir = std::filesystem::temp_directory_path() / "file2_zstd_tests"; std::filesystem::create_directories(temp_dir); // making sure