From 373db50010641d8d20e8e6edae62cd153bf6faf9 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 18 Mar 2018 17:14:28 +0000 Subject: [PATCH 01/16] added new koan; fixed README.md formatting --- README.markdown => README.md | 8 ++++---- headers/koan05_pointers.hpp | 3 +++ koans/koan05_pointers.cpp | 38 ++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 19 deletions(-) rename README.markdown => README.md (96%) diff --git a/README.markdown b/README.md similarity index 96% rename from README.markdown rename to README.md index 151ede5..64ab48c 100644 --- a/README.markdown +++ b/README.md @@ -50,8 +50,8 @@ 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. @@ -59,7 +59,7 @@ 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: @@ -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 \ No newline at end of file diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index 360ed52..e807f4f 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -57,6 +57,7 @@ class Koan05_pointers : Koan status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_really_just_variables ) ); status->eval_koan( *this, static_cast( &Koan05_pointers::they_have_power ) ); status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_not_almighty ) ); + status->eval_koan( *this, static_cast( &Koan05_pointers::they_can_manipulate_arrays ) ); status->episode_done( "sixth" ); } @@ -74,6 +75,8 @@ class Koan05_pointers : Koan void they_are_really_just_variables(); void they_have_power(); void they_are_not_almighty(); + void they_can_manipulate_arrays(); + }; #endif // KOAN05_POINTERS_HPP diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index 6e17a5d..b9d1e37 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -29,8 +29,8 @@ void Koan05_pointers::they_are_just_variables() { int an_int = 42; int *pointer_to_an_int = &an_int; - ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); + ASSERT_EQUAL( *pointer_to_an_int, 42 ); + ASSERT_EQUAL( pointer_to_an_int, &an_int ); } void Koan05_pointers::they_are_really_just_variables() @@ -38,35 +38,43 @@ void Koan05_pointers::they_are_really_just_variables() int an_int = 42; int another_int = 21; int *pointer_to_an_int = &an_int; - ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); + ASSERT_EQUAL( *pointer_to_an_int, 42 ); + ASSERT_EQUAL( pointer_to_an_int, &an_int ); pointer_to_an_int = &another_int; - ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); + ASSERT_EQUAL( *pointer_to_an_int, 21 ); + ASSERT_EQUAL( pointer_to_an_int, &another_int ); } void Koan05_pointers::they_have_power() { int an_int = 42; int *powerful_pointer = &an_int; - ASSERT_EQUAL( *powerful_pointer, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL ); + ASSERT_EQUAL( *powerful_pointer, 42 ); + ASSERT_EQUAL( powerful_pointer, &an_int ); *powerful_pointer = 21; - ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL ); + ASSERT_EQUAL( an_int, 21 ); + ASSERT_EQUAL( powerful_pointer, &an_int); } void Koan05_pointers::they_are_not_almighty() { const int an_int = 42; const int *wannabe_powerful = &an_int; - ASSERT_EQUAL( *wannabe_powerful, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); + ASSERT_EQUAL( *wannabe_powerful, 42 ); + ASSERT_EQUAL( wannabe_powerful, &an_int ); // Will this work? Think about it! // What do you need to change to make it work? -// *wannabe_powerful = 21; - ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); + //*wannabe_powerful = 21; + ASSERT_EQUAL( an_int, 42 ); + ASSERT_EQUAL( wannabe_powerful, &an_int ); } +void Koan05_pointers::they_can_manipulate_arrays() +{ + int array[4] = {1, 2, 3, 4}; + int *pa = array; + ASSERT_EQUAL( *(pa+2), FILL_THE_NUMBER_IN); +} + + // EOF From 37121b8e49e4ace81f55564c207720705b9daa8b Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 18 Mar 2018 17:20:53 +0000 Subject: [PATCH 02/16] Increased num_tests to 5 --- headers/koan05_pointers.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index e807f4f..e19eca4 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -33,7 +33,7 @@ class Koan05_pointers : Koan { private: KoanHandler *status; //! - static const int num_tests = 4; //! + static const int num_tests = 5; //! public: /** From a49104889615bdf40b30c1b7488b4ab19a09656e Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 18 Mar 2018 17:52:29 +0000 Subject: [PATCH 03/16] added new episode file for files --- cppkoans.cpp | 4 ++ headers/all_koans.hpp | 1 + headers/koan06_files.hpp | 83 ++++++++++++++++++++++++++++++++++++++++ koans/CMakeLists.txt | 1 + koans/koan06_files.cpp | 37 ++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 headers/koan06_files.hpp create mode 100644 koans/koan06_files.cpp diff --git a/cppkoans.cpp b/cppkoans.cpp index a854038..199d66c 100644 --- a/cppkoans.cpp +++ b/cppkoans.cpp @@ -54,6 +54,9 @@ 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 ); @@ -67,6 +70,7 @@ int main() koan03.run(); koan04.run(); koan05.run(); + koan06.run(); // koanXX.run(); // Done. diff --git a/headers/all_koans.hpp b/headers/all_koans.hpp index 8d80d37..35216c6 100644 --- a/headers/all_koans.hpp +++ b/headers/all_koans.hpp @@ -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" diff --git a/headers/koan06_files.hpp b/headers/koan06_files.hpp new file mode 100644 index 0000000..d409aaa --- /dev/null +++ b/headers/koan06_files.hpp @@ -0,0 +1,83 @@ +/* + Copyright (c) 2012 Torbjörn Klatt + + 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 = 1; //! + + 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( "sixth" ); + + status->eval_koan( *this, static_cast( &Koan06_files::a_sample_koan ) ); + //status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_just_variables ) ); + + 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 a_sample_koan(); +}; + +#endif // KOANXX_SAMPLE_KOANS_HPP + +// EOF diff --git a/koans/CMakeLists.txt b/koans/CMakeLists.txt index 3339168..161206c 100644 --- a/koans/CMakeLists.txt +++ b/koans/CMakeLists.txt @@ -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 diff --git a/koans/koan06_files.cpp b/koans/koan06_files.cpp new file mode 100644 index 0000000..fb0b07c --- /dev/null +++ b/koans/koan06_files.cpp @@ -0,0 +1,37 @@ +/* + Copyright (c) 2012 Torbjörn Klatt + + 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 "../headers/koan06_files.hpp" + +// The implementations of the different koans of this episode is done here. +// Don't forget to rename the above include. + +void Koan06_files::a_sample_koan() +{ + bool test = false; + ASSERT_EQUAL( test, true ); +} + +// EOF From d93d8e183c01a2c3d624a3387422ad0fdf5f3783 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 18 Mar 2018 20:04:04 +0000 Subject: [PATCH 04/16] Changed order to run files and pointers; also removed previous compeltions in pointers --- cppkoans.cpp | 4 ++-- koans/koan05_pointers.cpp | 31 ++++++++++++++++--------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cppkoans.cpp b/cppkoans.cpp index 199d66c..0e2f36a 100644 --- a/cppkoans.cpp +++ b/cppkoans.cpp @@ -64,13 +64,13 @@ int main() status.start(); // The Path of Enlightment + koan05.run(); + koan06.run(); koan00.run(); koan01.run(); koan02.run(); koan03.run(); koan04.run(); - koan05.run(); - koan06.run(); // koanXX.run(); // Done. diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index b9d1e37..65f5658 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -29,8 +29,8 @@ void Koan05_pointers::they_are_just_variables() { int an_int = 42; int *pointer_to_an_int = &an_int; - ASSERT_EQUAL( *pointer_to_an_int, 42 ); - ASSERT_EQUAL( pointer_to_an_int, &an_int ); + ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); } void Koan05_pointers::they_are_really_just_variables() @@ -38,37 +38,38 @@ void Koan05_pointers::they_are_really_just_variables() int an_int = 42; int another_int = 21; int *pointer_to_an_int = &an_int; - ASSERT_EQUAL( *pointer_to_an_int, 42 ); - ASSERT_EQUAL( pointer_to_an_int, &an_int ); + ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); pointer_to_an_int = &another_int; - ASSERT_EQUAL( *pointer_to_an_int, 21 ); - ASSERT_EQUAL( pointer_to_an_int, &another_int ); + ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); } void Koan05_pointers::they_have_power() { int an_int = 42; int *powerful_pointer = &an_int; - ASSERT_EQUAL( *powerful_pointer, 42 ); - ASSERT_EQUAL( powerful_pointer, &an_int ); + ASSERT_EQUAL( *powerful_pointer, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL ); *powerful_pointer = 21; - ASSERT_EQUAL( an_int, 21 ); - ASSERT_EQUAL( powerful_pointer, &an_int); + ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL ); } void Koan05_pointers::they_are_not_almighty() { const int an_int = 42; const int *wannabe_powerful = &an_int; - ASSERT_EQUAL( *wannabe_powerful, 42 ); - ASSERT_EQUAL( wannabe_powerful, &an_int ); + ASSERT_EQUAL( *wannabe_powerful, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); // Will this work? Think about it! // What do you need to change to make it work? - //*wannabe_powerful = 21; - ASSERT_EQUAL( an_int, 42 ); - ASSERT_EQUAL( wannabe_powerful, &an_int ); +// *wannabe_powerful = 21; + ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); } + void Koan05_pointers::they_can_manipulate_arrays() { int array[4] = {1, 2, 3, 4}; From 8ce0d7b353609b4f338e9d6ca62c1b071f08a7ea Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 18 Mar 2018 20:51:00 +0000 Subject: [PATCH 05/16] Updated README.md and enabled koan createion and edition --- README.md | 8 ++++---- headers/koan06_files.hpp | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 64ab48c..c1d7d92 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#CppKoans +# CppKoans Inspired by [RubyKoans](https://github.com/edgecase/ruby_koans) and [JavaScript-Koans](https://github.com/liammclennan/JavaScript-Koans), this is @@ -6,13 +6,13 @@ 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 @@ -25,7 +25,7 @@ I just tested it with a recent GCC. 3. Configure the build - cmake .. + cmake ./.. 4. Compile diff --git a/headers/koan06_files.hpp b/headers/koan06_files.hpp index d409aaa..163712a 100644 --- a/headers/koan06_files.hpp +++ b/headers/koan06_files.hpp @@ -56,10 +56,9 @@ class Koan06_files : Koan 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( "sixth" ); + status->episode_start( "seventh" ); status->eval_koan( *this, static_cast( &Koan06_files::a_sample_koan ) ); - //status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_just_variables ) ); status->episode_done( "seventh" ); } From 79fb7fab9ba0047dd52d9fe9a908a588419334d5 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 18 Mar 2018 20:51:00 +0000 Subject: [PATCH 06/16] Updated README.md and enabled koan episode creation and edition (koan06_files.cpp) --- README.md | 8 ++++---- headers/koan06_files.hpp | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 64ab48c..c1d7d92 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#CppKoans +# CppKoans Inspired by [RubyKoans](https://github.com/edgecase/ruby_koans) and [JavaScript-Koans](https://github.com/liammclennan/JavaScript-Koans), this is @@ -6,13 +6,13 @@ 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 @@ -25,7 +25,7 @@ I just tested it with a recent GCC. 3. Configure the build - cmake .. + cmake ./.. 4. Compile diff --git a/headers/koan06_files.hpp b/headers/koan06_files.hpp index d409aaa..163712a 100644 --- a/headers/koan06_files.hpp +++ b/headers/koan06_files.hpp @@ -56,10 +56,9 @@ class Koan06_files : Koan 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( "sixth" ); + status->episode_start( "seventh" ); status->eval_koan( *this, static_cast( &Koan06_files::a_sample_koan ) ); - //status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_just_variables ) ); status->episode_done( "seventh" ); } From 6229da1ecf6fb36bb675b973ba95d83bcd580034 Mon Sep 17 00:00:00 2001 From: kgashok Date: Mon, 19 Mar 2018 11:46:58 +0000 Subject: [PATCH 07/16] one more pointer koan --- headers/koan05_pointers.hpp | 20 ++++++++++++++------ koans/koan05_pointers.cpp | 10 ++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index e19eca4..d53a3ba 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -33,7 +33,7 @@ class Koan05_pointers : Koan { private: KoanHandler *status; //! - static const int num_tests = 5; //! + static const int num_tests = 6; //! public: /** @@ -53,11 +53,18 @@ class Koan05_pointers : Koan void run() { status->episode_start( "sixth" ); - status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_just_variables ) ); - status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_really_just_variables ) ); - status->eval_koan( *this, static_cast( &Koan05_pointers::they_have_power ) ); - status->eval_koan( *this, static_cast( &Koan05_pointers::they_are_not_almighty ) ); - status->eval_koan( *this, static_cast( &Koan05_pointers::they_can_manipulate_arrays ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_are_just_variables ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_are_really_just_variables ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_have_power ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_are_not_almighty ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_can_be_non_const_unlike_array_variables ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_can_manipulate_arrays ) ); status->episode_done( "sixth" ); } @@ -75,6 +82,7 @@ class Koan05_pointers : Koan 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(); }; diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index 65f5658..da4a914 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -69,6 +69,16 @@ void Koan05_pointers::they_are_not_almighty() ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); } +void Koan05_pointers::they_can_be_non_const_unlike_array_variables() +{ + int array[4]; + int *pa = array; + ASSERT_EQUAL( pa, THIS_IS_NOT_NULL); + + pa = pa + 3; + ASSERT_EQUAL( pa, THIS_IS_NOT_NULL); +} + void Koan05_pointers::they_can_manipulate_arrays() { From 37ceb77bb9962343c827ff1f1b4cf3694dbf781d Mon Sep 17 00:00:00 2001 From: kgashok Date: Sun, 25 Mar 2018 02:44:11 +0000 Subject: [PATCH 08/16] added pointer arithmetic basics; explicit init of sample array --- headers/koan05_pointers.hpp | 11 ++++++++--- koans/koan05_pointers.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index d53a3ba..a1da4c3 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -33,7 +33,7 @@ class Koan05_pointers : Koan { private: KoanHandler *status; //! - static const int num_tests = 6; //! + static const int num_tests = 8; //! public: /** @@ -65,7 +65,11 @@ class Koan05_pointers : Koan ( &Koan05_pointers::they_can_be_non_const_unlike_array_variables ) ); status->eval_koan( *this, static_cast ( &Koan05_pointers::they_can_manipulate_arrays ) ); - + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_can_be_assigned_addresses_and_pValues ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_can_do_arithmetic_with_integers_only ) ); + status->episode_done( "sixth" ); } @@ -84,7 +88,8 @@ class Koan05_pointers : Koan 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(); }; #endif // KOAN05_POINTERS_HPP diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index da4a914..720617d 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -87,5 +87,35 @@ void Koan05_pointers::they_can_manipulate_arrays() ASSERT_EQUAL( *(pa+2), FILL_THE_NUMBER_IN); } +void Koan05_pointers::they_can_be_assigned_addresses_and_pValues() +{ + int b; + int* pA; + int* pB = &b; + pA = pB; + ASSERT_EQUAL(*pA, FILL_THE_NUMBER_IN); +} + +void Koan05_pointers::they_can_do_arithmetic_with_integers_only() +{ + int a[10] = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; + a[5] = {8901}; + int *ap = a; + + // Do the necessary pointer arithmetic with ap + ASSERT_EQUAL(&a[5], THIS_IS_NOT_NULL); + + // What is the value pointed to by ap before + // and after the arithmetic? + ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); + + ap = ap + 5; + ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); + + ap = ap + 1; + ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); + +} + // EOF From 7feddb43738654de285d43c038ac332d05ead977 Mon Sep 17 00:00:00 2001 From: kgashok Date: Fri, 30 Mar 2018 17:21:00 +0000 Subject: [PATCH 09/16] 10 koans now for pointers --- headers/koan05_pointers.hpp | 10 ++++++++-- koans/koan05_pointers.cpp | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index a1da4c3..0c8a53e 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -32,8 +32,8 @@ class Koan05_pointers : Koan { private: - KoanHandler *status; //! - static const int num_tests = 8; //! + KoanHandler *status; //! + static const int num_tests = 10; //! public: /** @@ -69,6 +69,10 @@ class Koan05_pointers : Koan ( &Koan05_pointers::they_can_be_assigned_addresses_and_pValues ) ); status->eval_koan( *this, static_cast ( &Koan05_pointers::they_can_do_arithmetic_with_integers_only ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_can_be_initialized_to_dynamic_memory ) ); + status->eval_koan( *this, static_cast + ( &Koan05_pointers::they_can_be_used_to_access_dynamic_memory ) ); status->episode_done( "sixth" ); } @@ -90,6 +94,8 @@ class Koan05_pointers : Koan 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(); }; #endif // KOAN05_POINTERS_HPP diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index 720617d..91cf192 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -104,18 +104,30 @@ void Koan05_pointers::they_can_do_arithmetic_with_integers_only() // Do the necessary pointer arithmetic with ap ASSERT_EQUAL(&a[5], THIS_IS_NOT_NULL); - // What is the value pointed to by ap before // and after the arithmetic? ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); - ap = ap + 5; ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); - ap = ap + 1; ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); } +void Koan05_pointers::they_can_be_initialized_to_dynamic_memory(){ + char *p = 0; + p = (char *)malloc(5 * sizeof(char)); + ASSERT_EQUAL(p, THIS_IS_NOT_NULL); + +} + +void Koan05_pointers::they_can_be_used_to_access_dynamic_memory(){ + char *p = 0; + p = (char *)malloc(5 * sizeof(char)); + *p = 'a'; + ASSERT_EQUAL(*p, FILL_THE_CHAR_IN); + +} + // EOF From 5a66aaf9cd9bd3764513b020b261d0377f407e8f Mon Sep 17 00:00:00 2001 From: kgashok Date: Sat, 31 Mar 2018 07:34:52 +0000 Subject: [PATCH 10/16] Added first koan for new file "koan06"; almost got killed! --- headers/koan06_files.hpp | 5 +++-- koans/koan06_files.cpp | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/headers/koan06_files.hpp b/headers/koan06_files.hpp index 163712a..c77a0cd 100644 --- a/headers/koan06_files.hpp +++ b/headers/koan06_files.hpp @@ -58,7 +58,8 @@ class Koan06_files : Koan // The koans are executed in the order they are called here. status->episode_start( "seventh" ); - status->eval_koan( *this, static_cast( &Koan06_files::a_sample_koan ) ); + status->eval_koan( *this, static_cast + ( &Koan06_files::they_can_be_used_to_store_information ) ); status->episode_done( "seventh" ); } @@ -74,7 +75,7 @@ class Koan06_files : Koan // 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 a_sample_koan(); + void they_can_be_used_to_store_information(); }; #endif // KOANXX_SAMPLE_KOANS_HPP diff --git a/koans/koan06_files.cpp b/koans/koan06_files.cpp index fb0b07c..230305d 100644 --- a/koans/koan06_files.cpp +++ b/koans/koan06_files.cpp @@ -28,10 +28,24 @@ // The implementations of the different koans of this episode is done here. // Don't forget to rename the above include. -void Koan06_files::a_sample_koan() +void Koan06_files::they_can_be_used_to_store_information() { - bool test = false; - ASSERT_EQUAL( test, true ); + int marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; + + //------------------ + // typical file operations to store 'database' marks in file + FILE* fp = fopen("data.bin", "w+b"); + fwrite(marks, 10, sizeof(int), fp); + //fseek(fp, 0L, SEEK_SET); + int filesize = ftell(fp); + fclose(fp); + //------------------ + + // Is 'filesize' the same as size of marks database? + ASSERT_EQUAL(filesize, FILL_THE_NUMBER_IN ); + // And what is the actual size of the file? + ASSERT_EQUAL(filesize, FILL_THE_NUMBER_IN ); + } // EOF From d3821bd29542ec08b119b259fcacc75475d4c891 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sat, 31 Mar 2018 09:03:24 +0000 Subject: [PATCH 11/16] Added two koans to files --- headers/koan06_files.hpp | 5 ++++- koans/file_exists.txt | 1 + koans/koan06_files.cpp | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 koans/file_exists.txt diff --git a/headers/koan06_files.hpp b/headers/koan06_files.hpp index c77a0cd..d29c2c7 100644 --- a/headers/koan06_files.hpp +++ b/headers/koan06_files.hpp @@ -36,7 +36,7 @@ class Koan06_files : Koan KoanHandler *status; //! // When ever a koan is added at the very bottom, this counter needs to be // increased. - static const int num_tests = 1; //! + static const int num_tests = 2; //! public: /** @@ -58,6 +58,8 @@ class Koan06_files : Koan // The koans are executed in the order they are called here. status->episode_start( "seventh" ); + status->eval_koan( *this, static_cast + ( &Koan06_files::they_are_accessed_using_file_pointers ) ); status->eval_koan( *this, static_cast ( &Koan06_files::they_can_be_used_to_store_information ) ); @@ -75,6 +77,7 @@ class Koan06_files : Koan // 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(); }; diff --git a/koans/file_exists.txt b/koans/file_exists.txt new file mode 100644 index 0000000..fef1c83 --- /dev/null +++ b/koans/file_exists.txt @@ -0,0 +1 @@ +Used by the Koan06::they_are_accessed_using_file_pointers() \ No newline at end of file diff --git a/koans/koan06_files.cpp b/koans/koan06_files.cpp index 230305d..f47ccb0 100644 --- a/koans/koan06_files.cpp +++ b/koans/koan06_files.cpp @@ -28,6 +28,20 @@ // The implementations of the different koans of this episode is done here. // Don't forget to rename the above include. +void Koan06_files::they_are_accessed_using_file_pointers() +{ + FILE* fp = 0; + + fp = fopen("../koans/file_exists.txt", "r"); + ASSERT (fp != 0); + + fp = fopen("../koans/nonexistentfile", "r"); + ASSERT_EQUAL(fp, FILL_THE_NUMBER_IN); + + if(fp) + fclose(fp); + +} void Koan06_files::they_can_be_used_to_store_information() { int marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; From 95e4388aff9ed0d637184eb9e081414e093145b2 Mon Sep 17 00:00:00 2001 From: kgashok Date: Mon, 2 Apr 2018 15:41:54 +0000 Subject: [PATCH 12/16] added file retrieval koan --- headers/koan06_files.hpp | 5 ++++- koans/koan06_files.cpp | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/headers/koan06_files.hpp b/headers/koan06_files.hpp index d29c2c7..451bc52 100644 --- a/headers/koan06_files.hpp +++ b/headers/koan06_files.hpp @@ -36,7 +36,7 @@ class Koan06_files : Koan KoanHandler *status; //! // When ever a koan is added at the very bottom, this counter needs to be // increased. - static const int num_tests = 2; //! + static const int num_tests = 3; //! public: /** @@ -62,6 +62,8 @@ class Koan06_files : Koan ( &Koan06_files::they_are_accessed_using_file_pointers ) ); status->eval_koan( *this, static_cast ( &Koan06_files::they_can_be_used_to_store_information ) ); + status->eval_koan( *this, static_cast + ( &Koan06_files::they_can_be_used_for_retrieval ) ); status->episode_done( "seventh" ); } @@ -79,6 +81,7 @@ class Koan06_files : Koan // 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 diff --git a/koans/koan06_files.cpp b/koans/koan06_files.cpp index f47ccb0..f853a96 100644 --- a/koans/koan06_files.cpp +++ b/koans/koan06_files.cpp @@ -62,4 +62,25 @@ void Koan06_files::they_can_be_used_to_store_information() } +#include + +void Koan06_files::they_can_be_used_for_retrieval() { + int marks[10]; + memset(marks, 0, 10*sizeof(int)); + int sum = 0; + for (int i = 0; i < 10; i++) + sum += marks[i]; + ASSERT_EQUAL(sum, 0); + + FILE *fp = fopen("data.bin", "r+b"); + fread(marks, 10, sizeof(int), fp); + //marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; + for (int i = 0; i < 10; i++) + sum += marks[i]; + + ASSERT_EQUAL(sum, FILL_THE_NUMBER_IN); + +} + + // EOF From 36406998d83212868b104749fcc7c27c306db993 Mon Sep 17 00:00:00 2001 From: kgashok Date: Thu, 12 Apr 2018 17:03:03 +0000 Subject: [PATCH 13/16] formatted using clang-format --- koans/koan05_pointers.cpp | 107 +++++++++++++++++--------------------- koans/koan06_files.cpp | 44 +++++++--------- 2 files changed, 66 insertions(+), 85 deletions(-) diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index 91cf192..ad7b913 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -25,109 +25,96 @@ #include "../headers/koan05_pointers.hpp" -void Koan05_pointers::they_are_just_variables() -{ +void Koan05_pointers::they_are_just_variables() { int an_int = 42; int *pointer_to_an_int = &an_int; - ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); + ASSERT_EQUAL(*pointer_to_an_int, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(pointer_to_an_int, THIS_IS_NOT_NULL); } -void Koan05_pointers::they_are_really_just_variables() -{ +void Koan05_pointers::they_are_really_just_variables() { int an_int = 42; int another_int = 21; int *pointer_to_an_int = &an_int; - ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); + ASSERT_EQUAL(*pointer_to_an_int, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(pointer_to_an_int, THIS_IS_NOT_NULL); pointer_to_an_int = &another_int; - ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL ); + ASSERT_EQUAL(*pointer_to_an_int, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(pointer_to_an_int, THIS_IS_NOT_NULL); } -void Koan05_pointers::they_have_power() -{ +void Koan05_pointers::they_have_power() { int an_int = 42; int *powerful_pointer = &an_int; - ASSERT_EQUAL( *powerful_pointer, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL ); + ASSERT_EQUAL(*powerful_pointer, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(powerful_pointer, THIS_IS_NOT_NULL); *powerful_pointer = 21; - ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL ); + ASSERT_EQUAL(an_int, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(powerful_pointer, THIS_IS_NOT_NULL); } -void Koan05_pointers::they_are_not_almighty() -{ +void Koan05_pointers::they_are_not_almighty() { const int an_int = 42; const int *wannabe_powerful = &an_int; - ASSERT_EQUAL( *wannabe_powerful, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); + ASSERT_EQUAL(*wannabe_powerful, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(wannabe_powerful, THIS_IS_NOT_NULL); // Will this work? Think about it! // What do you need to change to make it work? -// *wannabe_powerful = 21; - ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN ); - ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL ); + // *wannabe_powerful = 21; + ASSERT_EQUAL(an_int, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(wannabe_powerful, THIS_IS_NOT_NULL); } -void Koan05_pointers::they_can_be_non_const_unlike_array_variables() -{ +void Koan05_pointers::they_can_be_non_const_unlike_array_variables() { int array[4]; int *pa = array; - ASSERT_EQUAL( pa, THIS_IS_NOT_NULL); - + ASSERT_EQUAL(pa, THIS_IS_NOT_NULL); + pa = pa + 3; - ASSERT_EQUAL( pa, THIS_IS_NOT_NULL); + ASSERT_EQUAL(pa, THIS_IS_NOT_NULL); } - -void Koan05_pointers::they_can_manipulate_arrays() -{ +void Koan05_pointers::they_can_manipulate_arrays() { int array[4] = {1, 2, 3, 4}; - int *pa = array; - ASSERT_EQUAL( *(pa+2), FILL_THE_NUMBER_IN); + int *pa = array; + ASSERT_EQUAL(*(pa + 2), FILL_THE_NUMBER_IN); } -void Koan05_pointers::they_can_be_assigned_addresses_and_pValues() -{ - int b; - int* pA; - int* pB = &b; +void Koan05_pointers::they_can_be_assigned_addresses_and_pValues() { + int b; + int *pA; + int *pB = &b; pA = pB; - ASSERT_EQUAL(*pA, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(*pA, FILL_THE_NUMBER_IN); } -void Koan05_pointers::they_can_do_arithmetic_with_integers_only() -{ - int a[10] = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; +void Koan05_pointers::they_can_do_arithmetic_with_integers_only() { + int a[10] = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; a[5] = {8901}; - int *ap = a; + int *ap = a; // Do the necessary pointer arithmetic with ap - ASSERT_EQUAL(&a[5], THIS_IS_NOT_NULL); - // What is the value pointed to by ap before - // and after the arithmetic? - ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(&a[5], THIS_IS_NOT_NULL); + // What is the value pointed to by ap before + // and after the arithmetic? + ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); ap = ap + 5; - ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); ap = ap + 1; - ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); - + ASSERT_EQUAL(*ap, FILL_THE_NUMBER_IN); } -void Koan05_pointers::they_can_be_initialized_to_dynamic_memory(){ - char *p = 0; - p = (char *)malloc(5 * sizeof(char)); +void Koan05_pointers::they_can_be_initialized_to_dynamic_memory() { + char *p = 0; + p = (char *)malloc(5 * sizeof(char)); ASSERT_EQUAL(p, THIS_IS_NOT_NULL); - } -void Koan05_pointers::they_can_be_used_to_access_dynamic_memory(){ - char *p = 0; - p = (char *)malloc(5 * sizeof(char)); - *p = 'a'; +void Koan05_pointers::they_can_be_used_to_access_dynamic_memory() { + char *p = 0; + p = (char *)malloc(5 * sizeof(char)); + *p = 'a'; ASSERT_EQUAL(*p, FILL_THE_CHAR_IN); - } - // EOF diff --git a/koans/koan06_files.cpp b/koans/koan06_files.cpp index f853a96..39c0ae6 100644 --- a/koans/koan06_files.cpp +++ b/koans/koan06_files.cpp @@ -28,59 +28,53 @@ // The implementations of the different koans of this episode is done here. // Don't forget to rename the above include. -void Koan06_files::they_are_accessed_using_file_pointers() -{ - FILE* fp = 0; - +void Koan06_files::they_are_accessed_using_file_pointers() { + FILE *fp = 0; + fp = fopen("../koans/file_exists.txt", "r"); - ASSERT (fp != 0); + ASSERT(fp != 0); fp = fopen("../koans/nonexistentfile", "r"); ASSERT_EQUAL(fp, FILL_THE_NUMBER_IN); - - if(fp) + + if (fp) fclose(fp); - } -void Koan06_files::they_can_be_used_to_store_information() -{ - int marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; +void Koan06_files::they_can_be_used_to_store_information() { + int marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; //------------------ // typical file operations to store 'database' marks in file - FILE* fp = fopen("data.bin", "w+b"); - fwrite(marks, 10, sizeof(int), fp); - //fseek(fp, 0L, SEEK_SET); - int filesize = ftell(fp); + FILE *fp = fopen("data.bin", "w+b"); + fwrite(marks, 10, sizeof(int), fp); + // fseek(fp, 0L, SEEK_SET); + int filesize = ftell(fp); fclose(fp); //------------------ // Is 'filesize' the same as size of marks database? - ASSERT_EQUAL(filesize, FILL_THE_NUMBER_IN ); + ASSERT_EQUAL(filesize, FILL_THE_NUMBER_IN); // And what is the actual size of the file? - ASSERT_EQUAL(filesize, FILL_THE_NUMBER_IN ); - + ASSERT_EQUAL(filesize, FILL_THE_NUMBER_IN); } #include void Koan06_files::they_can_be_used_for_retrieval() { int marks[10]; - memset(marks, 0, 10*sizeof(int)); + memset(marks, 0, 10 * sizeof(int)); int sum = 0; for (int i = 0; i < 10; i++) - sum += marks[i]; + sum += marks[i]; ASSERT_EQUAL(sum, 0); - + FILE *fp = fopen("data.bin", "r+b"); fread(marks, 10, sizeof(int), fp); - //marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; - for (int i = 0; i < 10; i++) + // marks[10] = {38, 54, 93, 41, 55, 86, 59, 100, 40, 92}; + for (int i = 0; i < 10; i++) sum += marks[i]; ASSERT_EQUAL(sum, FILL_THE_NUMBER_IN); - } - // EOF From 807b75e735bf43efcddcad1db8c1999d970ee147 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sat, 19 May 2018 04:17:50 +0000 Subject: [PATCH 14/16] Added two more koans based on Duke University course on Coursera --- headers/koan05_pointers.hpp | 140 ++++++++++++++++++++---------------- koans/koan05_pointers.cpp | 42 +++++++++++ 2 files changed, 119 insertions(+), 63 deletions(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index 0c8a53e..2c17386 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -29,73 +29,87 @@ #define KOAN05_POINTERS_HPP // Rename the Episode -class Koan05_pointers : Koan -{ - private: - KoanHandler *status; //! - static const int num_tests = 10; //! +class Koan05_pointers : Koan { +private: + KoanHandler *status; //! + static const int num_tests = 12; //! - 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 - ( &Koan05_pointers::they_are_just_variables ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_are_really_just_variables ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_have_power ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_are_not_almighty ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_can_be_non_const_unlike_array_variables ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_can_manipulate_arrays ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_can_be_assigned_addresses_and_pValues ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_can_do_arithmetic_with_integers_only ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_can_be_initialized_to_dynamic_memory ) ); - status->eval_koan( *this, static_cast - ( &Koan05_pointers::they_can_be_used_to_access_dynamic_memory ) ); - - status->episode_done( "sixth" ); - } + /** + * + */ + void run() { + status->episode_start("sixth"); - /** - * - */ - static int get_num_tests() { - return num_tests; - } + status->eval_koan(*this, static_cast( + &Koan05_pointers::they_are_just_variables)); + status->eval_koan(*this, + static_cast( + &Koan05_pointers::they_are_really_just_variables)); + status->eval_koan(*this, static_cast( + &Koan05_pointers::they_have_power)); + status->eval_koan(*this, static_cast( + &Koan05_pointers::they_are_not_almighty)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_can_be_non_const_unlike_array_variables)); + status->eval_koan(*this, static_cast( + &Koan05_pointers::they_can_manipulate_arrays)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_can_be_assigned_addresses_and_pValues)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_can_do_arithmetic_with_integers_only)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_can_be_initialized_to_dynamic_memory)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_can_be_used_to_access_dynamic_memory)); + status->eval_koan(*this, static_cast( + &Koan05_pointers::they_are_required_if_you_want_to_write_swap)); + status->eval_koan(*this, static_cast( + &Koan05_pointers::they_are_used_for_writing_indirection_code)); + + 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(); - 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(); + /** + * + */ + 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_for_writing_indirection_code(); }; #endif // KOAN05_POINTERS_HPP diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index ad7b913..22c59e9 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -117,4 +117,46 @@ void Koan05_pointers::they_can_be_used_to_access_dynamic_memory() { ASSERT_EQUAL(*p, FILL_THE_CHAR_IN); } +void swap(int *a, int *b) { + int temp = *a; + *a = *b; + *b = temp; +} + +void Koan05_pointers::they_are_required_if_you_want_to_write_swap() { + int a = 5; + int b = 3; + swap (&a, &b); + + ASSERT_EQUAL(a, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(b, FILL_THE_NUMBER_IN); +} + +int f(int ** r, int ** s) { + int temp = ** r; + int temp2 = **s; + int * z = *r; + *r = *s; + *s = z; + // printf("**r = %d\n",**r); + // printf("**s = %d\n",**s); + *z += 3; + **s -= 8; + **r -= 19; + return temp + temp2; +} + +void Koan05_pointers::they_are_used_for_writing_indirection_code() { + int a = 80; + int b = 12; + int *p = &a; + int *q = &b; + int x = f(&p, &q); + ASSERT_EQUAL(a, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(b, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(x, FILL_THE_NUMBER_IN); + +} + + // EOF From 9cef8e83398fada65436ca206c020224f9593176 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sat, 19 May 2018 04:29:17 +0000 Subject: [PATCH 15/16] formatted to clang-format --- headers/koan05_pointers.hpp | 14 +++++++++----- koans/koan05_pointers.cpp | 34 ++++++++++++++++------------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index 2c17386..7296a0a 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -83,11 +83,15 @@ class Koan05_pointers : Koan { *this, static_cast( &Koan05_pointers::they_can_be_used_to_access_dynamic_memory)); - status->eval_koan(*this, static_cast( - &Koan05_pointers::they_are_required_if_you_want_to_write_swap)); - status->eval_koan(*this, static_cast( - &Koan05_pointers::they_are_used_for_writing_indirection_code)); - + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_are_required_if_you_want_to_write_swap)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_are_used_for_writing_indirection_code)); + status->episode_done("sixth"); } diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index 22c59e9..dac860d 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -117,25 +117,25 @@ void Koan05_pointers::they_can_be_used_to_access_dynamic_memory() { ASSERT_EQUAL(*p, FILL_THE_CHAR_IN); } -void swap(int *a, int *b) { - int temp = *a; - *a = *b; +void swap(int *a, int *b) { + int temp = *a; + *a = *b; *b = temp; } -void Koan05_pointers::they_are_required_if_you_want_to_write_swap() { - int a = 5; - int b = 3; - swap (&a, &b); - - ASSERT_EQUAL(a, FILL_THE_NUMBER_IN); +void Koan05_pointers::they_are_required_if_you_want_to_write_swap() { + int a = 5; + int b = 3; + swap(&a, &b); + + ASSERT_EQUAL(a, FILL_THE_NUMBER_IN); ASSERT_EQUAL(b, FILL_THE_NUMBER_IN); } -int f(int ** r, int ** s) { - int temp = ** r; +int f(int **r, int **s) { + int temp = **r; int temp2 = **s; - int * z = *r; + int *z = *r; *r = *s; *s = z; // printf("**r = %d\n",**r); @@ -146,17 +146,15 @@ int f(int ** r, int ** s) { return temp + temp2; } -void Koan05_pointers::they_are_used_for_writing_indirection_code() { +void Koan05_pointers::they_are_used_for_writing_indirection_code() { int a = 80; int b = 12; int *p = &a; int *q = &b; int x = f(&p, &q); - ASSERT_EQUAL(a, FILL_THE_NUMBER_IN); - ASSERT_EQUAL(b, FILL_THE_NUMBER_IN); - ASSERT_EQUAL(x, FILL_THE_NUMBER_IN); - + ASSERT_EQUAL(a, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(b, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(x, FILL_THE_NUMBER_IN); } - // EOF From 064c8331ce2e6bdeb5b621b62e50ab3b051763a5 Mon Sep 17 00:00:00 2001 From: kgashok Date: Sat, 19 May 2018 20:27:15 +0000 Subject: [PATCH 16/16] added another koans based on Duke's learn2prog/11_read_ptr1 --- headers/koan05_pointers.hpp | 10 ++++++++-- koans/koan05_pointers.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/headers/koan05_pointers.hpp b/headers/koan05_pointers.hpp index 7296a0a..3fd0f18 100644 --- a/headers/koan05_pointers.hpp +++ b/headers/koan05_pointers.hpp @@ -32,7 +32,7 @@ class Koan05_pointers : Koan { private: KoanHandler *status; //! - static const int num_tests = 12; //! + static const int num_tests = 13; //! public: /** @@ -87,6 +87,10 @@ class Koan05_pointers : Koan { *this, static_cast( &Koan05_pointers::they_are_required_if_you_want_to_write_swap)); + status->eval_koan( + *this, + static_cast( + &Koan05_pointers::they_are_used_as_function_arguments_parameters)); status->eval_koan( *this, static_cast( @@ -101,7 +105,8 @@ class Koan05_pointers : Koan { static int get_num_tests() { return num_tests; } private: - // REMARK: Do not forget to increase this.num_tests when you add another koan + // 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(); @@ -113,6 +118,7 @@ class Koan05_pointers : Koan { 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(); }; diff --git a/koans/koan05_pointers.cpp b/koans/koan05_pointers.cpp index dac860d..04bf15b 100644 --- a/koans/koan05_pointers.cpp +++ b/koans/koan05_pointers.cpp @@ -132,6 +132,31 @@ void Koan05_pointers::they_are_required_if_you_want_to_write_swap() { ASSERT_EQUAL(b, FILL_THE_NUMBER_IN); } +void g(int x, int *y) { + printf("In g, x = %d, *y = %d\n", x, *y); + x++; + *y = *y - x; + y = &x; +} + +void fg(int *a, int b) { + printf("In fg, *a = %d, b = %d\n", *a, b); + *a += b; + b *= 2; + g(*a, &b); + printf("Back in fg, *a = %d, b = %d\n", *a, b); +} + +void Koan05_pointers::they_are_used_as_function_arguments_parameters() { + int x = 3; + int y = 4; + fg(&x, y); + printf("In main: x = %d, y = %d\n", x, y); + + ASSERT_EQUAL(x, FILL_THE_NUMBER_IN); + ASSERT_EQUAL(y, FILL_THE_NUMBER_IN); +} + int f(int **r, int **s) { int temp = **r; int temp2 = **s;