OLD | NEW |
(Empty) | |
| 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 class ByteBufferTest : public ::testing::Test {}; |
| 50 |
| 51 TEST_F(ByteBufferTest, CreateFromSingleSlice) { |
| 52 gpr_slice hello = gpr_slice_from_copied_string(kContent1); |
| 53 Slice s(hello, Slice::STEAL_REF); |
| 54 ByteBuffer buffer(&s, 1); |
| 55 } |
| 56 |
| 57 TEST_F(ByteBufferTest, CreateFromVector) { |
| 58 gpr_slice hello = gpr_slice_from_copied_string(kContent1); |
| 59 gpr_slice world = gpr_slice_from_copied_string(kContent2); |
| 60 std::vector<Slice> slices; |
| 61 slices.push_back(Slice(hello, Slice::STEAL_REF)); |
| 62 slices.push_back(Slice(world, Slice::STEAL_REF)); |
| 63 ByteBuffer buffer(&slices[0], 2); |
| 64 } |
| 65 |
| 66 TEST_F(ByteBufferTest, Clear) { |
| 67 gpr_slice hello = gpr_slice_from_copied_string(kContent1); |
| 68 Slice s(hello, Slice::STEAL_REF); |
| 69 ByteBuffer buffer(&s, 1); |
| 70 buffer.Clear(); |
| 71 } |
| 72 |
| 73 TEST_F(ByteBufferTest, Length) { |
| 74 gpr_slice hello = gpr_slice_from_copied_string(kContent1); |
| 75 gpr_slice world = gpr_slice_from_copied_string(kContent2); |
| 76 std::vector<Slice> slices; |
| 77 slices.push_back(Slice(hello, Slice::STEAL_REF)); |
| 78 slices.push_back(Slice(world, Slice::STEAL_REF)); |
| 79 ByteBuffer buffer(&slices[0], 2); |
| 80 EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length()); |
| 81 } |
| 82 |
| 83 bool SliceEqual(const Slice& a, gpr_slice b) { |
| 84 if (a.size() != GPR_SLICE_LENGTH(b)) { |
| 85 return false; |
| 86 } |
| 87 for (size_t i = 0; i < a.size(); i++) { |
| 88 if (a.begin()[i] != GPR_SLICE_START_PTR(b)[i]) { |
| 89 return false; |
| 90 } |
| 91 } |
| 92 return true; |
| 93 } |
| 94 |
| 95 TEST_F(ByteBufferTest, Dump) { |
| 96 gpr_slice hello = gpr_slice_from_copied_string(kContent1); |
| 97 gpr_slice world = gpr_slice_from_copied_string(kContent2); |
| 98 std::vector<Slice> slices; |
| 99 slices.push_back(Slice(hello, Slice::STEAL_REF)); |
| 100 slices.push_back(Slice(world, Slice::STEAL_REF)); |
| 101 ByteBuffer buffer(&slices[0], 2); |
| 102 slices.clear(); |
| 103 buffer.Dump(&slices); |
| 104 EXPECT_TRUE(SliceEqual(slices[0], hello)); |
| 105 EXPECT_TRUE(SliceEqual(slices[1], world)); |
| 106 } |
| 107 |
| 108 } // namespace |
| 109 } // namespace grpc |
| 110 |
| 111 int main(int argc, char** argv) { |
| 112 ::testing::InitGoogleTest(&argc, argv); |
| 113 return RUN_ALL_TESTS(); |
| 114 } |
OLD | NEW |