diff options
author | valmat <ufabiz@gmail.com> | 2014-06-11 19:34:40 +0600 |
---|---|---|
committer | valmat <ufabiz@gmail.com> | 2014-06-11 19:34:40 +0600 |
commit | 4f3769855dd65a8ef0b647c7e98860b8aa747c26 (patch) | |
tree | be9c261e7b6284c77a5ef19d125b25f2b37f4dbd | |
parent | e350c8f1b33d7c6c605f5450e8cf901cc3e5e1c4 (diff) |
done
-rw-r--r-- | include/value.h | 6 | ||||
-rw-r--r-- | tests/cpp/h/variables.h | 1 | ||||
-rw-r--r-- | tests/cpp/include/variables/028-029-compare.h | 194 | ||||
-rw-r--r-- | tests/cpp/main.cpp | 3 | ||||
-rw-r--r-- | tests/php/phpt/variables/028-compare1.phpt | 53 | ||||
-rw-r--r-- | tests/php/phpt/variables/029-compare2.phpt | 74 | ||||
-rw-r--r-- | zend/value.cpp | 128 |
7 files changed, 335 insertions, 124 deletions
diff --git a/include/value.h b/include/value.h index 307702e..e8337a0 100644 --- a/include/value.h +++ b/include/value.h @@ -322,12 +322,6 @@ public: bool operator> (const char *value) const { return ::strcmp(rawValue(), value) > 0; } /** - * Helper method to prepare for comparison - * @param value - */ - bool comparePrepare(const Value &value) const; - - /** * Comparison operators for hardcoded Value * @param value */ diff --git a/tests/cpp/h/variables.h b/tests/cpp/h/variables.h index 8a074cd..0b684dd 100644 --- a/tests/cpp/h/variables.h +++ b/tests/cpp/h/variables.h @@ -27,6 +27,7 @@ #include "../include/variables/025-post-raw1.h" #include "../include/variables/026-post-raw2.h" #include "../include/variables/027-env.h" +#include "../include/variables/028-029-compare.h" //#include "../include/variables/.h" diff --git a/tests/cpp/include/variables/028-029-compare.h b/tests/cpp/include/variables/028-029-compare.h new file mode 100644 index 0000000..bd95298 --- /dev/null +++ b/tests/cpp/include/variables/028-029-compare.h @@ -0,0 +1,194 @@ +/** + * + * Test variables + * phptname.phpt + * + */ + + + + +/** + * Set up namespace + */ +namespace TestVariables { + + /* + * Test bool Value::operator==(const Value &value) const + */ + void test_compare1() + { + Php::Value v1(5), v2(5.0), v3("5"), v4("5.0"); + + Php::out << "true:" << std::endl; + Php::out << (v1 == v2) << std::endl; + Php::out << (v1 == v3) << std::endl; + Php::out << (v1 == v4) << std::endl; + Php::out << (v2 == v1) << std::endl; + Php::out << (v2 == v3) << std::endl; + Php::out << (v2 == v4) << std::endl; + Php::out << (v3 == v1) << std::endl; + Php::out << (v3 == v2) << std::endl; + Php::out << (v3 == v4) << std::endl; + Php::out << (v4 == v1) << std::endl; + Php::out << (v4 == v2) << std::endl; + Php::out << (v4 == v3) << std::endl; + + Php::Value v5(6), v6(6.0), v7("6"), v8("6.0"); + + Php::out << "false:" << std::endl; + Php::out << (v1 == v5) << std::endl; + Php::out << (v1 == v6) << std::endl; + Php::out << (v1 == v7) << std::endl; + Php::out << (v1 == v8) << std::endl; + + Php::out << (v2 == v5) << std::endl; + Php::out << (v2 == v6) << std::endl; + Php::out << (v2 == v7) << std::endl; + Php::out << (v2 == v8) << std::endl; + + Php::out << (v3 == v5) << std::endl; + Php::out << (v3 == v6) << std::endl; + Php::out << (v3 == v7) << std::endl; + Php::out << (v3 == v8) << std::endl; + + Php::out << (v4 == v5) << std::endl; + Php::out << (v4 == v6) << std::endl; + Php::out << (v4 == v7) << std::endl; + Php::out << (v4 == v8) << std::endl; + + Php::Value v9, v10, v11, v12; + v9[0] = 5; + v9[1] = 6; + + v10[0] = 5; + v10[1] = "Hello!"; + + v11[0] = 5; + v11[1] = 6; + + v12[0] = 5; + + Php::out << "Compare array:" << std::endl; + Php::out << (v1 == v9) << std::endl; + Php::out << (v5 == v9) << std::endl; + Php::out << (v9 == v10) << std::endl; + Php::out << (v11 == v9) << std::endl; + Php::out << (v12 == v9) << std::endl; + + Php::Value v13 = false, v14, v15 = 0; + Php::out << "Compare NULL:" << std::endl; + Php::out << (v1 == v13) << std::endl; + Php::out << (v1 == v14) << std::endl; + Php::out << (v1 == v15) << std::endl; + + Php::out << (v13 == v14) << std::endl; + Php::out << (v13 == v15) << std::endl; + Php::out << (v14 == v15) << std::endl; + } + + /* + * Test bool Value::operator< (const Value &value) const + */ + void test_compare2() + { + Php::Value v1(5), v2(5.0), v3("5"), v4("5.0"); + + Php::out << "false:" << std::endl; + Php::out << (v1 < v2) << std::endl; + Php::out << (v1 < v3) << std::endl; + Php::out << (v1 < v4) << std::endl; + Php::out << (v2 < v1) << std::endl; + Php::out << (v2 < v3) << std::endl; + Php::out << (v2 < v4) << std::endl; + Php::out << (v3 < v1) << std::endl; + Php::out << (v3 < v2) << std::endl; + Php::out << (v3 < v4) << std::endl; + Php::out << (v4 < v1) << std::endl; + Php::out << (v4 < v2) << std::endl; + Php::out << (v4 < v3) << std::endl; + + Php::Value v5(6), v6(6.0), v7("6"), v8("6.0"); + + Php::out << "true:" << std::endl; + Php::out << (v1 < v5) << std::endl; + Php::out << (v1 < v6) << std::endl; + Php::out << (v1 < v7) << std::endl; + Php::out << (v1 < v8) << std::endl; + + Php::out << (v2 < v5) << std::endl; + Php::out << (v2 < v6) << std::endl; + Php::out << (v2 < v7) << std::endl; + Php::out << (v2 < v8) << std::endl; + + Php::out << (v3 < v5) << std::endl; + Php::out << (v3 < v6) << std::endl; + Php::out << (v3 < v7) << std::endl; + Php::out << (v3 < v8) << std::endl; + + Php::out << (v4 < v5) << std::endl; + Php::out << (v4 < v6) << std::endl; + Php::out << (v4 < v7) << std::endl; + Php::out << (v4 < v8) << std::endl; + + Php::out << "false:" << std::endl; + Php::out << (v1 > v5) << std::endl; + Php::out << (v1 > v6) << std::endl; + Php::out << (v1 > v7) << std::endl; + Php::out << (v1 > v8) << std::endl; + + Php::out << (v2 > v5) << std::endl; + Php::out << (v2 > v6) << std::endl; + Php::out << (v2 > v7) << std::endl; + Php::out << (v2 > v8) << std::endl; + + Php::out << (v3 > v5) << std::endl; + Php::out << (v3 > v6) << std::endl; + Php::out << (v3 > v7) << std::endl; + Php::out << (v3 > v8) << std::endl; + + Php::out << (v4 > v5) << std::endl; + Php::out << (v4 > v6) << std::endl; + Php::out << (v4 > v7) << std::endl; + Php::out << (v4 > v8) << std::endl; + + Php::Value v9, v10, v11, v12; + v9[0] = 5; + v9[1] = 6; + + v10[0] = 5; + v10[1] = "Hello!"; + + v11[0] = 5; + v11[1] = 6; + + v12[0] = 5; + + Php::out << "Compare array:" << std::endl; + Php::out << (v1 < v9) << std::endl; + Php::out << (v5 < v9) << std::endl; + Php::out << (v9 < v10) << std::endl; + Php::out << (v9 > v10) << std::endl; + Php::out << (v11 < v9) << std::endl; + Php::out << (v12 < v9) << std::endl; + + Php::Value v13 = false, v14, v15 = 0; + Php::out << "Compare NULL:" << std::endl; + Php::out << (v1 < v13) << std::endl; + Php::out << (v1 < v14) << std::endl; + Php::out << (v1 < v15) << std::endl; + + Php::out << (v1 > v13) << std::endl; + Php::out << (v1 > v14) << std::endl; + Php::out << (v1 > v15) << std::endl; + + Php::out << (v13 < v14) << std::endl; + Php::out << (v13 < v15) << std::endl; + Php::out << (v14 < v15) << std::endl; + } + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index 37ba5ed..47cd90d 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -125,6 +125,9 @@ extern "C" extension.add("TestVariables\\post_raw1", TestVariables::post_raw1); extension.add("TestVariables\\post_raw2", TestVariables::post_raw2); extension.add("TestVariables\\test_env", TestVariables::test_env); + extension.add("TestVariables\\test_compare1", TestVariables::test_compare1); + extension.add("TestVariables\\test_compare2", TestVariables::test_compare2); + diff --git a/tests/php/phpt/variables/028-compare1.phpt b/tests/php/phpt/variables/028-compare1.phpt new file mode 100644 index 0000000..2885fe8 --- /dev/null +++ b/tests/php/phpt/variables/028-compare1.phpt @@ -0,0 +1,53 @@ +--TEST-- +Test bool Value::operator==(const Value &value) const +--SKIPIF-- +<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?> +--FILEEOF-- +<?php + +TestVariables\test_compare1(); + +--EXPECT-- +true: +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +false: +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +Compare array: +0 +0 +0 +1 +0 +Compare NULL: +0 +0 +0 +1 +1 +1
\ No newline at end of file diff --git a/tests/php/phpt/variables/029-compare2.phpt b/tests/php/phpt/variables/029-compare2.phpt new file mode 100644 index 0000000..333ede1 --- /dev/null +++ b/tests/php/phpt/variables/029-compare2.phpt @@ -0,0 +1,74 @@ +--TEST-- +Test bool Value::operator< (const Value &value) const +--SKIPIF-- +<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?> +--FILEEOF-- +<?php + +TestVariables\test_compare2(); + +--EXPECT-- +false: +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +true: +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +false: +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +Compare array: +1 +1 +0 +1 +0 +1 +Compare NULL: +0 +0 +0 +1 +1 +1 +0 +0 +0
\ No newline at end of file diff --git a/zend/value.cpp b/zend/value.cpp index e1f6683..fe31493 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -1312,116 +1312,18 @@ Value Value::exec(const char *name, int argc, struct _zval_struct ***params) } /** - * Helper method to prepare for comparison - * @param value - */ -bool Value::comparePrepare(const Value &value) const -{ - Type thisTp = type(), thatTp = value.type(); - if(thisTp == Type::Resource || thatTp == Type::Resource) - { - throw Php::Exception("Resource types is not comparable"); - return false; - } - if(thisTp == Type::Constant || thatTp == Type::Constant || thisTp == Type::ConstantArray || thatTp == Type::ConstantArray) - { - throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); - return false; - } - if(thisTp == Type::Callable || thatTp == Type::Callable) - { - throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); - return false; - } - return true; -} - -/** * Comparison operators== for hardcoded Value * @param value */ bool Value::operator==(const Value &value) const { - if(!comparePrepare(value)) return false; - - Type thisTp = type(), thatTp = value.type(); - - //Type tp = type(); - //const _zval_struct *thatval = (tp == value.type()) ? value._val : value.clone(tp)._val; - - if( (thatTp == Type::Array && thisTp != Type::Array) || (thisTp == Type::Array && thatTp != Type::Array) ) - { - return false; - } - if(thatTp == Type::Array && thisTp == Type::Array) - { - if(size() != value.size()) return false; - auto thisIt = begin(), thatIt = value.begin(), thisEnd = end();; - while(thisIt != thisEnd) - { - if(*thisIt != *thatIt) return false; - ++thisIt; - ++thatIt; - } - return true; - } - - if(thatTp == Type::Null) - { - return (thisTp == Type::Null) || - ( - (thisTp == Type::Numeric || thisTp == Type::Bool) && - _val->value.lval == 0 - ) || - ( (thisTp == Type::String) && _val->value.str.len == 0); - } - if(thisTp == Type::Null) - { - return ( - (thatTp == Type::Numeric || thatTp == Type::Bool) && - value._val->value.lval == 0 - ) || - ( (thatTp == Type::String) && value._val->value.str.len == 0); - } - - if(thatTp == Type::Float) - { - return clone(Type::Float) == value._val->value.dval; - } - if(thisTp == Type::Float) - { - return value.clone(Type::Float) == _val->value.dval; - } - - if(thatTp == Type::String) - { - return clone(Type::String) == value.rawValue(); - } - if(thisTp == Type::String) - { - return value.clone(Type::String) == rawValue(); - } - - if(thatTp == Type::Numeric || thatTp == Type::Bool) - { - return clone(thatTp) == value._val->value.lval; - } - if(thisTp == Type::String) - { - return value.clone(thisTp) == _val->value.lval; - } - - - if( (thatTp == Type::Object && thisTp != Type::Object) || (thisTp == Type::Object && thatTp != Type::Object) ) + zval result; + if(SUCCESS != compare_function(&result, _val, value._val TSRMLS_CC) ) { + throw Php::Exception("Not comparable"); return false; } - if(thatTp == Type::Object && thisTp == Type::Object) - { - return !ClassImpl::compare(_val, value._val TSRMLS_CC); - } - - return false; + return (0 == result.value.lval); } /** @@ -1430,23 +1332,13 @@ bool Value::operator==(const Value &value) const */ bool Value::operator< (const Value &value) const { - if(!comparePrepare(value)) return false; - - Type tp = type(); - const _zval_struct *thatval = (tp == value.type()) ? value._val : value.clone(tp)._val; - - switch (tp) { - case Type::Null: return false; - case Type::Numeric: return (_val->value.lval < thatval->value.lval); - case Type::Float: return (_val->value.dval < thatval->value.dval); - case Type::Bool: return (_val->value.lval < thatval->value.lval); - case Type::String: return (::strcmp(_val->value.str.val, thatval->value.str.val) < 0); - // @todo - case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; - // @todo - case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; + zval result; + if(SUCCESS != compare_function(&result, _val, value._val TSRMLS_CC) ) + { + throw Php::Exception("Not comparable"); + return false; } - return true; + return (-1 == result.value.lval); } /** |