-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_deflate.cpp
71 lines (56 loc) · 1.8 KB
/
test_deflate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "igzip_wrapper.h"
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <thread>
#define READ_BUF_ONCE 1024 * 1024
#define BUFFER_SIZE 1ll << 31 // maximum 2GiB to store intermediate buffer
void load(FILE *fp, unsigned char *dst, size_t *length) {
size_t read;
for (*length = 0; *length < BUFFER_SIZE; *length += read) {
read = fread(dst + (*length), 1, READ_BUF_ONCE, fp);
if (read == 0) {
break;
} else if (read == -1) {
std::cerr << "error to read compared file" << std::endl;
}
}
}
int main(int argc, char *argv[]) {
unsigned char *src = (unsigned char *)malloc(BUFFER_SIZE);
if (src == NULL) {
log_print(ERROR, "Cannot malloc check buffer\n");
exit(-1);
}
FILE *src_fp = fopen(argv[1], "rb");
if (src_fp == NULL) {
log_print(ERROR, "Cannot open source file\n");
exit(-1);
}
size_t src_len = 0, decompress_len = 0;
load(src_fp, src, &src_len);
std::chrono::steady_clock::time_point begin =
std::chrono::steady_clock::now();
#ifdef HAVE_THREADS
// max compress level is 3, max thread nums is 8
compress_file(src, src_len, argv[2], 3, 8);
#else
compress_file(src, src_len, argv[2], 3, 1);
#endif
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout
<< "Compression elapse = "
<< std::chrono::duration_cast<std::chrono::seconds>(end - begin).count()
<< "s" << std::endl;
unsigned char *decompress = (unsigned char *)malloc(BUFFER_SIZE);
if (decompress == NULL) {
log_print(ERROR, "Cannot malloc decompression buffer\n");
exit(-1);
}
decompress_file(argv[2], decompress, &decompress_len);
assert(src_len == decompress_len);
assert(memcmp(src, decompress, src_len) == 0);
std::cout << "Passed!" << std::endl;
return 0;
}