Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: remoting/base/multiple_array_input_stream_unittest.cc

Issue 4779001: Added CompoundBuffer that will be used to store data in the encoding/decoding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/base/multiple_array_input_stream.cc ('k') | remoting/protocol/connection_to_client.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/scoped_ptr.h"
8 #include "net/base/io_buffer.h"
9 #include "remoting/base/multiple_array_input_stream.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace remoting {
13
14 // TODO(sergeyu): Add SCOPED_TRACE() for ReadFromInput() and ReadString().
15
16 static size_t ReadFromInput(MultipleArrayInputStream* input,
17 void* data, size_t size) {
18 uint8* out = reinterpret_cast<uint8*>(data);
19 int out_size = size;
20
21 const void* in;
22 int in_size = 0;
23
24 while (true) {
25 if (!input->Next(&in, &in_size)) {
26 return size - out_size;
27 }
28 EXPECT_GT(in_size, -1);
29
30 if (out_size <= in_size) {
31 memcpy(out, in, out_size);
32 if (in_size > out_size) {
33 input->BackUp(in_size - out_size);
34 }
35 return size; // Copied all of it.
36 }
37
38 memcpy(out, in, in_size);
39 out += in_size;
40 out_size -= in_size;
41 }
42 }
43
44 static void ReadString(MultipleArrayInputStream* input,
45 const std::string& str) {
46 scoped_array<char> buffer(new char[str.size() + 1]);
47 buffer[str.size()] = '\0';
48 EXPECT_EQ(ReadFromInput(input, buffer.get(), str.size()), str.size());
49 EXPECT_STREQ(str.data(), buffer.get());
50 }
51
52 // Construct and prepare data in the |output_stream|.
53 static void PrepareData(scoped_ptr<MultipleArrayInputStream>* stream) {
54 static const std::string kTestData =
55 "Hello world!"
56 "This is testing"
57 "MultipleArrayInputStream"
58 "for Chromoting";
59
60 // Determine how many segments to split kTestData. We split the data in
61 // 1 character, 2 characters, 1 character, 2 characters ...
62 int segments = (kTestData.length() / 3) * 2;
63 int remaining_chars = kTestData.length() % 3;
64 if (remaining_chars) {
65 if (remaining_chars == 1)
66 ++segments;
67 else
68 segments += 2;
69 }
70
71 MultipleArrayInputStream* mstream = new MultipleArrayInputStream();
72 const char* data = kTestData.data();
73 for (int i = 0; i < segments; ++i) {
74 int size = i % 2 == 0 ? 1 : 2;
75 mstream->AddBuffer(new net::StringIOBuffer(std::string(data, size)), size);
76 data += size;
77 }
78 stream->reset(mstream);
79 }
80
81 TEST(MultipleArrayInputStreamTest, BasicOperations) {
82 scoped_ptr<MultipleArrayInputStream> stream;
83 PrepareData(&stream);
84
85 ReadString(stream.get(), "Hello world!");
86 ReadString(stream.get(), "This ");
87 ReadString(stream.get(), "is test");
88 EXPECT_TRUE(stream->Skip(3));
89 ReadString(stream.get(), "MultipleArrayInput");
90 EXPECT_TRUE(stream->Skip(6));
91 ReadString(stream.get(), "f");
92 ReadString(stream.get(), "o");
93 ReadString(stream.get(), "r");
94 ReadString(stream.get(), " ");
95 ReadString(stream.get(), "Chromoting");
96 }
97
98 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/base/multiple_array_input_stream.cc ('k') | remoting/protocol/connection_to_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698