forked from richgel999/miniz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (101 loc) · 3.58 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (101 loc) · 3.58 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "catch_amalgamated.hpp"
#include "../miniz.h"
#include <assert.h>
#include <string>
#ifdef _WIN32
#define unlink _unlink
#else
#include <unistd.h>
#endif
#ifndef MINIZ_NO_STDIO
bool create_test_zip()
{
unlink("test.zip");
mz_zip_archive zip_archive = {};
auto b = mz_zip_writer_init_file(&zip_archive, "test.zip", 0);
if (!b)
return false;
b = mz_zip_writer_add_mem(&zip_archive, "test.txt", "foo", 3, MZ_DEFAULT_COMPRESSION);
if (!b)
return false;
b = mz_zip_writer_finalize_archive(&zip_archive);
if (!b)
return false;
b = mz_zip_writer_end(&zip_archive);
if (!b)
return false;
return true;
}
TEST_CASE("Zip writer tests")
{
auto b = create_test_zip();
REQUIRE(b);
SECTION("Test test.txt content correct")
{
mz_zip_archive zip_archive = {};
auto b = mz_zip_reader_init_file(&zip_archive, "test.zip", 0);
REQUIRE(b);
size_t content_size;
auto content = mz_zip_reader_extract_file_to_heap(&zip_archive, "test.txt", &content_size, 0);
std::string_view content_view(reinterpret_cast<char *>(content), content_size);
REQUIRE(content_view == "foo");
REQUIRE(content_view.size() == 3);
free(content);
mz_zip_reader_end(&zip_archive);
}
SECTION("Test repeated file addition to zip")
{
mz_zip_archive zip_archive = {};
auto b = mz_zip_writer_init_file(&zip_archive, "test2.zip", 0);
REQUIRE(b);
b = mz_zip_writer_finalize_archive(&zip_archive);
REQUIRE(b);
b = mz_zip_writer_end(&zip_archive);
REQUIRE(b);
for (int i = 0; i < 50; i++)
{
const char *str = "hello world";
b = mz_zip_add_mem_to_archive_file_in_place(
std::string("test2.zip").c_str(), ("file1.txt" + std::to_string(i)).c_str(),
str, (mz_uint16)strlen(str),
NULL, 0,
MZ_BEST_COMPRESSION);
REQUIRE(b);
}
}
}
#endif
TEST_CASE("Tinfl / tdefl tests")
{
SECTION("simple_test1")
{
size_t cmp_len = 0;
const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
size_t uncomp_len = strlen(p);
void *pComp_data = tdefl_compress_mem_to_heap(p, uncomp_len, &cmp_len, TDEFL_WRITE_ZLIB_HEADER);
REQUIRE(pComp_data);
size_t decomp_len = 0;
void *pDecomp_data = tinfl_decompress_mem_to_heap(pComp_data, cmp_len, &decomp_len, TINFL_FLAG_PARSE_ZLIB_HEADER);
REQUIRE(pDecomp_data);
REQUIRE(decomp_len == uncomp_len);
REQUIRE(memcmp(pDecomp_data, p, uncomp_len) == 0);
free(pComp_data);
free(pDecomp_data);
}
SECTION("simple_test2")
{
uint8_t cmp_buf[1024], decomp_buf[1024];
uLong cmp_len = sizeof(cmp_buf);
const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
uLong uncomp_len = (uLong)strlen(p);
int status = compress(cmp_buf, &cmp_len, (const uint8_t *)p, uncomp_len);
REQUIRE(status == Z_OK);
REQUIRE(cmp_len <= compressBound(uncomp_len));
uLong decomp_len = sizeof(decomp_buf);
status = uncompress(decomp_buf, &decomp_len, cmp_buf, cmp_len);
;
REQUIRE(status == Z_OK);
REQUIRE(decomp_len == uncomp_len);
REQUIRE(memcmp(decomp_buf, p, uncomp_len) == 0);
}
}