TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / common / message_channel_unittest.cpp
1 // Copyright 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "emugl/common/message_channel.h"
16
17 #include "emugl/common/testing/test_thread.h"
18
19 #include <gtest/gtest.h>
20
21 #include <string>
22
23 namespace emugl {
24
25 namespace {
26
27 struct PingPongState {
28     MessageChannel<std::string, 3U> in;
29     MessageChannel<std::string, 3U> out;
30 };
31
32 void* pingPongFunction(void* param) {
33     for (;;) {
34         PingPongState* s = static_cast<PingPongState*>(param);
35         std::string str;
36         s->in.receive(&str);
37         s->out.send(str);
38         if (str == "quit") {
39             break;
40         }
41     }
42     return 0;
43 }
44
45 }  // namespace
46
47 TEST(MessageChannel, SingleThreadWithInt) {
48     MessageChannel<int, 3U> channel;
49     channel.send(1);
50     channel.send(2);
51     channel.send(3);
52
53     int ret;
54     channel.receive(&ret);
55     EXPECT_EQ(1, ret);
56     channel.receive(&ret);
57     EXPECT_EQ(2, ret);
58     channel.receive(&ret);
59     EXPECT_EQ(3, ret);
60 }
61
62 TEST(MessageChannel, SingleThreadWithStdString) {
63     MessageChannel<std::string, 5U> channel;
64     channel.send(std::string("foo"));
65     channel.send(std::string("bar"));
66     channel.send(std::string("zoo"));
67
68     std::string str;
69     channel.receive(&str);
70     EXPECT_STREQ("foo", str.c_str());
71     channel.receive(&str);
72     EXPECT_STREQ("bar", str.c_str());
73     channel.receive(&str);
74     EXPECT_STREQ("zoo", str.c_str());
75 }
76
77 TEST(MessageChannel, TwoThreadsPingPong) {
78     PingPongState state;
79     TestThread* thread = new TestThread(pingPongFunction, &state);
80
81     std::string str;
82     const size_t kCount = 100;
83     for (size_t n = 0; n < kCount; ++n) {
84         state.in.send(std::string("foo"));
85         state.in.send(std::string("bar"));
86         state.in.send(std::string("zoo"));
87         state.out.receive(&str);
88         EXPECT_STREQ("foo", str.c_str());
89         state.out.receive(&str);
90         EXPECT_STREQ("bar", str.c_str());
91         state.out.receive(&str);
92         EXPECT_STREQ("zoo", str.c_str());
93     }
94     state.in.send(std::string("quit"));
95     state.out.receive(&str);
96     EXPECT_STREQ("quit", str.c_str());
97
98     thread->join();
99 }
100
101 }  // namespace emugl