File tree 6 files changed +93
-17
lines changed
6 files changed +93
-17
lines changed Original file line number Diff line number Diff line change 1
1
cmake_minimum_required (VERSION 3.25)
2
2
project (lzc-coro VERSION 0.1.0
3
3
DESCRIPTION "A simple header-only C++ library to use coroutines"
4
+ HOMEPAGE_URL "https://github.com/lazy-coders/lzc-coro"
4
5
LANGUAGES CXX)
5
6
6
7
#set(CMAKE_CXX_STANDARD 23)
@@ -34,4 +35,5 @@ install(TARGETS lzc-coro
34
35
# INSTALL_DESTINATION
35
36
# ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
36
37
38
+ enable_testing ()
37
39
add_subdirectory (tests)
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ struct task : std::coroutine_handle<promise<T>>
44
44
d (" Resuming this task" );
45
45
this ->resume ();
46
46
d (" Returning the result" );
47
- return this ->promise ().result_ . get_future (). get ();
47
+ return this ->promise ().get_result ();
48
48
}
49
49
50
50
auto get_future ()
@@ -96,6 +96,12 @@ struct promise
96
96
97
97
return t;
98
98
}
99
+
100
+ T get_result ()
101
+ {
102
+ return result_.get_future ().get ();
103
+ }
104
+
99
105
std::promise<T> result_;
100
106
std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
101
107
std::optional<std::coroutine_handle<>> continuation_;
@@ -108,7 +114,7 @@ struct promise<void>
108
114
std::suspend_always initial_suspend () noexcept { d (); return {}; }
109
115
std::suspend_always final_suspend () noexcept { d (); return {}; }
110
116
void unhandled_exception () { d (); throw ; }
111
- void return_void () { d (); }
117
+ void return_void () { d (); ended_. set_value ( true ); }
112
118
113
119
template <typename U>
114
120
auto await_transform (task<U> t)
@@ -127,6 +133,12 @@ struct promise<void>
127
133
128
134
return t;
129
135
}
136
+
137
+ void get_result ()
138
+ {
139
+ ended_.get_future ().get ();
140
+ }
141
+ std::promise<bool > ended_;
130
142
std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
131
143
};
132
144
Original file line number Diff line number Diff line change @@ -91,7 +91,7 @@ void d(const auto_location& t, Args&&... args)
91
91
std::clog << " \" }" << std::endl;
92
92
}
93
93
94
- void d (const std::source_location& sl = std::source_location::current())
94
+ inline void d (const std::source_location& sl = std::source_location::current())
95
95
{
96
96
d (auto_location (" " , sl));
97
97
}
Original file line number Diff line number Diff line change 1
- #CPMAddPackage(
2
- # NAME googletest
3
- # GITHUB_REPOSITORY google/googletest
4
- # GIT_TAG release-1.12.1
5
- # VERSION 1.12.1
6
- # OPTIONS "INSTALL_GTEST OFF" "gtest_force_shared_crt"
7
- #)
8
-
9
- #add_test(NAME lzc-coro-test )
10
- add_executable (lzc-coro-test main.cpp)
11
- target_link_libraries (lzc-coro-test PRIVATE lzc-coro)
12
- #target_include_directories(coro PUBLIC include)
1
+ project (lzc-coro-test )
2
+
3
+ include (../cmake/CPM.cmake)
4
+
5
+ CPMAddPackage(
6
+ NAME googletest
7
+ GITHUB_REPOSITORY google/googletest
8
+ GIT_TAG release-1.12.1
9
+ VERSION 1.12.1
10
+ OPTIONS "INSTALL_GTEST OFF" "gtest_force_shared_crt"
11
+ )
12
+
13
+ add_executable (lzc-coro-test test_simple.cpp test_simulated_http.cpp)
14
+ target_link_libraries (lzc-coro-test PRIVATE lzc-coro GTest::gtest_main GTest::gtest GTest::gmock)
15
+
16
+ add_test (lzc-coro-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} /lzc-coro-test )
Original file line number Diff line number Diff line change
1
+ #include < gtest/gtest.h>
2
+ #include < lzcoders/coro.hpp>
3
+ #include < condition_variable>
4
+ #include < memory>
5
+ #include < lzcoders/debug.hpp>
6
+
7
+ using namespace lzcoders ;
8
+
9
+ task<void > notify_at_end (std::shared_ptr<int > counter)
10
+ {
11
+ struct guard {
12
+ ~guard ()
13
+ {
14
+ d ();
15
+ (*counter)++;
16
+ }
17
+ std::shared_ptr<int > counter;
18
+ };
19
+ guard guard_{counter};
20
+
21
+ co_return ;
22
+ }
23
+
24
+ TEST (lzc_coro, task_with_void_return_should_be_waited_and_completed)
25
+ {
26
+ auto counter = std::make_shared<int >(0 );
27
+ auto t = notify_at_end (counter);
28
+ t ();
29
+ EXPECT_EQ (*counter, 1 );
30
+ }
31
+
32
+ task<int > return_same_value (int value)
33
+ {
34
+ co_return value;
35
+ }
36
+
37
+ TEST (lzc_coro, value_should_be_returned_correctly)
38
+ {
39
+ for (int i = -5 ; i < 10 ; i++)
40
+ {
41
+ auto t = return_same_value (i);
42
+ EXPECT_EQ (i, t ());
43
+ }
44
+ }
45
+
46
+ task<int > return_double_using_co_await_of_task (int value)
47
+ {
48
+ co_return co_await return_same_value (value) + co_await return_same_value (value);
49
+ }
50
+
51
+ TEST (lzc_coro, co_await_of_task_should_work)
52
+ {
53
+ for (int i = -5 ; i < 10 ; i++)
54
+ {
55
+ auto t = return_double_using_co_await_of_task (i);
56
+ EXPECT_EQ (i+i, t ());
57
+ }
58
+ }
Original file line number Diff line number Diff line change 3
3
#include < lzcoders/coro.hpp>
4
4
#include " io_reader.hpp"
5
5
#include " co_spawn.hpp"
6
+ #include < gtest/gtest.h>
6
7
constexpr std::size_t PoolSize = 1 ;
7
8
std::atomic<int > tasks = 1 ;
8
9
@@ -42,13 +43,12 @@ lzcoders::task<void> http_acceptor_loop(lzcoders::thread_pool<PoolSize>& pool)
42
43
co_return /* 'a'*/ ;
43
44
}
44
45
45
- int main ( )
46
+ TEST (lzc_coro, simulated_http )
46
47
{
47
48
lzcoders::thread_pool<PoolSize> pool;
48
49
49
50
lzcoders::co_spawn (pool, http_acceptor_loop (pool));
50
51
51
52
while (tasks > 0 )
52
53
;
53
- return 0 ;
54
54
}
You can’t perform that action at this time.
0 commit comments