Coverage for /Syzygy/core/zstream_unittest.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%65650.C++test

Line-by-line coverage:

   1    :  // Copyright 2012 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #include "syzygy/core/zstream.h"
  16    :  
  17    :  #include "gmock/gmock.h"
  18    :  #include "gtest/gtest.h"
  19    :  #include "syzygy/core/serialization.h"
  20    :  
  21    :  namespace core {
  22    :  
  23    :  namespace {
  24    :  
  25    :  const uint8 kSampleData[] = "This is some simple sample data. Simple is as "
  26    :                              "simple does. Similar samples are amply simple to "
  27    :                              "compress.";
  28    :  
  29    :  class ZOutStreamTest : public ::testing::Test {
  30    :   public:
  31  E :    void SetUp() {
  32  E :      out_stream_.reset(CreateByteOutStream(std::back_inserter(compressed_)));
  33  E :    }
  34    :  
  35    :    std::vector<uint8> compressed_;
  36    :    ScopedOutStreamPtr out_stream_;
  37    :  };
  38    :  
  39    :  class ZInStreamTest : public ::testing::Test {
  40    :   public:
  41  E :    void SetUp() {
  42    :      // Compress the sample data so that we have something to read from.
  43    :      ScopedOutStreamPtr out_stream(
  44  E :          CreateByteOutStream(std::back_inserter(compressed_)));
  45  E :      ZOutStream zout(out_stream.get());
  46  E :      ASSERT_TRUE(zout.Init());
  47  E :      ASSERT_TRUE(zout.Write(sizeof(kSampleData), kSampleData));
  48  E :      ASSERT_TRUE(zout.Flush());
  49  E :      ASSERT_LT(0u, compressed_.size());
  50    :  
  51    :      in_stream_.reset(CreateByteInStream(compressed_.begin(),
  52  E :                                          compressed_.end()));
  53  E :    }
  54    :  
  55    :    std::vector<uint8> compressed_;
  56    :    ScopedInStreamPtr in_stream_;
  57    :    uint8 buffer[2 * sizeof(kSampleData)];
  58    :  };
  59    :  
  60    :  }  // namespace
  61    :  
  62  E :  TEST_F(ZOutStreamTest, DoingNothingProducesNoData) {
  63  E :    ZOutStream zip_stream(out_stream_.get());
  64  E :    EXPECT_EQ(0u, compressed_.size());
  65  E :    EXPECT_TRUE(zip_stream.Init());
  66  E :    EXPECT_EQ(0u, compressed_.size());
  67  E :  }
  68    :  
  69  E :  TEST_F(ZOutStreamTest, DoingSomethingProducesData) {
  70  E :    ZOutStream zip_stream(out_stream_.get());
  71  E :    EXPECT_TRUE(zip_stream.Init());
  72  E :    EXPECT_TRUE(zip_stream.Write(sizeof(kSampleData), kSampleData));
  73  E :    EXPECT_TRUE(zip_stream.Flush());
  74  E :    EXPECT_LT(0u, compressed_.size());
  75  E :  }
  76    :  
  77  E :  TEST_F(ZInStreamTest, ReadingTruncatedDataFails) {
  78  E :    EXPECT_LT(2u, compressed_.size());
  79  E :    compressed_.resize(compressed_.size() / 2);
  80    :    in_stream_.reset(CreateByteInStream(compressed_.begin(),
  81  E :                                        compressed_.end()));
  82    :  
  83  E :    ZInStream unzip_stream(in_stream_.get());
  84  E :    EXPECT_TRUE(unzip_stream.Init());
  85  E :    size_t bytes_read = 0;
  86  E :    EXPECT_FALSE(unzip_stream.Read(sizeof(buffer), buffer, &bytes_read));
  87  E :  }
  88    :  
  89  E :  TEST_F(ZInStreamTest, DecompressionWorks) {
  90  E :    ZInStream unzip_stream(in_stream_.get());
  91  E :    EXPECT_TRUE(unzip_stream.Init());
  92  E :    EXPECT_TRUE(unzip_stream.Read(sizeof(kSampleData), buffer));
  93    :    EXPECT_STREQ(reinterpret_cast<const char*>(buffer),
  94  E :                 reinterpret_cast<const char*>(kSampleData));
  95  E :  }
  96    :  
  97  E :  TEST(ZStreamTest, RoundTrip) {
  98  E :    std::vector<uint8> compressed;
  99  E :    std::vector<uint8> decompressed;
 100    :  
 101    :    ScopedOutStreamPtr out_stream(
 102  E :        CreateByteOutStream(std::back_inserter(compressed)));
 103  E :    ZOutStream zip_stream(out_stream.get());
 104  E :    EXPECT_TRUE(zip_stream.Init());
 105  E :    EXPECT_TRUE(zip_stream.Write(sizeof(kSampleData), kSampleData));
 106  E :    EXPECT_TRUE(zip_stream.Flush());
 107    :  
 108    :    ScopedInStreamPtr in_stream(
 109  E :        CreateByteInStream(compressed.begin(), compressed.end()));
 110  E :    ZInStream unzip_stream(in_stream.get());
 111  E :    EXPECT_TRUE(unzip_stream.Init());
 112    :  
 113    :    // We deliberately try to read more data than necessary to ensure that the
 114    :    // decoder recognizes the end of stream on its own.
 115  E :    decompressed.resize(2 * sizeof(kSampleData));
 116  E :    size_t bytes_read = 0;
 117    :    EXPECT_TRUE(unzip_stream.Read(decompressed.size(),
 118    :                                  &decompressed[0],
 119  E :                                  &bytes_read));
 120  E :    EXPECT_EQ(sizeof(kSampleData), bytes_read);
 121  E :    decompressed.resize(bytes_read);
 122    :  
 123    :    // We shouldn't be able to read any more data from either stream.
 124  E :    uint8 buffer[1] = {};
 125  E :    bytes_read = 0;
 126  E :    EXPECT_TRUE(unzip_stream.Read(sizeof(buffer), buffer, &bytes_read));
 127  E :    EXPECT_EQ(0, bytes_read);
 128    :  
 129  E :    bytes_read = 0;
 130  E :    EXPECT_TRUE(in_stream->Read(sizeof(buffer), buffer, &bytes_read));
 131  E :    EXPECT_EQ(0, bytes_read);
 132    :  
 133  E :    EXPECT_THAT(decompressed, testing::ElementsAreArray(kSampleData));
 134  E :  }
 135    :  
 136    :  }  // namespace core

Coverage information generated Thu Jan 14 17:40:38 2016.