diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-08-17 19:31:37 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-08-17 19:31:37 -0300 |
commit | e0c29962e6f334452f0c9db2caaf6ed18065de85 (patch) | |
tree | cee27801c196fbcacf6130ad64216af133b555dd /tests/libsample |
The End Is the Beginning Is the End
Diffstat (limited to 'tests/libsample')
28 files changed, 1519 insertions, 0 deletions
diff --git a/tests/libsample/CMakeLists.txt b/tests/libsample/CMakeLists.txt new file mode 100644 index 000000000..a630b3cb4 --- /dev/null +++ b/tests/libsample/CMakeLists.txt @@ -0,0 +1,22 @@ +project(libsample) + +set(libsample_SRC +abstract.cpp +complex.cpp +derived.cpp +functions.cpp +implicitconv.cpp +kindergarten.cpp +listuser.cpp +modifications.cpp +pairuser.cpp +point.cpp +reference.cpp +samplenamespace.cpp +size.cpp +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_library(libsample SHARED ${libsample_SRC}) +set_property(TARGET libsample PROPERTY PREFIX "") + diff --git a/tests/libsample/abstract.cpp b/tests/libsample/abstract.cpp new file mode 100644 index 000000000..8b49f37f8 --- /dev/null +++ b/tests/libsample/abstract.cpp @@ -0,0 +1,64 @@ +#include <iostream> +#include "abstract.h" + +using namespace std; + +Abstract::Abstract(int id) : m_id(id) +{ + cout << __PRETTY_FUNCTION__; + show(); + cout << endl; +} + +Abstract::~Abstract() +{ + cout << __PRETTY_FUNCTION__; + show(); + cout << endl; +} + +void +Abstract::unpureVirtual() +{ + cout << __PRETTY_FUNCTION__ << endl; +} + +void +Abstract::callUnpureVirtual() +{ + cout << __PRETTY_FUNCTION__ << " --- BEGIN" << endl; + this->unpureVirtual(); + cout << __PRETTY_FUNCTION__ << " --- END" << endl; +} + + +void +Abstract::callPureVirtual() +{ + cout << __PRETTY_FUNCTION__ << " --- BEGIN" << endl; + this->pureVirtual(); + cout << __PRETTY_FUNCTION__ << " --- END" << endl; +} + +void +Abstract::show(PrintFormat format) +{ + cout << '<'; + switch(format) { + case Short: + cout << this; + break; + case Verbose: + cout << "class " << className() << " | cptr: " << this; + cout << ", id: " << m_id; + break; + case OnlyId: + cout << "id: " << m_id; + break; + case ClassNameAndId: + cout << className() << " - id: " << m_id; + break; + } + cout << '>'; +} + diff --git a/tests/libsample/abstract.h b/tests/libsample/abstract.h new file mode 100644 index 000000000..19e3a9dbf --- /dev/null +++ b/tests/libsample/abstract.h @@ -0,0 +1,37 @@ +#ifndef ABSTRACT_H +#define ABSTRACT_H + +class Abstract +{ +public: + enum PrintFormat { + Short, + Verbose, + OnlyId, + ClassNameAndId + }; + + Abstract(int id = -1); + virtual ~Abstract(); + + int id() { return m_id; } + + // factory method + static Abstract* createObject() { return 0; } + + virtual void pureVirtual() = 0; + virtual void unpureVirtual(); + + void callPureVirtual(); + void callUnpureVirtual(); + + void show(PrintFormat format = Verbose); + +protected: + virtual const char* className() { return "Abstract"; } + +private: + int m_id; +}; +#endif // ABSTRACT_H + diff --git a/tests/libsample/complex.cpp b/tests/libsample/complex.cpp new file mode 100644 index 000000000..9e93c961c --- /dev/null +++ b/tests/libsample/complex.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include "complex.h" + +using namespace std; + +Complex::Complex(double real, double imag) + : m_real(real), m_imag(imag) +{ + // cout << __PRETTY_FUNCTION__ << "[real=0.0, imag=0.0]" << endl; +} + +Complex +Complex::operator+(Complex& other) +{ + Complex result; + result.setReal(m_real + other.real()); + result.setImaginary(m_imag + other.imag()); + return result; +} + +void +Complex::show() +{ + cout << "(real: " << m_real << ", imag: " << m_imag << ")"; +} + + diff --git a/tests/libsample/complex.h b/tests/libsample/complex.h new file mode 100644 index 000000000..f8efb790c --- /dev/null +++ b/tests/libsample/complex.h @@ -0,0 +1,25 @@ +#ifndef COMPLEX_H +#define COMPLEX_H + +class Complex +{ +public: + Complex(double real = 0.0, double imag = 0.0); + ~Complex() {} + + double real() const { return m_real; } + void setReal(double real) { m_real = real; } + double imag() const { return m_imag; } + void setImaginary(double imag) { m_imag = imag; } + + Complex operator+(Complex& other); + + void show(); + +private: + double m_real; + double m_imag; +}; + +#endif // COMPLEX_H + diff --git a/tests/libsample/derived.cpp b/tests/libsample/derived.cpp new file mode 100644 index 000000000..9a86a9c94 --- /dev/null +++ b/tests/libsample/derived.cpp @@ -0,0 +1,80 @@ +#include <iostream> +#include "derived.h" + +using namespace std; + +Derived::Derived(int id) : Abstract(id) +{ + cout << __PRETTY_FUNCTION__; + show(); + cout << endl; +} + +Derived::~Derived() +{ + cout << __PRETTY_FUNCTION__; + show(); + cout << endl; +} + +Abstract* +Derived::createObject() +{ + static int id = 100; + return new Derived(id++); +} + +void +Derived::pureVirtual() +{ + cout << __PRETTY_FUNCTION__ << endl; +} + +void +Derived::unpureVirtual() +{ + cout << __PRETTY_FUNCTION__ << endl; +} + +bool +Derived::singleArgument(bool b) +{ + cout << __PRETTY_FUNCTION__ << endl; + return !b; +} + +double +Derived::defaultValue(int n) +{ + cout << __PRETTY_FUNCTION__ << "[n = 0]" << endl; + return ((double) n) + 0.1; +} + +PolymorphicFuncEnum +Derived::polymorphic(int i, int d) +{ + cout << __PRETTY_FUNCTION__ << "[i = 0, d = 0]" << endl; + return PolymorphicFunc_ii; +} + +PolymorphicFuncEnum +Derived::polymorphic(double n) +{ + cout << __PRETTY_FUNCTION__ << endl; + return PolymorphicFunc_d; +} + +Derived::OtherPolymorphicFuncEnum +Derived::otherPolymorphic(int a, int b, bool c, double d) +{ + cout << __PRETTY_FUNCTION__ << endl; + return OtherPolymorphicFunc_iibd; +} + +Derived::OtherPolymorphicFuncEnum +Derived::otherPolymorphic(int a, double b) +{ + cout << __PRETTY_FUNCTION__ << endl; + return OtherPolymorphicFunc_id; +} + diff --git a/tests/libsample/derived.h b/tests/libsample/derived.h new file mode 100644 index 000000000..5efc7c4b4 --- /dev/null +++ b/tests/libsample/derived.h @@ -0,0 +1,46 @@ +#ifndef DERIVED_H +#define DERIVED_H + +#include "abstract.h" + +enum PolymorphicFuncEnum { + PolymorphicFunc_ii, + PolymorphicFunc_d +}; + +class Derived : public Abstract +{ +public: + enum OtherPolymorphicFuncEnum { + OtherPolymorphicFunc_iibd, + OtherPolymorphicFunc_id + }; + + Derived(int id = -1); + virtual ~Derived(); + virtual void pureVirtual(); + virtual void unpureVirtual(); + + // factory method + static Abstract* createObject(); + + // single argument + bool singleArgument(bool b); + + // method with default value + double defaultValue(int n = 0); + + // overloads + PolymorphicFuncEnum polymorphic(int i = 0, int d = 0); + PolymorphicFuncEnum polymorphic(double n); + + // more overloads + OtherPolymorphicFuncEnum otherPolymorphic(int a, int b, bool c, double d); + OtherPolymorphicFuncEnum otherPolymorphic(int a, double b); + +protected: + const char* getClassName() { return className(); } + virtual const char* className() { return "Derived"; } +}; +#endif // DERIVED_H + diff --git a/tests/libsample/functions.cpp b/tests/libsample/functions.cpp new file mode 100644 index 000000000..c35bc5be3 --- /dev/null +++ b/tests/libsample/functions.cpp @@ -0,0 +1,85 @@ +#include "functions.h" +#include <string.h> +#include <iostream> + +using namespace std; + +void +printSomething() +{ + cout << __PRETTY_FUNCTION__ << endl; +} + +int +gimmeInt() +{ + static int val = 2; + val = val * 1.3; + return val; +} + +double +gimmeDouble() +{ + static double val = 7.77; + val = val * 1.3; + return val; +} + +std::list<Complex> +gimmeComplexList() +{ + std::list<Complex> lst; + lst.push_back(Complex()); + lst.push_back(Complex(1.1, 2.2)); + lst.push_back(Complex(1.3, 2.4)); + return lst; +} + +Complex +sumComplexPair(std::pair<Complex, Complex> cpx_pair) +{ + return cpx_pair.first + cpx_pair.second; +} + +double +multiplyPair(std::pair<double, double> pair) +{ + return pair.first * pair.second; +} + +int +countCharacters(const char* text) +{ + int count; + for(count = 0; text[count] != '\0'; count++) + ; + return count; +} + +char* +makeCString() +{ + char* string = new char[strlen(__FUNCTION__) + 1]; + strcpy(string, __FUNCTION__); + return string; +} + +const char* +returnCString() +{ + return __PRETTY_FUNCTION__; +} + +GlobalPolyFuncEnum +polymorphicFunc(int val) +{ + return GlobalPolyFunc_i; +} + +GlobalPolyFuncEnum +polymorphicFunc(double val) +{ + return GlobalPolyFunc_d; +} + diff --git a/tests/libsample/functions.h b/tests/libsample/functions.h new file mode 100644 index 000000000..77b2cb68f --- /dev/null +++ b/tests/libsample/functions.h @@ -0,0 +1,36 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +#include <list> +#include <utility> +#include "complex.h" + +enum GlobalEnum { + NoThing, + FirstThing, + SecondThing, + ThirdThing +}; + +enum GlobalPolyFuncEnum { + GlobalPolyFunc_i, + GlobalPolyFunc_d +}; + +void printSomething(); +int gimmeInt(); +double gimmeDouble(); +double multiplyPair(std::pair<double, double> pair); +std::list<Complex> gimmeComplexList(); +Complex sumComplexPair(std::pair<Complex, Complex> cpx_pair); + +int countCharacters(const char* text); +char* makeCString(); +const char* returnCString(); + +// Tests polymorphism on functions (!methods) +GlobalPolyFuncEnum polymorphicFunc(int val); +GlobalPolyFuncEnum polymorphicFunc(double val); + +#endif // FUNCTIONS_H + diff --git a/tests/libsample/implicitconv.cpp b/tests/libsample/implicitconv.cpp new file mode 100644 index 000000000..902707e18 --- /dev/null +++ b/tests/libsample/implicitconv.cpp @@ -0,0 +1,38 @@ +#include "implicitconv.h" + +ImplicitConv +ImplicitConv::implicitConvCommon(ImplicitConv implicit) +{ + return implicit; +} + +ImplicitConv +ImplicitConv::implicitConvDefault(ImplicitConv implicit) +{ + return implicit; +} + +ImplicitConv::ICPolymorphicFuncEnum +ImplicitConv::implicitConvPolymorphism(ImplicitConv implicit, int dummyArg) +{ + return ImplicitConv::PolyFunc_Ii; +} + +ImplicitConv::ICPolymorphicFuncEnum +ImplicitConv::implicitConvPolymorphism(ImplicitConv implicit, bool dummyArg) +{ + return ImplicitConv::PolyFunc_Ib; +} + +ImplicitConv::ICPolymorphicFuncEnum +ImplicitConv::implicitConvPolymorphism(int dummyArg) +{ + return ImplicitConv::PolyFunc_i; +} + +ImplicitConv::ICPolymorphicFuncEnum +ImplicitConv::implicitConvPolymorphism(CtorEnum dummyArg) +{ + return ImplicitConv::PolyFunc_C; +} + diff --git a/tests/libsample/implicitconv.h b/tests/libsample/implicitconv.h new file mode 100644 index 000000000..538150870 --- /dev/null +++ b/tests/libsample/implicitconv.h @@ -0,0 +1,44 @@ +#ifndef IMPLICITCONV_H +#define IMPLICITCONV_H + +class ImplicitConv +{ +public: + enum CtorEnum { + CtorNone, + CtorOne, + CtorTwo, + CtorThree + }; + + enum ICPolymorphicFuncEnum { + PolyFunc_Ii, + PolyFunc_Ib, + PolyFunc_i, + PolyFunc_C + }; + + ImplicitConv() : m_ctorEnum(CtorNone), m_objId(-1) {} + ImplicitConv(int objId) : m_ctorEnum(CtorOne), m_objId(objId) {} + ImplicitConv(CtorEnum ctorEnum) : m_ctorEnum(ctorEnum), m_objId(-1) {} + ~ImplicitConv() {} + + CtorEnum ctorEnum() { return m_ctorEnum; } + int objId() { return m_objId; } + + static ImplicitConv implicitConvCommon(ImplicitConv implicit); + + static ImplicitConv implicitConvDefault(ImplicitConv implicit = CtorTwo); + + static ICPolymorphicFuncEnum implicitConvPolymorphism(ImplicitConv implicit, int dummyArg); + static ICPolymorphicFuncEnum implicitConvPolymorphism(ImplicitConv implicit, bool dummyArg); + static ICPolymorphicFuncEnum implicitConvPolymorphism(int dummyArg); + static ICPolymorphicFuncEnum implicitConvPolymorphism(CtorEnum dummyArg); + +private: + CtorEnum m_ctorEnum; + int m_objId; +}; + +#endif // IMPLICITCONV_H + diff --git a/tests/libsample/kindergarten.cpp b/tests/libsample/kindergarten.cpp new file mode 100644 index 000000000..8332e4fe7 --- /dev/null +++ b/tests/libsample/kindergarten.cpp @@ -0,0 +1,64 @@ +#include <iostream> +#include "kindergarten.h" + +using namespace std; + +KinderGarten::~KinderGarten() +{ + cout << __PRETTY_FUNCTION__ << " ---- BEGIN" << endl; + killChildren(); + cout << __PRETTY_FUNCTION__ << " ---- END" << endl; +} + +void +KinderGarten::addChild(Abstract* child) +{ + m_children.push_back(child); +} + +void +KinderGarten::killChildren() +{ + cout << __PRETTY_FUNCTION__ << endl; + while (!m_children.empty()) { + m_children.back()->show(); + cout << endl; + delete m_children.back(); + m_children.pop_back(); + } +} + +void +KinderGarten::killChild(Abstract* child) +{ + cout << __PRETTY_FUNCTION__ << endl; + if (child) { + m_children.remove(child); +// delete child; + } +} + +Abstract* +KinderGarten::releaseChild(Abstract* child) +{ + for(ChildList::iterator child_iter = m_children.begin(); + child_iter != m_children.end(); child_iter++) { + if (child == *child_iter) { + m_children.erase(child_iter); + return child; + } + } +} + +void +KinderGarten::show() +{ + cout << "["; + for(ChildList::iterator child_iter = m_children.begin(); + child_iter != m_children.end(); child_iter++) { + if (child_iter != m_children.begin()) + cout << ", "; + (*child_iter)->show(); + } + cout << "]"; +} diff --git a/tests/libsample/kindergarten.h b/tests/libsample/kindergarten.h new file mode 100644 index 000000000..b0a083dce --- /dev/null +++ b/tests/libsample/kindergarten.h @@ -0,0 +1,29 @@ +#ifndef KINDERGARTEN_H +#define KINDERGARTEN_H + +#include <list> +#include "abstract.h" + +class KinderGarten +{ +public: + typedef std::list<Abstract*> ChildList; + + KinderGarten() {} + ~KinderGarten(); + + void addChild(Abstract* child); + Abstract* releaseChild(Abstract* child); + ChildList children() { return m_children; } + + void killChildren(); + void killChild(Abstract* child); + + void show(); + +private: + ChildList m_children; +}; + +#endif // KINDERGARTEN_H + diff --git a/tests/libsample/listuser.cpp b/tests/libsample/listuser.cpp new file mode 100644 index 000000000..0b9a14ff5 --- /dev/null +++ b/tests/libsample/listuser.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include <numeric> +#include <cstdlib> +#include "listuser.h" + +using namespace std; + +std::list<int> +ListUser::callCreateList() +{ + cout << __PRETTY_FUNCTION__ << endl; + return createList(); +} + +std::list<int> +ListUser::createList() +{ + cout << __PRETTY_FUNCTION__ << endl; + std::list<int> retval; + for (int i = 0; i < 4; i++) + retval.push_front(rand()); + return retval; +} + +std::list<Complex> +ListUser::createComplexList(Complex cpx0, Complex cpx1) +{ + cout << __PRETTY_FUNCTION__ << endl; + std::list<Complex> retval; + retval.push_back(cpx0); + retval.push_back(cpx1); + return retval; +} + +double +ListUser::sumList(std::list<int> vallist) +{ + return std::accumulate(vallist.begin(), vallist.end(), 0.0); +} + +double +ListUser::sumList(std::list<double> vallist) +{ + return std::accumulate(vallist.begin(), vallist.end(), 0.0); +} + + diff --git a/tests/libsample/listuser.h b/tests/libsample/listuser.h new file mode 100644 index 000000000..c1ea7091e --- /dev/null +++ b/tests/libsample/listuser.h @@ -0,0 +1,23 @@ +#ifndef LISTUSER_H +#define LISTUSER_H + +#include <list> +#include "complex.h" + +class ListUser +{ +public: + ListUser() {} + ~ListUser() {} + + virtual std::list<int> createList(); + std::list<int> callCreateList(); + + static std::list<Complex> createComplexList(Complex cpx0, Complex cpx1); + + double sumList(std::list<int> vallist); + double sumList(std::list<double> vallist); +}; + +#endif // LISTUSER_H + diff --git a/tests/libsample/main.cpp b/tests/libsample/main.cpp new file mode 100644 index 000000000..7fc563679 --- /dev/null +++ b/tests/libsample/main.cpp @@ -0,0 +1,216 @@ +#include <iostream> +#include <list> +#include "abstract.h" +#include "derived.h" +#include "kindergarten.h" +#include "complex.h" +#include "point.h" +#include "size.h" +#include "listuser.h" +#include "samplenamespace.h" + +using namespace std; + +int +main(int argv, char **argc) +{ + cout << endl; + + Derived derived; + + cout << endl; + + derived.unpureVirtual(); + derived.pureVirtual(); + derived.callPureVirtual(); + + cout << endl; + Abstract* abs; + abs = Abstract::createObject(); + cout << "Abstract::createObject(): " << abs << endl << endl; + delete abs; + + abs = Derived::createObject(); + cout << "Derived::createObject() : "; + abs->show(); + cout << endl; + delete abs; + cout << endl; + + abs = Derived::createObject(); + cout << "Derived::createObject() : "; + abs->show(); + cout << endl; + delete abs; + cout << endl; + + cout << endl << "-----------------------------------------" << endl; + + KinderGarten kg; + Derived* d[] = { 0, 0, 0 }; + + for (int i = 0; i < 3; i++) { + d[i] = new Derived(i); + d[i]->show(); + cout << endl; + kg.addChild(d[i]); + } + + kg.show(); + cout << endl; + + cout << endl << "* kill child "; + d[2]->show(); + cout << " ----------------" << endl; + kg.killChild(d[2]); + kg.show(); + cout << endl; + + cout << endl << "* release child "; + d[1]->show(); + cout << " -------------" << endl; + Abstract* released = kg.releaseChild(d[1]); + cout << "released: "; + released->show(); + cout << endl; + kg.show(); + cout << endl; + + cout << endl << "* kill children ------------------------------------" << endl; + kg.killChildren(); + kg.show(); + cout << endl << endl; + + cout << "-----------------------------------------" << endl; + ListUser lu; + cout << "ListUser::createList()" << endl; + std::list<int> intlist = lu.createList(); + for (std::list<int>::iterator it = intlist.begin(); it != intlist.end(); it++) { + cout << "* " << *it << endl; + } + + cout << "ListUser::createComplexList" << endl; + std::list<Complex> cpxlist = ListUser::createComplexList(Complex(1.1, 2.2), Complex(3.3, 4.4)); + for (std::list<Complex>::iterator it = cpxlist.begin(); it != cpxlist.end(); it++) { + cout << "* "; + (*it).show(); + cout << endl; + } + cout << endl; + + cout << "-----------------------------------------" << endl; + cout << "SampleNamespace" << endl; + + cout << "SampleNamespace::RandomNumber: "; + cout << SampleNamespace::getNumber(SampleNamespace::RandomNumber); + cout << endl; + cout << "SampleNamespace::UnixTime: "; + cout << SampleNamespace::getNumber(SampleNamespace::UnixTime); + cout << endl; + double val_d = 1.3; + cout << "SampleNamespace::powerOfTwo(" << val_d << "): "; + cout << SampleNamespace::powerOfTwo(val_d) << endl; + int val_i = 7; + cout << "SampleNamespace::powerOfTwo(" << val_i << "): "; + cout << SampleNamespace::powerOfTwo(val_i) << endl; + cout << endl; + + cout << "-----------------------------------------" << endl; + cout << "Point" << endl; + + Point p1(1.1, 2.2); + cout << "p1: "; + p1.show(); + cout << endl; + + Point p2(3.4, 5.6); + cout << "p2: "; + p2.show(); + cout << endl; + + cout << "p1 + p2 == "; + (p1 + p2).show(); + cout << endl; + + cout << "p1 * 2.0 == "; + (p1 * 2.0).show(); + cout << endl; + + cout << "1.5 * p2 == "; + (1.5 * p2).show(); + cout << endl; + + cout << "p1: "; + p1.show(); + cout << endl << "p2: "; + p2.show(); + cout << endl << "p1 += p2" << endl; + p1 += p2; + cout << "p1: "; + p1.show(); + cout << endl; + + cout << "p1 == p2 ? " << ((p1 == p2) ? "true" : "false") << endl; + cout << "p1 == p1 ? " << ((p1 == p1) ? "true" : "false") << endl; + cout << "p2 == p2 ? " << ((p2 == p2) ? "true" : "false") << endl; + + cout << "-----------------------------------------" << endl; + cout << "Size" << endl; + + Size s1(2, 2); + cout << "s1: "; + s1.show(); + cout << ", area: " << s1.calculateArea(); + cout << endl; + + Size s2(3, 5); + cout << "s2: "; + s2.show(); + cout << ", area: " << s2.calculateArea(); + cout << endl; + + cout << endl; + + cout << "s1 == s2 ? " << ((s1 == s2) ? "true" : "false") << endl; + cout << "s1 != s2 ? " << ((s1 != s2) ? "true" : "false") << endl; + + cout << "s1 < s2 ? " << ((s1 < s2) ? "true" : "false") << endl; + cout << "s1 <= s2 ? " << ((s1 <= s2) ? "true" : "false") << endl; + cout << "s1 > s2 ? " << ((s1 > s2) ? "true" : "false") << endl; + cout << "s1 >= s2 ? " << ((s1 >= s2) ? "true" : "false") << endl; + + cout << "s1 < 10 ? " << ((s1 < 10) ? "true" : "false") << endl; + cout << "s1 <= 10 ? " << ((s1 <= 10) ? "true" : "false") << endl; + cout << "s1 > 10 ? " << ((s1 > 10) ? "true" : "false") << endl; + cout << "s1 >= 10 ? " << ((s1 >= 10) ? "true" : "false") << endl; + cout << "s2 < 10 ? " << ((s2 < 10) ? "true" : "false") << endl; + cout << "s2 <= 10 ? " << ((s2 <= 10) ? "true" : "false") << endl; + cout << "s2 > 10 ? " << ((s2 > 10) ? "true" : "false") << endl; + cout << "s2 >= 10 ? " << ((s2 >= 10) ? "true" : "false") << endl; + cout << endl; + + cout << "s1: "; + s1.show(); + cout << endl << "s2: "; + s2.show(); + cout << endl << "s1 += s2" << endl; + s1 += s2; + cout << "s1: "; + s1.show(); + cout << endl; + + cout << endl; + + cout << "s1: "; + s1.show(); + cout << endl << "s1 *= 2.0" << endl; + s1 *= 2.0; + cout << "s1: "; + s1.show(); + cout << endl; + + cout << endl; + + return 0; +} + diff --git a/tests/libsample/modifications.cpp b/tests/libsample/modifications.cpp new file mode 100644 index 000000000..68cf6fe6c --- /dev/null +++ b/tests/libsample/modifications.cpp @@ -0,0 +1,68 @@ +#include <iostream> +#include "modifications.h" + +using namespace std; + +std::pair<double, double> +Modifications::pointToPair(Point pt, bool* ok) +{ + std::pair<double, double> retval(pt.x(), pt.y()); + *ok = true; + return retval; +} + +double +Modifications::multiplyPointCoordsPlusValue(bool* ok, Point pt, double value) +{ + double retval = (pt.x() * pt.y()) + value; + *ok = true; + return retval; +} + +int +Modifications::doublePlus(int value, int plus) +{ + return (2 * value) + plus; +} + +int +Modifications::power(int base, int exponent) +{ + if (exponent == 0) + return 1; + int retval = base; + for (int i = 1; i < exponent; i++) + retval = retval * base; + return retval; +} + +int +Modifications::timesTen(int number) +{ + return number * 10; +} + +int +Modifications::increment(int number) +{ + return ++number; +} + +void +Modifications::exclusiveCppStuff() +{ + cout << __PRETTY_FUNCTION__ << endl; +} + +int +Modifications::cppMultiply(int a, int b) +{ + return a * b; +} + +const char* +Modifications::className() +{ + return "Modifications"; +} + diff --git a/tests/libsample/modifications.h b/tests/libsample/modifications.h new file mode 100644 index 000000000..793808161 --- /dev/null +++ b/tests/libsample/modifications.h @@ -0,0 +1,73 @@ +#ifndef MODIFICATIONS_H +#define MODIFICATIONS_H + +#include <utility> +#include "point.h" + +class Modifications +{ +public: + Modifications() {} + ~Modifications() {} + + enum PolymorphicModFunc { + PolymorphicNone, + Polymorphic_ibid, + Polymorphic_ibib, + Polymorphic_ibiP, + Polymorphic_ibii, + Polymorphic_ibPP + }; + + // those polymorphic methods should be heavily modified + // to push the overload decisor to its limits + PolymorphicModFunc polymorphic(int a0, bool b0, int c0, double d0) { return Polymorphic_ibid; } + PolymorphicModFunc polymorphic(int a1, bool b1, int c1, bool d1) { return Polymorphic_ibib; } + PolymorphicModFunc polymorphic(int a2, bool b2, int c2, Point d2) { return Polymorphic_ibiP; } + PolymorphicModFunc polymorphic(int a3, bool b3, int c3 = 123, int d3 = 456) { return Polymorphic_ibii; } + PolymorphicModFunc polymorphic(int a4, bool b4, Point c4, Point d4) { return Polymorphic_ibPP; } + + // 'ok' must be removed and the return value will be changed + // to a tuple (PyObject*) containing the expected result plus + // the 'ok' value as a Python boolean + std::pair<double, double> pointToPair(Point pt, bool* ok); + + // same as 'pointToPair' except that this time 'ok' is the first argument + double multiplyPointCoordsPlusValue(bool* ok, Point pt, double value); + + // completely remove 'plus' from the Python side + int doublePlus(int value, int plus = 0); + + // the default value for both arguments must be changed in Python + int power(int base = 1, int exponent = 0); + + // in Python set argument default value to 10 + int timesTen(int number); + + // in Python remove the argument default value + int increment(int number = 0); + + // don't export this method to Python + void exclusiveCppStuff(); + + // change the name of this regular method + int cppMultiply(int a, int b); + + // change the name of this virtual method + virtual const char* className(); +}; + +class AbstractModifications : public Modifications +{ +public: + AbstractModifications() {} + ~AbstractModifications() {} + + bool invert(bool value) { return !value; } + + // completely remove this method in Python + virtual void pointlessPureVirtualMethod() = 0; +}; + +#endif // MODIFICATIONS_H + diff --git a/tests/libsample/pairuser.cpp b/tests/libsample/pairuser.cpp new file mode 100644 index 000000000..8983630e2 --- /dev/null +++ b/tests/libsample/pairuser.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include "pairuser.h" + +using namespace std; + +std::pair<int, int> +PairUser::callCreatePair() +{ + cout << __PRETTY_FUNCTION__ << endl; + return createPair(); +} + +std::pair<int, int> +PairUser::createPair() +{ + cout << __PRETTY_FUNCTION__ << endl; + return std::pair<int, int>(10, 20); +} + +std::pair<Complex, Complex> +PairUser::createComplexPair(Complex cpx0, Complex cpx1) +{ + cout << __PRETTY_FUNCTION__ << endl; + return std::pair<Complex, Complex>(cpx0, cpx1); +} + +double +PairUser::sumPair(std::pair<int, double> pair) +{ + return ((double) pair.first) + pair.second; +} + diff --git a/tests/libsample/pairuser.h b/tests/libsample/pairuser.h new file mode 100644 index 000000000..27cf76681 --- /dev/null +++ b/tests/libsample/pairuser.h @@ -0,0 +1,19 @@ +#ifndef PAIRUSER_H +#define PAIRUSER_H + +#include <utility> +#include "complex.h" + +class PairUser +{ +public: + PairUser() {} + ~PairUser() {} + + virtual std::pair<int, int> createPair(); + std::pair<int, int> callCreatePair(); + static std::pair<Complex, Complex> createComplexPair(Complex cpx0, Complex cpx1); + double sumPair(std::pair<int, double> pair); +}; +#endif // PAIRUSER_H + diff --git a/tests/libsample/point.cpp b/tests/libsample/point.cpp new file mode 100644 index 000000000..ad816025e --- /dev/null +++ b/tests/libsample/point.cpp @@ -0,0 +1,111 @@ +#include <iostream> +#include "point.h" + +using namespace std; + +Point::Point(int x, int y) : m_x(x), m_y(y) +{ + // cout << __PRETTY_FUNCTION__ << " [x=0, y=0]" << endl; +} + +Point::Point(double x, double y) : m_x(x), m_y(y) +{ + // cout << __PRETTY_FUNCTION__ << endl; +} + +void +Point::show() +{ + cout << "(x: " << m_x << ", y: " << m_y << ")"; +} + +bool +Point::operator==(const Point& other) +{ + return m_x == other.m_x && m_y == other.m_y; +} + +Point +Point::operator+(const Point& other) +{ + return Point(m_x + other.m_x, m_y + other.m_y); +} + +Point +Point::operator-(const Point& other) +{ + return Point(m_x - other.m_x, m_y - other.m_y); +} + +Point& +Point::operator+=(Point &other) +{ + m_x += other.m_x; + m_y += other.m_y; + return *this; +} + +Point& +Point::operator-=(Point &other) +{ + m_x -= other.m_x; + m_y -= other.m_y; + return *this; +} + +Point +operator*(Point& pt, double mult) +{ + return Point(pt.m_x * mult, pt.m_y * mult); +} + +Point +operator*(Point& pt, int mult) +{ + return Point(((int) pt.m_x) * mult, ((int) pt.m_y) * mult); +} + +Point +operator*(double mult, Point& pt) +{ + return Point(pt.m_x * mult, pt.m_y * mult); +} + +Point +operator*(int mult, Point& pt) +{ + return Point(((int) pt.m_x) * mult, ((int) pt.m_y) * mult); +} + +Point +operator-(const Point& pt) +{ + return Point(-pt.m_x, -pt.m_y); +} + +bool +operator!(const Point& pt) +{ + return (pt.m_x == 0.0 && pt.m_y == 0.0); +} + +Complex +transmutePointIntoComplex(Point point) +{ + Complex cpx(point.x(), point.y()); + // cout << __PRETTY_FUNCTION__ << " "; + // point.show(); + // cout << endl; + return cpx; +} + +Point +transmuteComplexIntoPoint(Complex cpx) +{ + Point pt(cpx.real(), cpx.imag()); + // cout << __PRETTY_FUNCTION__ << " "; + // cpx.show(); + // cout << endl; + return pt; +} + diff --git a/tests/libsample/point.h b/tests/libsample/point.h new file mode 100644 index 000000000..e5ec9c1bc --- /dev/null +++ b/tests/libsample/point.h @@ -0,0 +1,51 @@ +#ifndef POINT_H +#define POINT_H + +#include "complex.h" +#include <utility> + +class Point +{ +public: + Point(int x = 0, int y = 0); + Point(double x, double y); + ~Point() {} + + double x() const { return m_x; } + double y() const { return m_y; } + + bool operator==(const Point& other); + Point operator+(const Point& other); + Point operator-(const Point& other); + + friend Point operator*(Point& pt, double mult); + friend Point operator*(Point& pt, int mult); + friend Point operator*(double mult, Point& pt); + friend Point operator*(int mult, Point& pt); + friend Point operator-(const Point& pt); + friend bool operator!(const Point& pt); + + Point& operator+=(Point &other); + Point& operator-=(Point &other); + + void show(); + +private: + double m_x; + double m_y; +}; + +Point operator*(Point& pt, double mult); +Point operator*(Point& pt, int mult); +Point operator*(double mult, Point& pt); +Point operator*(int mult, Point& pt); +Point operator-(const Point& pt); +bool operator!(const Point& pt); + +Complex transmutePointIntoComplex(Point point); +Point transmuteComplexIntoPoint(Complex cpx); + +Point operator*(Point& pt, double multiplier); + +#endif // POINT_H + diff --git a/tests/libsample/reference.cpp b/tests/libsample/reference.cpp new file mode 100644 index 000000000..fb3e78e49 --- /dev/null +++ b/tests/libsample/reference.cpp @@ -0,0 +1,11 @@ +#include <iostream> +#include "reference.h" + +using namespace std; + +void +Reference::show() const +{ + cout << "Reference.objId: " << m_objId; +} + diff --git a/tests/libsample/reference.h b/tests/libsample/reference.h new file mode 100644 index 000000000..bafd2492a --- /dev/null +++ b/tests/libsample/reference.h @@ -0,0 +1,24 @@ +#ifndef REFERENCE_H +#define REFERENCE_H + +class Reference +{ +public: + explicit Reference(int objId = -1) + : m_objId(objId) {} + ~Reference() {} + + double objId() { return m_objId; } + void setObjId(int objId) { m_objId = objId; } + + static int usesReference(Reference& r) { return r.m_objId; } + static int usesConstReference(const Reference& r) { return r.m_objId; } + + void show() const; + +private: + int m_objId; +}; + +#endif // REFERENCE_H + diff --git a/tests/libsample/samplenamespace.cpp b/tests/libsample/samplenamespace.cpp new file mode 100644 index 000000000..ebf2ff74d --- /dev/null +++ b/tests/libsample/samplenamespace.cpp @@ -0,0 +1,48 @@ +#include <iostream> +#include <cstdlib> +#include <time.h> +#include "samplenamespace.h" + +using namespace std; + +namespace SampleNamespace +{ + +OutValue +enumInEnumOut(InValue in) +{ + OutValue retval; + switch(in) { + case ZeroIn: + retval = ZeroOut; + break; + case OneIn: + retval = OneOut; + break; + case TwoIn: + retval = TwoOut; + break; + default: + retval = (OutValue) -1; + } + return retval; +} + +int +getNumber(Option opt) +{ + int retval; + switch(opt) { + case RandomNumber: + retval = rand() % 100; + break; + case UnixTime: + retval = (int) time(0); + break; + default: + retval = 0; + } + return retval; +} + +} // namespace SampleNamespace diff --git a/tests/libsample/samplenamespace.h b/tests/libsample/samplenamespace.h new file mode 100644 index 000000000..2c65aeab0 --- /dev/null +++ b/tests/libsample/samplenamespace.h @@ -0,0 +1,36 @@ +#ifndef SAMPLENAMESPACE_H +#define SAMPLENAMESPACE_H + +namespace SampleNamespace +{ + +enum Option { + None, + RandomNumber, + UnixTime +}; + +enum InValue { + ZeroIn, + OneIn, + TwoIn +}; + +enum OutValue { + ZeroOut, + OneOut, + TwoOut +}; + +OutValue enumInEnumOut(InValue in); + +int getNumber(Option opt); + +inline double powerOfTwo(double num) { + return num * num; +} + +} // namespace SampleNamespace + +#endif // SAMPLENAMESPACE_H + diff --git a/tests/libsample/size.cpp b/tests/libsample/size.cpp new file mode 100644 index 000000000..63af9c828 --- /dev/null +++ b/tests/libsample/size.cpp @@ -0,0 +1,11 @@ +#include <iostream> +#include "size.h" + +using namespace std; + +void +Size::show() const +{ + cout << "(width: " << m_width << ", height: " << m_height << ")"; +} + diff --git a/tests/libsample/size.h b/tests/libsample/size.h new file mode 100644 index 000000000..48f0d772a --- /dev/null +++ b/tests/libsample/size.h @@ -0,0 +1,152 @@ +#ifndef SIZE_H +#define SIZE_H + +class Size +{ +public: + Size(double width = 0.0, double height = 0.0) : m_width(width), m_height(height) {} + ~Size() {} + + double width() { return m_width; } + void setWidth(double width) { m_width = width; } + double height() { return m_height; } + void setHeight(double height) { m_height = height; } + + double calculateArea() const { return m_width * m_height; } + + // Comparison Operators + inline bool operator==(const Size& other) + { + return m_width == other.m_width && m_height == other.m_height; + } + + inline bool operator<(const Size& other) + { + return calculateArea() < other.calculateArea(); + } + + inline bool operator>(const Size& other) + { + return calculateArea() > other.calculateArea(); + } + + inline bool operator<=(const Size& other) + { + return calculateArea() <= other.calculateArea(); + } + + inline bool operator>=(const Size& other) + { + return calculateArea() >= other.calculateArea(); + } + + inline bool operator<(double area) { return calculateArea() < area; } + inline bool operator>(double area) { return calculateArea() > area; } + inline bool operator<=(double area) { return calculateArea() <= area; } + inline bool operator>=(double area) { return calculateArea() >= area; } + + // Arithmetic Operators + Size& operator+=(const Size& s) + { + m_width += s.m_width; + m_height += s.m_height; + return *this; + } + + Size& operator-=(const Size& s) + { + m_width -= s.m_width; + m_height -= s.m_height; + return *this; + } + + Size& operator*=(double mult) + { + m_width *= mult; + m_height *= mult; + return *this; + } + + Size& operator/=(double div) + { + m_width /= div; + m_height /= div; + return *this; + } + + // TODO: add ++size, size++, --size, size-- + + // External operators + friend inline bool operator!=(const Size&, const Size&); + friend inline const Size operator+(const Size&, const Size&); + friend inline const Size operator-(const Size&, const Size&); + friend inline const Size operator*(const Size&, double); + friend inline const Size operator*(double, const Size&); + friend inline const Size operator/(const Size&, double); + + friend inline bool operator<(double, const Size&); + friend inline bool operator>(double, const Size&); + friend inline bool operator<=(double, const Size&); + friend inline bool operator>=(double, const Size&); + + void show() const; + +private: + double m_width; + double m_height; +}; + +// Comparison Operators +inline bool operator!=(const Size& s1, const Size& s2) +{ + return s1.m_width != s2.m_width || s1.m_height != s2.m_height; +} + +inline bool operator<(double area, const Size& s) +{ + return area < s.calculateArea(); +} + +inline bool operator>(double area, const Size& s) +{ + return area > s.calculateArea(); +} + +inline bool operator<=(double area, const Size& s) +{ + return area <= s.calculateArea(); +} + +inline bool operator>=(double area, const Size& s) +{ + return area >= s.calculateArea(); +} + +// Arithmetic Operators +inline const Size operator+(const Size& s1, const Size& s2) +{ + return Size(s1.m_width + s2.m_width, s1.m_height + s2.m_height); +} + +inline const Size operator-(const Size& s1, const Size& s2) +{ + return Size(s1.m_width - s2.m_width, s1.m_height - s2.m_height); +} + +inline const Size operator*(const Size& s, double mult) +{ + return Size(s.m_width * mult, s.m_height * mult); +} + +inline const Size operator*(double mult, const Size& s) +{ + return Size(s.m_width * mult, s.m_height * mult); +} + +inline const Size operator/(const Size& s, double div) +{ + return Size(s.m_width / div, s.m_height / div); +} + +#endif // SIZE_H + |