LCOV - code coverage report
Current view: top level - test/cpp/util - byte_buffer_test.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 50 52 96.2 %
Date: 2015-10-10 Functions: 26 27 96.3 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc++/support/byte_buffer.h>
      35             : 
      36             : #include <cstring>
      37             : #include <vector>
      38             : 
      39             : #include <grpc/support/slice.h>
      40             : #include <grpc++/support/slice.h>
      41             : #include <gtest/gtest.h>
      42             : 
      43             : namespace grpc {
      44             : namespace {
      45             : 
      46             : const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
      47             : const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
      48             : 
      49          10 : class ByteBufferTest : public ::testing::Test {};
      50             : 
      51           5 : TEST_F(ByteBufferTest, CreateFromSingleSlice) {
      52           1 :   gpr_slice hello = gpr_slice_from_copied_string(kContent1);
      53           1 :   Slice s(hello, Slice::STEAL_REF);
      54           1 :   ByteBuffer buffer(&s, 1);
      55           1 : }
      56             : 
      57           5 : TEST_F(ByteBufferTest, CreateFromVector) {
      58           1 :   gpr_slice hello = gpr_slice_from_copied_string(kContent1);
      59           1 :   gpr_slice world = gpr_slice_from_copied_string(kContent2);
      60           1 :   std::vector<Slice> slices;
      61           1 :   slices.push_back(Slice(hello, Slice::STEAL_REF));
      62           1 :   slices.push_back(Slice(world, Slice::STEAL_REF));
      63           1 :   ByteBuffer buffer(&slices[0], 2);
      64           1 : }
      65             : 
      66           5 : TEST_F(ByteBufferTest, Clear) {
      67           1 :   gpr_slice hello = gpr_slice_from_copied_string(kContent1);
      68           1 :   Slice s(hello, Slice::STEAL_REF);
      69           2 :   ByteBuffer buffer(&s, 1);
      70           2 :   buffer.Clear();
      71           1 : }
      72             : 
      73           5 : TEST_F(ByteBufferTest, Length) {
      74           1 :   gpr_slice hello = gpr_slice_from_copied_string(kContent1);
      75           1 :   gpr_slice world = gpr_slice_from_copied_string(kContent2);
      76           1 :   std::vector<Slice> slices;
      77           1 :   slices.push_back(Slice(hello, Slice::STEAL_REF));
      78           1 :   slices.push_back(Slice(world, Slice::STEAL_REF));
      79           2 :   ByteBuffer buffer(&slices[0], 2);
      80           2 :   EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
      81           1 : }
      82             : 
      83           2 : bool SliceEqual(const Slice& a, gpr_slice b) {
      84           2 :   if (a.size() != GPR_SLICE_LENGTH(b)) {
      85           0 :     return false;
      86             :   }
      87          77 :   for (size_t i = 0; i < a.size(); i++) {
      88          75 :     if (a.begin()[i] != GPR_SLICE_START_PTR(b)[i]) {
      89           0 :       return false;
      90             :     }
      91             :   }
      92           2 :   return true;
      93             : }
      94             : 
      95           5 : TEST_F(ByteBufferTest, Dump) {
      96           1 :   gpr_slice hello = gpr_slice_from_copied_string(kContent1);
      97           1 :   gpr_slice world = gpr_slice_from_copied_string(kContent2);
      98           1 :   std::vector<Slice> slices;
      99           1 :   slices.push_back(Slice(hello, Slice::STEAL_REF));
     100           1 :   slices.push_back(Slice(world, Slice::STEAL_REF));
     101           2 :   ByteBuffer buffer(&slices[0], 2);
     102           1 :   slices.clear();
     103           1 :   buffer.Dump(&slices);
     104           1 :   EXPECT_TRUE(SliceEqual(slices[0], hello));
     105           2 :   EXPECT_TRUE(SliceEqual(slices[1], world));
     106           1 : }
     107             : 
     108             : }  // namespace
     109             : }  // namespace grpc
     110             : 
     111           1 : int main(int argc, char** argv) {
     112           1 :   ::testing::InitGoogleTest(&argc, argv);
     113           1 :   return RUN_ALL_TESTS();
     114           3 : }

Generated by: LCOV version 1.10