File: stream_set_chunk_size.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (94 lines) | stat: -rw-r--r-- 2,653 bytes parent folder | download | duplicates (3)
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
--TEST--
stream_set_chunk_size basic tests
--FILE--
<?php
class test_wrapper {
    public $context;
    function stream_open($path, $mode, $openedpath) {
        return true;
    }
    function stream_eof() {
        return false;
    }
    function stream_read($count) {
        echo "read with size: ", $count, "\n";
        return str_repeat('a', $count);
    }
    function stream_write($data) {
        echo "write with size: ", strlen($data), "\n";
        return strlen($data);
    }
    function stream_set_option($option, $arg1, $arg2) {
        echo "option: ", $option, ", ", $arg1, ", ", $arg2, "\n";
        return false;
    }
}

var_dump(stream_wrapper_register('test', 'test_wrapper'));

$f = fopen("test://foo","r");

/* when the chunk size is 1, the read buffer is skipped, but the
 * the writes are made in chunks of size 1 (business as usual)
 * This should probably be revisited */
echo "should return previous chunk size (8192)\n";
var_dump(stream_set_chunk_size($f, 1));
echo "should be read without buffer (\$count == 10000)\n";
var_dump(strlen(fread($f, 10000)));
echo "should elicit 3 writes\n";
var_dump(fwrite($f, str_repeat('b', 3)));

echo "should return previous chunk size (1)\n";
var_dump(stream_set_chunk_size($f, 100));
echo "should elicit one read of size 100 (chunk size)\n";
var_dump(strlen(fread($f, 250)));
echo "should elicit one read of size 100 (chunk size)\n";
var_dump(strlen(fread($f, 50)));
echo "should elicit no read because there is sufficient cached data\n";
var_dump(strlen(fread($f, 50)));
echo "should elicit 3 writes\n";
var_dump(strlen(fwrite($f, str_repeat('b', 250))));

echo "\nerror conditions\n";
try {
    stream_set_chunk_size($f, 0);
} catch (ValueError $exception) {
    echo $exception->getMessage() . "\n";
}
try {
    stream_set_chunk_size($f, -1);
} catch (ValueError $exception) {
    echo $exception->getMessage() . "\n";
}
?>
--EXPECT--
bool(true)
should return previous chunk size (8192)
int(8192)
should be read without buffer ($count == 10000)
read with size: 10000
int(10000)
should elicit 3 writes
write with size: 1
write with size: 1
write with size: 1
int(3)
should return previous chunk size (1)
int(1)
should elicit one read of size 100 (chunk size)
read with size: 100
int(100)
should elicit one read of size 100 (chunk size)
read with size: 100
int(50)
should elicit no read because there is sufficient cached data
int(50)
should elicit 3 writes
write with size: 100
write with size: 100
write with size: 50
int(3)

error conditions
stream_set_chunk_size(): Argument #2 ($size) must be greater than 0
stream_set_chunk_size(): Argument #2 ($size) must be greater than 0