diff --git a/.gitignore b/.gitignore index 35d5ade..3dafc91 100755 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ *.slo *.lo *.o - +.vs # Compiled Dynamic libraries *.so diff --git a/CPPKoans.opensdf b/CPPKoans.opensdf deleted file mode 100755 index f683257..0000000 Binary files a/CPPKoans.opensdf and /dev/null differ diff --git a/CPPKoans.sdf b/CPPKoans.sdf index bcf44ac..286d2ab 100755 Binary files a/CPPKoans.sdf and b/CPPKoans.sdf differ diff --git a/CPPKoans/AboutAssert.cpp b/CPPKoans/AboutAssert.cpp index ce92499..17ed29d 100755 --- a/CPPKoans/AboutAssert.cpp +++ b/CPPKoans/AboutAssert.cpp @@ -2,18 +2,19 @@ #include "Assert.h" #include "AboutAssert.h" - -void aboutAssertParameterTypes(){ - 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",_______,string("cstring")); +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.", (int)3, 3); + expectThat("truncating information when casting to narrower type", (int)3.3, 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")); } -void AboutAssert::meditate(){ - aboutAssertParameterTypes(); -} +void AboutAssert::meditate() +{ + aboutAssertParameterTypes(); +} \ No newline at end of file diff --git a/CPPKoans/AboutClasses.cpp b/CPPKoans/AboutClasses.cpp index 63b4b06..809cbd3 100755 --- a/CPPKoans/AboutClasses.cpp +++ b/CPPKoans/AboutClasses.cpp @@ -8,53 +8,64 @@ #include "AboutClasses.h" #include "Assert.h" -class A { +class A +{ public: - A() { - } - virtual ~A() { - } + A() + { + } + virtual ~A() + { + } - char method0() { - return 'a'; - } - char method1() { - return 'a'; - } - virtual char method2() { - return 'a'; - } + char method0() + { + return 'a'; + } + char method1() + { + return 'a'; + } + virtual char method2() + { + return 'a'; + } }; -class B: public A { +class B : public A +{ public: - B() { - } - virtual ~B() { - } - char method1() { - return 'b'; - } - virtual char method2() { - return 'b'; - } + B() + { + } + virtual ~B() + { + } + char method1() + { + return 'b'; + } + virtual char method2() + { + return 'b'; + } }; -void aboutMemberPolymorphism() { - A a; - B b; - A c = b; - - 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.method2()); - expectThat("virtual method on b declared as A", ____, c.method2()); +void aboutMemberPolymorphism() +{ + A a; + B b; + A c = b; + 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.method2()); + expectThat("virtual method on b declared as A", ____, c.method2()); } -void AboutClasses::meditate() { - aboutMemberPolymorphism(); -} - +void AboutClasses::meditate() +{ + aboutMemberPolymorphism(); +} \ No newline at end of file diff --git a/CPPKoans/AboutFunctionCalls.cpp b/CPPKoans/AboutFunctionCalls.cpp index ee6a876..ca1c86d 100755 --- a/CPPKoans/AboutFunctionCalls.cpp +++ b/CPPKoans/AboutFunctionCalls.cpp @@ -8,38 +8,39 @@ #include "AboutFunctionCalls.h" #include "Assert.h" - - -void aboutPassingArrays(){ - - +void aboutPassingArrays() +{ } -void someFunction1(int x, int y){ - x = 0; - y++; +void someFunction1(int x, int y) +{ + x = 0; + y++; } -void aboutPassingByValue(){ - int a = 4711, b = 13; - someFunction1(a,b); - expectThat("if parameters are passed by value, ...",_____,a); - expectThat("they are not changed in the outside context",_____,b); +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); } // pass by reference: just add a & -void someFunction2(int x, int &y){ - x = 0; - y++; +void someFunction2(int x, int &y) +{ + x = 0; + y++; } -void aboutPassingByReference(){ - int a = 4711, b = 13; - someFunction2(a,b); - expectThat("if parameters are passed by reference, ...",_____,a); - expectThat("they *are* changed in the outside context",_____,b); +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); } - -void AboutFunctionCalls::meditate(){ - aboutPassingByValue(); - aboutPassingByReference(); - aboutPassingArrays(); -} +void AboutFunctionCalls::meditate() +{ + aboutPassingByValue(); + aboutPassingByReference(); + aboutPassingArrays(); +} \ No newline at end of file diff --git a/CPPKoans/AboutStrings.cpp b/CPPKoans/AboutStrings.cpp index 7616e23..77e7d5d 100755 --- a/CPPKoans/AboutStrings.cpp +++ b/CPPKoans/AboutStrings.cpp @@ -14,95 +14,101 @@ using namespace std; +void aboutCStrings() +{ + char aString[] = "a string literal is a c-string, an array of chars."; + // thus, aString is a pointer to char. + char *x = aString; + // and sizeof determines the size of the pointer! + int pointerSize = sizeof(x); + expectThat("the size of a pointer", 4, pointerSize); -void aboutCStrings(){ - char aString[] = "a string literal is a c-string, an array of chars."; - // thus, aString is a pointer to char. - char *x = aString; - // and sizeof determines the size of the pointer! - int pointerSize = sizeof(x); - 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",_____,stringLength); + // 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); } -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",________,(strcmp(hello1,hello2) == 0)); +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)); } -void aboutCPPStrings(){ - // C++ has a string library - string s; // calls default constructor - s = "big bang theory"; +void aboutCPPStrings() +{ + // C++ has a string library + string s; // calls default constructor + s = "big bang theory"; - int l = s.length(); - expectThat("length returns the length of the string",_____,l); + int l = s.length(); + expectThat("length returns the length of the string", 15, l); - 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",________, s1 == s2); + expectThat("elements can be accessed via [] as in arrays", 'b', s[4]); + string s1 = "hello"; + string s2 = "hello"; + expectThat("opposed to java, == works on C++ strings", true, s1 == s2); - s[4] = 'B'; + s[4] = 'B'; - expectThat("C++ strings are mutable.", _______,s); + expectThat("C++ strings are mutable.", string("big Bang theory"), s); } -void aboutStringAssignmentCreatesACopy(){ - string s1 = "hello"; - string s2 = s1; - s2[1] = 'a'; - expectThat("s1 is not changed",_______,s1); - expectThat("but s2 of course is changed",_______,s2); +void aboutStringAssignmentCreatesACopy() +{ + string s1 = "hello"; + string s2 = s1; + s2[1] = 'a'; + expectThat("s1 is not changed", _______, s1); + expectThat("but s2 of course is changed", _______, s2); } -string mutateString(string s){ - s[1] = 'a'; - return s; +string mutateString(string s) +{ + s[1] = 'a'; + return s; } -void aboutStringsAsParametersBeingCopied(){ - string s1 = "hello"; - string s2 = mutateString(s1); - expectThat("s1 is not changed by modification in function",_______,s1); - expectThat("but the return value s2 contains the change",_______,s2); +void aboutStringsAsParametersBeingCopied() +{ + string s1 = "hello"; + string s2 = mutateString(s1); + 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"; - //s*[1] = 'a'; +void mutateString(string *s_pointer) +{ + *s_pointer = "Hullu"; + //s*[1] = 'a'; } -void aboutPassingAsReferenceMutatesTheOriginalString(){ - string s1 = "hello"; - mutateString(&s1); - expectThat("if passed by reference, the string is modified",_______,s1); +void aboutPassingAsReferenceMutatesTheOriginalString() +{ + string s1 = "hello"; + mutateString(&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.",_______,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(s2+16)); - -} - -void AboutStrings::meditate() { - aboutCStrings(); - aboutStringLiterals(); - aboutCPPStrings(); - aboutStringAssignmentCreatesACopy(); - aboutStringsAsParametersBeingCopied(); - aboutPassingAsReferenceMutatesTheOriginalString(); - aboutTheConcatenationPitfall(); - +void aboutTheConcatenationPitfall() +{ + string s1 = "a "; + 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(s2 + 16)); } +void AboutStrings::meditate() +{ + aboutCStrings(); + aboutStringLiterals(); + aboutCPPStrings(); + /*aboutStringAssignmentCreatesACopy(); + aboutStringsAsParametersBeingCopied(); + aboutPassingAsReferenceMutatesTheOriginalString(); + aboutTheConcatenationPitfall();*/ +} \ No newline at end of file diff --git a/CPPKoans/Assert.h b/CPPKoans/Assert.h index 164931c..afe2c01 100755 --- a/CPPKoans/Assert.h +++ b/CPPKoans/Assert.h @@ -14,7 +14,6 @@ #define ________ false #define _________ true - #include #include #include @@ -23,47 +22,54 @@ using namespace std; template -void expectThatRaw (string message, T expected, T actual) { - if (expected != actual) { - reportUnmetExpectation(message,expected,actual); - } +void expectThatRaw(string message, T expected, T actual) +{ + if (expected != actual) + { + reportUnmetExpectation(message, expected, actual); + } } template -void expectThatNotRaw (string message, T expected, T actual) { - if (expected == actual) - reportUnmetExpectation(message,expected,actual); +void expectThatNotRaw(string message, T expected, T actual) +{ + if (expected == actual) + reportUnmetExpectation(message, expected, actual); } template -void expectThat (string message, T expected, T actual) { - expectThatRaw(message,expected,actual); +void expectThat(string message, T expected, T actual) +{ + expectThatRaw(message, expected, actual); } -template -void expectThat (string message, T expected, U actual) { - expectThatRaw(message+" (expected was casted to "+typeid(U).name()+")",U(expected),actual); +template +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"; + cout << "honeypot"; }*/ template -void expectThatNot (string message, T expected, T actual) { - expectThatNotRaw(message,expected,actual); +void expectThatNot(string message, T expected, T actual) +{ + expectThatNotRaw(message, expected, actual); } - -template -void expectThatNot (string message, T expected, U actual) { - expectThatNotRaw(message+" (expected was casted to "+typeid(U).name()+")",U(expected),actual); +template +void expectThatNot(string message, T expected, U actual) +{ + expectThatNotRaw(message + " (expected was casted to " + typeid(U).name() + ")", U(expected), actual); } template -void reportUnmetExpectation(string message, T expected, T actual){ - cout << "Expectation Failed: " << message << endl; - cout << "Expected: " << expected << ", but was: " << actual << endl; - if (QUIT_ON_UNMET_EXPECTATION) - throw StopMeditating(); +void reportUnmetExpectation(string message, T expected, T actual) +{ + cout << "Expectation Failed: " << message << endl; + cout << "Expected: " << expected << ", but was: " << actual << endl; + if (QUIT_ON_UNMET_EXPECTATION) + throw StopMeditating(); } #endif /* ASSERT_H_ */ diff --git a/CPPKoans/CPPKoans.cpp b/CPPKoans/CPPKoans.cpp index e431937..40cab88 100755 --- a/CPPKoans/CPPKoans.cpp +++ b/CPPKoans/CPPKoans.cpp @@ -26,24 +26,20 @@ using namespace std; * These are C++ */ -int main() { - cout << "Running CPP Koans. No further output should be produced." << endl; - try { - AboutControlStructures::meditate(); - AboutDataTypes::meditate(); - AboutErrorHandling::meditate(); - AboutPointers::meditate(); - AboutClasses::meditate(); - AboutArrays::meditate(); - AboutStrings::meditate(); - AboutFunctionCalls::meditate(); - AboutAssert::meditate(); - AboutBitwiseOperators::meditate(); - AboutUserDefinedTypes::meditate(); - } catch (const StopMeditating &e) { - cout << "catched StopMeditating." << endl; - } -// AboutVectors::meditate(); -// AboutStdInAndOut::meditate(); -} - +int main() +{ + cout << "Running CPP Koans. No further output should be produced." << endl; + try + { + AboutAssert::meditate(); + AboutFunctionCalls::meditate(); + AboutStrings::meditate(); + } + catch (const StopMeditating &e) + { + cout << "catched StopMeditating." << endl; + } + getchar(); + // AboutVectors::meditate(); + // AboutStdInAndOut::meditate(); +} \ No newline at end of file diff --git a/CPPKoans/CPPKoans.vcxproj b/CPPKoans/CPPKoans.vcxproj index 8bcfc77..030ed47 100755 --- a/CPPKoans/CPPKoans.vcxproj +++ b/CPPKoans/CPPKoans.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -20,12 +20,14 @@ Application true Unicode + v140 Application false true Unicode + v140