diff options
author | garykac@google.com <garykac@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-07 19:58:23 +0000 |
---|---|---|
committer | garykac@google.com <garykac@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-07 19:58:23 +0000 |
commit | cb3b1f93130040a150e7fcc57cd4d5a75569685a (patch) | |
tree | ff93665e3c1478c61663d1107cd42dc25b31448d /remoting/base/multiple_array_input_stream_unittest.cc | |
parent | b0110e822ac2b2db56d1b1542aad06da573cd544 (diff) | |
download | chromium_src-cb3b1f93130040a150e7fcc57cd4d5a75569685a.zip chromium_src-cb3b1f93130040a150e7fcc57cd4d5a75569685a.tar.gz chromium_src-cb3b1f93130040a150e7fcc57cd4d5a75569685a.tar.bz2 |
Copy the (early prototype of) remoting in Chrome into the public tree.
At the moment, this is a semi-functional demo.
BUG=none
TEST=build/run all unittests on linux
Review URL: http://codereview.chromium.org/2690003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49087 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/base/multiple_array_input_stream_unittest.cc')
-rw-r--r-- | remoting/base/multiple_array_input_stream_unittest.cc | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/remoting/base/multiple_array_input_stream_unittest.cc b/remoting/base/multiple_array_input_stream_unittest.cc new file mode 100644 index 0000000..810596c --- /dev/null +++ b/remoting/base/multiple_array_input_stream_unittest.cc @@ -0,0 +1,94 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "remoting/base/multiple_array_input_stream.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace remoting { + +static int ReadFromInput(MultipleArrayInputStream* input, + void* data, int size) { + uint8* out = reinterpret_cast<uint8*>(data); + int out_size = size; + + const void* in; + int in_size = 0; + + while (true) { + if (!input->Next(&in, &in_size)) { + return size - out_size; + } + EXPECT_GT(in_size, -1); + + if (out_size <= in_size) { + memcpy(out, in, out_size); + if (in_size > out_size) { + input->BackUp(in_size - out_size); + } + return size; // Copied all of it. + } + + memcpy(out, in, in_size); + out += in_size; + out_size -= in_size; + } +} + +static void ReadString(MultipleArrayInputStream* input, + const std::string& str) { + scoped_array<char> buffer(new char[str.size() + 1]); + buffer[str.size()] = '\0'; + EXPECT_EQ(ReadFromInput(input, buffer.get(), str.size()), str.size()); + EXPECT_STREQ(str.c_str(), buffer.get()); +} + +// Construct and prepare data in the |output_stream|. +static void PrepareData(scoped_ptr<MultipleArrayInputStream>* stream) { + static const std::string kTestData = + "Hello world!" + "This is testing" + "MultipleArrayInputStream" + "for Chromoting"; + + // Determine how many segments to split kTestData. We split the data in + // 1 character, 2 characters, 1 character, 2 characters ... + int segments = (kTestData.length() / 3) * 2; + int remaining_chars = kTestData.length() % 3; + if (remaining_chars) { + if (remaining_chars == 1) + ++segments; + else + segments += 2; + } + + MultipleArrayInputStream* mstream = new MultipleArrayInputStream(segments); + const char* data = kTestData.c_str(); + for (int i = 0; i < segments; ++i) { + int size = i % 2 == 0 ? 1 : 2; + mstream->SetBuffer(i, reinterpret_cast<const uint8*>(data), size); + data += size; + } + stream->reset(mstream); +} + +TEST(MultipleArrayInputStreamTest, BasicOperations) { + scoped_ptr<MultipleArrayInputStream> stream; + PrepareData(&stream); + + ReadString(stream.get(), "Hello world!"); + ReadString(stream.get(), "This "); + ReadString(stream.get(), "is test"); + EXPECT_TRUE(stream->Skip(3)); + ReadString(stream.get(), "MultipleArrayInput"); + EXPECT_TRUE(stream->Skip(6)); + ReadString(stream.get(), "f"); + ReadString(stream.get(), "o"); + ReadString(stream.get(), "r"); + ReadString(stream.get(), " "); + ReadString(stream.get(), "Chromoting"); +} + +} // namespace remoting |