~ubuntu-branches/ubuntu/trusty/protobuf/trusty-proposed

« back to all changes in this revision

Viewing changes to src/google/protobuf/io/zero_copy_stream_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
#include <sstream>
62
62
 
63
63
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
64
#include <google/protobuf/io/coded_stream.h>
64
65
 
65
66
#if HAVE_ZLIB
66
67
#include <google/protobuf/io/gzip_stream.h>
285
286
  }
286
287
}
287
288
 
 
289
TEST_F(IoTest, TwoSessionWrite) {
 
290
  // Test that two concatenated write sessions read correctly
 
291
 
 
292
  static const char* strA = "0123456789";
 
293
  static const char* strB = "WhirledPeas";
 
294
  const int kBufferSize = 2*1024;
 
295
  uint8* buffer = new uint8[kBufferSize];
 
296
  char* temp_buffer = new char[40];
 
297
 
 
298
  for (int i = 0; i < kBlockSizeCount; i++) {
 
299
    for (int j = 0; j < kBlockSizeCount; j++) {
 
300
      ArrayOutputStream* output =
 
301
          new ArrayOutputStream(buffer, kBufferSize, kBlockSizes[i]);
 
302
      CodedOutputStream* coded_output = new CodedOutputStream(output);
 
303
      coded_output->WriteVarint32(strlen(strA));
 
304
      coded_output->WriteRaw(strA, strlen(strA));
 
305
      delete coded_output;  // flush
 
306
      int64 pos = output->ByteCount();
 
307
      delete output;
 
308
      output = new ArrayOutputStream(
 
309
          buffer + pos, kBufferSize - pos, kBlockSizes[i]);
 
310
      coded_output = new CodedOutputStream(output);
 
311
      coded_output->WriteVarint32(strlen(strB));
 
312
      coded_output->WriteRaw(strB, strlen(strB));
 
313
      delete coded_output;  // flush
 
314
      int64 size = pos + output->ByteCount();
 
315
      delete output;
 
316
 
 
317
      ArrayInputStream* input =
 
318
          new ArrayInputStream(buffer, size, kBlockSizes[j]);
 
319
      CodedInputStream* coded_input = new CodedInputStream(input);
 
320
      uint32 insize;
 
321
      EXPECT_TRUE(coded_input->ReadVarint32(&insize));
 
322
      EXPECT_EQ(strlen(strA), insize);
 
323
      EXPECT_TRUE(coded_input->ReadRaw(temp_buffer, insize));
 
324
      EXPECT_EQ(0, memcmp(temp_buffer, strA, insize));
 
325
 
 
326
      EXPECT_TRUE(coded_input->ReadVarint32(&insize));
 
327
      EXPECT_EQ(strlen(strB), insize);
 
328
      EXPECT_TRUE(coded_input->ReadRaw(temp_buffer, insize));
 
329
      EXPECT_EQ(0, memcmp(temp_buffer, strB, insize));
 
330
 
 
331
      delete coded_input;
 
332
      delete input;
 
333
    }
 
334
  }
 
335
 
 
336
  delete [] temp_buffer;
 
337
  delete [] buffer;
 
338
}
 
339
 
