koans ready
This commit is contained in:
BIN
CPPKoans.sdf
BIN
CPPKoans.sdf
Binary file not shown.
@@ -4,16 +4,16 @@
|
||||
|
||||
|
||||
void aboutAssertParameterTypes(){
|
||||
expectThat("actual and expected need to be of the same type",3,3);
|
||||
expectThat("however, if they are not the same, expected is tried to be cast to the actual type.",3.0,3);
|
||||
expectThat("truncating information when casting to narrower type",3.1,3);
|
||||
expectThat("actual and expected need to be of the same type",_____,3);
|
||||
expectThat("however, if they are not the same, expected is tried to be cast to the actual type.",______,3);
|
||||
expectThat("truncating information when casting to narrower type",______,3);
|
||||
expectThatNot("and not if casted to a wider type",3,3.1);
|
||||
expectThatNot("you won't get an Error for bad types","expected",42);
|
||||
//this causes a crash
|
||||
//expectThat("this is the error message you get when the cast failed",42,"expected");
|
||||
expectThat("thus, you can also compare c-strings with cpp-strings","cstring",string("cstring"));
|
||||
expectThat("thus, you can also compare c-strings with cpp-strings",_______,string("cstring"));
|
||||
}
|
||||
|
||||
void AboutAssert::meditate(){
|
||||
aboutAssertParameterTypes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,29 +8,29 @@ void aboutShiftOperators(){
|
||||
// 1 looks like this: 0000000000000001
|
||||
// now we can shift the set bit around
|
||||
unsigned short value = unsigned_one << 5;
|
||||
expectThat("shifting to the left is like multiplying with 2^n",32,value);
|
||||
expectThat("shifting to the left is like multiplying with 2^n",_____,value);
|
||||
value = value >> 1;
|
||||
expectThat("shifting to the right divides by two",16,value);
|
||||
expectThat("shifting to the right divides by two",_____,value);
|
||||
value = value << 12;
|
||||
expectThat("careful though if out of bounds",0,value);
|
||||
expectThat("careful though if out of bounds",_____,value);
|
||||
short signed_value = one << 15;
|
||||
expectThat("and with the sign",-32768,signed_value);
|
||||
expectThat("and with the sign",_____,signed_value);
|
||||
|
||||
unsigned unsigned_integer = 1 << 15;
|
||||
expectThat("1 shifted 15 to the left is...",32768,unsigned_integer);
|
||||
expectThat("1 shifted 15 to the left is...",_____,unsigned_integer);
|
||||
short signed_short = short(unsigned_integer);
|
||||
expectThatRaw("and keep in mind things can happen when numbers are casted",short(-32768),signed_short);
|
||||
}
|
||||
void aboutOperators(){
|
||||
expectThat("| bitwise or",16,0|16);
|
||||
expectThat("& bitwise and",1,255&1);
|
||||
expectThat("| bitwise or",_____,0|16);
|
||||
expectThat("& bitwise and",_____,255&1);
|
||||
|
||||
unsigned fifthbitset = 1<<5;
|
||||
expectThat("| can be used to set the nth bit",32,0|fifthbitset);
|
||||
expectThat("& can be used to determine if the 5th bit is set like so",true,((255>>5)&1 == 1));
|
||||
expectThat("| can be used to set the nth bit",_____,0|fifthbitset);
|
||||
expectThat("& can be used to determine if the 5th bit is set like so",________,((255>>5)&1 == 1));
|
||||
|
||||
expectThat("^ bitwise XOR",6,3^5);
|
||||
expectThat("~ bitwise negation",-4,~3);
|
||||
expectThat("^ bitwise XOR",_____,3^5);
|
||||
expectThat("~ bitwise negation",_____,~3);
|
||||
}
|
||||
void AboutBitwiseOperators::meditate()
|
||||
{
|
||||
|
||||
@@ -44,14 +44,14 @@ void aboutMemberPolymorphism() {
|
||||
B b;
|
||||
A c = b;
|
||||
|
||||
expectThat("direct method call to a", 'a', a.method0());
|
||||
expectThat("b inherits method0 from a", 'a', b.method0());
|
||||
expectThat("direct method call to a", 'a', a.method1());
|
||||
expectThat("b overwrites method1", 'b', b.method1());
|
||||
expectThat("direct method call to a", ____, a.method0());
|
||||
expectThat("b inherits method0 from a", ____, b.method0());
|
||||
expectThat("direct method call to a", ____, a.method1());
|
||||
expectThat("b overwrites method1", ____, b.method1());
|
||||
expectThat("unless declared as virtual, methods are statically bound",
|
||||
'a', c.method1());
|
||||
expectThat("virtual method on b declared as B", 'b', b.method2());
|
||||
expectThat("virtual method on b declared as A", 'a', c.method2());
|
||||
expectThat("virtual method on b declared as B", ____, b.method2());
|
||||
expectThat("virtual method on b declared as A", ____, c.method2());
|
||||
|
||||
}
|
||||
void AboutClasses::meditate() {
|
||||
|
||||
@@ -19,7 +19,7 @@ void aboutIfStatement() {
|
||||
} else {
|
||||
result = 2;
|
||||
}
|
||||
expectThat("the if statement is just as in Java", 1, result);
|
||||
expectThat("the if statement is just as in Java", _____, result);
|
||||
}
|
||||
void aboutIntegersTreatedAsBoolean() {
|
||||
int a = 5;
|
||||
@@ -28,7 +28,7 @@ void aboutIntegersTreatedAsBoolean() {
|
||||
if (a = b) {
|
||||
actual = true;
|
||||
}
|
||||
expectThat("integers are treated as boolean!", true, actual);
|
||||
expectThat("integers are treated as boolean!", ________, actual);
|
||||
}
|
||||
|
||||
void aboutWhileLoop() {
|
||||
@@ -38,8 +38,7 @@ void aboutWhileLoop() {
|
||||
count++;
|
||||
++j;
|
||||
}
|
||||
expectThat("the while loop should have been run 100 times", 100,
|
||||
count);
|
||||
expectThat("the while loop should have been run 100 times", _____, count);
|
||||
}
|
||||
void aboutForLoop() {
|
||||
int count = 0;
|
||||
@@ -47,8 +46,7 @@ void aboutForLoop() {
|
||||
count++;
|
||||
}
|
||||
|
||||
expectThat("the for loop should have been run 100 times", 100,
|
||||
count);
|
||||
expectThat("the for loop should have been run 100 times", _____, count);
|
||||
}
|
||||
void aboutSwitchStatement() {
|
||||
const double cm_per_inch = 2.54;
|
||||
@@ -66,7 +64,7 @@ void aboutSwitchStatement() {
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
expectThat("it should have computed inch to cm", 2.54, result);
|
||||
expectThat("it should have computed inch to cm", ______, result);
|
||||
}
|
||||
void AboutControlStructures::meditate() {
|
||||
aboutIfStatement();
|
||||
|
||||
@@ -13,41 +13,40 @@ AboutDataTypes::AboutDataTypes() {
|
||||
void aboutDataTypeSizes(){
|
||||
|
||||
char decimal_point = '.';
|
||||
expectThat("char is on 1 byte",1,sizeof(decimal_point));
|
||||
expectThat("char is on 1 byte",_____,sizeof(decimal_point));
|
||||
|
||||
int number = 0;
|
||||
expectThat("integer is usually 32 bit",4,sizeof(number));
|
||||
expectThat("integer is usually 32 bit",_____,sizeof(number));
|
||||
|
||||
double flying_time = 2.9;
|
||||
expectThat("double takes 8 byte ",8,sizeof(flying_time));
|
||||
expectThat("double takes 8 byte ",_____,sizeof(flying_time));
|
||||
|
||||
bool tap = true;
|
||||
|
||||
expectThat("boolean takes 1 byte ",1,sizeof(tap));
|
||||
expectThat("boolean takes 1 byte ",_____,sizeof(tap));
|
||||
|
||||
|
||||
}
|
||||
void aboutSafeTypeConversions() {
|
||||
char c = 'a';
|
||||
int i1 = c;
|
||||
expectThat("converting char to int is save", 'a', (char)i1);
|
||||
expectThat("converting char to int is save", ____, (char)i1);
|
||||
|
||||
int i2 = 'a';
|
||||
char c2 = i2;
|
||||
expectThat("converting int to char works if value is small enough",
|
||||
'a', c2);
|
||||
expectThat("converting int to char works if value is small enough", ____, c2);
|
||||
}
|
||||
void aboutUnsafeTypeConversions() {
|
||||
int a = 128;
|
||||
char c3 = a; // doesn't fit in char - char is signed -128..127
|
||||
int b = c3;
|
||||
expectThat("int is cut off when pressed into a char", -128, b);
|
||||
expectThat("int is cut off when pressed into a char", _____, b);
|
||||
|
||||
|
||||
double d = 47.11;
|
||||
int i = d;
|
||||
double converted = i;
|
||||
expectThat("double is cut off when assigned to int", 47.00,converted);
|
||||
expectThat("double is cut off when assigned to int", ______,converted);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ void aboutThrowingSomething() {
|
||||
result = 2;
|
||||
}
|
||||
|
||||
expectThat("arbitrary classes can be thrown", 2, result);
|
||||
expectThat("arbitrary classes can be thrown", _____, result);
|
||||
}
|
||||
|
||||
void AboutErrorHandling::meditate() {
|
||||
|
||||
@@ -22,8 +22,8 @@ void someFunction1(int x, int y){
|
||||
void aboutPassingByValue(){
|
||||
int a = 4711, b = 13;
|
||||
someFunction1(a,b);
|
||||
expectThat("if parameters are passed by value, ...",4711,a);
|
||||
expectThat("they are not changed in the outside context",13,b);
|
||||
expectThat("if parameters are passed by value, ...",_____,a);
|
||||
expectThat("they are not changed in the outside context",_____,b);
|
||||
}
|
||||
// pass by reference: just add a &
|
||||
void someFunction2(int x, int &y){
|
||||
@@ -33,8 +33,8 @@ void someFunction2(int x, int &y){
|
||||
void aboutPassingByReference(){
|
||||
int a = 4711, b = 13;
|
||||
someFunction2(a,b);
|
||||
expectThat("if parameters are passed by reference, ...",4711,a);
|
||||
expectThat("they *are* changed in the outside context",14,b);
|
||||
expectThat("if parameters are passed by reference, ...",_____,a);
|
||||
expectThat("they *are* changed in the outside context",_____,b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
void aboutPointers(){
|
||||
int i = 5;
|
||||
int *i_pointer = &i;
|
||||
expectThat("pointers contain memory addresses - they are a reference to the variable",5,*i_pointer);
|
||||
expectThat("pointers contain memory addresses - they are a reference to the variable",_____,*i_pointer);
|
||||
}
|
||||
void AboutPointers::meditate(){
|
||||
aboutPointers();
|
||||
|
||||
@@ -21,20 +21,20 @@ void aboutCStrings(){
|
||||
char *x = aString;
|
||||
// and sizeof determines the size of the pointer!
|
||||
int pointerSize = sizeof(x);
|
||||
expectThat("the size of a pointer",4,pointerSize);
|
||||
expectThat("the size of a pointer",_____,pointerSize);
|
||||
|
||||
// arrays in c don't store their length.
|
||||
// there is a method in cstring which determines it for strings looking for the terminating char.
|
||||
char anotherString[] ="0123456789";
|
||||
int stringLength = strlen(anotherString);
|
||||
expectThat("the length of the string is determined by looking for the null char",10,stringLength);
|
||||
expectThat("the length of the string is determined by looking for the null char",_____,stringLength);
|
||||
}
|
||||
void aboutStringLiterals(){
|
||||
// internally, a c-string is an array of chars terminated by the null character \0
|
||||
// string literals - like "hello" - are short form for that.
|
||||
char hello1[] = {'h','e','l','l','o', '\0'};
|
||||
char hello2[] = "hello";
|
||||
expectThat("those strings are the same",true,(strcmp(hello1,hello2) == 0));
|
||||
expectThat("those strings are the same",________,(strcmp(hello1,hello2) == 0));
|
||||
}
|
||||
void aboutCPPStrings(){
|
||||
// C++ has a string library
|
||||
@@ -42,23 +42,23 @@ void aboutCPPStrings(){
|
||||
s = "big bang theory";
|
||||
|
||||
int l = s.length();
|
||||
expectThat("length returns the length of the string",15,l);
|
||||
expectThat("length returns the length of the string",_____,l);
|
||||
|
||||
expectThat("elements can be accessed via [] as in arrays",'b',s[4]);
|
||||
expectThat("elements can be accessed via [] as in arrays",____,s[4]);
|
||||
string s1 = "hello";
|
||||
string s2 = "hello";
|
||||
expectThat("opposed to java, == works on C++ strings",true, s1 == s2);
|
||||
expectThat("opposed to java, == works on C++ strings",________, s1 == s2);
|
||||
|
||||
s[4] = 'B';
|
||||
|
||||
expectThat("C++ strings are mutable.", string("big Bang theory"),s);
|
||||
expectThat("C++ strings are mutable.", _______,s);
|
||||
}
|
||||
void aboutStringAssignmentCreatesACopy(){
|
||||
string s1 = "hello";
|
||||
string s2 = s1;
|
||||
s2[1] = 'a';
|
||||
expectThat("s1 is not changed",string("hello"),s1);
|
||||
expectThat("but s2 of course is changed",string("hallo"),s2);
|
||||
expectThat("s1 is not changed",_______,s1);
|
||||
expectThat("but s2 of course is changed",_______,s2);
|
||||
}
|
||||
|
||||
string mutateString(string s){
|
||||
@@ -68,8 +68,8 @@ string mutateString(string s){
|
||||
void aboutStringsAsParametersBeingCopied(){
|
||||
string s1 = "hello";
|
||||
string s2 = mutateString(s1);
|
||||
expectThat("s1 is not changed by modification in function",string("hello"),s1);
|
||||
expectThat("but the return value s2 contains the change",string("hallo"),s2);
|
||||
expectThat("s1 is not changed by modification in function",_______,s1);
|
||||
expectThat("but the return value s2 contains the change",_______,s2);
|
||||
}
|
||||
void mutateString(string *s_pointer){
|
||||
*s_pointer = "Hullu";
|
||||
@@ -79,19 +79,19 @@ void mutateString(string *s_pointer){
|
||||
void aboutPassingAsReferenceMutatesTheOriginalString(){
|
||||
string s1 = "hello";
|
||||
mutateString(&s1);
|
||||
expectThat("if passed by reference, the string is modified",string("Hullu"),s1);
|
||||
expectThat("if passed by reference, the string is modified",_______,s1);
|
||||
}
|
||||
|
||||
void aboutTheConcatenationPitfall(){
|
||||
string s1 = "a ";
|
||||
expectThat("+ if overloaded to do string concatenation on C++ strings.",string("a cat"),s1+"cat");
|
||||
expectThat("can also be used with chars.",string("a c"),s1+'c');
|
||||
expectThat("+ if overloaded to do string concatenation on C++ strings.",_______,s1+"cat");
|
||||
expectThat("can also be used with chars.",_______,s1+'c');
|
||||
|
||||
// however: "a "+"cat"; won't compile - they are both c-strings
|
||||
expectThatNot("this will compile, but not result in the expected",string("a c"),string("a "+'c'));
|
||||
|
||||
char* s2 = "a123456789b123456789c";
|
||||
expectThat("in fact, it's a pointer manipulation II",string("6789c"),string(s2+16));
|
||||
expectThat("in fact, it's a pointer manipulation II",_______,string(s2+16));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,4 +7,4 @@
|
||||
#include "stdafx.h"
|
||||
#include "AboutUserDefinedTypes.h"
|
||||
|
||||
void AboutUserDefinedTypes::meditate(){};
|
||||
void AboutUserDefinedTypes::meditate(){};
|
||||
|
||||
@@ -43,7 +43,10 @@ template <class T,class U>
|
||||
void expectThat (string message, T expected, U actual) {
|
||||
expectThatRaw(message+" (expected was casted to "+typeid(U).name()+")",U(expected),actual);
|
||||
}
|
||||
|
||||
/*
|
||||
void expectThat(string message,int expected, string actual){
|
||||
cout << "honeypot";
|
||||
}*/
|
||||
template <class T>
|
||||
void expectThatNot (string message, T expected, T actual) {
|
||||
expectThatNotRaw(message,expected,actual);
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
require './replacer'
|
||||
puts File.realpath(__FILE__)
|
||||
require './scripts/replacer.rb'
|
||||
|
||||
|
||||
# this line has been replaced with int _ instead of string:
|
||||
# expectThat("in fact, it's a pointer manipulation II",_______,string(s2+16));
|
||||
|
||||
|
||||
|
||||
sourcedirname = "CPPKoans"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user