summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_fetch_assoc_bit.phpt
blob: 60c635995a2b310dae67d5f0fa1839cf95f5065b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--TEST--
mysqli_fetch_assoc() - BIT
--SKIPIF--
<?php
	require_once('skipif.inc');
	require_once('skipifemb.inc');
	require_once('skipifconnectfailure.inc');

	require_once('connect.inc');
	require_once('table.inc');
	if (mysqli_get_server_version($link) < 50003)
		// b'001' syntax not supported before 5.0.3
		die("skip Syntax used for test not supported with MySQL Server before 5.0.3");
	if (!$IS_MYSQLND && (mysqli_get_client_version() < 50003))
		// better don't trust libmysql before 5.0.3
		die("skip Syntax used for test not supported with MySQL Server before 5.0.3");
?>
--FILE--
<?php
    require('connect.inc');

    function dec32bin($dec, $bits) {

        $maxval = pow(2, $bits);
        $bin = '';
        for ($bitval = $maxval; $bitval >= 1; $bitval = $bitval / 2) {
            if (($dec / $bitval) >= 1) {
                $bin .= '1';
                $dec -= $bitval;
            } else {
                $bin .= '0';
            }
        }
        return $bin;
    }

    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
            $host, $user, $db, $port, $socket);

    for ($bits = 1; $bits <= 46; $bits++) {
        if (1 == $bits)
            $max_value = 1;
        else
            $max_value = pow(2, $bits) - 1;
        $tests = 0;
        if (!mysqli_query($link, "DROP TABLE IF EXISTS test") ||
            !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)))
            printf("[002 - %d] [%d] %s\n",$bits, mysqli_errno($link), mysqli_error($link));

        $tests = 0;
        $rand_max = mt_getrandmax();
        while ($tests < 10) {

            $tests++;
            if (1 == $tests)
                $value = 0;
            else if (2 == $tests)
                $value = $max_value;
            else {
                if ($max_value > $rand_max) {
                    $max_loops = floor($max_value/$rand_max);
                    $num_loops = mt_rand(1, $max_loops);
                    $value = 0;
                    for ($i = 0; $i < $num_loops; $i++)
                        $value += mt_rand(0, $rand_max);
                } else {
                    $value = mt_rand(0, $max_value);
                }
            }

            $bin = ($bits < 32) ? decbin($value) : dec32bin($value, $bits);
            $sql = sprintf("INSERT INTO test(id, bit_value) VALUES (%s, b'%s')", $value, $bin);
            for ($i = 0; ($i < strlen($bin)) && ($bin[$i] == '0'); $i++)
                ;
            $bin2 = substr($bin, $i, strlen($bin));

            if (!mysqli_query($link, $sql))
                printf("[003 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));

            $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);
            if (!$res = mysqli_query($link, $sql))
                printf("[004 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));

            if (!$row = mysqli_fetch_assoc($res))
                printf("[005 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));

            if (($value != $row['id']) || (($bin != $row['_bin']) && ($bin2 != $row['_bin']))) {
                debug_zval_dump($row);
                printf("[006 - %d] Insert of %s in BIT(%d) column might have failed. id = %s, bin = %s (%s/%s)\n",
                    $bits, $value, $bits, $row['id'], $row['_bin'], $bin, $bin2);
                break;
            }
            if ($value != $row['bit_value']) {
                debug_zval_dump($row);
                printf("%10s %64s\n%10s %64s\n", '_bin', $row['_bin'], 'insert', $bin);
                printf("[007 - %d] Expecting %s got %s\n", $bits, $value, $row['bit_value']);
                break;
            }

            if (null !== $row['bit_null']) {
                debug_zval_dump($row);
                printf("[008 - %d] Expecting null got %s/%s\n", $bits, gettype($row['bit_value']), $row['bit_value']);
                break;
            }
        }
    }

    mysqli_close($link);
    print "done!";
?>
--CLEAN--
<?php
	require_once("clean_table.inc");
?>
--EXPECT--
done!