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