diff options
author | valmat <ufabiz@gmail.com> | 2014-03-28 00:42:09 +0600 |
---|---|---|
committer | valmat <ufabiz@gmail.com> | 2014-03-28 00:42:09 +0600 |
commit | 28152192b85cbe9bf5ea08ebe8c706d12e2e13e5 (patch) | |
tree | d8a5cbee7e39f6b21c0e698d1ac10ac764b00193 /tests | |
parent | f56cff9e4f67a949e6f0ae35ca63aaaea16d9a41 (diff) |
add test: class_obj/003-comparable.phpt
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cpp/include/Classes_and_objects.h (renamed from tests/cpp/include/MyCustomClass.h) | 62 | ||||
-rw-r--r-- | tests/cpp/main.cpp | 15 | ||||
-rw-r--r-- | tests/php/dbg.php | 34 | ||||
-rw-r--r-- | tests/php/phpt/class_obj/001.phpt (renamed from tests/php/phpt/base/001.phpt) | 0 | ||||
-rw-r--r-- | tests/php/phpt/class_obj/002.phpt (renamed from tests/php/phpt/base/002.phpt) | 0 | ||||
-rw-r--r-- | tests/php/phpt/class_obj/003-comparable.phpt | 46 | ||||
-rw-r--r-- | tests/php/phpt/class_obj/readme (renamed from tests/php/phpt/base/readme) | 0 |
7 files changed, 151 insertions, 6 deletions
diff --git a/tests/cpp/include/MyCustomClass.h b/tests/cpp/include/Classes_and_objects.h index 02ff44b..e9b7434 100644 --- a/tests/cpp/include/MyCustomClass.h +++ b/tests/cpp/include/Classes_and_objects.h @@ -1,7 +1,7 @@ /** * * - * + * Classes_and_objects.h * */ @@ -12,10 +12,6 @@ namespace TestBaseClass { -/** - * Namespace to use - */ - using namespace std; class MyCustomClass : public Php::Base, public Php::Countable { @@ -61,6 +57,62 @@ namespace TestBaseClass { }; + /** + * custom comparison operator + */ + class Comparable : public Php::Base + { + private: + /** + * Internal value of the class + * @var int + */ + static int count; + int _nom; + int _value; + + public: + /** + * C++ constructor + */ + Comparable() + { + // start with random value + //_value = rand(); + _nom = ++count; + _value = _nom%2+1; + } + + /** + * C++ destructor + */ + virtual ~Comparable() {} + + /** + * Cast the object to a string + * @return std::string + */ + std::string __toString() + { + return "Obj#" + std::to_string(_nom) + "(" + std::to_string(_value) + ")"; + } + + /** + * Compare with a different object + * @param that + * @return int + */ + int __compare(const Comparable &that) const + { + return _value - that._value; + } + }; + int Comparable::count = 0; + + + + + /** * End of namespace diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index e5150ed..7569a0b 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -9,7 +9,7 @@ // Test includes #include "include/ValueIterator.h" -#include "include/MyCustomClass.h" +#include "include/Classes_and_objects.h" #include "include/variables.h" @@ -33,7 +33,14 @@ extern "C" // add the interface to the extension //extension.add(interface); + + + + /** + * Classes and objects + * + */ // we are going to define a class Php::Class<TestBaseClass::MyCustomClass> customClass("TestBaseClass\\MyClass"); @@ -45,6 +52,12 @@ extern "C" // add the class to the extension extension.add(customClass); + // Comparable + extension.add( Php::Class<TestBaseClass::Comparable>("TestBaseClass\\Comparable") ); + + + + /** * tests for Iterators * diff --git a/tests/php/dbg.php b/tests/php/dbg.php index 2040f01..b9841c7 100644 --- a/tests/php/dbg.php +++ b/tests/php/dbg.php @@ -12,3 +12,37 @@ +//- + +// initialize a couple of objects +$object1 = new TestBaseClass\Comparable(); +$object2 = new TestBaseClass\Comparable(); +$object3 = new TestBaseClass\Comparable(); + +// compare the objects +if ($object1 < $object2) +{ + echo("$object1 is smaller than $object2\n"); +} +else +{ + echo("$object1 is bigger than $object2\n"); +} + +if ($object1 == $object3) +{ + echo("$object1 is equal to $object3\n"); +} +else +{ + echo("$object1 is not equal to $object3\n"); +} + +if ($object1 != $object2) +{ + echo("$object1 is not equal to $object2\n"); +} +else +{ + echo("$object1 is equal to $object2\n"); +}
\ No newline at end of file diff --git a/tests/php/phpt/base/001.phpt b/tests/php/phpt/class_obj/001.phpt index b22a44c..b22a44c 100644 --- a/tests/php/phpt/base/001.phpt +++ b/tests/php/phpt/class_obj/001.phpt diff --git a/tests/php/phpt/base/002.phpt b/tests/php/phpt/class_obj/002.phpt index 847e602..847e602 100644 --- a/tests/php/phpt/base/002.phpt +++ b/tests/php/phpt/class_obj/002.phpt diff --git a/tests/php/phpt/class_obj/003-comparable.phpt b/tests/php/phpt/class_obj/003-comparable.phpt new file mode 100644 index 0000000..8ffc076 --- /dev/null +++ b/tests/php/phpt/class_obj/003-comparable.phpt @@ -0,0 +1,46 @@ +--TEST-- +Test constructor & destructor +--SKIPIF-- +<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?> +--FILEEOF-- +<?php + +// initialize a couple of objects +$object1 = new TestBaseClass\Comparable(); +$object2 = new TestBaseClass\Comparable(); +$object3 = new TestBaseClass\Comparable(); + +// compare the objects +if ($object1 < $object2) +{ + echo("$object1 is smaller than $object2\n"); +} +else +{ + echo("$object1 is bigger than $object2\n"); +} + +if ($object1 == $object3) +{ + echo("$object1 is equal to $object3\n"); +} +else +{ + echo("$object1 is not equal to $object3\n"); +} + +if ($object1 != $object2) +{ + echo("$object1 is not equal to $object2\n"); +} +else +{ + echo("$object1 is equal to $object2\n"); +} + + +//echo PHP_EOL; +--EXPECT-- +Obj#1(2) is bigger than Obj#2(1) +Obj#1(2) is equal to Obj#3(2) +Obj#1(2) is not equal to Obj#2(1)
\ No newline at end of file diff --git a/tests/php/phpt/base/readme b/tests/php/phpt/class_obj/readme index 6c89a94..6c89a94 100644 --- a/tests/php/phpt/base/readme +++ b/tests/php/phpt/class_obj/readme |