Skip to content

Commit f06b9b5

Browse files
author
Dane Springmeyer
committed
remove strategy to simplify API
1 parent a217d04 commit f06b9b5

File tree

4 files changed

+26
-190
lines changed

4 files changed

+26
-190
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ std::string value = gzip::compress(node::Buffer::Data(obj), node::Buffer::Length
3333
```
3434
#### Compress
3535
```c++
36-
// Optionally include compression level and strategy
36+
// Optionally include compression level
3737
std::size_t size; // No default value, but what happens when not passed??
3838
int level = Z_DEFAULT_COMPRESSION; // Z_DEFAULT_COMPRESSION is the default if no arg is passed
39-
int strategy = Z_DEFAULT_STRATEGY; // Z_DEFAULT_STRATEGY is the defaul if no arg is passed
4039

41-
std::string compressed_data = gzip::compress(tile->data(), size, level, strategy);
40+
std::string compressed_data = gzip::compress(tile->data(), size, level);
4241
```
4342
#### Decompress
4443
```c++

include/gzip/compress.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ class Compressor
1414
{
1515
std::size_t max_;
1616
int level_;
17-
int strategy_;
1817

1918
public:
2019
Compressor(int level = Z_DEFAULT_COMPRESSION,
21-
int strategy = Z_DEFAULT_STRATEGY,
2220
std::size_t max_bytes = 2000000000) // by default refuse operation if uncompressed data is > 2GB
2321
: max_(max_bytes),
24-
level_(level),
25-
strategy_(strategy)
22+
level_(level)
2623
{
2724
}
2825

@@ -69,7 +66,7 @@ class Compressor
6966

7067
#pragma GCC diagnostic push
7168
#pragma GCC diagnostic ignored "-Wold-style-cast"
72-
if (deflateInit2(&deflate_s, level_, Z_DEFLATED, window_bits, mem_level, strategy_) != Z_OK)
69+
if (deflateInit2(&deflate_s, level_, Z_DEFLATED, window_bits, mem_level, Z_DEFAULT_STRATEGY) != Z_OK)
7370
{
7471
throw std::runtime_error("deflate init failed");
7572
}
@@ -105,10 +102,9 @@ class Compressor
105102

106103
inline std::string compress(const char* data,
107104
std::size_t size,
108-
int level = Z_DEFAULT_COMPRESSION,
109-
int strategy = Z_DEFAULT_STRATEGY)
105+
int level = Z_DEFAULT_COMPRESSION)
110106
{
111-
Compressor comp(level, strategy);
107+
Compressor comp(level);
112108
std::string output;
113109
comp.compress(output, data, size);
114110
return output;

include/gzip/utils.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <cstdlib>
22

3-
#include <string>
4-
53
namespace gzip {
64

75
// These live in gzip.hpp because it doesnt need to use deps.
@@ -22,8 +20,4 @@ inline bool is_compressed(const char* data, std::size_t size)
2220
(static_cast<uint8_t>(data[0]) == 0x1F && static_cast<uint8_t>(data[1]) == 0x8B));
2321
}
2422

25-
inline bool is_compressed(std::string const& input)
26-
{
27-
return is_compressed(input.data(), input.size());
28-
}
2923
}

test/test.cpp

Lines changed: 20 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ TEST_CASE("successful decompress - pointer")
5555
const char* pointer = data.data();
5656
std::string compressed_data = gzip::compress(pointer, data.size());
5757
const char* compressed_pointer = compressed_data.data();
58-
59-
std::string value = gzip::decompress(compressed_pointer, data.size());
58+
std::string value = gzip::decompress(compressed_pointer, compressed_data.size());
6059
REQUIRE(data == value);
6160
}
6261

