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 (C) 2018 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
33
34#include <tool/coroutine.h>
35
36#include <common.h>
37
38
43{
44 enum class TYPE
45 {
46 START,
47 CALL,
48 YIELD,
50 END,
51 };
52
55
56 bool operator==( const COROUTINE_TEST_EVENT& aOther ) const
57 {
58 return m_type == aOther.m_type && m_value == aOther.m_value;
59 }
60
61 bool operator!=( const COROUTINE_TEST_EVENT& aOther ) const
62 {
63 return !operator==( aOther );
64 }
65};
66
67
73std::ostream& operator<<( std::ostream& os, const COROUTINE_TEST_EVENT& aObj )
74{
75 os << "COROUTINE_TEST_EVENT[ type: " << (int) aObj.m_type << ", value: " << aObj.m_value
76 << " ]";
77 return os;
78}
79
80
88{
89public:
94
95 using EVT_HANDLER = std::function<void( const COROUTINE_TEST_EVENT& )>;
96
98 : m_handler( aHandler ), m_count( aCount )
99 {
100 }
101
102 int CountTo( int n )
103 {
104 m_handler( { COROUTINE_TEST_EVENT::TYPE::START, 0 } );
105
106 for( int i = 1; i <= n; i++ )
107 {
108 m_handler( { COROUTINE_TEST_EVENT::TYPE::YIELD, i } );
109 m_cofunc->KiYield( i );
110 }
111
112 return 0;
113 }
114
115 void Run()
116 {
117 m_cofunc =
118 std::make_unique<TEST_COROUTINE>( this, &COROUTINE_INCREMENTING_HARNESS::CountTo );
119 m_handler( { COROUTINE_TEST_EVENT::TYPE::CALL, m_count } );
120 m_cofunc->Call( m_count );
121
122 int ret_val = 0;
123
124 while( m_cofunc->Running() )
125 {
126 ret_val = m_cofunc->ReturnValue();
127 m_handler( { COROUTINE_TEST_EVENT::TYPE::RETURNED, ret_val } );
128 m_cofunc->Resume();
129 }
130
131 m_handler( { COROUTINE_TEST_EVENT::TYPE::END, ret_val } );
132 }
133
135 std::unique_ptr<TEST_COROUTINE> m_cofunc;
137};
138
142BOOST_AUTO_TEST_SUITE( Coroutine )
143
144
145
150{
151 const int count = 2;
152
153 const std::vector<COROUTINE_TEST_EVENT> exp_events = {
154 { COROUTINE_TEST_EVENT::TYPE::CALL, count },
155 { COROUTINE_TEST_EVENT::TYPE::START, 0 },
156 { COROUTINE_TEST_EVENT::TYPE::YIELD, 1 },
157 { COROUTINE_TEST_EVENT::TYPE::RETURNED, 1 },
158 { COROUTINE_TEST_EVENT::TYPE::YIELD, 2 },
159 { COROUTINE_TEST_EVENT::TYPE::RETURNED, 2 },
160 { COROUTINE_TEST_EVENT::TYPE::END, 2 },
161 };
162
163 std::vector<COROUTINE_TEST_EVENT> received_events;
164
165 auto handler = [&]( const COROUTINE_TEST_EVENT& aEvent ) {
166 received_events.push_back( aEvent );
167 };
168
169 COROUTINE_INCREMENTING_HARNESS harness( handler, count );
170
171 harness.Run();
172
173 BOOST_CHECK_EQUAL_COLLECTIONS(
174 received_events.begin(), received_events.end(), exp_events.begin(), exp_events.end() );
175}
176
177BOOST_AUTO_TEST_SUITE_END()
Simple coroutine harness that runs a coroutine that increments a number up to a pre-set limit,...
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:84
The common library.
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1170
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_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(Increment)
Declare the test suite.