Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some extra koans for pointers; new koan for file handling #8

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
16 changes: 8 additions & 8 deletions README.markdown → README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#CppKoans
# CppKoans

Inspired by [RubyKoans](https://github.com/edgecase/ruby_koans) and
[JavaScript-Koans](https://github.com/liammclennan/JavaScript-Koans), this is
an attempt to write such koans for C/C++.

Some ideas were taken from [PointerKoans](https://github.com/paytonrules/PointerKoan).

###Prerequesites
### Prerequesites
You will need [CMake](http://cmake.org/).

And of course a C++ compiler.
I just tested it with a recent GCC.

##How to walk the path to enlightment
## How to walk the path to enlightment
1. Get the sources

git clone git://github.com/torbjoernk/CppKoans.git
Expand All @@ -25,7 +25,7 @@ I just tested it with a recent GCC.

3. Configure the build

cmake ..
cmake ./..

4. Compile

Expand All @@ -50,16 +50,16 @@ Thus, walking the path to enlightment is a repetition of these steps:
3. Read the master's reply with `./CppKoans/build/CppKoans`


##Adding further Koans
###To existing episodes
## Adding further Koans
### To existing episodes
Just define a new `private void` function without parameters in the bottom
section of the episode's header file it should belong to.
Then go to the implementation file of that episode and implement your new koan.
Finally add your newly created koan to the `run()` function in the header file
of that episode and increase the `num_tests` counter by one (or whatevery amount
of koans you added).

###New episodes
### New episodes
There is a sample episode, which can be used as a template for new episodes.
After copying and renaming of `~/headers/koanXX_sample_koans.hpp` and
`~/koans/koanXX_sample_koans.cpp` the following steps are necessary:
Expand All @@ -71,5 +71,5 @@ After copying and renaming of `~/headers/koanXX_sample_koans.hpp` and
3. enable and activate the new episode in `~/cppkoans.cpp`


##Licence
## Licence
MIT License Copyright 2012 Torbjörn Klatt - opensource eht torbjoern minus klatt dot de
6 changes: 5 additions & 1 deletion cppkoans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ int main()
// Koan 05: pointers
Koan05_pointers koan05 = Koan05_pointers( &status );

// Koan 06: files
Koan06_files koan06 = Koan06_files( &status );

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

// Welcome message
status.start();

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

// Done.
Expand Down
1 change: 1 addition & 0 deletions headers/all_koans.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
OTHER DEALINGS IN THE SOFTWARE.
*/

#include "koan06_files.hpp"
#include "koan00_get_started.hpp"
#include "koan01_number_types.hpp"
#include "koan02_character_types.hpp"
Expand Down
126 changes: 86 additions & 40 deletions headers/koan05_pointers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,97 @@
#define KOAN05_POINTERS_HPP

// Rename the Episode
class Koan05_pointers : Koan
{
private:
KoanHandler *status; //!
static const int num_tests = 4; //!
class Koan05_pointers : Koan {
private:
KoanHandler *status; //!
static const int num_tests = 13; //!

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

/**
*
*/
void run() {
status->episode_start( "sixth" );

status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_are_just_variables ) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_are_really_just_variables ) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_have_power ) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_are_not_almighty ) );
/**
*
*/
void run() {
status->episode_start("sixth");

status->episode_done( "sixth" );
}
status->eval_koan(*this, static_cast<void (Koan::*)()>(
&Koan05_pointers::they_are_just_variables));
status->eval_koan(*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_are_really_just_variables));
status->eval_koan(*this, static_cast<void (Koan::*)()>(
&Koan05_pointers::they_have_power));
status->eval_koan(*this, static_cast<void (Koan::*)()>(
&Koan05_pointers::they_are_not_almighty));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_can_be_non_const_unlike_array_variables));
status->eval_koan(*this, static_cast<void (Koan::*)()>(
&Koan05_pointers::they_can_manipulate_arrays));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_can_be_assigned_addresses_and_pValues));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_can_do_arithmetic_with_integers_only));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_can_be_initialized_to_dynamic_memory));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_can_be_used_to_access_dynamic_memory));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_are_required_if_you_want_to_write_swap));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_are_used_as_function_arguments_parameters));
status->eval_koan(
*this,
static_cast<void (Koan::*)()>(
&Koan05_pointers::they_are_used_for_writing_indirection_code));

/**
*
*/
static int get_num_tests() {
return num_tests;
}
status->episode_done("sixth");
}

private:
// REMARK: Do not forget to increase this.num_tests when you add another koan
void they_are_just_variables();
void they_are_really_just_variables();
void they_have_power();
void they_are_not_almighty();
/**
*
*/
static int get_num_tests() { return num_tests; }

private:
// REMARK: Do not forget to increase this.num_tests when you add another
// koan
void they_are_just_variables();
void they_are_really_just_variables();
void they_have_power();
void they_are_not_almighty();
void they_can_be_non_const_unlike_array_variables();
void they_can_manipulate_arrays();
void they_can_be_assigned_addresses_and_pValues();
void they_can_do_arithmetic_with_integers_only();
void they_can_be_initialized_to_dynamic_memory();
void they_can_be_used_to_access_dynamic_memory();
void they_are_required_if_you_want_to_write_swap();
void they_are_used_as_function_arguments_parameters();
void they_are_used_for_writing_indirection_code();
};

#endif // KOAN05_POINTERS_HPP
Expand Down
89 changes: 89 additions & 0 deletions headers/koan06_files.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright (c) 2012 Torbjörn Klatt <[email protected]>

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 KOAN06_FILES_HPP
#define KOAN06_FILES_HPP

// Rename the Episode
class Koan06_files : 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 = 3; //!

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

/**
*
*/
void run() {
// For each koan in this episode, one line needs to be written.
// The koans are executed in the order they are called here.
status->episode_start( "seventh" );

status->eval_koan( *this, static_cast<void ( Koan:: * )()>
( &Koan06_files::they_are_accessed_using_file_pointers ) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>
( &Koan06_files::they_can_be_used_to_store_information ) );
status->eval_koan( *this, static_cast<void ( Koan:: * )()>
( &Koan06_files::they_can_be_used_for_retrieval ) );

status->episode_done( "seventh" );
}

/**
*
*/
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/koanXX_sample_koans.cpp
// REMARK: Do not forget to increase this.num_tests when you add another koan
void they_are_accessed_using_file_pointers();
void they_can_be_used_to_store_information();
void they_can_be_used_for_retrieval();
};

#endif // KOANXX_SAMPLE_KOANS_HPP

// EOF
1 change: 1 addition & 0 deletions koans/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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/koan06_files.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
1 change: 1 addition & 0 deletions koans/file_exists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Used by the Koan06::they_are_accessed_using_file_pointers()
Loading