KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_inplace_function.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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#define BOOST_TEST_NO_MAIN
20#include <boost/test/unit_test.hpp>
21
22#include <functional>
23#include <string>
24
26
27BOOST_AUTO_TEST_SUITE( InplaceFunction )
28
29// Mirror the DRC deferred-eval usage: a closure capturing a few pointers, moved into the slot.
30BOOST_AUTO_TEST_CASE( MoveAssignAndInvoke )
31{
32 int a = 1, b = 2, c = 3;
33
34 INPLACE_FUNCTION<int()> slot;
35 BOOST_CHECK( !slot );
36
37 slot = INPLACE_FUNCTION<int()>( [&a, &b, &c]() { return a + b + c; } );
38 BOOST_CHECK( bool( slot ) );
39 BOOST_CHECK_EQUAL( slot(), 6 );
40
41 // Reassigning a live slot replaces the target rather than leaking the old one.
42 slot = INPLACE_FUNCTION<int()>( [&a]() { return a * 10; } );
43 BOOST_CHECK_EQUAL( slot(), 10 );
44}
45
46BOOST_AUTO_TEST_CASE( CopyKeepsSourceLive )
47{
48 int v = 7;
49 INPLACE_FUNCTION<int()> src( [&v]() { return v; } );
50
51 INPLACE_FUNCTION<int()> copy = src;
52 BOOST_CHECK_EQUAL( copy(), 7 );
53 BOOST_CHECK_EQUAL( src(), 7 );
54}
55
56BOOST_AUTO_TEST_CASE( MoveLeavesSourceEmpty )
57{
58 int v = 9;
59 INPLACE_FUNCTION<int()> src( [&v]() { return v; } );
60
61 INPLACE_FUNCTION<int()> dst = std::move( src );
62 BOOST_CHECK_EQUAL( dst(), 9 );
63 BOOST_CHECK( !src );
64}
65
66BOOST_AUTO_TEST_CASE( SelfAssignmentIsSafe )
67{
68 int v = 11;
69 INPLACE_FUNCTION<int()> fn( [&v]() { return v; } );
70
71 fn = fn;
72 BOOST_CHECK_EQUAL( fn(), 11 );
73}
74
75// An empty INPLACE_FUNCTION must report bad_function_call, not dereference a null target.
76BOOST_AUTO_TEST_CASE( EmptyInvocationThrows )
77{
79 BOOST_CHECK_THROW( empty(), std::bad_function_call );
80}
81
82BOOST_AUTO_TEST_CASE( StringReturnType )
83{
84 INPLACE_FUNCTION<std::string()> fn( []() { return std::string( "ok" ); } );
85 BOOST_CHECK_EQUAL( fn(), "ok" );
86}
87
A std::function-style callable wrapper that stores the target in a fixed inline buffer and never allo...
static bool empty(const wxTextEntryBase *aCtrl)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(MoveAssignAndInvoke)
BOOST_CHECK_EQUAL(result, "25.4")