do some tests
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,7 +2,7 @@
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
|
||||
.vs
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
|
||||
|
||||
BIN
CPPKoans.opensdf
BIN
CPPKoans.opensdf
Binary file not shown.
BIN
CPPKoans.sdf
BIN
CPPKoans.sdf
Binary file not shown.
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();*/
|
||||
}
|
||||
@@ -14,7 +14,6 @@
|
||||
#define ________ false
|
||||
#define _________ true
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <typeinfo>
|
||||
@@ -23,47 +22,54 @@
|
||||
using namespace std;
|
||||
|
||||
template <class T>
|
||||
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 <class T>
|
||||
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 <class T>
|
||||
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 <class T,class U>
|
||||
void expectThat (string message, T expected, U actual) {
|
||||
expectThatRaw(message+" (expected was casted to "+typeid(U).name()+")",U(expected),actual);
|
||||
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";
|
||||
cout << "honeypot";
|
||||
}*/
|
||||
template <class T>
|
||||
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 <class T,class U>
|
||||
void expectThatNot (string message, T expected, U actual) {
|
||||
expectThatNotRaw(message+" (expected was casted to "+typeid(U).name()+")",U(expected),actual);
|
||||
template <class T, class U>
|
||||
void expectThatNot(string message, T expected, U actual)
|
||||
{
|
||||
expectThatNotRaw(message + " (expected was casted to " + typeid(U).name() + ")", U(expected), actual);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
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_ */
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
@@ -20,12 +20,14 @@
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
|
||||
Reference in New Issue
Block a user