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