-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathtest_stream.cc
239 lines (186 loc) · 7.76 KB
/
test_stream.cc
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* test_stream.cc - Test basic streaming functionality
part of the minizip-ng project
Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#include <gtest/gtest.h>
typedef void (*stream_test_cb)(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
mz_stream_find_cb find_cb);
static void test_stream_find_begin(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
mz_stream_find_cb find_cb) {
void *mem_stream = NULL;
int32_t i = 0;
int32_t x = 0;
int64_t last_pos = 0;
int64_t position = 0;
MZ_UNUSED(name);
ASSERT_GT(find_size, 0);
ASSERT_NE(find, nullptr);
ASSERT_NE(find_cb, nullptr);
for (i = 0; i < count; i++) {
mem_stream = mz_stream_mem_create();
ASSERT_NE(mem_stream, nullptr);
mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
/* Find when the needle is at the beginning of the stream */
for (x = 0; x < i && x < find_size; x++)
mz_stream_write_uint8(mem_stream, find[x]);
while (x++ < i)
mz_stream_write_uint8(mem_stream, 0);
if (find_cb == mz_stream_find)
mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
find_cb(mem_stream, (const void *)find, find_size, i, &position);
/* Should always find at the start of the stream if entire needle
was written to stream */
EXPECT_EQ(position, (i < find_size) ? -1 : 0)
<< "name: " << name << std::endl
<< "find_size: " << find_size << std::endl
<< "index: " << i << std::endl;
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
last_pos = mz_stream_tell(mem_stream);
mz_stream_mem_delete(&mem_stream);
/* Shouldn't be at the end of the stream */
EXPECT_NE(position, last_pos);
}
}
static void test_stream_find_end(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
mz_stream_find_cb find_cb) {
void *mem_stream = NULL;
int32_t i = 0;
int32_t x = 0;
int32_t y = 0;
int64_t last_pos = 0;
int64_t position = 0;
MZ_UNUSED(name);
ASSERT_GT(find_size, 0);
ASSERT_NE(find, nullptr);
ASSERT_NE(find_cb, nullptr);
for (i = 0; i < count; i++) {
mem_stream = mz_stream_mem_create();
ASSERT_NE(mem_stream, nullptr);
mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
/* Find when the needle is at the end of the stream */
for (x = 0; x < i - find_size; x++)
mz_stream_write_uint8(mem_stream, 0);
for (y = 0; x + y < i && y < find_size; y++)
mz_stream_write_uint8(mem_stream, find[y]);
if (find_cb == mz_stream_find)
mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
find_cb(mem_stream, (const void *)find, find_size, i, &position);
/* Should always find after zeros if entire needle
was written to stream */
EXPECT_EQ(position, (i < find_size) ? -1 : (i - find_size))
<< "name: " << name << std::endl
<< "find_size: " << find_size << std::endl
<< "index: " << i << std::endl;
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
last_pos = mz_stream_tell(mem_stream);
mz_stream_mem_delete(&mem_stream);
/* Shouldn't be at the end of the stream */
EXPECT_NE(position, last_pos);
}
}
static void test_stream_find_middle(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
mz_stream_find_cb find_cb) {
void *mem_stream = NULL;
int32_t i = 0;
int32_t x = 0;
int64_t last_pos = 0;
int64_t position = 0;
MZ_UNUSED(name);
ASSERT_GT(find_size, 0);
ASSERT_NE(find, nullptr);
ASSERT_NE(find_cb, nullptr);
for (i = 0; i < count; i++) {
mem_stream = mz_stream_mem_create();
ASSERT_NE(mem_stream, nullptr);
mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
/* Find when the neddle is in the middle of the stream */
for (x = 0; x < i; x++)
mz_stream_write_uint8(mem_stream, 0);
mz_stream_write(mem_stream, find, find_size);
for (x = 0; x < i; x++)
mz_stream_write_uint8(mem_stream, 0);
if (find_cb == mz_stream_find)
mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
find_cb(mem_stream, (const void *)find, find_size, i + find_size+ i, &position);
/* Should always find after initial set of zeros */
EXPECT_EQ(position, i)
<< "name: " << name << std::endl
<< "find_size: " << find_size << std::endl
<< "index: " << i << std::endl;
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
last_pos = mz_stream_tell(mem_stream);
mz_stream_mem_delete(&mem_stream);
/* Shouldn't be at the end of the stream */
EXPECT_NE(position, last_pos);
}
}
static void test_stream_find_middle_odd(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
mz_stream_find_cb find_cb) {
void *mem_stream = NULL;
int32_t i = 0;
int32_t x = 0;
int64_t last_pos = 0;
int64_t position = 0;
MZ_UNUSED(name);
ASSERT_GT(find_size, 0);
ASSERT_NE(find, nullptr);
ASSERT_NE(find_cb, nullptr);
for (i = 0; i < count; i++) {
mem_stream = mz_stream_mem_create();
ASSERT_NE(mem_stream, nullptr);
mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
/* Find when the needle is in the middle of the stream */
for (x = 0; x < i; x++)
mz_stream_write_uint8(mem_stream, 0);
mz_stream_write(mem_stream, find, find_size);
for (x = 0; x < i + 1; x++)
mz_stream_write_uint8(mem_stream, 0);
if (find_cb == mz_stream_find)
mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
find_cb(mem_stream, (const void *)find, find_size, i + find_size + i + 1, &position);
/* Should always find after initial set of zeros */
EXPECT_EQ(position, i)
<< "name: " << name << std::endl
<< "find_size: " << find_size << std::endl
<< "index: " << i << std::endl;
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
last_pos = mz_stream_tell(mem_stream);
mz_stream_mem_delete(&mem_stream);
/* Shouldn't be at the end of the stream */
EXPECT_NE(position, last_pos);
}
}
struct stream_find_param {
const char *name;
stream_test_cb test_cb;
mz_stream_find_cb find_cb;
friend std::ostream &operator<<(std::ostream &os, const stream_find_param ¶m) {
return os << "name: " << param.name;
}
};
constexpr stream_find_param find_tests[] = {
{ "begin", test_stream_find_begin, mz_stream_find },
{ "begin reverse", test_stream_find_begin, mz_stream_find_reverse },
{ "end", test_stream_find_end, mz_stream_find },
{ "end reverse", test_stream_find_end, mz_stream_find_reverse },
{ "middle", test_stream_find_middle, mz_stream_find },
{ "middle reverse", test_stream_find_middle, mz_stream_find_reverse },
{ "middle odd", test_stream_find_middle_odd, mz_stream_find },
{ "middle odd reverse", test_stream_find_middle_odd, mz_stream_find_reverse }
};
class stream_find : public ::testing::TestWithParam<stream_find_param> {
};
INSTANTIATE_TEST_SUITE_P(stream, stream_find, testing::ValuesIn(find_tests));
TEST_P(stream_find, find) {
const auto ¶m = GetParam();
const char *find = "0123456789";
int32_t c = 1;
for (c = 1; c < (int32_t)strlen(find); c += 1)
param.test_cb(param.name, 2096, (const uint8_t *)find, c, param.find_cb);
}