Skip to content

Commit 64354fa

Browse files
committed
added some simple pointer koans
1 parent 8be53c0 commit 64354fa

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

headers/koan05_pointers.hpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Koan05_pointers : Koan
3333
{
3434
private:
3535
KoanHandler *status; //!
36-
static const int num_tests = 1; //!
36+
static const int num_tests = 4; //!
3737

3838
public:
3939
/**
@@ -51,7 +51,10 @@ class Koan05_pointers : Koan
5151
*
5252
*/
5353
void run() {
54-
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::a_sample_koan ) );
54+
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_are_just_variables ) );
55+
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_are_really_just_variables ) );
56+
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_have_power ) );
57+
status->eval_koan( *this, static_cast<void ( Koan:: * )()>( &Koan05_pointers::they_are_not_almighty ) );
5558

5659
status->episode_done( "fifth" );
5760
}
@@ -65,7 +68,10 @@ class Koan05_pointers : Koan
6568

6669
private:
6770
// REMARK: Do not forget to increase this.num_tests when you add another koan
68-
void a_sample_koan();
71+
void they_are_just_variables();
72+
void they_are_really_just_variables();
73+
void they_have_power();
74+
void they_are_not_almighty();
6975
};
7076

7177
#endif // KOAN05_POINTERS_HPP

helper.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333

3434
#define FILL_THE_NUMBER_IN 0
3535
#define FILL_THE_CHAR_IN ' '
36+
#define THIS_IS_NOT_NULL NULL
3637

3738
// EOF

koans/koan05_pointers.cpp

+41-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,48 @@
2525

2626
#include "../headers/koan05_pointers.hpp"
2727

28-
void Koan05_pointers::a_sample_koan()
28+
void Koan05_pointers::they_are_just_variables()
2929
{
30-
bool test = false;
31-
ASSERT_EQUAL( test, true );
30+
int an_int = 42;
31+
int *pointer_to_an_int = &an_int;
32+
ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN );
33+
ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL );
34+
}
35+
36+
void Koan05_pointers::they_are_really_just_variables()
37+
{
38+
int an_int = 42;
39+
int another_int = 21;
40+
int *pointer_to_an_int = &an_int;
41+
ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN );
42+
ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL );
43+
pointer_to_an_int = &another_int;
44+
ASSERT_EQUAL( *pointer_to_an_int, FILL_THE_NUMBER_IN );
45+
ASSERT_EQUAL( pointer_to_an_int, THIS_IS_NOT_NULL );
46+
}
47+
48+
void Koan05_pointers::they_have_power()
49+
{
50+
int an_int = 42;
51+
int *powerful_pointer = &an_int;
52+
ASSERT_EQUAL( *powerful_pointer, FILL_THE_NUMBER_IN );
53+
ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL );
54+
*powerful_pointer = 21;
55+
ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN );
56+
ASSERT_EQUAL( powerful_pointer, THIS_IS_NOT_NULL );
57+
}
58+
59+
void Koan05_pointers::they_are_not_almighty()
60+
{
61+
const int an_int = 42;
62+
const int *wannabe_powerful = &an_int;
63+
ASSERT_EQUAL( *wannabe_powerful, FILL_THE_NUMBER_IN );
64+
ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL );
65+
// Will this work? Think about it!
66+
// What do you need to change to make it work?
67+
// *wannabe_powerful = 21;
68+
ASSERT_EQUAL( an_int, FILL_THE_NUMBER_IN );
69+
ASSERT_EQUAL( wannabe_powerful, THIS_IS_NOT_NULL );
3270
}
3371

3472
// EOF

0 commit comments

Comments
 (0)