1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_async2/poll.h"
16
17 #include <type_traits>
18
19 #include "gtest/gtest.h"
20 #include "pw_result/result.h"
21
22 namespace pw::async2 {
23 namespace {
24
25 class MoveOnly {
26 public:
27 MoveOnly() = delete;
MoveOnly(int value)28 MoveOnly(int value) : value_(value) {}
29 MoveOnly(const MoveOnly&) = delete;
30 MoveOnly& operator=(const MoveOnly&) = delete;
31 MoveOnly(MoveOnly&&) = default;
32 MoveOnly& operator=(MoveOnly&&) = default;
value() const33 int value() const { return value_; }
34
35 private:
36 int value_;
37 };
38
39 struct Immovable {
40 public:
41 Immovable() = delete;
Immovablepw::async2::__anond9034ae80111::Immovable42 Immovable(MoveOnly&& value) : value_(std::move(value)) {}
43 Immovable(const Immovable&) = delete;
44 Immovable& operator=(const Immovable&) = delete;
45 Immovable(Immovable&&) = delete;
46 Immovable& operator=(Immovable&&) = delete;
valuepw::async2::__anond9034ae80111::Immovable47 int value() const { return value_.value(); }
48
49 private:
50 MoveOnly value_;
51 };
52
TEST(Poll,ConstructsReadyInPlace)53 TEST(Poll, ConstructsReadyInPlace) {
54 Poll<Immovable> mr(std::in_place, MoveOnly(5));
55 EXPECT_TRUE(mr.IsReady());
56 EXPECT_EQ(mr->value(), 5);
57 }
58
TEST(Poll,ConstructsReadyFromValueType)59 TEST(Poll, ConstructsReadyFromValueType) {
60 Poll<MoveOnly> mr = MoveOnly(5);
61 EXPECT_TRUE(mr.IsReady());
62 EXPECT_EQ(mr->value(), 5);
63 }
64
TEST(Poll,ConstructsFromValueConvertibleToValueType)65 TEST(Poll, ConstructsFromValueConvertibleToValueType) {
66 Poll<Immovable> mr(MoveOnly(5));
67 EXPECT_TRUE(mr.IsReady());
68 EXPECT_EQ(mr->value(), 5);
69 }
70
TEST(Poll,ConstructsFromPollWithValueConvertibleToValueType)71 TEST(Poll, ConstructsFromPollWithValueConvertibleToValueType) {
72 Poll<MoveOnly> move_poll(MoveOnly(5));
73 Poll<Immovable> no_move_poll(std::move(move_poll));
74 EXPECT_TRUE(no_move_poll.IsReady());
75 EXPECT_EQ(no_move_poll->value(), 5);
76 }
77
TEST(Poll,ConstructsPendingFromPendingType)78 TEST(Poll, ConstructsPendingFromPendingType) {
79 Poll<MoveOnly> mr(Pending());
80 EXPECT_FALSE(mr.IsReady());
81 }
82
TEST(Poll,ConstructorInfersValueType)83 TEST(Poll, ConstructorInfersValueType) {
84 auto res = Poll("hello");
85 static_assert(std::is_same_v<decltype(res), Poll<const char*>>);
86 EXPECT_TRUE(res.IsReady());
87 EXPECT_STREQ(res.value(), "hello");
88 }
89
TEST(Poll,ReadinessOnReadyValueReturnsReadyWithoutValue)90 TEST(Poll, ReadinessOnReadyValueReturnsReadyWithoutValue) {
91 Poll<int> v = Ready(5);
92 Poll<> readiness = v.Readiness();
93 EXPECT_TRUE(readiness.IsReady());
94 }
95
TEST(Poll,ReadinessOnPendingValueReturnsPendingWithoutValue)96 TEST(Poll, ReadinessOnPendingValueReturnsPendingWithoutValue) {
97 Poll<int> v = Pending();
98 Poll<> readiness = v.Readiness();
99 EXPECT_TRUE(readiness.IsPending());
100 }
101
TEST(Poll,ReadyToString)102 TEST(Poll, ReadyToString) {
103 char buffer[128] = {};
104 Poll<> v = Ready();
105 EXPECT_EQ(5u, ToString(v, buffer).size());
106 EXPECT_STREQ("Ready", buffer);
107 }
108
TEST(Poll,ReadyValueToString)109 TEST(Poll, ReadyValueToString) {
110 char buffer[128] = {};
111 Poll<uint16_t> v = 5;
112 EXPECT_EQ(8u, ToString(v, buffer).size());
113 EXPECT_STREQ("Ready(5)", buffer);
114 }
115
TEST(Poll,PendingToString)116 TEST(Poll, PendingToString) {
117 char buffer[128] = {};
118 Poll<uint16_t> v = Pending();
119 EXPECT_EQ(7u, ToString(v, buffer).size());
120 EXPECT_STREQ("Pending", buffer);
121 }
122
TEST(PendingFunction,ReturnsValueConvertibleToPendingPoll)123 TEST(PendingFunction, ReturnsValueConvertibleToPendingPoll) {
124 Poll<MoveOnly> mr = Pending();
125 EXPECT_FALSE(mr.IsReady());
126 }
127
TEST(ReadyFunction,CalledWithNoArgumentsReturnsPollWithReadyType)128 TEST(ReadyFunction, CalledWithNoArgumentsReturnsPollWithReadyType) {
129 Poll<> mr = Ready();
130 EXPECT_TRUE(mr.IsReady());
131 [[maybe_unused]] ReadyType& ready_value = mr.value();
132 }
133
TEST(ReadyFunction,ConstructsReadyInPlace)134 TEST(ReadyFunction, ConstructsReadyInPlace) {
135 Poll<Immovable> mr = Ready<Immovable>(std::in_place, MoveOnly(5));
136 EXPECT_TRUE(mr.IsReady());
137 EXPECT_EQ(mr->value(), 5);
138 }
139
TEST(ReadyFunction,ConstructsReadyFromValueType)140 TEST(ReadyFunction, ConstructsReadyFromValueType) {
141 Poll<MoveOnly> mr = Ready(MoveOnly(5));
142 EXPECT_TRUE(mr.IsReady());
143 EXPECT_EQ(mr->value(), 5);
144 }
145
EndToEndTest(int input)146 Poll<Result<int>> EndToEndTest(int input) {
147 if (input == 0) {
148 // Check that returning plain ``Status`` works.
149 return Status::PermissionDenied();
150 }
151 if (input == 1) {
152 // Check that returning ``Pending`` works.
153 return Pending();
154 }
155 if (input == 2) {
156 // Check that returning ``Result<int>`` works.
157 Result<int> v = 2;
158 return v;
159 }
160 if (input == 3) {
161 // Check that returning plain ``int`` works.
162 return 3;
163 }
164 if (input == 4) {
165 // Check that returning ``Poll<int>`` works.
166 return Ready(4);
167 }
168 if (input == 5) {
169 // Check that returning ``Poll<Status>`` works.
170 return Ready(Status::DataLoss());
171 }
172 return Status::Unknown();
173 }
174
TEST(EndToEndTest,ReturnsStatus)175 TEST(EndToEndTest, ReturnsStatus) {
176 Poll<Result<int>> result = EndToEndTest(0);
177 ASSERT_TRUE(result.IsReady());
178 EXPECT_EQ(result->status(), Status::PermissionDenied());
179 }
180
TEST(EndToEndTest,ReturnsPending)181 TEST(EndToEndTest, ReturnsPending) {
182 Poll<Result<int>> result = EndToEndTest(1);
183 EXPECT_FALSE(result.IsReady());
184 }
185
TEST(EndToEndTest,ReturnsValue)186 TEST(EndToEndTest, ReturnsValue) {
187 Poll<Result<int>> result = EndToEndTest(3);
188 ASSERT_TRUE(result.IsReady());
189 ASSERT_TRUE(result->ok());
190 EXPECT_EQ(**result, 3);
191 }
192
TEST(EndToEndTest,ReturnsReady)193 TEST(EndToEndTest, ReturnsReady) {
194 Poll<Result<int>> result = EndToEndTest(4);
195 ASSERT_TRUE(result.IsReady());
196 ASSERT_TRUE(result->ok());
197 EXPECT_EQ(**result, 4);
198 }
199
TEST(EndToEndTest,ReturnsPollStatus)200 TEST(EndToEndTest, ReturnsPollStatus) {
201 Poll<Result<int>> result = EndToEndTest(5);
202 ASSERT_TRUE(result.IsReady());
203 EXPECT_EQ(result->status(), Status::DataLoss());
204 }
205
206 } // namespace
207 } // namespace pw::async2
208