X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fexternal%2Fandroid-emugl%2Fshared%2Femugl%2Fcommon%2Fmessage_channel_unittest.cpp;fp=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fexternal%2Fandroid-emugl%2Fshared%2Femugl%2Fcommon%2Fmessage_channel_unittest.cpp;h=2e9d55ecb3a7f16955f4e08d7e1f9be464d94915;hb=e26c1ec581be598521517829adba8c8dd23a768f;hp=0000000000000000000000000000000000000000;hpb=6699c1aea74eeb0eb400e6299079f0c7576f716f;p=iec.git diff --git a/src/type3_AndroidCloud/anbox-master/external/android-emugl/shared/emugl/common/message_channel_unittest.cpp b/src/type3_AndroidCloud/anbox-master/external/android-emugl/shared/emugl/common/message_channel_unittest.cpp new file mode 100644 index 0000000..2e9d55e --- /dev/null +++ b/src/type3_AndroidCloud/anbox-master/external/android-emugl/shared/emugl/common/message_channel_unittest.cpp @@ -0,0 +1,101 @@ +// Copyright 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "emugl/common/message_channel.h" + +#include "emugl/common/testing/test_thread.h" + +#include + +#include + +namespace emugl { + +namespace { + +struct PingPongState { + MessageChannel in; + MessageChannel out; +}; + +void* pingPongFunction(void* param) { + for (;;) { + PingPongState* s = static_cast(param); + std::string str; + s->in.receive(&str); + s->out.send(str); + if (str == "quit") { + break; + } + } + return 0; +} + +} // namespace + +TEST(MessageChannel, SingleThreadWithInt) { + MessageChannel channel; + channel.send(1); + channel.send(2); + channel.send(3); + + int ret; + channel.receive(&ret); + EXPECT_EQ(1, ret); + channel.receive(&ret); + EXPECT_EQ(2, ret); + channel.receive(&ret); + EXPECT_EQ(3, ret); +} + +TEST(MessageChannel, SingleThreadWithStdString) { + MessageChannel channel; + channel.send(std::string("foo")); + channel.send(std::string("bar")); + channel.send(std::string("zoo")); + + std::string str; + channel.receive(&str); + EXPECT_STREQ("foo", str.c_str()); + channel.receive(&str); + EXPECT_STREQ("bar", str.c_str()); + channel.receive(&str); + EXPECT_STREQ("zoo", str.c_str()); +} + +TEST(MessageChannel, TwoThreadsPingPong) { + PingPongState state; + TestThread* thread = new TestThread(pingPongFunction, &state); + + std::string str; + const size_t kCount = 100; + for (size_t n = 0; n < kCount; ++n) { + state.in.send(std::string("foo")); + state.in.send(std::string("bar")); + state.in.send(std::string("zoo")); + state.out.receive(&str); + EXPECT_STREQ("foo", str.c_str()); + state.out.receive(&str); + EXPECT_STREQ("bar", str.c_str()); + state.out.receive(&str); + EXPECT_STREQ("zoo", str.c_str()); + } + state.in.send(std::string("quit")); + state.out.receive(&str); + EXPECT_STREQ("quit", str.c_str()); + + thread->join(); +} + +} // namespace emugl