Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ cmake_minimum_required(VERSION 2.8)

project(CppKoans)

if (NOT WIN32)
add_definitions(
-std=c++0x -g -O0
-std=c++14 -g -O0
)
endif()

set(cppkoans_SOURCES
${PROJECT_SOURCE_DIR}/cppkoans.cpp
Expand Down
31 changes: 21 additions & 10 deletions cppkoans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "headers/koan_handler.hpp"
#include "headers/all_koans.hpp"

using namespace std;

/**
*
*/
Expand All @@ -54,24 +52,37 @@ int main()
// Koan 05: pointers
Koan05_pointers koan05 = Koan05_pointers( &status );

// Koan 22: lambdas
Koan11_lambdas koan11 = Koan11_lambdas( &status );

// Koan 22: auto
Koan10_auto koan10 = Koan10_auto(&status);

// Koan XX: sample koans
// KoanXX_sample_koans koanXX = KoanXX_sample_koans( &status );

// Welcome message
status.start();

// The Path of Enlightment
koan00.run();
koan01.run();
koan02.run();
koan03.run();
koan04.run();
koan05.run();
// The Path of Enlightment
koan00.run();
koan01.run();
koan02.run();
koan03.run();
koan04.run();
koan05.run();

koan10.run();
koan11.run();
// koanXX.run();

// Done.
status.end();
return( 0 );
#ifdef WIN32
// make sure the console display stays on top until user presses a key
system("pause");
#endif
return 0;
}

// EOF
2 changes: 2 additions & 0 deletions headers/all_koans.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "koan03_further_types.hpp"
#include "koan04_arrays.hpp"
#include "koan05_pointers.hpp"
#include "koan10_auto.hpp"
#include "koan11_lambdas.hpp"
// When an episode of koans is added, it must be appended here
// #include "koanXX_sample_koans.hpp"

Expand Down
2 changes: 1 addition & 1 deletion headers/fill_me_in_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FillMeInException
if (!(a == b)) \
{ \
std::ostringstream expect;\
expect << a;\
expect << a; \
throw FillMeInException(__FILE__, __LINE__, msg, expect.str()); \
}; \
} while(0)
Expand Down
108 changes: 108 additions & 0 deletions headers/koan10_auto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright (c) 2016 Thibauld Nion <tibonihoo@yahoo.fr>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include "../helper.hpp"

// Do not to forget to rename the preprocessor directives as well!
#ifndef KOAN10_AUTO_HPP
#define KOAN10_AUTO_HPP

// Rename the Episode
class Koan10_auto : Koan
{
private:
KoanHandler *status; //!
// When ever a koan is added at the very bottom, this counter needs to be
// increased.
static const int num_tests = 9; //!

public:
/**
*
*/
Koan10_auto( KoanHandler *status ) : status( status ) {
status->register_koans( num_tests );
}
/**
*
*/
~Koan10_auto() {}

/**
*
*/
void run() {

status->episode_start( "auto" );

// For each koan in this episode, one line needs to be written.
// The koans are executed in the order they are called here.
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::use_decltype_to_get_an_expression_type) );
status->eval_koan(*this, static_cast<void (Koan:: *)()>(&Koan10_auto::static_cast_is_how_you_force_a_variable_type));
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::auto_ignores_constness) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::auto_ignores_referenceness) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::auto_with_universal_reference_distinguishes_lvalue_and_rvalue) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::auto_decays_arrays_and_functions) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::auto_assumes_braces_are_initializer_list) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan10_auto::decltype_auto_use_decltype_type_deduction_rules) );

status->episode_done( "auto" );
}

/**
*
*/
static int get_num_tests() {
return num_tests;
}

private:
// Add further Koans down here by defining their name.
// The implementation of these is done in ~/koans/koan10_auto.cpp
// REMARK: Do not forget to increase this.num_tests when you add
// another koan

void use_decltype_to_get_an_expression_type();

void static_cast_is_how_you_force_a_variable_type();

void auto_ignores_constness();

void auto_ignores_referenceness();

void auto_with_universal_reference_distinguishes_lvalue_and_rvalue();

void auto_decays_arrays_and_functions();

void auto_assumes_braces_are_initializer_list();