@@ -90,194 +89,42 @@ TEST_CASE("round trip compression - gzip")
9089
{
9190
const std::string data("this is a sentence that will be compressed into something");
9291

93-
CHECK(!gzip::is_compressed(data));
94-
95-
int strategy;
96-
97-
SECTION("strategy - invalid compression")
98-
{
99-
strategy = 99;
100-
int level = Z_DEFAULT_COMPRESSION;
101-
102-
CHECK_THROWS(gzip::compress(data, level, strategy));
103-
}
92+
CHECK(!gzip::is_compressed(data.data(),data.size()));
10493

10594
SECTION("compression level - invalid")
10695
{
107-
strategy = Z_DEFAULT_STRATEGY;
10896
int level = 99;
10997

110-
CHECK_THROWS(gzip::compress(data, level, strategy));
111-
}
112-
113-
SECTION("strategy - default")
114-
{
115-
strategy = Z_DEFAULT_STRATEGY;
116-
117-
SECTION("no compression")
118-
{
119-
int level = Z_NO_COMPRESSION;
120-
std::string compressed_data = gzip::compress(data, level, strategy);
121-
CHECK(gzip::is_compressed(compressed_data));
122-
std::string new_data = gzip::decompress(compressed_data);
123-
CHECK(data == new_data);
124-
}
125-
126-
SECTION("default compression level")
127-
{
128-
int level = Z_DEFAULT_COMPRESSION;
129-
std::string compressed_data = gzip::compress(data, level, strategy);
130-
CHECK(gzip::is_compressed(compressed_data));
131-
std::string new_data = gzip::decompress(compressed_data);
132-
CHECK(data == new_data);
133-
}
134-
135-
SECTION("compression level -- min to max")
136-
{
137-
for (int level = Z_BEST_SPEED; level <= Z_BEST_COMPRESSION; ++level)
138-
{
139-
std::string compressed_data = gzip::compress(data, level, strategy);
140-
CHECK(gzip::is_compressed(compressed_data));
141-
std::string new_data = gzip::decompress(compressed_data);
142-
CHECK(data == new_data);
143-
}
144-
}
145-
}
146-
147-
SECTION("strategy - filtered")
148-
{
149-
strategy = Z_FILTERED;
150-
151-
SECTION("no compression")
152-
{
153-
int level = Z_NO_COMPRESSION;
154-
std::string compressed_data = gzip::compress(data, level, strategy);
155-
CHECK(gzip::is_compressed(compressed_data));
156-
std::string new_data = gzip::decompress(compressed_data);
157-
CHECK(data == new_data);
158-
}
159-
160-
SECTION("default compression level")
161-
{
162-
int level = Z_DEFAULT_COMPRESSION;
163-
std::string compressed_data = gzip::compress(data, level, strategy);
164-
CHECK(gzip::is_compressed(compressed_data));
165-
std::string new_data = gzip::decompress(compressed_data);
166-
CHECK(data == new_data);
167-
}
168-
169-
SECTION("compression level -- min to max")
170-
{
171-
for (int level = Z_BEST_SPEED; level <= Z_BEST_COMPRESSION; ++level)
172-
{
173-
std::string compressed_data = gzip::compress(data, level, strategy);
174-
CHECK(gzip::is_compressed(compressed_data));
175-
std::string new_data = gzip::decompress(compressed_data);
176-
CHECK(data == new_data);
177-
}
178-
}
98+
CHECK_THROWS(gzip::compress(data.data(), data.size(), level));
17999
}
180100

181-
SECTION("strategy - huffman only")
101+
SECTION("no compression")
182102
{
183-
strategy = Z_HUFFMAN_ONLY;
184-
185-
SECTION("no compression")
186-
{
187-
int level = Z_NO_COMPRESSION;
188-
std::string compressed_data = gzip::compress(data, level, strategy);
189-
CHECK(gzip::is_compressed(compressed_data));
190-
std::string new_data = gzip::decompress(compressed_data);
191-
CHECK(data == new_data);
192-
}
193-
194-
SECTION("default compression level")
195-
{
196-
int level = Z_DEFAULT_COMPRESSION;
197-
std::string compressed_data = gzip::compress(data, level, strategy);
198-
CHECK(gzip::is_compressed(compressed_data));
199-
std::string new_data = gzip::decompress(compressed_data);
200-
CHECK(data == new_data);
201-
}
202-
203-
SECTION("compression level -- min to max")
204-
{
205-
for (int level = Z_BEST_SPEED; level <= Z_BEST_COMPRESSION; ++level)
206-
{
207-
std::string compressed_data = gzip::compress(data, level, strategy);
208-
CHECK(gzip::is_compressed(compressed_data));
209-
std::string new_data = gzip::decompress(compressed_data);
210-
CHECK(data == new_data);
211-
}
212-
}
103+
int level = Z_NO_COMPRESSION;
104+
std::string compressed_data = gzip::compress(data.data(),data.size());
105+
CHECK(gzip::is_compressed(compressed_data.data(),compressed_data.size()));
106+
std::string new_data = gzip::decompress(compressed_data.data(),compressed_data.size());
107+
CHECK(data == new_data);
213108
}
214109

215-
SECTION("strategy - rle")
110+
SECTION("default compression level")
216111
{
217-
strategy = Z_RLE;
218-
219-
SECTION("no compression")
220-
{
221-
int level = Z_NO_COMPRESSION;
222-
std::string compressed_data = gzip::compress(data, level, strategy);
223-
CHECK(gzip::is_compressed(compressed_data));
224-
std::string new_data = gzip::decompress(compressed_data);
225-
CHECK(data == new_data);
226-
}
227-
228-
SECTION("default compression level")
229-
{
230-
int level = Z_DEFAULT_COMPRESSION;
231-
std::string compressed_data = gzip::compress(data, level, strategy);
232-
CHECK(gzip::is_compressed(compressed_data));
233-
std::string new_data = gzip::decompress(compressed_data);
234-
CHECK(data == new_data);
235-
}
236-
237-
SECTION("compression level -- min to max")
238-
{
239-
for (int level = Z_BEST_SPEED; level <= Z_BEST_COMPRESSION; ++level)
240-
{
241-
std::string compressed_data = gzip::compress(data, level, strategy);
242-
CHECK(gzip::is_compressed(compressed_data));
243-
std::string new_data = gzip::decompress(compressed_data);
244-
CHECK(data == new_data);
245-
}
246-
}
112+
int level = Z_DEFAULT_COMPRESSION;
113+
std::string compressed_data = gzip::compress(data.data(),data.size());
114+
CHECK(gzip::is_compressed(compressed_data.data(),compressed_data.size()));
115+
std::string new_data = gzip::decompress(compressed_data.data(),compressed_data.size());
116+
CHECK(data == new_data);
247117
}
248118

249-
SECTION("strategy - fixed")
119+
SECTION("compression level -- min to max")
250120
{
251-
strategy = Z_FIXED;
252-
253-
SECTION("no compression")
254-
{
255-
int level = Z_NO_COMPRESSION;
256-
std::string compressed_data = gzip::compress(data, level, strategy);
257-
CHECK(gzip::is_compressed(compressed_data));
258-
std::string new_data = gzip::decompress(compressed_data);
259-
CHECK(data == new_data);
260-
}
261-
262-
SECTION("default compression level")
121+
for (int level = Z_BEST_SPEED; level <= Z_BEST_COMPRESSION; ++level)
263122
{
264-
int level = Z_DEFAULT_COMPRESSION;
265-
std::string compressed_data = gzip::compress(data, level, strategy);
266-
CHECK(gzip::is_compressed(compressed_data));
267-
std::string new_data = gzip::decompress(compressed_data);
123+
std::string compressed_data = gzip::compress(data.data(),data.size());
124+
CHECK(gzip::is_compressed(compressed_data.data(),compressed_data.size()));
125+
std::string new_data = gzip::decompress(compressed_data.data(),compressed_data.size());
268126
CHECK(data == new_data);
269127
}
270-
271-
SECTION("compression level -- min to max")
272-
{
273-
for (int level = Z_BEST_SPEED; level <= Z_BEST_COMPRESSION; ++level)
274-
{
275-
std::string compressed_data = gzip::compress(data, level, strategy);
276-
CHECK(gzip::is_compressed(compressed_data));
277-
std::string new_data = gzip::decompress(compressed_data);
278-
CHECK(data == new_data);
279-
}
280-
}
281128
}
282129
}
283130

0 commit comments

Comments
 (0)