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