mysqli_fetch_assoc_bit.phpt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. --TEST--
  2. mysqli_fetch_assoc() - BIT
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifemb.inc');
  7. require_once('skipifconnectfailure.inc');
  8. require_once('connect.inc');
  9. require_once('table.inc');
  10. if (mysqli_get_server_version($link) < 50003)
  11. // b'001' syntax not supported before 5.0.3
  12. die("skip Syntax used for test not supported with MySQL Server before 5.0.3");
  13. if (!$IS_MYSQLND && (mysqli_get_client_version() < 50003))
  14. // better don't trust libmysql before 5.0.3
  15. die("skip Syntax used for test not supported with MySQL Server before 5.0.3");
  16. ?>
  17. --FILE--
  18. <?php
  19. require('connect.inc');
  20. function dec32bin($dec, $bits) {
  21. $maxval = pow(2, $bits);
  22. $bin = '';
  23. for ($bitval = $maxval; $bitval >= 1; $bitval = $bitval / 2) {
  24. if (($dec / $bitval) >= 1) {
  25. $bin .= '1';
  26. $dec -= $bitval;
  27. } else {
  28. $bin .= '0';
  29. }
  30. }
  31. return $bin;
  32. }
  33. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  34. printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  35. $host, $user, $db, $port, $socket);
  36. for ($bits = 1; $bits <= 46; $bits++) {
  37. if (1 == $bits)
  38. $max_value = 1;
  39. else
  40. $max_value = pow(2, $bits) - 1;
  41. $tests = 0;
  42. if (!mysqli_query($link, "DROP TABLE IF EXISTS test") ||
  43. !mysqli_query($link, $sql = sprintf('CREATE TABLE test(id BIGINT, bit_value BIT(%d) NOT NULL, bit_null BIT(%d) DEFAULT NULL) ENGINE="%s"', $bits, $bits, $engine)))
  44. printf("[002 - %d] [%d] %s\n",$bits, mysqli_errno($link), mysqli_error($link));
  45. $tests = 0;
  46. $rand_max = mt_getrandmax();
  47. while ($tests < 10) {
  48. $tests++;
  49. if (1 == $tests)
  50. $value = 0;
  51. else if (2 == $tests)
  52. $value = $max_value;
  53. else {
  54. if ($max_value > $rand_max) {
  55. $max_loops = floor($max_value/$rand_max);
  56. $num_loops = mt_rand(1, $max_loops);
  57. $value = 0;
  58. for ($i = 0; $i < $num_loops; $i++)
  59. $value += mt_rand(0, $rand_max);
  60. } else {
  61. $value = mt_rand(0, $max_value);
  62. }
  63. }
  64. $bin = ($bits < 32) ? decbin($value) : dec32bin($value, $bits);
  65. $sql = sprintf("INSERT INTO test(id, bit_value) VALUES (%s, b'%s')", $value, $bin);
  66. for ($i = 0; ($i < strlen($bin)) && ($bin[$i] == '0'); $i++)
  67. ;
  68. $bin2 = substr($bin, $i, strlen($bin));
  69. if (!mysqli_query($link, $sql))
  70. printf("[003 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));
  71. $sql = sprintf("SELECT id, BIN(bit_value) AS _bin, bit_value + 0 AS _bit_value0, bit_value, bit_null FROM test WHERE id = %s", $value);
  72. if (!$res = mysqli_query($link, $sql))
  73. printf("[004 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));
  74. if (!$row = mysqli_fetch_assoc($res))
  75. printf("[005 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));
  76. if (($value != $row['id']) || (($bin != $row['_bin']) && ($bin2 != $row['_bin']))) {
  77. debug_zval_dump($row);
  78. printf("[006 - %d] Insert of %s in BIT(%d) column might have failed. id = %s, bin = %s (%s/%s)\n",
  79. $bits, $value, $bits, $row['id'], $row['_bin'], $bin, $bin2);
  80. break;
  81. }
  82. if ($value != $row['bit_value']) {
  83. debug_zval_dump($row);
  84. printf("%10s %64s\n%10s %64s\n", '_bin', $row['_bin'], 'insert', $bin);
  85. printf("[007 - %d] Expecting %s got %s\n", $bits, $value, $row['bit_value']);
  86. break;
  87. }
  88. if (null !== $row['bit_null']) {
  89. debug_zval_dump($row);
  90. printf("[008 - %d] Expecting null got %s/%s\n", $bits, gettype($row['bit_value']), $row['bit_value']);
  91. break;
  92. }
  93. }
  94. }
  95. mysqli_close($link);
  96. print "done!";
  97. ?>
  98. --CLEAN--
  99. <?php
  100. require_once("clean_table.inc");
  101. ?>
  102. --EXPECTF--
  103. done!