comparison OrthancFramework/UnitTestsSources/ZipTests.cpp @ 4670:b12faca76a52

support of output streams in ZipWriter
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 28 May 2021 18:26:40 +0200
parents d9473bd5ed43
children d9942d48fea7
comparison
equal deleted inserted replaced
4669:b14989f9ff8b 4670:b12faca76a52
206 ASSERT_EQ("world/hello", filename); 206 ASSERT_EQ("world/hello", filename);
207 ASSERT_EQ("Hello world", content); 207 ASSERT_EQ("Hello world", content);
208 ASSERT_FALSE(reader->ReadNextFile(filename, content)); 208 ASSERT_FALSE(reader->ReadNextFile(filename, content));
209 } 209 }
210 210
211
212
213 TEST(ZipWriter, Stream)
214 {
215 std::string memory;
216
217 std::string large;
218 large.resize(4 * 65536);
219 for (size_t i = 0; i < large.size(); i++)
220 {
221 large[i] = rand() % 256;
222 }
223
224 for (int i = 0; i < 2; i++)
225 {
226 {
227 Orthanc::ZipWriter w;
228 w.SetZip64(i == 0);
229 w.SetMemoryOutput(memory);
230 w.Open();
231
232 w.OpenFile("world/hello");
233 w.Write(large);
234 w.OpenFile("world/hello2");
235 w.Write(large);
236 w.OpenFile("world/hello3");
237 w.Write("Hello world");
238 w.OpenFile("world/hello4");
239 w.Write(large);
240 }
241
242 std::unique_ptr<ZipReader> reader(ZipReader::CreateFromMemory(memory));
243
244 ASSERT_EQ(4u, reader->GetFilesCount());
245
246 {
247 std::string filename, content;
248 ASSERT_TRUE(reader->ReadNextFile(filename, content));
249 ASSERT_EQ("world/hello", filename);
250 ASSERT_EQ(large.size(), content.size());
251 ASSERT_TRUE(memcmp(large.c_str(), content.c_str(), large.size()) == 0);
252 }
253
254 {
255 std::string filename, content;
256 ASSERT_TRUE(reader->ReadNextFile(filename, content));
257 ASSERT_EQ("world/hello2", filename);
258 ASSERT_EQ(large.size(), content.size());
259 ASSERT_TRUE(memcmp(large.c_str(), content.c_str(), large.size()) == 0);
260 }
261
262 {
263 std::string filename, content;
264 ASSERT_TRUE(reader->ReadNextFile(filename, content));
265 ASSERT_EQ("world/hello3", filename);
266 ASSERT_EQ("Hello world", content);
267 }
268
269 {
270 std::string filename, content;
271 ASSERT_TRUE(reader->ReadNextFile(filename, content));
272 ASSERT_EQ("world/hello4", filename);
273 ASSERT_EQ(large.size(), content.size());
274 ASSERT_TRUE(memcmp(large.c_str(), content.c_str(), large.size()) == 0);
275 }
276
277 {
278 std::string filename, content;
279 ASSERT_FALSE(reader->ReadNextFile(filename, content));
280 }
281 }
282 }
283
284
285 namespace Orthanc
286 {
287 // The namespace is necessary because of FRIEND_TEST
288 // http://code.google.com/p/googletest/wiki/AdvancedGuide#Private_Class_Members
289
290 TEST(ZipWriter, BufferWithSeek)
291 {
292 ZipWriter::BufferWithSeek buffer;
293 ASSERT_EQ(0u, buffer.GetSize());
294
295 std::string s;
296 buffer.Flush(s);
297 ASSERT_TRUE(s.empty());
298
299 buffer.Write("hello");
300 ASSERT_EQ(5u, buffer.GetSize());
301 ASSERT_EQ(5u, buffer.GetPosition());
302 buffer.Write("world");
303 ASSERT_EQ(10u, buffer.GetSize());
304 ASSERT_EQ(10u, buffer.GetPosition());
305 buffer.Flush(s);
306 ASSERT_EQ("helloworld", s);
307 ASSERT_EQ(0u, buffer.GetSize());
308 ASSERT_EQ(0u, buffer.GetPosition());
309
310 buffer.Write("hello world");
311 buffer.Seek(4);
312 ASSERT_EQ(4u, buffer.GetPosition());
313 buffer.Write("ab");
314 ASSERT_EQ(6u, buffer.GetPosition());
315 buffer.Flush(s);
316 ASSERT_EQ("hellabworld", s);
317 ASSERT_EQ(0u, buffer.GetPosition());
318
319 buffer.Seek(0);
320 ASSERT_EQ(0u, buffer.GetPosition());
321 buffer.Write("abc");
322 buffer.Write("");
323 ASSERT_EQ(3u, buffer.GetPosition());
324 buffer.Seek(3);
325 ASSERT_THROW(buffer.Seek(4), OrthancException);
326 ASSERT_EQ(3u, buffer.GetPosition());
327 buffer.Write("de");
328 buffer.Write("");
329 ASSERT_EQ(5u, buffer.GetPosition());
330 buffer.Seek(3);
331 buffer.Seek(3);
332 ASSERT_EQ(3u, buffer.GetPosition());
333 ASSERT_THROW(buffer.Write("def"), OrthancException);
334 buffer.Write("");
335 ASSERT_EQ(3u, buffer.GetPosition());
336 buffer.Write("fg");
337 ASSERT_EQ(5u, buffer.GetPosition());
338 buffer.Write("hi");
339 ASSERT_EQ(7u, buffer.GetPosition());
340 buffer.Flush(s);
341 ASSERT_EQ("abcfghi", s);
342 ASSERT_EQ(0u, buffer.GetPosition());
343
344 buffer.Write("abc");
345 ASSERT_EQ(3u, buffer.GetPosition());
346 buffer.Seek(2);
347 ASSERT_EQ(2u, buffer.GetPosition());
348 buffer.Write("z");
349 ASSERT_EQ(3u, buffer.GetPosition());
350 buffer.Seek(1);
351 ASSERT_EQ(1u, buffer.GetPosition());
352 buffer.Write("y");
353 ASSERT_EQ(2u, buffer.GetPosition());
354 buffer.Flush(s);
355 ASSERT_EQ("ayz", s);
356 ASSERT_EQ(0u, buffer.GetPosition());
357
358 ASSERT_EQ(0u, buffer.GetPosition());
359 buffer.Write("abc");
360 ASSERT_EQ(3u, buffer.GetPosition());
361 buffer.Seek(1);
362 ASSERT_EQ(1u, buffer.GetPosition());
363 buffer.Write("z");
364 ASSERT_EQ(2u, buffer.GetPosition());
365 buffer.Seek(3);
366 ASSERT_EQ(3u, buffer.GetPosition());
367 buffer.Write("y");
368 ASSERT_EQ(4u, buffer.GetPosition());
369 buffer.Flush(s);
370 ASSERT_EQ("azcy", s);
371 ASSERT_EQ(0u, buffer.GetPosition());
372
373 buffer.Flush(s);
374 ASSERT_TRUE(s.empty());
375 ASSERT_EQ(0u, buffer.GetPosition());
376 }
377 }