KiCad PCB EDA Suite
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,
49  RETURNED,
50  END,
51  };
52 
54  int m_value;
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 
73 std::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 {
89 public:
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  {
105 
106  for( int i = 1; i <= n; i++ )
107  {
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 );
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();
128  m_cofunc->Resume();
129  }
130 
132  }
133 
135  std::unique_ptr<TEST_COROUTINE> m_cofunc;
136  int m_count;
137 };
138 
142 BOOST_AUTO_TEST_SUITE( Coroutine )
143 
144 
145 
150 {
151  const int count = 2;
152 
153  const std::vector<COROUTINE_TEST_EVENT> exp_events = {
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 
177 BOOST_AUTO_TEST_SUITE_END()
Implement a coroutine.
Definition: coroutine.h:75
bool operator==(const COROUTINE_TEST_EVENT &aOther) const
An event in a simple coroutine harness.
std::function< void(const COROUTINE_TEST_EVENT &)> EVT_HANDLER
std::unique_ptr< TEST_COROUTINE > m_cofunc
bool operator!=(const COROUTINE_TEST_EVENT &aOther) const
BOOST_AUTO_TEST_CASE(Increment)
Declare the test suite.
std::ostream & operator<<(std::ostream &os, const COROUTINE_TEST_EVENT &aObj)
Define a stream function for logging this type.
Simple coroutine harness that runs a coroutine that increments a number up to a pre-set limit,...
The common library.
COROUTINE_INCREMENTING_HARNESS(EVT_HANDLER aHandler, int aCount)