void decltype_auto_use_decltype_type_deduction_rules();
};



#endif // KOAN10_AUTO_HPP

// EOF
112 changes: 112 additions & 0 deletions headers/koan11_lambdas.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright (c) 2016 Thibauld Nion <tibonihoo@yahoo.fr>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include "../helper.hpp"

// Do not to forget to rename the preprocessor directives as well!
#ifndef KOAN11_LAMBDAS_HPP
#define KOAN11_LAMBDAS_HPP

// Rename the Episode
class Koan11_lambdas : Koan
{
private:
KoanHandler *status; //!
// When ever a koan is added at the very bottom, this counter needs to be
// increased.
static const int num_tests = 8; //!

public:
/**
*
*/
Koan11_lambdas( KoanHandler *status ) : status( status ) {
status->register_koans( num_tests );
}
/**
*
*/
~Koan11_lambdas() {}

/**
*
*/
void run() {

status->episode_start( "lambdas" );

// For each koan in this episode, one line needs to be written.
// The koans are executed in the order they are called here.
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::bracket_paren_mustache_makes_a_lambda) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::lambdas_make_generic_algorithms_easier_to_user) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::lambdas_can_capture_their_creation_context_in_a_closure) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::lambdas_can_capture_references_to_their_creation_context) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::lambdas_can_modify_their_creation_context) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::stay_away_from_default_capture_mode_reference) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::stay_away_from_default_capture_mode_copy) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan11_lambdas::lambdas_can_help_with_your_curry_ing) );

status->episode_done( "lambdas" );
}

/**
*
*/
static int get_num_tests() {
return num_tests;
}

private:
// Add further Koans down here by defining their name.
// The implementation of these is done in ~/koans/koan11_lambdas.cpp
// REMARK: Do not forget to increase this.num_tests when you add
// another koan

void bracket_paren_mustache_makes_a_lambda();

void lambdas_make_generic_algorithms_easier_to_user();

void lambdas_can_capture_their_creation_context_in_a_closure();

void lambdas_can_capture_references_to_their_creation_context();

void lambdas_can_modify_their_creation_context();

void stay_away_from_default_capture_mode_reference();

void stay_away_from_default_capture_mode_copy();

void lambdas_can_help_with_your_curry_ing();

};

#define REPLACE_WITH_CORRECT_LAMBDA []() { return 0; }

#define REPLACE_WITH_CORRECT_LAMBDA_I0 [](int i) { return 0; }


#endif // KOAN11_LAMBDAS_HPP

// EOF
4 changes: 4 additions & 0 deletions headers/koan_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ void KoanHandler::eval_koan( Koan obj, void ( Koan::*koan )() )
this->total_num_passed++;
} catch( FillMeInException ex ) {
this->print_failure( ex );
#ifdef WIN32
// make sure the console display stays on top until user presses a key
system("pause");
#endif
exit( 1 );
}
}
Expand Down
2 changes: 2 additions & 0 deletions koans/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set(cppkoans_SOURCES
${PROJECT_SOURCE_DIR}/koans/koan03_further_types.cpp
${PROJECT_SOURCE_DIR}/koans/koan04_arrays.cpp
${PROJECT_SOURCE_DIR}/koans/koan05_pointers.cpp
${PROJECT_SOURCE_DIR}/koans/koan10_auto.cpp
${PROJECT_SOURCE_DIR}/koans/koan11_lambdas.cpp
# When an episode of koans is added, it must be appended here
# ${PROJECT_SOURCE_DIR}/koans/koanXX_sample_koans.cpp
PARENT_SCOPE
Expand Down
4 changes: 2 additions & 2 deletions koans/koan01_number_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ void Koan01_number_types::integers_can_be_negative()

void Koan01_number_types::simple_floats()
{
float a_float = 4.2;
float a_float = 4.2f;
ASSERT_EQUAL( a_float, FILL_THE_NUMBER_IN );
}

void Koan01_number_types::floats_have_a_size()
{
float a_float = 4.2;
float a_float = 4.2f;
ASSERT_EQUAL( sizeof( float ), FILL_THE_NUMBER_IN );
ASSERT_EQUAL( sizeof( a_float ), FILL_THE_NUMBER_IN );
}
Expand Down
Loading