lzokay/test.cpp

38 lines
1.2 KiB
C++
Raw Permalink Normal View History

2018-12-23 17:43:24 -08:00
#include "lzokay.hpp"
#include <cstring>
int compress_and_decompress(const uint8_t* data, std::size_t length) {
lzokay::EResult error;
/* This variable and 5th parameter of compress() is optional, but may
* be reused across multiple compression runs; avoiding repeat
* allocation/deallocation of the work memory used by the compressor.
*/
lzokay::Dict<> dict;
std::size_t compressed_size = lzokay::compress_worst_size(length);
std::unique_ptr<uint8_t[]> compressed(new uint8_t[compressed_size]);
2019-06-13 17:40:21 -07:00
error = lzokay::compress(data, length, compressed.get(), compressed_size,
compressed_size, dict);
2018-12-23 17:43:24 -08:00
if (error < lzokay::EResult::Success)
return 1;
std::unique_ptr<uint8_t[]> decompressed(new uint8_t[length]);
2019-06-13 17:40:21 -07:00
std::size_t decompressed_size;
2018-12-23 17:43:24 -08:00
error = lzokay::decompress(compressed.get(), compressed_size,
2019-06-13 17:40:21 -07:00
decompressed.get(), length, decompressed_size);
2018-12-23 17:43:24 -08:00
if (error < lzokay::EResult::Success)
return 1;
if (std::memcmp(data, decompressed.get(), decompressed_size) != 0)
return 1;
return 0;
}
int main(int argc, char** argv) {
const char* testdata = "Hello World!";
int ret = compress_and_decompress(reinterpret_cast<const uint8_t*>(testdata), 12);
return ret;
}