1 /*
2 * Copyright (C) 2020 Matthieu Gautier <mgautier@kymeria.fr>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12 * NON-INFRINGEMENT. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
20 #include <algorithm>
21 #include <memory>
22 #include "gtest/gtest.h"
23
24 #include <zim/zim.h>
25
26 #include "../src/compression.h"
27
28 namespace
29 {
30
31 template<typename T>
32 class CompressionTest : public testing::Test {
33 protected:
34 typedef zim::Compressor<T> CompressorT;
35 typedef zim::Uncompressor<T> DecompressorT;
36 };
37
38 using CompressionAlgo = ::testing::Types<
39 LZMA_INFO,
40 ZSTD_INFO
41 >;
42
43 TYPED_TEST_CASE(CompressionTest, CompressionAlgo);
44
TYPED_TEST(CompressionTest,compress)45 TYPED_TEST(CompressionTest, compress) {
46 std::string data;
47 data.reserve(100000);
48 for (int i=0; i<100000; i++) {
49 data.append(1, (char)(i%256));
50 }
51 data[99999] = 0;
52
53 auto initialSizes = std::vector<unsigned int>{32, 1024, 1024*1024};
54 auto chunkSizes = std::vector<unsigned long>{32, 512, 1024*1024};
55 for (auto initialSize: initialSizes) {
56 for (auto chunkSize: chunkSizes) {
57 typename TestFixture::CompressorT compressor(initialSize);
58 {
59 bool first=true;
60 unsigned long size = data.size();
61 size_t offset = 0;
62 while (size) {
63 if (first) {
64 compressor.init(const_cast<char*>(data.c_str()));
65 first = false;
66 }
67 auto adjustedChunkSize = std::min(size, chunkSize);
68 compressor.feed(data.c_str()+offset, adjustedChunkSize);
69 offset += adjustedChunkSize;
70 size -= adjustedChunkSize;
71 }
72 }
73
74 zim::zsize_t comp_size;
75 auto comp_data = compressor.get_data(&comp_size);
76
77 typename TestFixture::DecompressorT decompressor(initialSize);
78 {
79 bool first=true;
80 unsigned long size = comp_size.v;
81 size_t offset = 0;
82 while (size) {
83 if (first) {
84 decompressor.init(comp_data.get());
85 first = false;
86 }
87 auto adjustedChunkSize = std::min(size, chunkSize);
88 decompressor.feed(comp_data.get()+offset, adjustedChunkSize);
89 offset += adjustedChunkSize;
90 size -= adjustedChunkSize;
91 }
92 }
93
94 zim::zsize_t decomp_size;
95 auto decomp_data = decompressor.get_data(&decomp_size);
96
97 ASSERT_EQ(decomp_size.v, data.size());
98 ASSERT_EQ(data, std::string(decomp_data.get(), decomp_size.v));
99 }
100 }
101 }
102
103
104 } // namespace
105