Coverage for /Syzygy/core/zstream_unittest.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%67670.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_t kSampleData[] =
  26    :      "This is some simple sample data. Simple is as "
  27    :      "simple does. Similar samples are amply simple to "
  28    :      "compress.";
  29    :  
  30    :  class ZOutStreamTest : public ::testing::Test {
  31    :   public:
  32  E :    void SetUp() {
  33  E :      out_stream_.reset(CreateByteOutStream(std::back_inserter(compressed_)));
  34  E :    }
  35    :  
  36    :    std::vector<uint8_t> compressed_;
  37    :    ScopedOutStreamPtr out_stream_;
  38    :  };
  39    :  
  40    :  class ZInStreamTest : public ::testing::Test {
  41    :   public:
  42  E :    void SetUp() {
  43    :      // Compress the sample data so that we have something to read from.
  44  E :      ScopedOutStreamPtr out_stream(
  45    :          CreateByteOutStream(std::back_inserter(compressed_)));
  46  E :      ZOutStream zout(out_stream.get());
  47  E :      ASSERT_TRUE(zout.Init());
  48  E :      ASSERT_TRUE(zout.Write(sizeof(kSampleData), kSampleData));
  49  E :      ASSERT_TRUE(zout.Flush());
  50  E :      ASSERT_LT(0u, compressed_.size());
  51    :  
  52  E :      in_stream_.reset(CreateByteInStream(compressed_.begin(),
  53    :                                          compressed_.end()));
  54  E :    }
  55    :  
  56    :    std::vector<uint8_t> compressed_;
  57    :    ScopedInStreamPtr in_stream_;
  58    :    uint8_t buffer[2 * sizeof(kSampleData)];
  59    :  };
  60    :  
  61    :  }  // namespace
  62    :  
  63  E :  TEST_F(ZOutStreamTest, DoingNothingProducesNoData) {
  64  E :    ZOutStream zip_stream(out_stream_.get());
  65  E :    EXPECT_EQ(0u, compressed_.size());
  66  E :    EXPECT_TRUE(zip_stream.Init());
  67  E :    EXPECT_EQ(0u, compressed_.size());
  68  E :  }
  69    :  
  70  E :  TEST_F(ZOutStreamTest, DoingSomethingProducesData) {
  71  E :    ZOutStream zip_stream(out_stream_.get());
  72  E :    EXPECT_TRUE(zip_stream.Init());
  73  E :    EXPECT_TRUE(zip_stream.Write(sizeof(kSampleData), kSampleData));
  74  E :    EXPECT_TRUE(zip_stream.Flush());
  75  E :    EXPECT_LT(0u, compressed_.size());
  76  E :  }
  77    :  
  78  E :  TEST_F(ZInStreamTest, ReadingTruncatedDataFails) {
  79  E :    EXPECT_LT(2u, compressed_.size());
  80  E :    compressed_.resize(compressed_.size() / 2);
  81  E :    in_stream_.reset(CreateByteInStream(compressed_.begin(),
  82    :                                        compressed_.end()));
  83    :  
  84  E :    ZInStream unzip_stream(in_stream_.get());
  85  E :    EXPECT_TRUE(unzip_stream.Init());
  86  E :    size_t bytes_read = 0;
  87  E :    EXPECT_FALSE(unzip_stream.Read(sizeof(buffer), buffer, &bytes_read));
  88  E :  }
  89    :  
  90  E :  TEST_F(ZInStreamTest, DecompressionWorks) {
  91  E :    ZInStream unzip_stream(in_stream_.get());
  92  E :    EXPECT_TRUE(unzip_stream.Init());
  93  E :    EXPECT_TRUE(unzip_stream.Read(sizeof(kSampleData), buffer));
  94  E :    EXPECT_STREQ(reinterpret_cast<const char*>(buffer),
  95  E :                 reinterpret_cast<const char*>(kSampleData));
  96  E :  }
  97    :  
  98  E :  TEST(ZStreamTest, RoundTrip) {
  99  E :    std::vector<uint8_t> compressed;
 100  E :    std::vector<uint8_t> decompressed;
 101    :  
 102  E :    ScopedOutStreamPtr out_stream(
 103    :        CreateByteOutStream(std::back_inserter(compressed)));
 104  E :    ZOutStream zip_stream(out_stream.get());
 105  E :    EXPECT_TRUE(zip_stream.Init());
 106  E :    EXPECT_TRUE(zip_stream.Write(sizeof(kSampleData), kSampleData));
 107  E :    EXPECT_TRUE(zip_stream.Flush());
 108    :  
 109  E :    ScopedInStreamPtr in_stream(
 110    :        CreateByteInStream(compressed.begin(), compressed.end()));
 111  E :    ZInStream unzip_stream(in_stream.get());
 112  E :    EXPECT_TRUE(unzip_stream.Init());
 113    :  
 114    :    // We deliberately try to read more data than necessary to ensure that the
 115    :    // decoder recognizes the end of stream on its own.
 116  E :    decompressed.resize(2 * sizeof(kSampleData));
 117  E :    size_t bytes_read = 0;
 118  E :    EXPECT_TRUE(unzip_stream.Read(decompressed.size(),
 119    :                                  &decompressed[0],
 120  E :                                  &bytes_read));
 121  E :    EXPECT_EQ(sizeof(kSampleData), bytes_read);
 122  E :    decompressed.resize(bytes_read);
 123    :  
 124    :    // We shouldn't be able to read any more data from either stream.
 125  E :    uint8_t buffer[1] = {};
 126  E :    bytes_read = 0;
 127  E :    EXPECT_TRUE(unzip_stream.Read(sizeof(buffer), buffer, &bytes_read));
 128  E :    EXPECT_EQ(0, bytes_read);
 129    :  
 130  E :    bytes_read = 0;
 131  E :    EXPECT_TRUE(in_stream->Read(sizeof(buffer), buffer, &bytes_read));
 132  E :    EXPECT_EQ(0, bytes_read);
 133    :  
 134  E :    EXPECT_THAT(decompressed, testing::ElementsAreArray(kSampleData));
 135  E :  }
 136    :  
 137    :  }  // namespace core

Coverage information generated Fri Jul 29 11:00:21 2016.