288
340
#if HAVE_ZLIB
289
341
TEST_F(IoTest, GzipIo) {
290
342
  const int kBufferSize = 2*1024;
296
348
        int size;
297
349
        {
298
350
          ArrayOutputStream output(buffer, kBufferSize, kBlockSizes[i]);
299
 
          GzipOutputStream gzout(
300
 
              &output, GzipOutputStream::GZIP, gzip_buffer_size);
 
351
          GzipOutputStream::Options options;
 
352
          options.format = GzipOutputStream::GZIP;
 
353
          if (gzip_buffer_size != -1) {
 
354
            options.buffer_size = gzip_buffer_size;
 
355
          }
 
356
          GzipOutputStream gzout(&output, options);
301
357
          WriteStuff(&gzout);
302
358
          gzout.Close();
303
359
          size = output.ByteCount();
324
380
        int size;
325
381
        {
326
382
          ArrayOutputStream output(buffer, kBufferSize, kBlockSizes[i]);
327
 
          GzipOutputStream gzout(
328
 
              &output, GzipOutputStream::ZLIB, gzip_buffer_size);
 
383
          GzipOutputStream::Options options;
 
384
          options.format = GzipOutputStream::ZLIB;
 
385
          if (gzip_buffer_size != -1) {
 
386
            options.buffer_size = gzip_buffer_size;
 
387
          }
 
388
          GzipOutputStream gzout(&output, options);
329
389
          WriteStuff(&gzout);
330
390
          gzout.Close();
331
391
          size = output.ByteCount();
348
408
  int size;
349
409
  {
350
410
    ArrayOutputStream output(buffer, kBufferSize);
351
 
    GzipOutputStream gzout(&output, GzipOutputStream::ZLIB);
 
411
    GzipOutputStream::Options options;
 
412
    options.format = GzipOutputStream::ZLIB;
 
413
    GzipOutputStream gzout(&output, options);
352
414
    WriteStuff(&gzout);
353
415
    gzout.Close();
354
416
    size = output.ByteCount();
360
422
  }
361
423
  {
362
424
    ArrayOutputStream output(buffer, kBufferSize);
363
 
    GzipOutputStream gzout(&output, GzipOutputStream::GZIP);
 
425
    GzipOutputStream::Options options;
 
426
    options.format = GzipOutputStream::GZIP;
 
427
    GzipOutputStream gzout(&output, options);
364
428
    WriteStuff(&gzout);
365
429
    gzout.Close();
366
430
    size = output.ByteCount();
432
496
  EXPECT_TRUE(Uncompress(gzip_compressed) == golden);
433
497
  EXPECT_TRUE(Uncompress(zlib_compressed) == golden);
434
498
}
 
499
 
 
500
TEST_F(IoTest, TwoSessionWriteGzip) {
 
501
  // Test that two concatenated gzip streams can be read correctly
 
502
 
 
503
  static const char* strA = "0123456789";
 
504
  static const char* strB = "QuickBrownFox";
 
505
  const int kBufferSize = 2*1024;
 
506
  uint8* buffer = new uint8[kBufferSize];
 
507
  char* temp_buffer = new char[40];
 
508
 
 
509
  for (int i = 0; i < kBlockSizeCount; i++) {
 
510
    for (int j = 0; j < kBlockSizeCount; j++) {
 
511
      ArrayOutputStream* output =
 
512
          new ArrayOutputStream(buffer, kBufferSize, kBlockSizes[i]);
 
513
      GzipOutputStream* gzout = new GzipOutputStream(output);
 
514
      CodedOutputStream* coded_output = new CodedOutputStream(gzout);
 
515
      int32 outlen = strlen(strA) + 1;
 
516
      coded_output->WriteVarint32(outlen);
 
517
      coded_output->WriteRaw(strA, outlen);
 
518
      delete coded_output;  // flush
 
519
      delete gzout;  // flush
 
520
      int64 pos = output->ByteCount();
 
521
      delete output;
 
522
      output = new ArrayOutputStream(
 
523
          buffer + pos, kBufferSize - pos, kBlockSizes[i]);
 
524
      gzout = new GzipOutputStream(output);
 
525
      coded_output = new CodedOutputStream(gzout);
 
526
      outlen = strlen(strB) + 1;
 
527
      coded_output->WriteVarint32(outlen);
 
528
      coded_output->WriteRaw(strB, outlen);
 
529
      delete coded_output;  // flush
 
530
      delete gzout;  // flush
 
531
      int64 size = pos + output->ByteCount();
 
532
      delete output;
 
533
 
 
534
      ArrayInputStream* input =
 
535
          new ArrayInputStream(buffer, size, kBlockSizes[j]);
 
536
      GzipInputStream* gzin = new GzipInputStream(input);
 
537
      CodedInputStream* coded_input = new CodedInputStream(gzin);
 
538
      uint32 insize;
 
539
      EXPECT_TRUE(coded_input->ReadVarint32(&insize));
 
540
      EXPECT_EQ(strlen(strA) + 1, insize);
 
541
      EXPECT_TRUE(coded_input->ReadRaw(temp_buffer, insize));
 
542
      EXPECT_EQ(0, memcmp(temp_buffer, strA, insize))
 
543
          << "strA=" << strA << " in=" << temp_buffer;
 
544
 
 
545
      EXPECT_TRUE(coded_input->ReadVarint32(&insize));
 
546
      EXPECT_EQ(strlen(strB) + 1, insize);
 
547
      EXPECT_TRUE(coded_input->ReadRaw(temp_buffer, insize));
 
548
      EXPECT_EQ(0, memcmp(temp_buffer, strB, insize))
 
549
          << " out_block_size=" << kBlockSizes[i]
 
550
          << " in_block_size=" << kBlockSizes[j]
 
551
          << " pos=" << pos
 
552
          << " size=" << size
 
553
          << " strB=" << strB << " in=" << temp_buffer;
 
554
 
 
555
      delete coded_input;
 
556
      delete gzin;
 
557
      delete input;
 
558
    }
 
559
  }
 
560
 
 
561
  delete [] temp_buffer;
 
562
  delete [] buffer;
 
563
}
435
564
#endif
436
565
 
437
566
// There is no string input, only string output.  Also, it doesn't support