libcopp  2.2.0
future_test.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
5 
6 #include <cstdio>
7 #include <cstring>
8 #include <iostream>
9 #include <string>
10 
11 #include "frame/test_macros.h"
12 
17  virtual int get_type() { return 1; }
18 
19  int data;
20 };
21 
26  virtual int get_type() { return 2; }
27 };
28 
30  test_trivial_clazz() = default;
31  test_trivial_clazz(int a) : data(a) {}
32  test_trivial_clazz(int a, int) : data(a) {}
33 
34  int data;
35 };
36 
37 #if (defined(__cplusplus) && __cplusplus >= 201402L) || ((defined(_MSVC_LANG) && _MSVC_LANG >= 201402L))
38 static_assert(std::is_trivially_constructible<test_trivial_clazz>::value &&
39  std::is_trivially_copyable<test_trivial_clazz>::value,
40  "trivial check");
41 #elif (defined(__cplusplus) && __cplusplus >= 201103L) || ((defined(_MSVC_LANG) && _MSVC_LANG >= 201103L))
42 static_assert(std::is_trivial<test_trivial_clazz>::value, "trivial check");
43 #endif
44 
45 CASE_TEST(future, poll_void) {
46  copp::future::poller<void> p1;
47  CASE_EXPECT_FALSE(p1.is_ready());
48 
49  copp::future::poller<void> p2(123);
50  CASE_EXPECT_TRUE(p2.is_ready() && p2.data());
51  CASE_EXPECT_EQ(p2.data(), &p2.raw_ptr());
52 
53  std::unique_ptr<bool> param3 = std::unique_ptr<bool>(new bool(false));
54  copp::future::poller<void> p3(std::move(param3));
55  CASE_EXPECT_TRUE(p3.is_ready() && p3.data());
56  CASE_EXPECT_EQ(p3.data(), &p3.raw_ptr());
57 
58  copp::future::poller<void> p4;
59  // set ready
60  p4 = true;
61  CASE_EXPECT_TRUE(p4.is_ready() && p4.data());
62  CASE_EXPECT_EQ(p4.data(), &p4.raw_ptr());
63 }
64 
65 CASE_TEST(future, poll_trival) {
66  copp::future::poller<test_trivial_clazz> p1;
67  CASE_EXPECT_FALSE(p1.is_ready());
68 
69  copp::future::poller<test_trivial_clazz> p2(123);
70  CASE_EXPECT_TRUE(p2.is_ready() && p2.data());
71  CASE_EXPECT_EQ(p2.data() ? p2.data()->data : 0, 123);
72 
73  std::unique_ptr<test_trivial_clazz> param3 = std::unique_ptr<test_trivial_clazz>(new test_trivial_clazz(234));
74  copp::future::poller<test_trivial_clazz> p3(std::move(param3));
75  CASE_EXPECT_TRUE(p3.is_ready() && p3.data());
76  CASE_EXPECT_EQ(p3.data() ? p3.data()->data : 0, 234);
77 
78  copp::future::poller<test_trivial_clazz> p4(test_trivial_clazz(345));
79  CASE_EXPECT_TRUE(p4.is_ready() && p4.data());
80  CASE_EXPECT_EQ(p4.data() ? p4.data()->data : 0, 345);
81 
82  copp::future::poller<test_trivial_clazz> p5(456, 456);
83  CASE_EXPECT_TRUE(p5.is_ready() && p5.data());
84  CASE_EXPECT_EQ(p5.data() ? p5.data()->data : 0, 456);
85 }
86 
87 CASE_TEST(future, poll_trival_object) {
88  copp::future::poller<int> p1;
89  CASE_EXPECT_FALSE(p1.is_ready());
90 
91  copp::future::poller<int> p2(123);
92  CASE_EXPECT_TRUE(p2.is_ready() && p2.data());
93  CASE_EXPECT_EQ(p2.data() ? *p2.data() : 0, 123);
94 
95  std::unique_ptr<int> param3 = std::unique_ptr<int>(new int(234));
96  copp::future::poller<int> p3(std::move(param3));
97  CASE_EXPECT_TRUE(p3.is_ready() && p3.data());
98  CASE_EXPECT_EQ(p3.data() ? *p3.data() : 0, 234);
99 }
100 
101 CASE_TEST(future, poll_no_trivial) {
102  typedef copp::future::poller<test_no_trivial_parent_clazz> test_poll_type;
103 
104  test_poll_type p1;
105  CASE_EXPECT_FALSE(p1.is_ready());
106 
107  test_poll_type p2(std::unique_ptr<test_no_trivial_parent_clazz>(new test_no_trivial_parent_clazz(123)));
108  CASE_EXPECT_TRUE(p2.is_ready() && p2.data());
109  CASE_EXPECT_EQ(p2.data() ? p2.data()->data : 0, 123);
110 
111  test_poll_type p3(std::unique_ptr<test_no_trivial_child_clazz>(new test_no_trivial_child_clazz(234)));
112  CASE_EXPECT_EQ(p3.data() ? p3.data()->data : 0, -234);
113 }
114 
115 CASE_TEST(future, poll_shared_ptr) {
116  typedef copp::future::poller<test_no_trivial_parent_clazz, std::shared_ptr<test_no_trivial_parent_clazz> >
117  test_poll_type;
118 
119  test_poll_type p1;
120  CASE_EXPECT_FALSE(p1.is_ready());
121 
122  test_poll_type p2(std::unique_ptr<test_no_trivial_parent_clazz>(new test_no_trivial_parent_clazz(123)));
123  CASE_EXPECT_TRUE(p2.is_ready() && p2.data());
124  CASE_EXPECT_EQ(p2.data() ? p2.data()->data : 0, 123);
125  CASE_EXPECT_EQ(p2.data() ? p2.data()->get_type() : 0, 1);
126 
127  test_poll_type p3(std::make_shared<test_no_trivial_parent_clazz>(234));
128  CASE_EXPECT_TRUE(p3.is_ready() && p3.data());
129  CASE_EXPECT_EQ(p3.data() ? p3.data()->data : 0, 234);
130  CASE_EXPECT_EQ(p3.data() ? p3.data()->get_type() : 0, 1);
131 
132  test_poll_type p4(std::unique_ptr<test_no_trivial_child_clazz>(new test_no_trivial_child_clazz(345)));
133  CASE_EXPECT_TRUE(p4.is_ready() && p4.data());
134  CASE_EXPECT_EQ(p4.data() ? p4.data()->data : 0, -345);
135  CASE_EXPECT_EQ(p4.data() ? p4.data()->get_type() : 0, 2);
136 
137  test_poll_type p5(std::make_shared<test_no_trivial_child_clazz>(456));
138  CASE_EXPECT_TRUE(p5.is_ready() && p5.data());
139  CASE_EXPECT_EQ(p5.data() ? p5.data()->data : 0, -456);
140  CASE_EXPECT_EQ(p5.data() ? p5.data()->get_type() : 0, 2);
141 }
142 
143 CASE_TEST(future, poll_void_reset_and_swap) {
144  // using copp::future::swap;
145  {
146  copp::future::poller<void> p1;
147  copp::future::poller<void> p2;
148 
149  p1 = true;
150  CASE_EXPECT_TRUE(p1.is_ready());
151  CASE_EXPECT_TRUE(p2.is_pending());
152 
153  swap(p1, p2);
154 
155  CASE_EXPECT_FALSE(p1.is_ready());
156  CASE_EXPECT_FALSE(p2.is_pending());
157 
158  p2.reset();
159  CASE_EXPECT_TRUE(p2.is_pending());
160  }
161 }
162 
163 CASE_TEST(future, poll_trivial_reset_and_swap) {
164  // using copp::future::swap;
165  {
166  copp::future::poller<int> p1;
167  copp::future::poller<int> p2;
168 
169  p1 = 123;
170  CASE_EXPECT_TRUE(p1.is_ready());
171  CASE_EXPECT_TRUE(p2.is_pending());
172 
173  swap(p1, p2);
174 
175  CASE_EXPECT_FALSE(p1.is_ready());
176  CASE_EXPECT_FALSE(p2.is_pending());
177  CASE_EXPECT_EQ(123, *p2.data());
178 
179  p2.reset();
180  CASE_EXPECT_TRUE(p2.is_pending());
181  }
182 }
183 
184 CASE_TEST(future, poll_no_trivial_reset_and_swap) {
185  // using copp::future::swap;
186  {
187  copp::future::poller<test_no_trivial_parent_clazz> p1;
188  copp::future::poller<test_no_trivial_parent_clazz> p2;
189 
190  p1 = copp::future::make_unique<test_no_trivial_parent_clazz>(123);
191  CASE_EXPECT_TRUE(p1.is_ready());
192  CASE_EXPECT_TRUE(p2.is_pending());
193 
194  swap(p1, p2);
195 
196  CASE_EXPECT_FALSE(p1.is_ready());
197  CASE_EXPECT_FALSE(p2.is_pending());
198  CASE_EXPECT_EQ(123, p2.data()->data);
199 
200  p2.reset();
201  CASE_EXPECT_TRUE(p2.is_pending());
202  }
203 }
204 
205 CASE_TEST(future, swap_trivial_result) {
206  // using copp::future::swap;
207  {
208  copp::future::result_type<test_no_trivial_parent_clazz, int> p1 =
209  copp::future::result_type<test_no_trivial_parent_clazz, int>::create_success(123);
210  copp::future::result_type<test_no_trivial_parent_clazz, int> p2 =
211  copp::future::result_type<test_no_trivial_parent_clazz, int>::create_error(456);
212 
213  CASE_EXPECT_TRUE(p1.is_success());
214  CASE_EXPECT_TRUE(p2.is_error());
215 
216  swap(p1, p2);
217 
218  CASE_EXPECT_FALSE(p1.is_success());
219  CASE_EXPECT_FALSE(p2.is_error());
220 
221  CASE_EXPECT_EQ(456, *p1.get_error());
222  CASE_EXPECT_EQ(123, p2.get_success()->data);
223  }
224 }
225 
226 CASE_TEST(future, swap_no_trivial_result) {
227  // using copp::future::swap;
228  {
229  copp::future::result_type<int, int> p1 = copp::future::result_type<int, int>::create_success(123);
230  copp::future::result_type<int, int> p2 = copp::future::result_type<int, int>::create_error(456);
231 
232  CASE_EXPECT_TRUE(p1.is_success());
233  CASE_EXPECT_TRUE(p2.is_error());
234 
235  swap(p1, p2);
236 
237  CASE_EXPECT_FALSE(p1.is_success());
238  CASE_EXPECT_FALSE(p2.is_error());
239 
240  CASE_EXPECT_EQ(456, *p1.get_error());
241  CASE_EXPECT_EQ(123, *p2.get_success());
242  }
243 }
244 
245 CASE_TEST(future, future_with_void_result) {
246  copp::future::future<void> fut;
247 
248  CASE_EXPECT_FALSE(fut.is_ready());
249  CASE_EXPECT_TRUE(fut.is_pending());
250  CASE_EXPECT_EQ(nullptr, fut.data());
251  CASE_EXPECT_EQ(nullptr, fut.raw_ptr().get());
252 
253  fut.reset_data(true);
254  CASE_EXPECT_TRUE(fut.is_ready());
255  CASE_EXPECT_FALSE(fut.is_pending());
256 }
257 
258 CASE_TEST(future, future_with_trival_result) {
259  copp::future::future<int32_t> fut;
260  int32_t simulator_result = 12345678;
261 
262  CASE_EXPECT_FALSE(fut.is_ready());
263  CASE_EXPECT_TRUE(fut.is_pending());
264  CASE_EXPECT_EQ(nullptr, fut.data());
265  CASE_EXPECT_EQ(nullptr, fut.raw_ptr().get());
266 
267  fut.reset_data(simulator_result);
268  CASE_EXPECT_TRUE(fut.is_ready());
269  CASE_EXPECT_FALSE(fut.is_pending());
270 
271  CASE_EXPECT_EQ(simulator_result, *fut.data());
272 }
273 
274 CASE_TEST(future, future_with_no_trival_result) {
275  copp::future::future<std::string> fut;
276 
277  CASE_EXPECT_FALSE(fut.is_ready());
278  CASE_EXPECT_TRUE(fut.is_pending());
279  CASE_EXPECT_EQ(nullptr, fut.data());
280  CASE_EXPECT_EQ(nullptr, fut.raw_ptr().get());
281 
282  fut.reset_data("hello");
283  CASE_EXPECT_TRUE(fut.is_ready());
284  CASE_EXPECT_FALSE(fut.is_pending());
285 
286  CASE_EXPECT_EQ("hello", *fut.data());
287 }
288 
289 CASE_TEST(future, future_with_copp_trivial_result) {
290  using result_type = copp::future::result_type<int, long>;
291  static_assert(COPP_IS_TIRVIALLY_COPYABLE_V(result_type), "result_type<int, long> must be trivial");
292 
293  copp::future::future<result_type> fut;
294 
295  CASE_EXPECT_FALSE(fut.is_ready());
296  CASE_EXPECT_TRUE(fut.is_pending());
297  CASE_EXPECT_EQ(nullptr, fut.data());
298  CASE_EXPECT_EQ(nullptr, fut.raw_ptr().get());
299 
300  fut.reset_data(result_type::make_success(171));
301 
302  CASE_EXPECT_TRUE(fut.data()->is_success());
303  CASE_EXPECT_FALSE(fut.data()->is_error());
304  CASE_EXPECT_EQ(171, *fut.data()->get_success());
305 }
306 
307 CASE_TEST(future, future_with_copp_no_trivial_result) {
308  using result_type = copp::future::result_type<int, std::string>;
309  static_assert(!COPP_IS_TIRVIALLY_COPYABLE_V(result_type), "result_type<int, std::string> must not be trivial");
310 
311  copp::future::future<result_type> fut;
312 
313  CASE_EXPECT_FALSE(fut.is_ready());
314  CASE_EXPECT_TRUE(fut.is_pending());
315  CASE_EXPECT_EQ(nullptr, fut.data());
316  CASE_EXPECT_EQ(nullptr, fut.raw_ptr().get());
317 
318  fut.reset_data(result_type::make_error("hello"));
319 
320  CASE_EXPECT_TRUE(fut.is_ready());
321  CASE_EXPECT_FALSE(fut.is_pending());
322  // already moved into fut
323  CASE_EXPECT_FALSE(fut.data()->is_success());
324  CASE_EXPECT_TRUE(fut.data()->is_error());
325 
326  CASE_EXPECT_EQ("hello", *fut.data()->get_error());
327 }
CASE_TEST(future, poll_void)
Definition: future_test.cpp:45
Definition: future.h:18
void swap(intrusive_ptr< T > &lhs, intrusive_ptr< T > &rhs)
virtual ~test_no_trivial_child_clazz()
Definition: future_test.cpp:25
test_trivial_clazz()=default
test_trivial_clazz(int a, int)
Definition: future_test.cpp:32
#define CASE_EXPECT_FALSE(c)
Definition: test_macros.h:95
#define CASE_EXPECT_EQ(l, r)
Definition: test_macros.h:96
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:94
#define COPP_IS_TIRVIALLY_COPYABLE_V(X)
Definition: type_traits.h:43