Skip to content

Commit a365c14

Browse files
author
Samuel Rodriguez Sevilla
committed
adding tests
1 parent 304c358 commit a365c14

File tree

6 files changed

+93
-17
lines changed

6 files changed

+93
-17
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.25)
22
project(lzc-coro VERSION 0.1.0
33
DESCRIPTION "A simple header-only C++ library to use coroutines"
4+
HOMEPAGE_URL "https://github.com/lazy-coders/lzc-coro"
45
LANGUAGES CXX)
56

67
#set(CMAKE_CXX_STANDARD 23)
@@ -34,4 +35,5 @@ install(TARGETS lzc-coro
3435
# INSTALL_DESTINATION
3536
# ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
3637

38+
enable_testing()
3739
add_subdirectory(tests)

include/lzcoders/coro.hpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct task : std::coroutine_handle<promise<T>>
4444
d("Resuming this task");
4545
this->resume();
4646
d("Returning the result");
47-
return this->promise().result_.get_future().get();
47+
return this->promise().get_result();
4848
}
4949

5050
auto get_future()
@@ -96,6 +96,12 @@ struct promise
9696

9797
return t;
9898
}
99+
100+
T get_result()
101+
{
102+
return result_.get_future().get();
103+
}
104+
99105
std::promise<T> result_;
100106
std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
101107
std::optional<std::coroutine_handle<>> continuation_;
@@ -108,7 +114,7 @@ struct promise<void>
108114
std::suspend_always initial_suspend() noexcept { d(); return {}; }
109115
std::suspend_always final_suspend() noexcept { d(); return {}; }
110116
void unhandled_exception() { d(); throw; }
111-
void return_void() { d(); }
117+
void return_void() { d(); ended_.set_value(true); }
112118

113119
template<typename U>
114120
auto await_transform(task<U> t)
@@ -127,6 +133,12 @@ struct promise<void>
127133

128134
return t;
129135
}
136+
137+
void get_result()
138+
{
139+
ended_.get_future().get();
140+
}
141+
std::promise<bool> ended_;
130142
std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
131143
};
132144

include/lzcoders/debug.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void d(const auto_location& t, Args&&... args)
9191
std::clog << "\" }" << std::endl;
9292
}
9393

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())
9595
{
9696
d(auto_location("", sl));
9797
}

tests/CMakeLists.txt

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
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)

tests/test_simple.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

tests/main.cpp renamed to tests/test_simulated_http.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <lzcoders/coro.hpp>
44
#include "io_reader.hpp"
55
#include "co_spawn.hpp"
6+
#include <gtest/gtest.h>
67
constexpr std::size_t PoolSize = 1;
78
std::atomic<int> tasks = 1;
89

@@ -42,13 +43,12 @@ lzcoders::task<void> http_acceptor_loop(lzcoders::thread_pool<PoolSize>& pool)
4243
co_return /*'a'*/;
4344
}
4445

45-
int main()
46+
TEST(lzc_coro, simulated_http)
4647
{
4748
lzcoders::thread_pool<PoolSize> pool;
4849

4950
lzcoders::co_spawn(pool, http_acceptor_loop(pool));
5051

5152
while(tasks > 0)
5253
;
53-
return 0;
5454
}

0 commit comments

Comments
 (0)