xref: /PHP-8.0/ext/shmop/tests/001.phpt (revision c5401854)
1--TEST--
2shmop extension test
3--SKIPIF--
4<?php
5    if (!extension_loaded("shmop")) {
6        die("skip shmop() extension not available");
7    }
8?>
9--FILE--
10<?php
11    $hex_shm_id = 0xff3;
12    $write_d1 = "test #1 of the shmop() extension";
13    $write_d2 = "test #2 append data to shared memory segment";
14
15    echo "shm open for create: ";
16    $shm_id = shmop_open($hex_shm_id, "n", 0644, 1024);
17    if (!$shm_id) {
18        die("failed\n");
19    } else {
20        echo "ok\n";
21    }
22
23    echo "shm size is: " . ($shm_size = shmop_size($shm_id)) . "\n";
24
25    echo "shm write test #1: ";
26    $written = shmop_write($shm_id, $write_d1, 0);
27    if ($written != strlen($write_d1)) {
28        echo "failed\n";
29    } else {
30        echo "ok\n";
31    }
32
33    echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
34
35    echo "shm open for read only: ";
36    $shm_id = shmop_open($hex_shm_id, "a", 0644, 1024);
37    if (!$shm_id) {
38        echo "failed\n";
39    } else {
40        echo "ok\n";
41    }
42
43    echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
44
45    /* try to append data to the shared memory segment, this should fail */
46    try {
47        shmop_write($shm_id, $write_d1, $written);
48    } catch (Error $exception) {
49        echo $exception->getMessage() . "\n";
50    }
51
52    echo "shm open for read only: ";
53    $shm_id = shmop_open($hex_shm_id, "w", 0644, 1024);
54    if (!$shm_id) {
55        echo "failed\n";
56    } else {
57        echo "ok\n";
58    }
59
60    echo "shm write test #2: ";
61    $written = shmop_write($shm_id, $write_d2, $written);
62    if ($written != strlen($write_d2)) {
63        die("failed\n");
64    } else {
65        echo "ok\n";
66    }
67
68    echo "data in memory is: " . shmop_read($shm_id, 0, strlen($write_d1 . $write_d2)) . "\n";
69
70    echo "deletion of shm segment: ";
71    if (!shmop_delete($shm_id)) {
72        echo "failed\n";
73    } else {
74        echo "ok\n";
75    }
76?>
77--EXPECT--
78shm open for create: ok
79shm size is: 1024
80shm write test #1: ok
81data in memory is: test #1 of the shmop() extension
82shm open for read only: ok
83data in memory is: test #1 of the shmop() extension
84Read-only segment cannot be written
85shm open for read only: ok
86shm write test #2: ok
87data in memory is: test #1 of the shmop() extensiontest #2 append data to shared memory segment
88deletion of shm segment: ok
89