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
71std::ostream& boost_test_print_type( std::ostream& os, const COROUTINE_TEST_EVENT& aObj )
72{
73 os << "COROUTINE_TEST_EVENT[ type: " << (int) aObj.m_type << ", value: " << aObj.m_value
74 << " ]";
75 return os;
76}
77
78
86{
87public:
92
93 using EVT_HANDLER = std::function<void( const COROUTINE_TEST_EVENT& )>;
94
96 : m_handler( aHandler ), m_count( aCount )
97 {
98 }
99
100 int CountTo( int n )
101 {
102 m_handler( { COROUTINE_TEST_EVENT::TYPE::START, 0 } );
103
104 for( int i = 1; i <= n; i++ )
105 {
106 m_handler( { COROUTINE_TEST_EVENT::TYPE::YIELD, i } );
107 m_cofunc->KiYield( i );
108 }
109
110 return 0;
111 }
112
113 void Run()
114 {
115 m_cofunc =
116 std::make_unique<TEST_COROUTINE>( this, &COROUTINE_INCREMENTING_HARNESS::CountTo );
117 m_handler( { COROUTINE_TEST_EVENT::TYPE::CALL, m_count } );
118 m_cofunc->Call( m_count );
119
120 int ret_val = 0;
121
122 while( m_cofunc->Running() )
123 {
124 ret_val = m_cofunc->ReturnValue();
125 m_handler( { COROUTINE_TEST_EVENT::TYPE::RETURNED, ret_val } );
126 m_cofunc->Resume();
127 }
128
129 m_handler( { COROUTINE_TEST_EVENT::TYPE::END, ret_val } );
130 }
131
133 std::unique_ptr<TEST_COROUTINE> m_cofunc;
135};
136
140BOOST_AUTO_TEST_SUITE( Coroutine )
141
142
143
148{
149 const int count = 2;
150
151 const std::vector<COROUTINE_TEST_EVENT> exp_events = {
152 { COROUTINE_TEST_EVENT::TYPE::CALL, count },
153 { COROUTINE_TEST_EVENT::TYPE::START, 0 },
154 { COROUTINE_TEST_EVENT::TYPE::YIELD, 1 },
155 { COROUTINE_TEST_EVENT::TYPE::RETURNED, 1 },
156 { COROUTINE_TEST_EVENT::TYPE::YIELD, 2 },
157 { COROUTINE_TEST_EVENT::TYPE::RETURNED, 2 },
158 { COROUTINE_TEST_EVENT::TYPE::END, 2 },
159 };
160
161 std::vector<COROUTINE_TEST_EVENT> received_events;
162
163 auto handler = [&]( const COROUTINE_TEST_EVENT& aEvent ) {
164 received_events.push_back( aEvent );
165 };
166
167 COROUTINE_INCREMENTING_HARNESS harness( handler, count );
168
169 harness.Run();
170
171 BOOST_CHECK_EQUAL_COLLECTIONS(
172 received_events.begin(), received_events.end(), exp_events.begin(), exp_events.end() );
173}
174
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.
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.
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()