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