OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "remoting/base/multiple_array_input_stream.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace remoting { |
| 11 |
| 12 static int ReadFromInput(MultipleArrayInputStream* input, |
| 13 void* data, int size) { |
| 14 uint8* out = reinterpret_cast<uint8*>(data); |
| 15 int out_size = size; |
| 16 |
| 17 const void* in; |
| 18 int in_size = 0; |
| 19 |
| 20 while (true) { |
| 21 if (!input->Next(&in, &in_size)) { |
| 22 return size - out_size; |
| 23 } |
| 24 EXPECT_GT(in_size, -1); |
| 25 |
| 26 if (out_size <= in_size) { |
| 27 memcpy(out, in, out_size); |
| 28 if (in_size > out_size) { |
| 29 input->BackUp(in_size - out_size); |
| 30 } |
| 31 return size; // Copied all of it. |
| 32 } |
| 33 |
| 34 memcpy(out, in, in_size); |
| 35 out += in_size; |
| 36 out_size -= in_size; |
| 37 } |
| 38 } |
| 39 |
| 40 static void ReadString(MultipleArrayInputStream* input, |
| 41 const std::string& str) { |
| 42 scoped_array<char> buffer(new char[str.size() + 1]); |
| 43 buffer[str.size()] = '\0'; |
| 44 EXPECT_EQ(ReadFromInput(input, buffer.get(), str.size()), str.size()); |
| 45 EXPECT_STREQ(str.c_str(), buffer.get()); |
| 46 } |
| 47 |
| 48 // Construct and prepare data in the |output_stream|. |
| 49 static void PrepareData(scoped_ptr<MultipleArrayInputStream>* stream) { |
| 50 static const std::string kTestData = |
| 51 "Hello world!" |
| 52 "This is testing" |
| 53 "MultipleArrayInputStream" |
| 54 "for Chromoting"; |
| 55 |
| 56 // Determine how many segments to split kTestData. We split the data in |
| 57 // 1 character, 2 characters, 1 character, 2 characters ... |
| 58 int segments = (kTestData.length() / 3) * 2; |
| 59 int remaining_chars = kTestData.length() % 3; |
| 60 if (remaining_chars) { |
| 61 if (remaining_chars == 1) |
| 62 ++segments; |
| 63 else |
| 64 segments += 2; |
| 65 } |
| 66 |
| 67 MultipleArrayInputStream* mstream = new MultipleArrayInputStream(segments); |
| 68 const char* data = kTestData.c_str(); |
| 69 for (int i = 0; i < segments; ++i) { |
| 70 int size = i % 2 == 0 ? 1 : 2; |
| 71 mstream->SetBuffer(i, reinterpret_cast<const uint8*>(data), size); |
| 72 data += size; |
| 73 } |
| 74 stream->reset(mstream); |
| 75 } |
| 76 |
| 77 TEST(MultipleArrayInputStreamTest, BasicOperations) { |
| 78 scoped_ptr<MultipleArrayInputStream> stream; |
| 79 PrepareData(&stream); |
| 80 |
| 81 ReadString(stream.get(), "Hello world!"); |
| 82 ReadString(stream.get(), "This "); |
| 83 ReadString(stream.get(), "is test"); |
| 84 EXPECT_TRUE(stream->Skip(3)); |
| 85 ReadString(stream.get(), "MultipleArrayInput"); |
| 86 EXPECT_TRUE(stream->Skip(6)); |
| 87 ReadString(stream.get(), "f"); |
| 88 ReadString(stream.get(), "o"); |
| 89 ReadString(stream.get(), "r"); |
| 90 ReadString(stream.get(), " "); |
| 91 ReadString(stream.get(), "Chromoting"); |
| 92 } |
| 93 |
| 94 } // namespace remoting |
OLD | NEW |