php-sqlsrv/test/functional/sqlsrv/sqlsrv_param_floats.phpt

102 lines
2.2 KiB
Plaintext
Raw Normal View History

2017-02-24 06:40:51 +01:00
--TEST--
Test insertion with floats
2017-11-29 23:40:47 +01:00
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
2017-02-24 06:40:51 +01:00
--FILE--
<?php
require_once('MsCommon.inc');
2017-02-24 06:40:51 +01:00
2017-11-29 23:40:47 +01:00
function execData($withParams)
2017-02-24 06:40:51 +01:00
{
set_time_limit(0);
sqlsrv_configure('WarningsReturnAsErrors', 1);
// connect
2017-11-29 23:40:47 +01:00
$conn = AE\connect();
$tableName = 'param_floats';
$columns = array(new AE\ColumnMeta('float', 'c1_float'),
new AE\ColumnMeta('real', 'c2_real'));
$stmt = AE\createTable($conn, $tableName, $columns);
if (!$stmt) {
fatalError("Failed to create table $tableName\n");
}
2017-11-29 23:40:47 +01:00
if ($withParams) {
2017-02-24 06:40:51 +01:00
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_float, c2_real) VALUES (?, ?)", array(array(&$v1, SQLSRV_PARAM_IN), array(&$v2, SQLSRV_PARAM_IN)));
} else {
$stmt = sqlsrv_prepare($conn, "INSERT INTO $tableName (c1_float, c2_real) VALUES (?, ?)", array(&$v1, &$v2));
2017-02-24 06:40:51 +01:00
}
2017-02-24 06:40:51 +01:00
$values = array();
$v1 = 1.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v1);
$v2 = 2.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v2);
sqlsrv_execute($stmt);
$v1 = 11.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v1);
$v2 = 12.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v2);
sqlsrv_execute($stmt);
$v1 = 21.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v1);
$v2 = 22.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v2);
sqlsrv_execute($stmt);
$v1 = 31.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v1);
$v2 = 32.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v2);
sqlsrv_execute($stmt);
$v1 = 41.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v1);
$v2 = 42.0;
2017-02-24 06:40:51 +01:00
array_push($values, $v2);
sqlsrv_execute($stmt);
sqlsrv_free_stmt($stmt);
2017-02-24 06:40:51 +01:00
$idx = 0;
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
$epsilon = 0.00001;
while ($result = sqlsrv_fetch($stmt)) {
for ($i = 0; $i < 2; $i++) {
$value = sqlsrv_get_field($stmt, $i);
2017-02-24 06:40:51 +01:00
$expected = $values[$idx++];
$diff = abs(($value - $expected) / $expected);
if ($diff > $epsilon) {
echo "Value $value is unexpected\n";
2017-02-24 06:40:51 +01:00
}
}
}
sqlsrv_free_stmt($stmt);
2017-11-29 23:40:47 +01:00
dropTable($conn, $tableName);
sqlsrv_close($conn);
2017-02-24 06:40:51 +01:00
}
2017-11-29 23:40:47 +01:00
echo "\nTest begins...\n";
2017-02-24 06:40:51 +01:00
2017-11-29 23:40:47 +01:00
try {
execData(true);
execData(false);
} catch (Exception $e) {
echo $e->getMessage();
}
echo "\nDone\n";
2017-02-24 06:40:51 +01:00
?>
--EXPECT--

Test begins...
2017-02-24 06:40:51 +01:00
Done