Debian Codegraph @

test/test_stream.cc

raw symbols SCIP
1 /* test_stream.cc - Test basic streaming functionality
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6
7 This program is distributed under the terms of the same license as zlib.
8 See the accompanying LICENSE file for the full text of the license.
9 */
10
11 #include "mz.h"
12 #include "mz_strm.h"
13 #include "mz_strm_mem.h"
14
15 #include <gtest/gtest.h>
16
17 typedef void (*stream_test_cb)(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
18 mz_stream_find_cb find_cb);
19
20 static void test_stream_find_begin(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
21 mz_stream_find_cb find_cb) {
22 void *mem_stream = NULL;
23 int32_t i = 0;
24 int32_t x = 0;
25 int64_t last_pos = 0;
26 int64_t position = 0;
27
28 MZ_UNUSED(name);
29
30 ASSERT_GT(find_size, 0);
31 ASSERT_NE(find, nullptr);
32 ASSERT_NE(find_cb, nullptr);
33
34 for (i = 0; i < count; i++) {
35 mem_stream = mz_stream_mem_create();
36 ASSERT_NE(mem_stream, nullptr);
37 mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
38
39 /* Find when the needle is at the beginning of the stream */
40 for (x = 0; x < i && x < find_size; x++)
41 mz_stream_write_uint8(mem_stream, find[x]);
42 while (x++ < i)
43 mz_stream_write_uint8(mem_stream, 0);
44
45 if (find_cb == mz_stream_find)
46 mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
47
48 find_cb(mem_stream, (const void *)find, find_size, i, &position);
49
50 /* Should always find at the start of the stream if entire needle
51 was written to stream */
52 EXPECT_EQ(position, (i < find_size) ? -1 : 0) << "name: " << name << std::endl
53 << "find_size: " << find_size << std::endl
54 << "index: " << i << std::endl;
55
56 mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
57 last_pos = mz_stream_tell(mem_stream);
58 mz_stream_mem_delete(&mem_stream);
59
60 /* Shouldn't be at the end of the stream */
61 EXPECT_NE(position, last_pos);
62 }
63 }
64
65 static void test_stream_find_end(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
66 mz_stream_find_cb find_cb) {
67 void *mem_stream = NULL;
68 int32_t i = 0;
69 int32_t x = 0;
70 int32_t y = 0;
71 int64_t last_pos = 0;
72 int64_t position = 0;
73
74 MZ_UNUSED(name);
75
76 ASSERT_GT(find_size, 0);
77 ASSERT_NE(find, nullptr);
78 ASSERT_NE(find_cb, nullptr);
79
80 for (i = 0; i < count; i++) {
81 mem_stream = mz_stream_mem_create();
82 ASSERT_NE(mem_stream, nullptr);
83 mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
84
85 /* Find when the needle is at the end of the stream */
86 for (x = 0; x < i - find_size; x++)
87 mz_stream_write_uint8(mem_stream, 0);
88 for (y = 0; x + y < i && y < find_size; y++)
89 mz_stream_write_uint8(mem_stream, find[y]);
90
91 if (find_cb == mz_stream_find)
92 mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
93
94 find_cb(mem_stream, (const void *)find, find_size, i, &position);
95
96 /* Should always find after zeros if entire needle
97 was written to stream */
98 EXPECT_EQ(position, (i < find_size) ? -1 : (i - find_size)) << "name: " << name << std::endl
99 << "find_size: " << find_size << std::endl
100 << "index: " << i << std::endl;
101
102 mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
103 last_pos = mz_stream_tell(mem_stream);
104 mz_stream_mem_delete(&mem_stream);
105
106 /* Shouldn't be at the end of the stream */
107 EXPECT_NE(position, last_pos);
108 }
109 }
110
111 static void test_stream_find_middle(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
112 mz_stream_find_cb find_cb) {
113 void *mem_stream = NULL;
114 int32_t i = 0;
115 int32_t x = 0;
116 int64_t last_pos = 0;
117 int64_t position = 0;
118
119 MZ_UNUSED(name);
120
121 ASSERT_GT(find_size, 0);
122 ASSERT_NE(find, nullptr);
123 ASSERT_NE(find_cb, nullptr);
124
125 for (i = 0; i < count; i++) {
126 mem_stream = mz_stream_mem_create();
127 ASSERT_NE(mem_stream, nullptr);
128 mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
129
130 /* Find when the neddle is in the middle of the stream */
131 for (x = 0; x < i; x++)
132 mz_stream_write_uint8(mem_stream, 0);
133 mz_stream_write(mem_stream, find, find_size);
134 for (x = 0; x < i; x++)
135 mz_stream_write_uint8(mem_stream, 0);
136
137 if (find_cb == mz_stream_find)
138 mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
139
140 find_cb(mem_stream, (const void *)find, find_size, i + find_size + i, &position);
141
142 /* Should always find after initial set of zeros */
143 EXPECT_EQ(position, i) << "name: " << name << std::endl
144 << "find_size: " << find_size << std::endl
145 << "index: " << i << std::endl;
146
147 mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
148 last_pos = mz_stream_tell(mem_stream);
149 mz_stream_mem_delete(&mem_stream);
150
151 /* Shouldn't be at the end of the stream */
152 EXPECT_NE(position, last_pos);
153 }
154 }
155
156 static void test_stream_find_middle_odd(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
157 mz_stream_find_cb find_cb) {
158 void *mem_stream = NULL;
159 int32_t i = 0;
160 int32_t x = 0;
161 int64_t last_pos = 0;
162 int64_t position = 0;
163
164 MZ_UNUSED(name);
165
166 ASSERT_GT(find_size, 0);
167 ASSERT_NE(find, nullptr);
168 ASSERT_NE(find_cb, nullptr);
169
170 for (i = 0; i < count; i++) {
171 mem_stream = mz_stream_mem_create();
172 ASSERT_NE(mem_stream, nullptr);
173 mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
174
175 /* Find when the needle is in the middle of the stream */
176 for (x = 0; x < i; x++)
177 mz_stream_write_uint8(mem_stream, 0);
178 mz_stream_write(mem_stream, find, find_size);
179 for (x = 0; x < i + 1; x++)
180 mz_stream_write_uint8(mem_stream, 0);
181
182 if (find_cb == mz_stream_find)
183 mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
184
185 find_cb(mem_stream, (const void *)find, find_size, i + find_size + i + 1, &position);
186
187 /* Should always find after initial set of zeros */
188 EXPECT_EQ(position, i) << "name: " << name << std::endl
189 << "find_size: " << find_size << std::endl
190 << "index: " << i << std::endl;
191
192 mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
193 last_pos = mz_stream_tell(mem_stream);
194 mz_stream_mem_delete(&mem_stream);
195
196 /* Shouldn't be at the end of the stream */
197 EXPECT_NE(position, last_pos);
198 }
199 }
200
201 struct stream_find_param {
202 const char *name;
203 stream_test_cb test_cb;
204 mz_stream_find_cb find_cb;
205
206 friend std::ostream &operator<<(std::ostream &os, const stream_find_param &param) {
207 return os << "name: " << param.name;
208 }
209 };
210
211 constexpr stream_find_param find_tests[] = {
212 { "begin", test_stream_find_begin, mz_stream_find},
213 { "begin reverse", test_stream_find_begin, mz_stream_find_reverse},
214 { "end", test_stream_find_end, mz_stream_find},
215 { "end reverse", test_stream_find_end, mz_stream_find_reverse},
216 { "middle", test_stream_find_middle, mz_stream_find},
217 { "middle reverse", test_stream_find_middle, mz_stream_find_reverse},
218 { "middle odd", test_stream_find_middle_odd, mz_stream_find},
219 {"middle odd reverse", test_stream_find_middle_odd, mz_stream_find_reverse}
220 };
221
222 class stream_find : public ::testing::TestWithParam<stream_find_param> {};
223
224 INSTANTIATE_TEST_SUITE_P(stream, stream_find, testing::ValuesIn(find_tests));
225
226 TEST_P(stream_find, find) {
227 const auto &param = GetParam();
228 const char *find = "0123456789";
229 int32_t c = 1;
230
231 for (c = 1; c < (int32_t)strlen(find); c += 1)
232 param.test_cb(param.name, 2096, (const uint8_t *)find, c, param.find_cb);
233 }