Last active
October 13, 2016 20:16
-
-
Save alexlekrow/9cb560b4b552157766a81d8d7f2690fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "composite_functor.h" | |
| #include <string> | |
| #include <cassert> | |
| //============================================================================== | |
| int square_int( int a ) { | |
| return ( a * a ); | |
| } | |
| //============================================================================== | |
| struct Data { | |
| int x; | |
| int y; | |
| int sum( int sum ) { | |
| return sum + x + y; | |
| } | |
| }; | |
| //============================================================================== | |
| void composite_int() | |
| { | |
| Composite_Functor<int> composite; | |
| composite.add( { | |
| square_int | |
| , []( int a ) { return a + a; } | |
| , []( int a ) { return ( a + 1 ); } | |
| } ); | |
| // Composite should evaluate to 9 here | |
| const auto result = composite( 2 ); | |
| assert( ( result == 9) ); | |
| const int factor = 2; | |
| composite.add( [=]( int a ) { return ( a + factor ); } ); | |
| composite.add( [=]( int a ) { return ( a * factor ); } ); | |
| // Composite should evaluate to 22 here | |
| const auto another_result = composite( 2 ); | |
| ASSERT_EQ( 22, another_result ); | |
| } | |
| //============================================================================== | |
| void invariant_int() | |
| { | |
| Invariant_Functor<int> composite; | |
| composite.add( square_int ); | |
| composite.add( []( int a ) { return a + a; } ); | |
| composite.add( []( int a ) { return ( a + 1 ); } ); | |
| // Composite should evaluate to 9 here | |
| const auto result = composite( 2 ); | |
| ASSERT_EQ( 9, result ); | |
| } | |
| //============================================================================== | |
| void composite_int_member() | |
| { | |
| Composite_Functor<int> composite; | |
| Data data { 2, 4 }; | |
| // need to bind member functions to a closure | |
| // placeholder _1 will be substituted for our aggregate result. | |
| using std::placeholders::_1; | |
| composite.add( std::bind( &Data::sum, data, _1 ) ); | |
| const auto result = composite( 2 ); | |
| ASSERT_EQ( 8, result ); | |
| } | |
| //============================================================================== | |
| void composite_string() | |
| { | |
| Composite_Functor<std::string> str_composite; | |
| str_composite.add( []( std::string a ) -> std::string { | |
| return std::string( ( a == "1" ) ? ( "2" ) : ( "x" ) ); | |
| } ); | |
| str_composite.add( []( std::string a ) { | |
| return std::string( ( a == "2" ) ? ( "3" ) : ( "X" ) ); | |
| } ); | |
| const auto str_result = str_composite( "1" ); | |
| // std::cout << "Composite_Functor<string> evaluated = " << str_result << "\n"; | |
| assert( "3" == str_result ); | |
| } | |
| //============================================================================== | |
| int main() { | |
| composite_int(); | |
| invariant_int(); | |
| composite_string(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment