summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorvalmat <ufabiz@gmail.com>2014-03-27 22:44:38 +0600
committervalmat <ufabiz@gmail.com>2014-03-27 22:44:38 +0600
commit465f4b7b72a17102e91f05f5d36d024b73e3787c (patch)
treecb848df412fdf544dd2f96232ab5eefb83927814 /tests
parentc0e1f20eeab6445d6355ea11ef7d264cb37c52cf (diff)
Fix test value-casting-operators
Diffstat (limited to 'tests')
-rw-r--r--tests/cpp/include/doubl2str.h102
-rw-r--r--tests/cpp/include/variables.h36
-rw-r--r--tests/cpp/main.cpp5
-rw-r--r--tests/php/phpt/variables/011-value-casting-operators.phpt (renamed from tests/php/phpt/variables/005-value-casting-operators.phpt-)26
-rw-r--r--tests/php/phpt/variables/012-value-casting-operators-double.phpt80
5 files changed, 214 insertions, 35 deletions
diff --git a/tests/cpp/include/doubl2str.h b/tests/cpp/include/doubl2str.h
new file mode 100644
index 0000000..1a5e3fc
--- /dev/null
+++ b/tests/cpp/include/doubl2str.h
@@ -0,0 +1,102 @@
+/**
+ *
+ * double -> string
+ *
+ */
+
+
+
+char num2char(unsigned int num)
+{
+ switch(num)
+ {
+ case 0:
+ return '0';
+ case 1:
+ return '1';
+ case 2:
+ return '2';
+ case 3:
+ return '3';
+ case 4:
+ return '4';
+ case 5:
+ return '5';
+ case 6:
+ return '6';
+ case 7:
+ return '7';
+ case 8:
+ return '8';
+ case 9:
+ return '9';
+ }
+
+ //return '\0';
+ return '-';
+}
+
+std::string double2str(long double D)
+{
+ int sign = (D > 0) ? 1 : (D*=-1.0, -1);
+ unsigned long long int Ceil = D;
+
+ // if D is ceil
+ if(Ceil == D)
+ {
+ return std::to_string( (long long)(D*sign) );
+ }
+
+ // size of result buffer
+ const int bs = 32;
+
+ // size of temporary buffer
+ const int pw = 16;
+ // Temporary buffer
+ char buf[pw];
+ // Result buffer
+ char rez[bs];
+
+ int i, size = 0;
+ // set sign
+ if(sign < 0) rez[size++] = '-';
+
+ // set ceil
+ std::string sceil = std::to_string(Ceil);
+ const char * bceil = sceil.c_str();
+ int sceillen = sceil.size();
+ for(i = 0; i < sceillen; i++)
+ {
+ rez[size++] = bceil[i];
+ }
+
+ // set point
+ rez[size++] = '.';
+
+ unsigned long long int I = D * 10000000000000000; // D * 10**pw
+ // .14159265359 -> 14159265359000000
+ I -= Ceil * 10000000000000000;
+
+ // Remove the tail of zeros
+ // 14159265359000000 -> 14159265359
+ while(0 == I % 10) I /= 10;
+
+ int ind = 0;
+ while(I > 0)
+ {
+ buf[ind++] = num2char(I%10);
+ I = (I - I%10) / 10;
+ }
+
+ // set fraction part
+ for(i = 0; i < ind; i++)
+ {
+ rez[size] = buf[ind-i-1];
+ size++;
+ }
+
+ return std::string(rez, size);
+ //rez[size] = '\0';
+}
+
+
diff --git a/tests/cpp/include/variables.h b/tests/cpp/include/variables.h
index f242653..b7063ef 100644
--- a/tests/cpp/include/variables.h
+++ b/tests/cpp/include/variables.h
@@ -4,6 +4,8 @@
*
*/
+ #include "doubl2str.h"
+
/**
* Set up namespace
*/
@@ -120,20 +122,36 @@ namespace TestVariables {
return r;
}
+
/*
- * Test Php::Value casting operators
- */
+ * Test Php::Value casting operators
+ */
void value_casting(Php::Parameters &params)
{
- Php::Value value = params[0];
+ Php::Value value = params[0];
- long value1 = value;
- std::string value2 = value;
- //long double value3 = value; // <------------ error: conversion from ‘Php::Value’ to ‘long double’ is ambiguous
- double value3 = value;
- bool value4 = value;
+ long value1 = value;
+ std::string value2 = value;
+ bool value4 = value;
- Php::out << " long:" << value1 << "\n string:" << value2 << "\n double:" << value3 << "\n bool:" << bool2str(value4) << std::endl;
+ Php::out << " long:" << value1 << "\n string:" << value2 << "\n bool:" << bool2str(value4) << std::endl;
+ }
+
+
+ /*
+ * Test Php::Value casting operators
+ */
+ void value_cast2double(Php::Parameters &params)
+ {
+ Php::Value value = params[0];
+ double value3 = value;
+
+ /*
+ * The remark (from valmat).
+ * Somehow std::to_string truncates the tail of numbers of type `double` when converting it to a string.
+ * So I wrote my own function `double2str()`, which does not have this drawback.
+ */
+ Php::out << double2str(value3) << std::endl;
}
/*
diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp
index 2928f60..6b1502b 100644
--- a/tests/cpp/main.cpp
+++ b/tests/cpp/main.cpp
@@ -63,11 +63,12 @@ extern "C"
extension.add("TestVariables\\value_types", TestVariables::value_types);
extension.add("TestVariables\\scalar_store", TestVariables::scalar_store);
extension.add("TestVariables\\value_casting", TestVariables::value_casting);
+ extension.add("TestVariables\\value_cast2double", TestVariables::value_cast2double);
extension.add("TestVariables\\value_cast2str", TestVariables::value_cast2str);
extension.add("TestVariables\\overloaded_op", TestVariables::overloaded_op);
extension.add("TestVariables\\value_arrays", TestVariables::value_arrays);
- extension.add("TestVariables\\value_object1", TestVariables::value_object1);
- extension.add("TestVariables\\value_object2", TestVariables::value_object2);
+ extension.add("TestVariables\\value_object1", TestVariables::value_object1);
+ extension.add("TestVariables\\value_object2", TestVariables::value_object2);
diff --git a/tests/php/phpt/variables/005-value-casting-operators.phpt- b/tests/php/phpt/variables/011-value-casting-operators.phpt
index 5e0fb8e..3fe6817 100644
--- a/tests/php/phpt/variables/005-value-casting-operators.phpt-
+++ b/tests/php/phpt/variables/011-value-casting-operators.phpt
@@ -3,13 +3,9 @@ Test Php::Value casting operators
--DESCRIPTION--
The Php::Value class has casting operators to cast the object to almost every thinkable native type.
-
-It seems need to replace double on long double
-
native_value_casting - repeats the behavior of c++ functions TestVariables\value_casting
the output of each of these functions should be the same
-
--SKIPIF--
<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?>
--FILEEOF--
@@ -20,7 +16,8 @@ function bool2str($b) {
return $b ? "Yes" : "No";
}
function native_value_casting($value) {
- echo " long:", (int)$value, "\n string:", (string)$value, "\n double:", (double)$value, "\n bool:", bool2str((bool)$value), PHP_EOL;
+ //echo " long:", (int)$value, "\n string:", (string)$value, "\n double:", (double)$value, "\n bool:", bool2str((bool)$value), PHP_EOL;
+ echo " long:", (int)$value, "\n string:", (string)$value, "\n bool:", bool2str((bool)$value), PHP_EOL;
}
@@ -61,114 +58,95 @@ out(0x1A); // hexadecimal number (equivalent to 26 decimal)
Test 5555555555555555:
long:5555555555555555
string:5555555555555555
- double:5.5555555555556E+15
bool:Yes
Test '9223372036854775807':
long:9223372036854775807
string:9223372036854775807
- double:9.2233720368548E+18
bool:Yes
Test 9223372036854775807:
long:9223372036854775807
string:9223372036854775807
- double:9.2233720368548E+18
bool:Yes
Test 5:
long:5
string:5
- double:5
bool:Yes
Test -99999999:
long:-99999999
string:-99999999
- double:-99999999
bool:Yes
Test '18':
long:18
string:18
- double:18
bool:Yes
Test '3.14159265359':
long:3
string:3.14159265359
- double:3.14159265359
bool:Yes
Test 3.14159265359:
long:3
string:3.14159265359
- double:3.14159265359
bool:Yes
Test 3.5714285714286:
long:3
string:3.5714285714286
- double:3.5714285714286
bool:Yes
Test ' this is a string ':
long:0
string: this is a string
- double:0
bool:Yes
Test ' 2-nd string ':
long:2
string: 2-nd string
- double:2
bool:Yes
Test 'false':
long:0
string:false
- double:0
bool:Yes
Test false:
long:0
string:
- double:0
bool:No
Test 'true':
long:0
string:true
- double:0
bool:Yes
Test true:
long:1
string:1
- double:1
bool:Yes
Test 0:
long:0
string:0
- double:0
bool:No
Test '0':
long:0
string:0
- double:0
bool:No
Test 83:
long:83
string:83
- double:83
bool:Yes
Test 26:
long:26
string:26
- double:26
bool:Yes
diff --git a/tests/php/phpt/variables/012-value-casting-operators-double.phpt b/tests/php/phpt/variables/012-value-casting-operators-double.phpt
new file mode 100644
index 0000000..32086ef
--- /dev/null
+++ b/tests/php/phpt/variables/012-value-casting-operators-double.phpt
@@ -0,0 +1,80 @@
+--TEST--
+Test Php::Value casting operators (double)
+--DESCRIPTION--
+The Php::Value class has casting operators to cast the object to almost every thinkable native type.
+
+native_value_casting - repeats the behavior of c++ functions TestVariables\value_casting
+the output of each of these functions should be the same
+--SKIPIF--
+<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?>
+--FILEEOF--
+<?php
+
+
+
+function out($v) {
+ echo 'Test ';
+ var_export($v);
+ echo ':', PHP_EOL;
+ TestVariables\value_cast2double($v);
+ //echo (double)$v, PHP_EOL;;
+ //echo PHP_EOL;
+}
+
+out(5555555555555555);
+out("999999999999999");
+out(999999999999999);
+out(5);
+out(-99999999);
+out("18");
+out("3.14159265359");
+out(3.14159265359);
+out(" this is a string ");
+out(" 2-nd string ");
+out("false");
+out(false);
+out("true");
+out(true);
+out(0);
+out("0");
+out(0123); // octal number (equivalent to 83 decimal)
+out(0x1A); // hexadecimal number (equivalent to 26 decimal)
+
+
+--EXPECT--
+Test 5555555555555555:
+5555555555555555
+Test '999999999999999':
+999999999999999
+Test 999999999999999:
+999999999999999
+Test 5:
+5
+Test -99999999:
+-99999999
+Test '18':
+18
+Test '3.14159265359':
+3.14159265359
+Test 3.14159265359:
+3.14159265359
+Test ' this is a string ':
+0
+Test ' 2-nd string ':
+2
+Test 'false':
+0
+Test false:
+0
+Test 'true':
+0
+Test true:
+1
+Test 0:
+0
+Test '0':
+0
+Test 83:
+83
+Test 26:
+26 \ No newline at end of file