KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_coroutine.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
27
29
30#include <tool/coroutine.h>
31
32#include <common.h>
33
34
39{
40 enum class TYPE
41 {
47 };
48
51
52 bool operator==( const COROUTINE_TEST_EVENT& aOther ) const
53 {
54 return m_type == aOther.m_type && m_value == aOther.m_value;
55 }
56
57 bool operator!=( const COROUTINE_TEST_EVENT& aOther ) const
58 {
59 return !operator==( aOther );
60 }
61};
62
63
67std::ostream& boost_test_print_type( std::ostream& os, const COROUTINE_TEST_EVENT& aObj )
68{
69 os << "COROUTINE_TEST_EVENT[ type: " << (int) aObj.m_type << ", value: " << aObj.m_value
70 << " ]";
71 return os;
72}
73
74
82{
83public:
88
89 using EVT_HANDLER = std::function<void( const COROUTINE_TEST_EVENT& )>;
90
92 : m_handler( aHandler ), m_count( aCount )
93 {
94 }
95
96 int CountTo( int n )
97 {
98 m_handler( { COROUTINE_TEST_EVENT::TYPE::START, 0 } );
99
100 for( int i = 1; i <= n; i++ )
101 {
102 m_handler( { COROUTINE_TEST_EVENT::TYPE::YIELD, i } );
103 m_cofunc->KiYield( i );
104 }
105
106 return 0;
107 }
108
109 void Run()
110 {
111 m_cofunc =
112 std::make_unique<TEST_COROUTINE>( this, &COROUTINE_INCREMENTING_HARNESS::CountTo );
113 m_handler( { COROUTINE_TEST_EVENT::TYPE::CALL, m_count } );
114 m_cofunc->Call( m_count );
115
116 int ret_val = 0;
117
118 while( m_cofunc->Running() )
119 {
120 ret_val = m_cofunc->ReturnValue();
121 m_handler( { COROUTINE_TEST_EVENT::TYPE::RETURNED, ret_val } );
122 m_cofunc->Resume();
123 }
124
125 m_handler( { COROUTINE_TEST_EVENT::TYPE::END, ret_val } );
126 }
127
129 std::unique_ptr<TEST_COROUTINE> m_cofunc;
131};
132
136BOOST_AUTO_TEST_SUITE( Coroutine )
137
138
139
144{
145 const int count = 2;
146
147 const std::vector<COROUTINE_TEST_EVENT> exp_events = {
148 { COROUTINE_TEST_EVENT::TYPE::CALL, count },
149 { COROUTINE_TEST_EVENT::TYPE::START, 0 },
150 { COROUTINE_TEST_EVENT::TYPE::YIELD, 1 },
151 { COROUTINE_TEST_EVENT::TYPE::RETURNED, 1 },
152 { COROUTINE_TEST_EVENT::TYPE::YIELD, 2 },
153 { COROUTINE_TEST_EVENT::TYPE::RETURNED, 2 },
154 { COROUTINE_TEST_EVENT::TYPE::END, 2 },
155 };
156
157 std::vector<COROUTINE_TEST_EVENT> received_events;
158
159 auto handler = [&]( const COROUTINE_TEST_EVENT& aEvent ) {
160 received_events.push_back( aEvent );
161 };
162
163 COROUTINE_INCREMENTING_HARNESS harness( handler, count );
164
165 harness.Run();
166
167 BOOST_CHECK_EQUAL_COLLECTIONS(
168 received_events.begin(), received_events.end(), exp_events.begin(), exp_events.end() );
169}
170
Simple coroutine harness that runs a coroutine that increments a number up to a pre-set limit,...
COROUTINE< int, int > TEST_COROUTINE
The coroutine test take ints and returns them.
std::unique_ptr< TEST_COROUTINE > m_cofunc
std::function< void(const COROUTINE_TEST_EVENT &)> EVT_HANDLER
COROUTINE_INCREMENTING_HARNESS(EVT_HANDLER aHandler, int aCount)
Implement a coroutine.
Definition coroutine.h:80
The common library.
An event in a simple coroutine harness.
bool operator!=(const COROUTINE_TEST_EVENT &aOther) const
bool operator==(const COROUTINE_TEST_EVENT &aOther) const
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
std::ostream & boost_test_print_type(std::ostream &os, const COROUTINE_TEST_EVENT &aObj)
Define a stream function for logging this type.
BOOST_AUTO_TEST_SUITE_END()